Skip to content

Commit

Permalink
wip: refactor and functionnalities to find the runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
chevdor committed Jul 20, 2021
1 parent 3631ef1 commit f93c1fb
Show file tree
Hide file tree
Showing 15 changed files with 519 additions and 145 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 48 additions & 60 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ fn main() {
const ONE_HOUR: u64 = 60 * 60;

let tag = get_image_tag(Some(ONE_HOUR)).expect("Issue getting the image tag");

info!("Using {}:{}", image, tag);

let command = match opts.subcmd {
match opts.subcmd {
SubCommand::Pull(_) => {
println!("Found {tag}, we will be using {image}:{tag} for the build", tag = tag, image = image);
format!("docker pull {image}:{tag}", image = image, tag = tag,)
Runner::pull(&image, &tag);
}

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

let search_info = RuntimeCrateSearchInfo { workdir: workdir.to_owned(), options: None };
let _rtm_crate = RuntimeCrate::search(&search_info);

debug!("app: '{}'", &app);
debug!("json: '{}'", &json);
debug!("chain: '{}'", &chain);
Expand All @@ -72,58 +76,48 @@ fn main() {
debug!("digest: '{}'", &digest);
debug!("no-cache: '{}'", build_opts.no_cache);

let path = fs::canonicalize(&build_opts.path).unwrap();

format!(
"docker run --name srtool --rm \
-e PACKAGE={package} \
-e RUNTIME_DIR={runtime_dir} \
-e BUILD_OPTS={c_build_opts} \
-e DEFAULT_FEATURES={default_features} \
-e PROFILE={profile} \
-e IMAGE={digest} \
-v {dir}:/build \
{cache_mount} \
{image}:{tag} build{app}{json}",
package = build_opts.package,
dir = path.display(),
cache_mount = cache_mount,
image = image,
tag = tag,
runtime_dir = runtime_dir.display(),
c_build_opts = build_opts.build_opts.unwrap_or_else(|| String::from("")),
default_features = build_opts.default_features.unwrap_or_else(|| String::from("")),
profile = build_opts.profile,
json = json,
app = app,
digest = digest,
)
let specs = RunSpecs::new(&package, &runtime_dir, &profile, &image, &tag);
let opts = srtool_lib::BuildOpts { json: json == "json", app: app == "app", workdir };
Runner::build(&specs, &opts);

// format!(
// "docker run --name srtool --rm \
// -e PACKAGE={package} \
// -e RUNTIME_DIR={runtime_dir} \
// -e BUILD_OPTS={c_build_opts} \
// -e DEFAULT_FEATURES={default_features} \
// -e PROFILE={profile} \
// -e IMAGE={digest} \
// -v {dir}:/build \
// {cache_mount} \
// {image}:{tag} build{app}{json}",
// package = build_opts.package,
// dir = path.display(),
// cache_mount = cache_mount,
// image = image,
// tag = tag,
// runtime_dir = runtime_dir.display(),
// c_build_opts = build_opts.build_opts.unwrap_or_else(|| String::from("")),
// default_features = build_opts.default_features.unwrap_or_else(|| String::from("")),
// profile = build_opts.profile,
// json = json,
// app = app,
// digest = digest,
// )
}

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

debug!("chain: '{}'", &chain);
debug!("default_runtime_dir: '{}'", &default_runtime_dir);
debug!("runtime_dir: '{}'", &runtime_dir.display());

format!(
"docker run --name srtool --rm \
-v {dir}:/build \
-e RUNTIME_DIR={runtime_dir} \
{image}:{tag} info",
dir = path.display(),
runtime_dir = runtime_dir.display(),
image = image,
tag = tag,
)
let specs = RunSpecs::new("", &runtime_dir, "release", &image, &tag);
Runner::info(&specs, &runtime_dir);
}

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

SubCommand::Verify(verify_opts) => {
Expand All @@ -132,22 +126,16 @@ fn main() {
let reader = BufReader::new(file);
let content: V2 = serde_json::from_reader(reader).unwrap();
let digest_json = json!({ "V2": content });
// let digest = Digest::from
// debug!("digest = {:#?}", digest);
// let specs = digest.get_run_specs();
// debug!("specs = {:#?}", specs);
let digest = DigestJson::load(json!(digest_json)).unwrap();
debug!("digest = {:#?}", digest);
let specs = digest.get_run_specs().unwrap();
debug!("specs = {:#?}", specs);
let build_opts = srtool_lib::BuildOpts { json: true, app: true, workdir: "/projects/polkadot".into() };

Runner::build(&specs, &build_opts);
todo!()
}
};

debug!("command = {:?}", command);

if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", command.as_str()]).output().expect("failed to execute process");
} else {
let _ =
Command::new("sh").arg("-c").arg(command).spawn().expect("failed to execute process").wait_with_output();
}
}

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ log = "0.4"
semver = {version = "1.0", features = ["serde"]}
serde = {version = "1.0", features = ["derive"]}
serde_json = "1.0"
toml = "0.5.8"
ureq = "2.1"
46 changes: 46 additions & 0 deletions lib/src/digest/digest_json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use super::digest_source::DigestSource;
use super::versionned_digest::Digest;
use serde_json::Result;
use serde_json::Value;
type Json = Value;

pub struct DigestJson {}

impl DigestSource<Json> for DigestJson {
fn load(src: Json) -> Result<Digest> {
let digest: Digest = serde_json::from_str(&src.to_string())?;
Ok(digest)
}
}

#[cfg(test)]
mod test_super {
use super::*;
use crate::{
digest::digest_v2,
samples::{SAMPLE_V1, SAMPLE_V2},
};
use serde_json::json;

#[test]
fn test_v1() {
let v1: Value = serde_json::from_str(SAMPLE_V1).unwrap();
let digest = DigestJson::load(json!({ "V1": v1 })).unwrap();

match digest {
Digest::V1(v1) => assert!(v1.src == "git"),
Digest::V2(v2) => assert!(v2.info.src == digest_v2::Source::Git),
}
}

#[test]
fn test_v2() {
let v2: Value = serde_json::from_str(SAMPLE_V2).unwrap();
let digest = DigestJson::load(json!({ "V2": v2 })).unwrap();

match digest {
Digest::V1(v1) => assert!(v1.src == "git"),
Digest::V2(v2) => assert!(v2.info.src == digest_v2::Source::Git),
}
}
}
File renamed without changes.
18 changes: 16 additions & 2 deletions lib/src/digest_v1.rs → lib/src/digest/digest_v1.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
use semver::Version;
use serde::{Deserialize, Serialize};
use serde::{de, Deserialize, Deserializer, Serialize};
use std::fmt::Display;
use std::str::FromStr;

//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.

fn from_str<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
T: FromStr,
T::Err: Display,
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
T::from_str(&s).map_err(de::Error::custom)
}

/// A srtool digest. The schema of the data srtool produces may
/// change over time. This struct can load all version and make
/// the common and relevant data available.
Expand All @@ -18,7 +30,9 @@ pub struct V1 {
pub(crate) rustc: String,
pub(crate) pkg: String,
pub(crate) tmsp: String,
pub(crate) size: uisze,

#[serde(deserialize_with = "from_str")]
pub(crate) size: usize,
pub(crate) prop: String,
pub(crate) ipfs: String,
pub(crate) sha256: String,
Expand Down
20 changes: 10 additions & 10 deletions lib/src/digest_v2.rs → lib/src/digest/digest_v2.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use semver::Version;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -29,25 +31,25 @@ pub struct GitInfo {
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Info {
/// Information about the tooling used for the build.
generator: Generator,
pub(crate) generator: Generator,

/// Whether the build from an Archive or from a Git repo.
src: Source,
pub(crate) src: Source,

/// The version of the crate/package to build
version: Version,
pub(crate) version: Version,

/// Optionnal Git information if the src was Git
git: Option<GitInfo>,
pub(crate) git: Option<GitInfo>,

/// Rust compiler version
rustc: String,
pub(crate) rustc: String,

/// Package
pkg: String,
pub(crate) pkg: String,

/// Profile. Always 'release'.
profile: String,
pub(crate) profile: String,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
Expand All @@ -62,7 +64,7 @@ pub struct DockerContext {
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Context {
pub(crate) docker: DockerContext,
pub(crate) runtime_dir: String,
pub(crate) runtime_dir: PathBuf,
pub(crate) package: String,
pub(crate) profile: String,
}
Expand All @@ -75,6 +77,4 @@ pub struct Context {
pub struct V2 {
pub(crate) info: Info,
pub(crate) context: Context,
#[serde(alias = "src")]
pub(crate) source: Source,
}
11 changes: 11 additions & 0 deletions lib/src/digest/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
mod digest_json;
mod digest_source;
mod digest_v1;
mod digest_v2;
mod versionned_digest;

pub use digest_json::*;
pub use digest_source::*;
pub use digest_v1::*;
pub use digest_v2::*;
pub use versionned_digest::*;
Loading

0 comments on commit f93c1fb

Please sign in to comment.