1use crate::{
3 registry::{redep, Manifest, Registry},
4 result::Result,
5};
6use etc::{Etc, FileSystem, Write};
7use serde::Serialize;
8use std::{path::PathBuf, process::Command};
9use toml::Serializer;
10
11pub fn workspace(target: &PathBuf, registry: &Registry) -> Result<Manifest> {
13 let crates = etc::find_all(target, "Cargo.toml")?;
14 let ts = target.to_str().unwrap_or_default();
15 let mut mani = Manifest::default();
16 let mut members = vec![];
17 for ct in crates {
18 let ps = ct.to_string_lossy();
19 if ps.contains("/target/") {
20 continue;
21 }
22
23 redep(&ct, registry)?;
25
26 let begin = ps.find(ts).unwrap_or(0) + ts.len();
28 if ps.len() > (begin + 12) {
29 members.push(ps[(begin + 1)..ps.len() - 11].to_string())
30 }
31 }
32
33 mani.workspace.members = members;
34 Ok(mani)
35}
36
37pub fn rustup() -> Result<()> {
39 let info = String::from_utf8_lossy(&Command::new("rustup").args(vec!["show"]).output()?.stdout)
40 .to_string();
41
42 if !info.contains("wasm32-unknown-unknown") && !info.contains("nightly-2020-10-05") {
43 Command::new("rustup")
44 .args(vec!["install", "nightly"])
45 .status()?;
46 Command::new("rustup")
47 .args(vec!["override", "set", "nightly-2020-10-05"])
48 .status()?;
49 Command::new("rustup")
50 .args(vec![
51 "target",
52 "add",
53 "wasm32-unknown-unknown",
54 "--toolchain",
55 "nightly-2020-10-05",
56 ])
57 .status()?;
58 }
59
60 Ok(())
61}
62
63pub fn exec(
65 mut registry: Registry,
66 target: PathBuf,
67 skip: bool,
68 tag: Option<String>,
69) -> Result<()> {
70 if !skip {
71 rustup()?;
72 }
73
74 let tags = registry.tag()?;
76 if tag.is_some() && tags.is_empty() {
77 registry.update()?;
78 }
79
80 if let Some(tag) = &tag {
81 registry.checkout(tag)?;
82 }
83
84 let substrate = Etc::from(®istry.dir);
85 if let Ok(template) = substrate.find("node-template") {
86 etc::cp_r(template, PathBuf::from(&target))?;
87
88 let mani = workspace(&target, ®istry)?;
90 let mut dst = String::with_capacity(128);
91 mani.serialize(Serializer::pretty(&mut dst).pretty_array(true))?;
92 Etc::from(&target).open("Cargo.toml")?.write(dst)?;
93
94 if let Some(tag) = &tag {
95 println!(
96 "Created node-template {:?} with tag {} succeed!",
97 &target, &tag
98 );
99 } else {
100 println!("Created node-template {:?} without tag succeed!", &target,);
101 }
102 } else {
103 if let Some(tag) = &tag {
104 println!(
105 "The registry {} at tag {} doesn't have node-template",
106 ®istry.config.node.registry, &tag
107 );
108 } else {
109 println!(
110 "The registry {} without tag doesn't have node-template",
111 ®istry.config.node.registry
112 )
113 }
114 println!("failed.");
115 return Ok(());
116 }
117
118 registry.checkout("master")?;
120 if let Some(tag) = tag {
121 registry.config.node.tag(&tag);
122 }
123
124 registry.config.gen(target)?;
125 println!("ok!");
126 Ok(())
127}