-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcreate.cppm
More file actions
278 lines (252 loc) · 10.3 KB
/
Copy pathcreate.cppm
File metadata and controls
278 lines (252 loc) · 10.3 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// mcpp.scaffold.create — project creation: package-shipped templates
// (fetch + instantiate) and the builtin bin/gui skeletons.
// Bodies moved verbatim from the CLI layer. Zero behavior change.
module;
#include <cstdio>
#include <cstdlib>
export module mcpp.scaffold.create;
import std;
import mcpp.config;
import mcpp.fetcher;
import mcpp.fetcher.progress;
import mcpp.manifest;
import mcpp.pm.compat;
import mcpp.pm.resolver;
import mcpp.scaffold;
import mcpp.ui;
namespace mcpp::scaffold {
// ─── Package-based templates (design v2: multi-level --template) ──────
//
// Resolve SPEC's package@version through the index, ensure the package
// sources are installed (same cache as dependencies), and return the
// package root (the directory containing mcpp.toml).
struct FetchedTemplatePackage {
std::filesystem::path root;
std::string name; // short package name (e.g. "imgui")
std::string version; // resolved exact version
};
std::expected<FetchedTemplatePackage, std::string>
fetch_template_package(const mcpp::scaffold::TemplateSpec& spec) {
auto cfg = mcpp::config::load_or_init(/*quiet=*/false,
mcpp::fetcher::make_bootstrap_progress_callback());
if (!cfg) return std::unexpected(cfg.error().message);
mcpp::pm::Fetcher fetcher(*cfg);
// Namespace candidates mirror dependency lookup: index root first,
// then the compat namespace.
std::optional<std::string> lua;
for (std::string cand : {std::string{}, std::string{"compat"}}) {
if (auto l = fetcher.read_xpkg_lua(cand, spec.pkg)) {
lua = std::move(*l);
break;
}
}
if (!lua) {
return std::unexpected(std::format(
"template package '{}' not found in the index "
"(check the name, or run `mcpp index update`)", spec.pkg));
}
// The filename hit alone does not carry the namespace: a bare spec
// like "llmapi" finds pkgs/l/llmapi.lua even though the descriptor
// declares `namespace = "mcpplibs"`, and xlings resolves install
// targets by the descriptor's qualified name. Derive the structured
// (namespace, shortName) from the descriptor fields.
auto coords = mcpp::pm::compat::descriptor_coordinates(
spec.pkg,
mcpp::manifest::extract_xpkg_namespace(*lua),
mcpp::manifest::extract_xpkg_name(*lua));
const std::string& ns = coords.namespace_;
const std::string& shortName = coords.shortName;
std::string version = spec.version;
if (version.empty()) {
auto v = mcpp::pm::resolve_semver(ns, shortName, "*", fetcher);
if (!v) return std::unexpected(v.error());
version = *v;
}
auto installed = fetcher.install_path(ns, shortName, version);
if (!installed) {
auto fq = ns.empty() ? shortName : std::format("{}.{}", ns, shortName);
mcpp::ui::info("Downloading", std::format("{} v{}", fq, version));
mcpp::fetcher::InstallProgressHandler progress;
std::vector<std::string> targets{ std::format("{}@{}", fq, version) };
auto r = fetcher.install(targets, &progress);
if (!r) return std::unexpected(std::format(
"fetch '{}@{}': {}", fq, version, r.error().message));
if (r->exitCode != 0) return std::unexpected(std::format(
"fetch '{}@{}' failed (exit {})", fq, version, r->exitCode));
installed = fetcher.install_path(ns, shortName, version);
if (!installed) return std::unexpected(std::format(
"package '{}@{}' install path missing after fetch", fq, version));
}
// Package root = the directory holding mcpp.toml (tarballs usually wrap
// everything in a single top-level directory).
std::filesystem::path root = *installed;
if (!std::filesystem::exists(root / "mcpp.toml")) {
std::error_code ec;
for (auto& e : std::filesystem::directory_iterator(root, ec)) {
if (e.is_directory()
&& std::filesystem::exists(e.path() / "mcpp.toml")) {
root = e.path();
break;
}
}
}
if (!std::filesystem::exists(root / "mcpp.toml")) {
return std::unexpected(std::format(
"package '{}@{}' has no mcpp.toml", spec.pkg, version));
}
return FetchedTemplatePackage{root, shortName, version};
}
void print_template_listing(const FetchedTemplatePackage& pkg,
const std::vector<mcpp::scaffold::TemplateEntry>& entries) {
std::println("Templates in {}@{}:", pkg.name, pkg.version);
for (auto& t : entries) {
std::println(" {:<14}{}{}", t.name,
t.meta.isDefault ? "(default) " : " ",
t.meta.description);
}
std::println("");
std::println("usage: mcpp new <name> --template {}[@ver][:<template>]", pkg.name);
}
export int list_package_templates(const mcpp::scaffold::TemplateSpec& spec) {
auto pkg = fetch_template_package(spec);
if (!pkg) { mcpp::ui::error(pkg.error()); return 1; }
auto entries = mcpp::scaffold::list_templates(pkg->root);
if (!entries) { mcpp::ui::error(entries.error()); return 1; }
print_template_listing(*pkg, *entries);
return 0;
}
export int new_from_package_template(const std::string& name, const std::string& specStr) {
auto spec = mcpp::scaffold::parse_spec(specStr);
if (spec.listOnly) return list_package_templates(spec);
auto pkg = fetch_template_package(spec);
if (!pkg) { mcpp::ui::error(pkg.error()); return 1; }
auto entries = mcpp::scaffold::list_templates(pkg->root);
if (!entries) { mcpp::ui::error(entries.error()); return 1; }
const mcpp::scaffold::TemplateEntry* chosen = nullptr;
if (spec.tmpl.empty()) {
for (auto& t : *entries) if (t.meta.isDefault) { chosen = &t; break; }
if (!chosen) {
mcpp::ui::error(std::format(
"package '{}' declares no default template — pick one explicitly:",
pkg->name));
print_template_listing(*pkg, *entries);
return 1;
}
} else {
for (auto& t : *entries) if (t.name == spec.tmpl) { chosen = &t; break; }
if (!chosen) {
mcpp::ui::error(std::format(
"package '{}@{}' has no template '{}'",
pkg->name, pkg->version, spec.tmpl));
print_template_listing(*pkg, *entries);
return 1;
}
}
std::filesystem::path root = std::filesystem::current_path() / name;
if (std::filesystem::exists(root)) {
mcpp::ui::error(std::format("'{}' already exists", root.string()));
return 1;
}
std::error_code ec;
std::filesystem::create_directories(root, ec);
if (ec) {
mcpp::ui::error(std::format("cannot create '{}': {}",
root.string(), ec.message()));
return 1;
}
mcpp::scaffold::RenderVars vars{name, pkg->name, pkg->version};
if (auto err = mcpp::scaffold::instantiate(
pkg->root / "templates" / chosen->name, root, vars)) {
mcpp::ui::error(*err);
return 1;
}
if (auto err = mcpp::scaffold::inject_self_dependency(
root / "mcpp.toml", vars, chosen->meta.injectSelfFeatures)) {
mcpp::ui::error(*err);
return 1;
}
mcpp::ui::status("Created", std::format(
"{} (template {}@{}:{})", name, pkg->name, pkg->version, chosen->name));
if (!chosen->meta.postMessage.empty())
std::println("{}", chosen->meta.postMessage);
return 0;
}
// Builtin `mcpp new` skeleton (bin, plus the transitional gui alias).
export int create_builtin_project(const std::string& name, bool gui) {
std::filesystem::path root = std::filesystem::current_path() / name;
if (std::filesystem::exists(root)) {
std::println(stderr, "error: '{}' already exists", root.string());
return 1;
}
std::error_code ec;
std::filesystem::create_directories(root / "src", ec);
if (ec) {
std::println(stderr, "error: cannot create '{}': {}", root.string(), ec.message());
return 1;
}
// mcpp.toml
{
std::ofstream os(root / "mcpp.toml");
os << mcpp::manifest::default_template(name);
if (gui) {
// The GUI template depends on the imgui module package. It does not
// pin a toolchain — mcpp resolves the environment/default toolchain
// and the GL runtime is closed by the ecosystem (compat.glx-runtime).
os << "\n[dependencies]\nimgui = \"0.0.5\"\n";
}
}
// src/main.cpp — template with PROJECT placeholder, replaced with `name`.
{
std::string body = gui ? R"GUI(// PROJECT — generated by `mcpp new --template gui`
// Tier-0 zero-boilerplate window via the imgui.app facade. No #include.
import imgui.core;
import imgui.app;
int main() {
return ImGui::App::run([] {
ImGui::Begin("PROJECT");
ImGui::TextUnformatted("Hello from mcpp + imgui (imgui.app facade)");
ImGui::End();
});
}
)GUI" : R"(// PROJECT — generated by `mcpp new`
import std;
int main(int argc, char* argv[]) {
std::println("Hello from PROJECT!");
std::println("Built with import std + std::println on modular C++23.");
if (argc > 1) {
for (int i = 1; i < argc; ++i) std::println(" arg[{}] = {}", i, argv[i]);
}
return 0;
}
)";
std::size_t pos;
while ((pos = body.find("PROJECT")) != std::string::npos) {
body.replace(pos, 7, name);
}
std::ofstream os(root / "src" / "main.cpp");
os << body;
}
// tests/test_smoke.cpp — bundled smoke test (`mcpp test` works out-of-the-box).
{
std::filesystem::create_directories(root / "tests", ec);
std::ofstream os(root / "tests" / "test_smoke.cpp");
os << R"(// Smoke test — verifies the project compiles + a binary runs.
// Add more tests as tests/test_*.cpp files; mcpp test discovers them
// automatically (one binary per file).
import std;
int main() {
std::println("test_smoke: ok");
return 0;
}
)";
}
// .gitignore
{
std::ofstream os(root / ".gitignore");
os << "target/\n";
}
std::println("Created {} package '{}' at {}", gui ? "gui" : "bin", name, root.string());
std::println("Next: cd {} && mcpp build && mcpp run (or `mcpp test`)", name);
return 0;
}
} // namespace mcpp::scaffold