From 0330ae2af5728beaf15ce93ae9eaae3ad5c9ca9d Mon Sep 17 00:00:00 2001 From: Linfeng Qian Date: Wed, 10 Aug 2022 15:50:01 +0800 Subject: [PATCH] feat: allow custom rustup dir --- src/bin/capsule.rs | 17 ++++++++++++++++- src/project_context.rs | 2 ++ src/recipe/rust.rs | 6 +++++- templates/capsule.toml | 2 +- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/bin/capsule.rs b/src/bin/capsule.rs index 89d1c7e..63b9d33 100644 --- a/src/bin/capsule.rs +++ b/src/bin/capsule.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::env; use std::fs; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::process::exit; use std::str::FromStr; @@ -104,6 +104,7 @@ fn run_cli() -> Result<()> { .arg(Arg::with_name("release").long("release").help("Build contracts in release mode.")) .arg(Arg::with_name("debug-output").long("debug-output").help("Always enable debugging output")) .arg(Arg::with_name("host").long("host").help("Docker runs in host mode")) + .arg(Arg::with_name("rustup-dir").long("rustup-dir").takes_value(true).help("Mount the directory to /root/.rustup in docker image")) .display_order(3)) .subcommand(SubCommand::with_name("run").about("Run command in contract build image").usage("ckb_capsule run --name 'echo list contract dir: && ls'") .args(&[Arg::with_name("name").short("n").long("name").required(true).takes_value(true).help("contract name"), @@ -279,8 +280,22 @@ fn run_cli() -> Result<()> { BuildEnv::Debug }; let always_debug = args.is_present("debug-output"); + let rustup_dir = args + .value_of("rustup-dir") + .map(|value| { + let path = Path::new(value); + if !path.exists() { + return Err(anyhow!("rustup path not exists: {}", value)); + } + if !path.is_dir() { + return Err(anyhow!("rustup path is not directory: {}", value)); + } + Ok(value.to_string()) + }) + .transpose()?; context.use_docker_host = args.is_present("host"); context.docker_env_file = docker_env_file; + context.rustup_dir = rustup_dir; let build_config = BuildConfig { build_env, always_debug, diff --git a/src/project_context.rs b/src/project_context.rs index 60f3d4e..acaeecb 100644 --- a/src/project_context.rs +++ b/src/project_context.rs @@ -62,6 +62,7 @@ pub struct Context { pub config: Config, pub use_docker_host: bool, pub docker_env_file: String, + pub rustup_dir: Option, } impl Context { @@ -92,6 +93,7 @@ impl Context { project_path, use_docker_host: false, docker_env_file: String::new(), + rustup_dir: None, }) } diff --git a/src/recipe/rust.rs b/src/recipe/rust.rs index bfd5122..e532bf4 100644 --- a/src/recipe/rust.rs +++ b/src/recipe/rust.rs @@ -168,7 +168,7 @@ impl Recipe for Rust { fn run(&self, contract: &Contract, build_cmd: String, signal: &Signal) -> Result<()> { let project_path = self.context.project_path.to_str().expect("path"); let contract_relative_path = self.contract_relative_path(&contract.name); - let cmd = DockerCommand::with_context( + let mut cmd = DockerCommand::with_context( &self.context, self.docker_image(), project_path.to_string(), @@ -181,6 +181,9 @@ impl Recipe for Rust { .fix_dir_permission("/code/target".to_string()) .fix_dir_permission("/code/Cargo.lock".to_string()) .host_network(self.context.use_docker_host); + if let Some(rustup_dir) = self.context.rustup_dir.as_ref() { + cmd = cmd.map_volume(rustup_dir.to_string(), "/root/.rustup".to_string()); + } cmd.run(build_cmd, &signal)?; Ok(()) @@ -224,6 +227,7 @@ impl Recipe for Rust { rust_target = RUST_TARGET, build_env = build_cmd_opt ); + log::debug!("[build cmd]: {}", build_cmd); self.run(contract, build_cmd, signal)?; // copy to build dir diff --git a/templates/capsule.toml b/templates/capsule.toml index 8918b42..dcc1753 100644 --- a/templates/capsule.toml +++ b/templates/capsule.toml @@ -8,4 +8,4 @@ # capsule version version = "{{ version }}" # path of deployment config file -deployment = "deployment.toml" +deployment = "deployment.toml" \ No newline at end of file