cargo-playdate 0.3.3

Build tool for neat yellow console.
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
use std::borrow::Cow;
use std::ffi::OsStr;
use std::path::Path;
use std::process::Stdio;

use cargo::CargoResult;
use playdate::metadata::METADATA_FIELD;
use playdate::consts::SDK_ENV_VAR;

use crate::build::rustflags::Rustflags;
use crate::cli::target::PlaydateTarget;
use crate::config::Config;
use crate::proc;


pub fn new_or_init<'cfg>(config: &'cfg Config<'cfg>) -> CargoResult<()> {
	let cty_requested = config.args
	                          .iter()
	                          .find(|arg| arg.as_os_str() == OsStr::new("--lib"))
	                          .or_else(|| {
		                          config.args
		                                .iter()
		                                .find(|arg| arg.as_os_str() == OsStr::new("--bin"))
	                          })
	                          .map(|arg| arg.to_string_lossy().strip_prefix("--").map(ToString::to_string))
	                          .flatten();
	let mut cargo = proc::cargo_proxy_cmd(config, &config.cmd)?;
	if let Some(cty) = cty_requested {
		let cty = match cty.as_str() {
			"lib" => "library",
			"bin" => "executable binary",
			s => s,
		};
		config.log().status("Creating", format!("{cty} (requested)."));
	} else {
		cargo.arg("--lib");
		config.log().status("Creating", "library (default).");
	}

	config.log().verbose(|mut log| {
		            log.status("Cargo", proc::args_line_for_proc(&cargo));
	            });
	cargo.stderr(Stdio::inherit());
	cargo.stdout(Stdio::inherit());
	cargo.status()?.exit_ok()?;


	let path = config.create_path
	                 .as_deref()
	                 .ok_or(anyhow::anyhow!("Path not set"))?;
	let is_bin = path.join("src").join("main.rs").try_exists()?;

	// open manifest
	let manifest_path = path.join("Cargo.toml");
	let manifest_src = std::fs::read_to_string(&manifest_path)?;
	let mut manifest = manifest_src.parse::<toml_edit::Document>()?;


	// add metadata:
	if config.create_full_metadata {
		add_full_metadata(config, &mut manifest)?;
	} else {
		add_min_metadata(config, &mut manifest)?;
		reformat_metadata(config, &mut manifest)?;
	}


	// TODO: add dependencies
	let (deps_to_add, hl) = add_dependencies(config, &mut manifest)?;


	// sources:
	lib(config, &path, &mut manifest, hl)?;

	if is_bin {
		bin(config, &path, &mut manifest, hl)?;
	}


	// export manifest:
	let manifest = manifest.to_string();
	std::fs::write(manifest_path, manifest.trim_start())?;


	// cargo config:
	cargo_config(config, path.join(".cargo").join("config.toml"))?;


	// TODO: deps_to_add
	for dep in deps_to_add {
		// TODO call cargo add WITH PWD=path

		let mut cargo = proc::cargo(config.workspace.config().into())?;
		cargo.current_dir(path);
		cargo.arg("add");
		cargo.arg(dep.as_ref());
		cargo.stderr(Stdio::inherit());
		cargo.stdout(Stdio::inherit());
		cargo.status()?.exit_ok()?;
	}


	Ok(())
}


/// Take uor metadata table, render it like a normal,
/// then add as trailing raw string to the end of file.
fn reformat_metadata(_config: &Config, manifest: &mut toml_edit::Document) -> CargoResult<()> {
	manifest["package"]["metadata"][METADATA_FIELD].as_inline_table_mut()
	                                               .map(|t| {
		                                               t.set_dotted(true);
		                                               t.fmt();
	                                               });


	let metadata = &manifest["package"]["metadata"][METADATA_FIELD];

	// TODO: Add comment https://sdk.play.date/Inside%20Playdate.html#pdxinfo
	//       Or better to playdate-build crate documentation.
	let mut raw_metadata_lines = vec![
	                                  "\n".to_owned(),
	                                  "# doc: // TODO: https://github.com/boozook/playdate/#configuration".to_owned(),
	                                  format!("[package.metadata.{METADATA_FIELD}]"),
	];

	if let Some(table) = metadata.as_table_like() {
		let mut keys = Vec::new();
		for (key, value) in table.iter() {
			keys.push(key.to_owned());
			raw_metadata_lines.push(format!("{key} = {}", value.to_string()));
		}
	}

	// remove original our metadata table:
	manifest["package"]["metadata"].as_table_like_mut()
	                               .map(|table| table.remove(METADATA_FIELD));
	// remove original entire metadata table if it empty:
	if manifest["package"]["metadata"].as_table_like()
	                                  .into_iter()
	                                  .filter(|t| t.is_empty())
	                                  .next()
	                                  .is_some()
	{
		manifest["package"].as_table_like_mut()
		                   .map(|table| table.remove("metadata"));
	}

	let raw_metadata = raw_metadata_lines.join("\n");
	manifest.set_trailing(raw_metadata);

	Ok(())
}


fn add_min_metadata(_config: &Config<'_>, manifest: &mut toml_edit::Document) -> CargoResult<()> {
	use toml_edit::value;

	let name = manifest["package"]["name"].as_str()
	                                      .unwrap_or("hello-world")
	                                      .to_owned();
	let bundle_id = name.replace("_", ".").replace("-", ".");
	let has_description = manifest["package"].as_table()
	                                         .map(|t| t.contains_key("description"))
	                                         .unwrap_or(false);

	let meta = &mut manifest["package"]["metadata"][METADATA_FIELD];

	meta["bundle-id"] = value(format!("com.{bundle_id}"));
	if !has_description {
		meta["description"] = value(format!("Description of {name} program."));
	}

	// options:
	meta["options"]["assets"]["dependencies"] = value(true);
	meta["options"]["assets"]["overwrite"] = value(true);

	Ok(())
}

fn add_full_metadata(_config: &Config<'_>, manifest: &mut toml_edit::Document) -> CargoResult<()> {
	use toml_edit::Document;

	let name = manifest["package"]["name"].as_str()
	                                      .unwrap_or("hello-world")
	                                      .to_owned();
	let bundle_id = name.replace("_", ".").replace("-", ".");
	let version = manifest["package"]["version"].as_str().unwrap_or("0.0");

	let author_default = "You, Inc";
	let author: Cow<str> = if manifest["package"].as_table()
	                                             .map(|t| t.contains_key("authors"))
	                                             .unwrap_or(false)
	{
		manifest["package"]["authors"].as_array()
		                              .into_iter()
		                              .flat_map(|arr| arr.get(0))
		                              .flat_map(|v| v.as_str().map(Into::into))
		                              .next()
		                              .unwrap_or(author_default.into())
	} else {
		author_default.into()
	};


	let description_default = format!("Description for the {name} program.");
	let description: Cow<str> = if manifest["package"].as_table()
	                                                  .map(|t| t.contains_key("description"))
	                                                  .unwrap_or(false)
	{
		manifest["package"]["description"].as_str()
		                                  .map(Into::into)
		                                  .unwrap_or_else(|| description_default.into())
	} else {
		description_default.into()
	};

	let toml = format!(
	                   include_str!("full-metadata.toml"),
	                   name = name,
	                   author = author,
	                   version = version,
	                   bundle_id = bundle_id,
	                   description = description
	);
	toml.parse::<Document>().expect("invalid doc");

	let raw_metadata = format!("\n\n{}\n", toml.trim());
	manifest.set_trailing(raw_metadata);

	Ok(())
}


fn cargo_add<'s>(config: &Config<'_>,
                 pwd: &Path,
                 manifest: &Path,
                 name: &str,
                 git: bool,
                 rename: Option<&str>,
                 features: Option<impl IntoIterator<Item = &'s str>>)
                 -> CargoResult<()> {
	let mut cargo = proc::cargo(config.workspace.config().into())?;
	cargo.current_dir(pwd);

	cargo.arg("add");
	cargo.arg(name);

	if let Some(name) = rename {
		cargo.arg("--rename");
		cargo.arg(name);
	}

	if let Some(features) = features {
		let features = features.into_iter().collect::<Vec<_>>().join(",");
		cargo.arg("--features");
		cargo.arg(features);
	}

	// git => --git="URL"

	cargo.arg("manifest-path");
	cargo.arg(manifest);

	cargo.stderr(Stdio::inherit());
	cargo.stdout(Stdio::inherit());
	cargo.status()?.exit_ok()?;
	Ok(())
}


fn add_dependencies<'cfg>(config: &'cfg Config<'_>,
                          manifest: &mut toml_edit::Document)
                          -> CargoResult<(Vec<Cow<'cfg, str>>, bool)> {
	use toml_edit::value;
	use crate::cli::deps::Dependency as Dep;
	use crate::cli::deps::DependencyName as Name;
	use crate::cli::deps::DependencySource as Src;

	fn add_dep(dep: &Dep<'_>, manifest: &mut toml_edit::Document) -> CargoResult<()> {
		match dep.source {
			Src::CratesIo => manifest["dependencies"][dep.name.to_string()] = value("*"),
			Src::Git => {
				let name = dep.name.to_string();
				let url = dep.git()
				             .ok_or(anyhow::anyhow!("Don't know git repository for {}", name))?;
				manifest["dependencies"][&name] = toml_edit::table();
				manifest["dependencies"][&name]["git"] = value(url);
				manifest["dependencies"][&name]["version"] = value("*");
			},
		}
		Ok(())
	}

	let mut add_pd = false;

	if config.create_deps_sys_only {
		let default = Dep { name: Name::Sys,
		                    source: Src::CratesIo };
		let dep = config.create_deps
		                .iter()
		                .find(|d| matches!(d.name, Name::Sys))
		                .unwrap_or(&default);
		add_dep(dep, manifest)?;
		Ok((vec![], add_pd))
	} else {
		let known = config.create_deps
		                  .iter()
		                  .filter(|d| !matches!(d.name, Name::Other(_)));
		for dep in known {
			add_pd = matches!(dep.name, Name::Playdate) || add_pd;
			add_dep(dep, manifest)?;
		}

		let others = config.create_deps.iter().filter_map(|d| {
			                                      if let Name::Other(dep) = &d.name {
				                                      Some(dep.to_owned())
			                                      } else {
				                                      None
			                                      }
		                                      });
		Ok((others.collect(), add_pd))
	}
}


/// 1. render rustflags to config
/// 2. add `[unstable]` section with required nightly features
fn cargo_config<P: AsRef<Path>>(config: &Config, path: P) -> CargoResult<()> {
	let mut doc = toml::Table::new();

	// target tables:
	let mut targets = toml::Table::new();

	// default
	if !config.create_full_config {
		// target[].rustflags:
		let rustflags: Vec<toml::Value> = Rustflags::rustflags_bin_playdate().into_iter()
		                                                                     .map(|s| toml::Value::String(s.to_string()))
		                                                                     .collect();

		let mut target_table = toml::Table::new();
		target_table.insert("rustflags".into(), rustflags.into());

		let ct = PlaydateTarget::device_compile_target();
		targets.insert(ct.short_name().into(), target_table.into());
	} else {
		let rustflags = Rustflags::try_default(config)?;
		for (kind, flags) in rustflags.targets()
		                              .map(|k| rustflags.get(k).map(|flags| (k, flags)))
		                              .flatten()
		{
			let rustflags: Vec<toml::Value> = flags.into_iter()
			                                       .map(|s| toml::Value::String(s.to_string()))
			                                       .collect();
			let mut target_table = toml::Table::new();
			target_table.insert("rustflags".into(), rustflags.into());

			let ct_name = match kind {
				cargo::core::compiler::CompileKind::Host => config.host_target.short_name(),
				cargo::core::compiler::CompileKind::Target(ct) => ct.short_name(),
			};
			targets.insert(ct_name.into(), target_table.into());
		}

		// also add env:
		let mut env = toml::Table::new();
		env.insert(
		           SDK_ENV_VAR.into(),
		           config.sdk()?.path().display().to_string().into(),
		);
		doc.insert("env".into(), env.into());
	}
	doc.insert("target".into(), targets.into());


	// unstable features:
	let mut unstable = toml::Table::new();
	unstable.insert("unstable-options".into(), true.into());
	unstable.insert("avoid-dev-deps".into(), true.into());
	let check_cfg: Vec<toml::Value> = ["names", "values", "output"].into_iter()
	                                                               .map(|s| toml::Value::String(s.to_string()))
	                                                               .collect();
	unstable.insert("check-cfg".into(), check_cfg.into());
	doc.insert("unstable".into(), unstable.into());


	// export:
	if let Some(parent) = path.as_ref().parent() {
		if !parent.try_exists()? {
			std::fs::create_dir_all(parent)?;
		}
	}
	std::fs::write(path, doc.to_string())?;

	Ok(())
}


fn lib(config: &Config, root: &Path, manifest: &mut toml_edit::Document, hl: bool) -> CargoResult<()> {
	use toml_edit::Document;

	let toml = r#"
[lib]
path = "src/lib.rs"
crate-type = [
	"staticlib", # for hardware
	"dylib",     # for simulator
	"rlib",      # to link with bin
]
"#;
	manifest["lib"] = toml.parse::<Document>().expect("invalid doc")["lib"].clone();

	let path = root.join("src").join("lib.rs");
	let src = if config.create_deps_sys_only || !hl {
		&include_bytes!("lib-ll.rs")[..]
	} else {
		&include_bytes!("lib-hl.rs")[..]
	};
	std::fs::write(path, src)?;
	Ok(())
}

fn bin(config: &Config, root: &Path, manifest: &mut toml_edit::Document, hl: bool) -> CargoResult<()> {
	let path = root.join("src").join("main.rs");
	let name = manifest["package"]["name"].as_str()
	                                      .unwrap_or("hello-world")
	                                      .to_owned()
	                                      .replace("-", "_");
	let src = if config.create_deps_sys_only || !hl {
		format!(include_str!("bin-ll.rs"), crate_name = name)
	} else {
		format!(include_str!("bin-hl.rs"), crate_name = name)
	};
	std::fs::write(path, src)?;
	Ok(())
}


#[cfg(test)]
mod tests {
	use toml_edit::Document;


	#[test]
	fn full_metadata_is_valid() {
		let toml = format!(
		                   include_str!("full-metadata.toml"),
		                   name = "name",
		                   author = "author",
		                   version = "version",
		                   bundle_id = "bundle-id",
		                   description = "description"
		);
		toml.parse::<Document>().expect("invalid doc");
	}
}