Skip to content

Commit f93c1fb

Browse files
committed
wip: refactor and functionnalities to find the runtime
1 parent 3631ef1 commit f93c1fb

15 files changed

+519
-145
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/src/main.rs

Lines changed: 48 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,11 @@ fn main() {
3838
const ONE_HOUR: u64 = 60 * 60;
3939

4040
let tag = get_image_tag(Some(ONE_HOUR)).expect("Issue getting the image tag");
41-
4241
info!("Using {}:{}", image, tag);
4342

44-
let command = match opts.subcmd {
43+
match opts.subcmd {
4544
SubCommand::Pull(_) => {
46-
println!("Found {tag}, we will be using {image}:{tag} for the build", tag = tag, image = image);
47-
format!("docker pull {image}:{tag}", image = image, tag = tag,)
45+
Runner::pull(&image, &tag);
4846
}
4947

5048
SubCommand::Build(build_opts) => {
@@ -57,12 +55,18 @@ fn main() {
5755
let runtime_dir = build_opts.runtime_dir.unwrap_or_else(|| PathBuf::from(&default_runtime_dir));
5856
let tmpdir = env::temp_dir().join("cargo");
5957
let digest = get_image_digest(&image, &tag).unwrap_or_default();
60-
let cache_mount = if !build_opts.no_cache {
58+
let package = build_opts.package;
59+
let profile = build_opts.profile;
60+
let workdir = fs::canonicalize(&build_opts.path).unwrap();
61+
let _cache_mount = if !build_opts.no_cache {
6162
format!("-v {tmpdir}:/cargo-home", tmpdir = tmpdir.display())
6263
} else {
6364
String::new()
6465
};
6566

67+
let search_info = RuntimeCrateSearchInfo { workdir: workdir.to_owned(), options: None };
68+
let _rtm_crate = RuntimeCrate::search(&search_info);
69+
6670
debug!("app: '{}'", &app);
6771
debug!("json: '{}'", &json);
6872
debug!("chain: '{}'", &chain);
@@ -72,58 +76,48 @@ fn main() {
7276
debug!("digest: '{}'", &digest);
7377
debug!("no-cache: '{}'", build_opts.no_cache);
7478

75-
let path = fs::canonicalize(&build_opts.path).unwrap();
76-
77-
format!(
78-
"docker run --name srtool --rm \
79-
-e PACKAGE={package} \
80-
-e RUNTIME_DIR={runtime_dir} \
81-
-e BUILD_OPTS={c_build_opts} \
82-
-e DEFAULT_FEATURES={default_features} \
83-
-e PROFILE={profile} \
84-
-e IMAGE={digest} \
85-
-v {dir}:/build \
86-
{cache_mount} \
87-
{image}:{tag} build{app}{json}",
88-
package = build_opts.package,
89-
dir = path.display(),
90-
cache_mount = cache_mount,
91-
image = image,
92-
tag = tag,
93-
runtime_dir = runtime_dir.display(),
94-
c_build_opts = build_opts.build_opts.unwrap_or_else(|| String::from("")),
95-
default_features = build_opts.default_features.unwrap_or_else(|| String::from("")),
96-
profile = build_opts.profile,
97-
json = json,
98-
app = app,
99-
digest = digest,
100-
)
79+
let specs = RunSpecs::new(&package, &runtime_dir, &profile, &image, &tag);
80+
let opts = srtool_lib::BuildOpts { json: json == "json", app: app == "app", workdir };
81+
Runner::build(&specs, &opts);
82+
83+
// format!(
84+
// "docker run --name srtool --rm \
85+
// -e PACKAGE={package} \
86+
// -e RUNTIME_DIR={runtime_dir} \
87+
// -e BUILD_OPTS={c_build_opts} \
88+
// -e DEFAULT_FEATURES={default_features} \
89+
// -e PROFILE={profile} \
90+
// -e IMAGE={digest} \
91+
// -v {dir}:/build \
92+
// {cache_mount} \
93+
// {image}:{tag} build{app}{json}",
94+
// package = build_opts.package,
95+
// dir = path.display(),
96+
// cache_mount = cache_mount,
97+
// image = image,
98+
// tag = tag,
99+
// runtime_dir = runtime_dir.display(),
100+
// c_build_opts = build_opts.build_opts.unwrap_or_else(|| String::from("")),
101+
// default_features = build_opts.default_features.unwrap_or_else(|| String::from("")),
102+
// profile = build_opts.profile,
103+
// json = json,
104+
// app = app,
105+
// digest = digest,
106+
// )
101107
}
102108

103109
SubCommand::Info(info_opts) => {
104-
let path = fs::canonicalize(&info_opts.path).unwrap();
110+
// let path = fs::canonicalize(&info_opts.path).unwrap();
105111
let chain = info_opts.package.replace("-runtime", "");
106112
let default_runtime_dir = format!("runtime/{}", chain);
107113
let runtime_dir = info_opts.runtime_dir.unwrap_or_else(|| PathBuf::from(&default_runtime_dir));
108114

109-
debug!("chain: '{}'", &chain);
110-
debug!("default_runtime_dir: '{}'", &default_runtime_dir);
111-
debug!("runtime_dir: '{}'", &runtime_dir.display());
112-
113-
format!(
114-
"docker run --name srtool --rm \
115-
-v {dir}:/build \
116-
-e RUNTIME_DIR={runtime_dir} \
117-
{image}:{tag} info",
118-
dir = path.display(),
119-
runtime_dir = runtime_dir.display(),
120-
image = image,
121-
tag = tag,
122-
)
115+
let specs = RunSpecs::new("", &runtime_dir, "release", &image, &tag);
116+
Runner::info(&specs, &runtime_dir);
123117
}
124118

125119
SubCommand::Version(_) => {
126-
format!("docker run --name srtool --rm {image}:{tag} version", image = image, tag = tag,)
120+
Runner::version(&image, &tag);
127121
}
128122

129123
SubCommand::Verify(verify_opts) => {
@@ -132,22 +126,16 @@ fn main() {
132126
let reader = BufReader::new(file);
133127
let content: V2 = serde_json::from_reader(reader).unwrap();
134128
let digest_json = json!({ "V2": content });
135-
// let digest = Digest::from
136-
// debug!("digest = {:#?}", digest);
137-
// let specs = digest.get_run_specs();
138-
// debug!("specs = {:#?}", specs);
129+
let digest = DigestJson::load(json!(digest_json)).unwrap();
130+
debug!("digest = {:#?}", digest);
131+
let specs = digest.get_run_specs().unwrap();
132+
debug!("specs = {:#?}", specs);
133+
let build_opts = srtool_lib::BuildOpts { json: true, app: true, workdir: "/projects/polkadot".into() };
134+
135+
Runner::build(&specs, &build_opts);
139136
todo!()
140137
}
141138
};
142-
143-
debug!("command = {:?}", command);
144-
145-
if cfg!(target_os = "windows") {
146-
Command::new("cmd").args(&["/C", command.as_str()]).output().expect("failed to execute process");
147-
} else {
148-
let _ =
149-
Command::new("sh").arg("-c").arg(command).spawn().expect("failed to execute process").wait_with_output();
150-
}
151139
}
152140

153141
#[cfg(test)]

lib/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ log = "0.4"
1111
semver = {version = "1.0", features = ["serde"]}
1212
serde = {version = "1.0", features = ["derive"]}
1313
serde_json = "1.0"
14+
toml = "0.5.8"
1415
ureq = "2.1"

lib/src/digest/digest_json.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use super::digest_source::DigestSource;
2+
use super::versionned_digest::Digest;
3+
use serde_json::Result;
4+
use serde_json::Value;
5+
type Json = Value;
6+
7+
pub struct DigestJson {}
8+
9+
impl DigestSource<Json> for DigestJson {
10+
fn load(src: Json) -> Result<Digest> {
11+
let digest: Digest = serde_json::from_str(&src.to_string())?;
12+
Ok(digest)
13+
}
14+
}
15+
16+
#[cfg(test)]
17+
mod test_super {
18+
use super::*;
19+
use crate::{
20+
digest::digest_v2,
21+
samples::{SAMPLE_V1, SAMPLE_V2},
22+
};
23+
use serde_json::json;
24+
25+
#[test]
26+
fn test_v1() {
27+
let v1: Value = serde_json::from_str(SAMPLE_V1).unwrap();
28+
let digest = DigestJson::load(json!({ "V1": v1 })).unwrap();
29+
30+
match digest {
31+
Digest::V1(v1) => assert!(v1.src == "git"),
32+
Digest::V2(v2) => assert!(v2.info.src == digest_v2::Source::Git),
33+
}
34+
}
35+
36+
#[test]
37+
fn test_v2() {
38+
let v2: Value = serde_json::from_str(SAMPLE_V2).unwrap();
39+
let digest = DigestJson::load(json!({ "V2": v2 })).unwrap();
40+
41+
match digest {
42+
Digest::V1(v1) => assert!(v1.src == "git"),
43+
Digest::V2(v2) => assert!(v2.info.src == digest_v2::Source::Git),
44+
}
45+
}
46+
}
File renamed without changes.

lib/src/digest_v1.rs renamed to lib/src/digest/digest_v1.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
use semver::Version;
2-
use serde::{Deserialize, Serialize};
2+
use serde::{de, Deserialize, Deserializer, Serialize};
3+
use std::fmt::Display;
4+
use std::str::FromStr;
35

46
//TODO: in V2, in order to NOT break compatibility, some fields are duplicated. That must be reworked. The profile for instance should be in the Context only.
57

8+
fn from_str<'de, T, D>(deserializer: D) -> Result<T, D::Error>
9+
where
10+
T: FromStr,
11+
T::Err: Display,
12+
D: Deserializer<'de>,
13+
{
14+
let s = String::deserialize(deserializer)?;
15+
T::from_str(&s).map_err(de::Error::custom)
16+
}
17+
618
/// A srtool digest. The schema of the data srtool produces may
719
/// change over time. This struct can load all version and make
820
/// the common and relevant data available.
@@ -18,7 +30,9 @@ pub struct V1 {
1830
pub(crate) rustc: String,
1931
pub(crate) pkg: String,
2032
pub(crate) tmsp: String,
21-
pub(crate) size: uisze,
33+
34+
#[serde(deserialize_with = "from_str")]
35+
pub(crate) size: usize,
2236
pub(crate) prop: String,
2337
pub(crate) ipfs: String,
2438
pub(crate) sha256: String,

lib/src/digest_v2.rs renamed to lib/src/digest/digest_v2.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::path::PathBuf;
2+
13
use semver::Version;
24
use serde::{Deserialize, Serialize};
35

@@ -29,25 +31,25 @@ pub struct GitInfo {
2931
#[derive(Debug, PartialEq, Serialize, Deserialize)]
3032
pub struct Info {
3133
/// Information about the tooling used for the build.
32-
generator: Generator,
34+
pub(crate) generator: Generator,
3335

3436
/// Whether the build from an Archive or from a Git repo.
35-
src: Source,
37+
pub(crate) src: Source,
3638

3739
/// The version of the crate/package to build
38-
version: Version,
40+
pub(crate) version: Version,
3941

4042
/// Optionnal Git information if the src was Git
41-
git: Option<GitInfo>,
43+
pub(crate) git: Option<GitInfo>,
4244

4345
/// Rust compiler version
44-
rustc: String,
46+
pub(crate) rustc: String,
4547

4648
/// Package
47-
pkg: String,
49+
pub(crate) pkg: String,
4850

4951
/// Profile. Always 'release'.
50-
profile: String,
52+
pub(crate) profile: String,
5153
}
5254

5355
#[derive(Debug, PartialEq, Serialize, Deserialize)]
@@ -62,7 +64,7 @@ pub struct DockerContext {
6264
#[derive(Debug, PartialEq, Serialize, Deserialize)]
6365
pub struct Context {
6466
pub(crate) docker: DockerContext,
65-
pub(crate) runtime_dir: String,
67+
pub(crate) runtime_dir: PathBuf,
6668
pub(crate) package: String,
6769
pub(crate) profile: String,
6870
}
@@ -75,6 +77,4 @@ pub struct Context {
7577
pub struct V2 {
7678
pub(crate) info: Info,
7779
pub(crate) context: Context,
78-
#[serde(alias = "src")]
79-
pub(crate) source: Source,
8080
}

lib/src/digest/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
mod digest_json;
2+
mod digest_source;
3+
mod digest_v1;
4+
mod digest_v2;
5+
mod versionned_digest;
6+
7+
pub use digest_json::*;
8+
pub use digest_source::*;
9+
pub use digest_v1::*;
10+
pub use digest_v2::*;
11+
pub use versionned_digest::*;

0 commit comments

Comments
 (0)