提交 e731ccdd authored 作者: Serhij S's avatar Serhij S

restart command and build-custom

上级 f3b80c42
......@@ -81,6 +81,10 @@ pub fn find_robo_toml() -> Option<PathBuf> {
}
}
if !current_dir.pop() {
let local_path = PathBuf::from(CONFIG_FILE_NAME);
if local_path.exists() {
return Some(local_path);
}
break;
}
}
......
......@@ -10,6 +10,8 @@ pub struct Config {
pub remote: Remote,
#[serde(default)]
pub build: Build,
#[serde(default, rename = "build-custom")]
pub build_custom: BuildCustom,
}
#[derive(Deserialize, Serialize, Default, Debug)]
......@@ -32,6 +34,14 @@ pub struct Build {
pub cargo_args: Option<String>,
}
#[derive(Deserialize, Serialize, Default, Debug)]
pub struct BuildCustom {
#[serde(skip_serializing_if = "Option::is_none")]
pub command: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub file: Option<PathBuf>,
}
#[derive(Deserialize, Debug)]
struct GlobalConfig {
remote: BTreeMap<String, Remote>,
......
......@@ -21,10 +21,13 @@ fn flash_file(
url: &str,
key: &str,
agent: Agent,
file: PathBuf,
file: &Path,
force: bool,
run: bool,
) -> Result<(), Box<dyn std::error::Error>> {
if !file.exists() {
return Err(format!("File not found: {}", file.display()).into());
}
let (content_type, data) = MultipartBuilder::new()
.add_file("file", file)?
.add_text(
......@@ -47,15 +50,55 @@ fn flash_file(
Ok(())
}
fn run_build_custom(
url: &str,
key: &str,
agent: Agent,
force: bool,
run: bool,
cmd: &str,
file: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
println!("Remote: {}", url.yellow());
println!("Build command line: {}", cmd.yellow());
println!("Binary: {}", file.display().to_string().yellow());
println!("Compiling...");
let result = std::process::Command::new("sh")
.args(["-c", cmd])
.status()?;
if !result.success() {
return Err("Compilation failed".into());
}
println!("Flashing...");
if !file.exists() {
return Err(format!("File not found: {}", file.display()).into());
}
flash_file(url, key, agent, file, force, run)?;
Ok(())
}
pub fn flash(
url: &str,
key: &str,
agent: Agent,
opts: FlashCommand,
build_config: config::Build,
build_custom: config::BuildCustom,
) -> Result<(), Box<dyn std::error::Error>> {
if let Some(file) = opts.file {
flash_file(url, key, agent, file, opts.force, opts.run)?;
flash_file(url, key, agent, &file, opts.force, opts.run)?;
} else if let Some(custom_cmd) = build_custom.command {
run_build_custom(
url,
key,
agent,
opts.force,
opts.run,
&custom_cmd,
&build_custom
.file
.ok_or("Custom build command requires a file")?,
)?;
} else {
let mut cargo_target: Option<String> = None;
if let Some(c) = opts.cargo_target {
......@@ -120,7 +163,7 @@ pub fn flash(
return Err("Compilation failed".into());
}
println!("Flashing...");
flash_file(url, key, agent, binary_name, opts.force, opts.run)?;
flash_file(url, key, agent, &binary_name, opts.force, opts.run)?;
}
report_ok()
}
......
......@@ -40,6 +40,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
let mut maybe_timeout = args.timeout;
let mut build_config = None;
let mut build_custom = None;
if let SubCommand::New(_) = args.subcmd {
// do not parse robo.toml for `new` command
} else if let Some(robo_toml_path) = find_robo_toml() {
......@@ -55,6 +56,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
maybe_timeout = robo_toml.remote.timeout;
}
build_config = Some(robo_toml.build);
build_custom = Some(robo_toml.build_custom);
}
maybe_url = maybe_url.map(|v| {
let mut u = v.trim_end_matches('/').to_owned();
......@@ -92,7 +94,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
remote::set_mode(&url, &key, &agent, Mode::Run, true)?;
}
SubCommand::Flash(opts) => {
flashing::flash(&url, &key, agent, opts, build_config.unwrap_or_default())?;
flashing::flash(
&url,
&key,
agent,
opts,
build_config.unwrap_or_default(),
build_custom.unwrap_or_default(),
)?;
}
SubCommand::Purge => {
remote::purge(&url, &key, agent)?;
......
......@@ -43,6 +43,7 @@ pub fn create(
timeout: maybe_timeout,
},
build: <_>::default(),
build_custom: <_>::default(),
};
std::fs::write(CONFIG_FILE_NAME, toml::to_string_pretty(&robo_toml)?)?;
std::fs::write("src/main.rs", prepare_main(TPL_DEFAULT_RS, &robo_features))?;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论