-
Notifications
You must be signed in to change notification settings - Fork 199
/
main.rs
115 lines (101 loc) · 3.23 KB
/
main.rs
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
#![deny(rust_2018_idioms)]
use rust_playground_top_crates::*;
use serde::Serialize;
use std::{
collections::BTreeMap,
fs::{self, File},
path::{Path, PathBuf},
};
/// A Cargo.toml file.
#[derive(Serialize)]
#[serde(rename_all = "kebab-case")]
struct TomlManifest {
package: TomlPackage,
profile: Profiles,
dependencies: BTreeMap<String, DependencySpec>,
build_dependencies: BTreeMap<String, DependencySpec>,
}
/// Header of Cargo.toml file.
#[derive(Serialize)]
struct TomlPackage {
name: String,
version: String,
authors: Vec<String>,
resolver: String,
}
/// Profile used for build dependencies (build scripts, proc macros, and their
/// dependencies).
#[derive(Serialize)]
#[serde(rename_all = "kebab-case")]
struct BuildOverride {
codegen_units: u32,
debug: bool,
}
/// A profile section in a Cargo.toml file
#[derive(Serialize)]
#[serde(rename_all = "kebab-case")]
struct Profile {
codegen_units: u32,
incremental: bool,
build_override: BuildOverride,
}
/// Available profile types
#[derive(Serialize)]
struct Profiles {
dev: Profile,
release: Profile,
}
fn main() {
let d = fs::read_to_string("crate-modifications.toml")
.expect("unable to read crate modifications file");
let modifications: Modifications =
toml::from_str(&d).expect("unable to parse crate modifications file");
let (dependencies, infos) = rust_playground_top_crates::generate_info(&modifications);
// Construct playground's Cargo.toml.
let manifest = TomlManifest {
package: TomlPackage {
name: "playground".to_owned(),
version: "0.0.1".to_owned(),
authors: vec!["The Rust Playground".to_owned()],
resolver: "2".to_owned(),
},
profile: Profiles {
dev: Profile {
codegen_units: 1,
incremental: false,
build_override: BuildOverride {
codegen_units: 1,
debug: true,
},
},
release: Profile {
codegen_units: 1,
incremental: false,
build_override: BuildOverride {
codegen_units: 1,
debug: false,
},
},
},
dependencies: dependencies.clone(),
build_dependencies: dependencies,
};
// Write manifest file.
let base_directory: PathBuf = std::env::args_os()
.nth(1)
.unwrap_or_else(|| "../compiler/base".into())
.into();
let cargo_toml = base_directory.join("Cargo.toml");
write_manifest(manifest, &cargo_toml);
println!("wrote {}", cargo_toml.display());
let path = base_directory.join("crate-information.json");
let mut f = File::create(&path)
.unwrap_or_else(|e| panic!("Unable to create {}: {}", path.display(), e));
serde_json::to_writer_pretty(&mut f, &infos)
.unwrap_or_else(|e| panic!("Unable to write {}: {}", path.display(), e));
println!("Wrote {}", path.display());
}
fn write_manifest(manifest: TomlManifest, path: impl AsRef<Path>) {
let content = toml::to_string(&manifest).expect("Couldn't serialize TOML");
fs::write(path, content).expect("Couldn't write Cargo.toml");
}