MCPcopy Create free account
hub / github.com/faern/rustscript / build_script_crate

Function build_script_crate

src/scriptbuilder.rs:44–69  ·  view source on GitHub ↗
(script_path: P,
                                                          output_crate_dir: Q,
                                                          verbose: bool)

Source from the content-addressed store, hash-verified

42}
43
44pub fn build_script_crate<P: AsRef<Path>, Q: AsRef<Path>>(script_path: P,
45 output_crate_dir: Q,
46 verbose: bool)
47 -> Result<()> {
48 let script_path = script_path.as_ref();
49 let script_name = script_path.file_name()
50 .ok_or("Script has no name")?
51 .to_str()
52 .ok_or("Script name is not valid utf-8")?;
53 let output_crate_dir = output_crate_dir.as_ref();
54 let src_dir = output_crate_dir.join("src");
55 if !src_dir.exists() {
56 fs::create_dir(&src_dir).chain_err(|| "unable to create src dir")?;
57 }
58 let main_path = src_dir.join("main.rs");
59
60 let code = read_file(script_path)?;
61 let (crates, code) = extract_extern_crates(&code)?;
62
63 let formatted_code = format_code(&crates, &code);
64 let mut output_f = fs::File::create(main_path).chain_err(|| "Unable to write main.rs")?;
65 output_f.write_all(formatted_code.as_bytes()).chain_err(|| "Unable to write main.rs")?;
66
67 create_toml(&script_name, &crates, &output_crate_dir)?;
68 compile(output_crate_dir, verbose)
69}
70
71fn create_toml<P: AsRef<Path>>(script_name: &str,
72 extern_crates: &[CrateImport],

Callers 1

runFunction · 0.85

Calls 5

read_fileFunction · 0.85
extract_extern_cratesFunction · 0.85
format_codeFunction · 0.85
create_tomlFunction · 0.85
compileFunction · 0.85

Tested by

no test coverage detected