-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtemplate.cppm
More file actions
231 lines (212 loc) · 8.52 KB
/
Copy pathtemplate.cppm
File metadata and controls
231 lines (212 loc) · 8.52 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// mcpp.scaffold — package-based project templates (design v2).
//
// Mechanism owned by mcpp: SPEC grammar, rendering, dependency injection.
// Vocabulary owned by packages: template names, contents, default choice
// (closed grammar / open vocabulary applied to scaffolding).
//
// A package opts in by shipping a `templates/` directory:
// templates/<name>/template.toml — metadata (description/default/inject)
// templates/<name>/**.in — rendered ({{var}} placeholders)
// templates/<name>/** (non-.in) — copied verbatim
//
// Trust boundary: templates are pure data — render + copy, no hooks and no
// script execution.
export module mcpp.scaffold;
import std;
import mcpp.libs.toml;
export namespace mcpp::scaffold {
// `--template` package-form SPEC:
// pkg | pkg:tmpl | pkg@ver | pkg@ver:tmpl | pkg: (trailing ':' = list)
struct TemplateSpec {
std::string pkg;
std::string version; // "" = latest
std::string tmpl; // "" = the package's default template
bool listOnly = false;
};
TemplateSpec parse_spec(std::string_view s) {
TemplateSpec spec;
std::string_view left = s;
if (auto c = s.find(':'); c != std::string_view::npos) {
left = s.substr(0, c);
auto right = s.substr(c + 1);
if (right.empty()) spec.listOnly = true;
else spec.tmpl = std::string(right);
}
if (auto a = left.find('@'); a != std::string_view::npos) {
spec.pkg = std::string(left.substr(0, a));
spec.version = std::string(left.substr(a + 1));
} else {
spec.pkg = std::string(left);
}
return spec;
}
struct TemplateMeta {
std::string description;
bool isDefault = false;
std::string postMessage;
// [template.inject] self = { features = [...] }
std::vector<std::string> injectSelfFeatures;
};
std::expected<TemplateMeta, std::string>
load_meta(const std::filesystem::path& templateDir) {
auto metaPath = templateDir / "template.toml";
if (!std::filesystem::exists(metaPath)) {
return std::unexpected(std::format(
"template '{}' has no template.toml", templateDir.filename().string()));
}
auto doc = mcpp::libs::toml::parse_file(metaPath);
if (!doc) {
return std::unexpected(std::format(
"template '{}': bad template.toml: {}",
templateDir.filename().string(), doc.error().message));
}
TemplateMeta meta;
if (auto v = doc->get_string("template.description")) meta.description = *v;
if (auto v = doc->get_string("template.post_message")) meta.postMessage = *v;
if (auto* t = doc->get_table("template")) {
if (auto it = t->find("default"); it != t->end() && it->second.is_bool())
meta.isDefault = it->second.as_bool();
if (auto it = t->find("inject"); it != t->end() && it->second.is_table()) {
auto& inj = it->second.as_table();
if (auto self = inj.find("self");
self != inj.end() && self->second.is_table()) {
auto& st = self->second.as_table();
if (auto f = st.find("features");
f != st.end() && f->second.is_array()) {
for (auto& fv : f->second.as_array())
if (fv.is_string())
meta.injectSelfFeatures.push_back(fv.as_string());
}
}
}
}
return meta;
}
struct TemplateEntry {
std::string name;
TemplateMeta meta;
};
// Enumerate templates/<name>/ entries of a package root (sorted by name).
std::expected<std::vector<TemplateEntry>, std::string>
list_templates(const std::filesystem::path& packageRoot) {
auto dir = packageRoot / "templates";
if (!std::filesystem::exists(dir)) {
return std::unexpected("package ships no templates/ directory");
}
std::vector<TemplateEntry> out;
std::error_code ec;
for (auto& e : std::filesystem::directory_iterator(dir, ec)) {
if (!e.is_directory()) continue;
auto meta = load_meta(e.path());
if (!meta) return std::unexpected(meta.error());
out.push_back({e.path().filename().string(), std::move(*meta)});
}
std::ranges::sort(out, {}, &TemplateEntry::name);
int defaults = 0;
for (auto& t : out) defaults += t.meta.isDefault ? 1 : 0;
if (defaults > 1) {
return std::unexpected(
"package declares more than one default template (template.toml "
"`default = true` must appear at most once)");
}
return out;
}
// The placeholder vocabulary is deliberately minimal and mcpp-owned;
// template variability comes from packages shipping multiple templates,
// not from growing the renderer into a programming language.
struct RenderVars {
std::string projectName;
std::string selfName;
std::string selfVersion;
};
std::string render_text(std::string text, const RenderVars& vars) {
auto replace_all = [&](std::string_view from, std::string_view to) {
std::size_t pos = 0;
while ((pos = text.find(from, pos)) != std::string::npos) {
text.replace(pos, from.size(), to);
pos += to.size();
}
};
replace_all("{{project.name}}", vars.projectName);
replace_all("{{self.name}}", vars.selfName);
replace_all("{{self.version}}", vars.selfVersion);
return text;
}
// Instantiate templateDir into destDir: `.in` files are rendered (suffix
// stripped), everything else copied verbatim; template.toml is metadata
// only and never copied.
std::optional<std::string>
instantiate(const std::filesystem::path& templateDir,
const std::filesystem::path& destDir,
const RenderVars& vars) {
std::error_code ec;
for (auto& e : std::filesystem::recursive_directory_iterator(templateDir, ec)) {
auto rel = std::filesystem::relative(e.path(), templateDir, ec);
if (rel == "template.toml") continue;
auto dest = destDir / rel;
if (e.is_directory()) {
std::filesystem::create_directories(dest, ec);
continue;
}
std::filesystem::create_directories(dest.parent_path(), ec);
if (rel.extension() == ".in") {
std::ifstream is(e.path());
std::stringstream ss; ss << is.rdbuf();
auto rendered = render_text(ss.str(), vars);
dest.replace_extension(); // strip ".in"
std::ofstream os(dest);
os << rendered;
} else {
std::filesystem::copy_file(
e.path(), dest,
std::filesystem::copy_options::overwrite_existing, ec);
if (ec) {
return std::format("copy '{}' failed: {}",
rel.string(), ec.message());
}
}
}
return std::nullopt;
}
// Ensure the generated manifest depends on the template's own package.
// If the template already declares it (typically via {{self.version}}),
// nothing is injected — template wins.
std::optional<std::string>
inject_self_dependency(const std::filesystem::path& manifestPath,
const RenderVars& vars,
const std::vector<std::string>& features) {
std::ifstream is(manifestPath);
if (!is) return std::format("cannot read '{}'", manifestPath.string());
std::stringstream ss; ss << is.rdbuf();
std::string content = ss.str();
is.close();
if (content.find(vars.selfName + " =") != std::string::npos
|| content.find(vars.selfName + "=") != std::string::npos) {
return std::nullopt; // already declared by the template
}
std::string depLine;
if (features.empty()) {
depLine = std::format("{} = \"{}\"\n", vars.selfName, vars.selfVersion);
} else {
std::string flist;
for (auto& f : features) {
if (!flist.empty()) flist += ", ";
flist += std::format("\"{}\"", f);
}
depLine = std::format("{} = {{ version = \"{}\", features = [{}] }}\n",
vars.selfName, vars.selfVersion, flist);
}
constexpr std::string_view header = "[dependencies]";
if (auto pos = content.find(header); pos != std::string::npos) {
auto eol = content.find('\n', pos);
if (eol == std::string::npos) content += "\n" + depLine;
else content.insert(eol + 1, depLine);
} else {
if (!content.empty() && content.back() != '\n') content += '\n';
content += "\n[dependencies]\n" + depLine;
}
std::ofstream os(manifestPath);
os << content;
return std::nullopt;
}
} // namespace mcpp::scaffold