-
Notifications
You must be signed in to change notification settings - Fork 0
/
archetype.rhai
139 lines (122 loc) · 3.24 KB
/
archetype.rhai
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
let context = #{};
const CASE_STRATEGY = CasedIdentityCasedValue(PROGRAMMING_CASES);
if ANSWERS.applications != () {
context.applications = case_applications(ANSWERS.applications);
}
if type_of(ANSWERS.model) == "map" {
context.model = case_model(ANSWERS.model);
} else if ANSWERS.default_model != () {
let model = generate_model(ANSWERS.default_model);
if type_of(model) == "map" {
context.model = case_model(model);
}
}
fn case_applications(applications) {
for application_key in applications.keys() {
applications[application_key] = case_application(applications[application_key]);
}
applications
}
fn case_application(application) {
let prefix = application["prefix"];
let suffix = application["suffix"];
if prefix != () {
application += set("project-prefix", prefix, #{
cases: [
global::CASE_STRATEGY,
],
});
}
if suffix != () {
application += set("project-suffix", suffix, #{
cases: [
global::CASE_STRATEGY,
],
});
}
if prefix != () && suffix != () {
application += set("project-name", `${prefix}-${suffix}`, #{
cases: [
global::CASE_STRATEGY,
FixedKeyCasedValue("project-title", TitleCase),
],
});
}
let model = application.model;
if model != () {
application.model = case_model(model);
}
application
}
fn case_model(model) {
for entity_key in model.entities.keys() {
model.entities[entity_key] = case_entity(model.entities[entity_key]);
}
model
}
fn case_entity(entity) {
if entity != () {
let name = entity["name"];
if name != () {
entity += set("entity-name", name, #{
cases: [
global::CASE_STRATEGY,
FixedKeyCasedValue("entity-title", TitleCase),
],
});
for field_key in entity.fields.keys() {
entity.fields.set(field_key, case_field(entity.fields[field_key]));
}
}
}
entity
}
fn case_field(field) {
if field != () {
let name = field["name"];
field += set("field-name", name, #{
cases: [
global::CASE_STRATEGY,
FixedKeyCasedValue("field-title", TitleCase),
],
});
}
field
}
fn generate_model(model) {
if type_of(model) == "list" {
let entities = #{};
for entity_key in model {
entities[entity_key] = generate_entity(entity_key);
}
return #{
"entities": entities,
};
}
if type_of(model) == "string" {
let entities = #{};
let entity_key = model;
entities[entity_key] = generate_entity(entity_key);
return #{
"entities": entities,
}
}
}
fn generate_entity(name) {
let entity = #{
name: name,
fields: #{
contents: #{
name: "contents",
type: "String",
required: true,
},
},
};
entity
}
if switch_enabled("debug-context") {
display(as_yaml(context));
}
// Return Context
context