1- use anyhow:: Context as _;
2-
31use cargo_util_schemas:: manifest:: PackageName ;
42
53use crate :: util:: restricted_names;
64use crate :: CargoResult ;
7- use crate :: GlobalContext ;
85
9- pub ( super ) fn expand_manifest (
10- content : & str ,
11- path : & std:: path:: Path ,
12- gctx : & GlobalContext ,
13- ) -> CargoResult < String > {
6+ pub ( super ) fn expand_manifest ( content : & str ) -> CargoResult < String > {
147 let source = ScriptSource :: parse ( content) ?;
158 if let Some ( frontmatter) = source. frontmatter ( ) {
169 match source. info ( ) {
@@ -24,74 +17,13 @@ pub(super) fn expand_manifest(
2417 }
2518 }
2619
27- // HACK: until rustc has native support for this syntax, we have to remove it from the
28- // source file
29- use std:: fmt:: Write as _;
30- let hash = crate :: util:: hex:: short_hash ( & path. to_string_lossy ( ) ) ;
31- let mut rel_path = std:: path:: PathBuf :: new ( ) ;
32- rel_path. push ( "target" ) ;
33- rel_path. push ( & hash[ 0 ..2 ] ) ;
34- rel_path. push ( & hash[ 2 ..] ) ;
35- let target_dir = gctx. home ( ) . join ( rel_path) ;
36- let hacked_path = target_dir
37- . join (
38- path. file_name ( )
39- . expect ( "always a name for embedded manifests" ) ,
40- )
41- . into_path_unlocked ( ) ;
42- let mut hacked_source = String :: new ( ) ;
43- if let Some ( shebang) = source. shebang ( ) {
44- writeln ! ( hacked_source, "{shebang}" ) ?;
45- }
46- writeln ! ( hacked_source) ?; // open
47- for _ in 0 ..frontmatter. lines ( ) . count ( ) {
48- writeln ! ( hacked_source) ?;
49- }
50- writeln ! ( hacked_source) ?; // close
51- writeln ! ( hacked_source, "{}" , source. content( ) ) ?;
52- if let Some ( parent) = hacked_path. parent ( ) {
53- cargo_util:: paths:: create_dir_all ( parent) ?;
54- }
55- cargo_util:: paths:: write_if_changed ( & hacked_path, hacked_source) ?;
56-
57- let manifest = inject_bin_path ( & frontmatter, & hacked_path)
58- . with_context ( || format ! ( "failed to parse manifest at `{}`" , path. display( ) ) ) ?;
59- let manifest = toml:: to_string_pretty ( & manifest) ?;
60- Ok ( manifest)
20+ Ok ( frontmatter. to_owned ( ) )
6121 } else {
6222 let frontmatter = "" ;
63- let manifest = inject_bin_path ( frontmatter, path)
64- . with_context ( || format ! ( "failed to parse manifest at `{}`" , path. display( ) ) ) ?;
65- let manifest = toml:: to_string_pretty ( & manifest) ?;
66- Ok ( manifest)
23+ Ok ( frontmatter. to_owned ( ) )
6724 }
6825}
6926
70- /// HACK: Add a `[[bin]]` table to the `original_toml`
71- fn inject_bin_path ( manifest : & str , path : & std:: path:: Path ) -> CargoResult < toml:: Table > {
72- let mut manifest: toml:: Table = toml:: from_str ( & manifest) ?;
73-
74- let bin_path = path. to_string_lossy ( ) . into_owned ( ) ;
75- let file_stem = path
76- . file_stem ( )
77- . ok_or_else ( || anyhow:: format_err!( "no file name" ) ) ?
78- . to_string_lossy ( ) ;
79- let name = sanitize_name ( file_stem. as_ref ( ) ) ;
80- let bin_name = name. clone ( ) ;
81-
82- let mut bin = toml:: Table :: new ( ) ;
83- bin. insert ( "name" . to_owned ( ) , toml:: Value :: String ( bin_name) ) ;
84- bin. insert ( "path" . to_owned ( ) , toml:: Value :: String ( bin_path) ) ;
85- manifest
86- . entry ( "bin" )
87- . or_insert_with ( || Vec :: < toml:: Value > :: new ( ) . into ( ) )
88- . as_array_mut ( )
89- . ok_or_else ( || anyhow:: format_err!( "`bin` must be an array" ) ) ?
90- . push ( toml:: Value :: Table ( bin) ) ;
91-
92- Ok ( manifest)
93- }
94-
9527/// Ensure the package name matches the validation from `ops::cargo_new::check_name`
9628pub fn sanitize_name ( name : & str ) -> String {
9729 let placeholder = if name. contains ( '_' ) {
@@ -584,25 +516,12 @@ fn main() {}
584516
585517 #[ track_caller]
586518 fn expand ( source : & str ) -> String {
587- let shell = crate :: Shell :: from_write ( Box :: new ( Vec :: new ( ) ) ) ;
588- let cwd = std:: env:: current_dir ( ) . unwrap ( ) ;
589- let home = home:: cargo_home_with_cwd ( & cwd) . unwrap ( ) ;
590- let gctx = GlobalContext :: new ( shell, cwd, home) ;
591- expand_manifest ( source, std:: path:: Path :: new ( "/home/me/test.rs" ) , & gctx)
592- . unwrap_or_else ( |err| panic ! ( "{}" , err) )
519+ expand_manifest ( source) . unwrap_or_else ( |err| panic ! ( "{}" , err) )
593520 }
594521
595522 #[ test]
596523 fn expand_default ( ) {
597- assert_data_eq ! (
598- expand( r#"fn main() {}"# ) ,
599- str ![ [ r#"
600- [[bin]]
601- name = "test-"
602- path = "/home/me/test.rs"
603-
604- "# ] ]
605- ) ;
524+ assert_data_eq ! ( expand( r#"fn main() {}"# ) , str ![ "" ] ) ;
606525 }
607526
608527 #[ test]
@@ -617,12 +536,8 @@ fn main() {}
617536"#
618537 ) ,
619538 str ![ [ r#"
620- [[bin]]
621- name = "test-"
622- path = [..]
623-
624539[dependencies]
625- time = "0.1.25"
540+ time= "0.1.25"
626541
627542"# ] ]
628543 ) ;
0 commit comments