mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 23:31:12 +00:00
roc run
passes through arguments and exit code
This commit is contained in:
parent
962de95492
commit
da7dffe0e9
4 changed files with 178 additions and 70 deletions
138
cli/src/lib.rs
138
cli/src/lib.rs
|
@ -1,12 +1,13 @@
|
|||
#[macro_use]
|
||||
extern crate clap;
|
||||
|
||||
use build::{build_file, BuildOutcome, BuiltFile};
|
||||
use bumpalo::Bump;
|
||||
use clap::ArgMatches;
|
||||
use clap::{App, Arg};
|
||||
use clap::{App, AppSettings, Arg, ArgMatches};
|
||||
use roc_build::link::LinkType;
|
||||
use roc_gen::llvm::build::OptLevel;
|
||||
use roc_load::file::LoadingProblem;
|
||||
use std::env;
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process;
|
||||
|
@ -16,18 +17,25 @@ use target_lexicon::Triple;
|
|||
pub mod build;
|
||||
pub mod repl;
|
||||
|
||||
pub static FLAG_DEBUG: &str = "debug";
|
||||
pub static FLAG_OPTIMIZE: &str = "optimize";
|
||||
pub static FLAG_ROC_FILE: &str = "ROC_FILE";
|
||||
pub static DIRECTORY_OR_FILES: &str = "DIRECTORY_OR_FILES";
|
||||
pub const CMD_RUN: &str = "run";
|
||||
pub const CMD_BUILD: &str = "build";
|
||||
pub const CMD_REPL: &str = "repl";
|
||||
pub const CMD_EDIT: &str = "edit";
|
||||
pub const CMD_DOCS: &str = "docs";
|
||||
|
||||
pub const FLAG_DEBUG: &str = "debug";
|
||||
pub const FLAG_OPTIMIZE: &str = "optimize";
|
||||
pub const ROC_FILE: &str = "ROC_FILE";
|
||||
pub const DIRECTORY_OR_FILES: &str = "DIRECTORY_OR_FILES";
|
||||
pub const ARGS_FOR_APP: &str = "ARGS_FOR_APP";
|
||||
|
||||
pub fn build_app<'a>() -> App<'a> {
|
||||
App::new("roc")
|
||||
.version(crate_version!())
|
||||
.subcommand(App::new("build")
|
||||
.subcommand(App::new(CMD_BUILD)
|
||||
.about("Build a program")
|
||||
.arg(
|
||||
Arg::with_name(FLAG_ROC_FILE)
|
||||
Arg::with_name(ROC_FILE)
|
||||
.help("The .roc file to build")
|
||||
.required(true),
|
||||
)
|
||||
|
@ -44,13 +52,9 @@ pub fn build_app<'a>() -> App<'a> {
|
|||
.required(false),
|
||||
)
|
||||
)
|
||||
.subcommand(App::new("run")
|
||||
.subcommand(App::new(CMD_RUN)
|
||||
.about("Build and run a program")
|
||||
.arg(
|
||||
Arg::with_name(FLAG_ROC_FILE)
|
||||
.help("The .roc file to build and run")
|
||||
.required(true),
|
||||
)
|
||||
.setting(AppSettings::TrailingVarArg)
|
||||
.arg(
|
||||
Arg::with_name(FLAG_OPTIMIZE)
|
||||
.long(FLAG_OPTIMIZE)
|
||||
|
@ -63,11 +67,21 @@ pub fn build_app<'a>() -> App<'a> {
|
|||
.help("Store LLVM debug information in the generated program")
|
||||
.required(false),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name(ROC_FILE)
|
||||
.help("The .roc file of an app to build and run")
|
||||
.required(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name(ARGS_FOR_APP)
|
||||
.help("Arguments to pass into the app being run")
|
||||
.multiple(true),
|
||||
)
|
||||
)
|
||||
.subcommand(App::new("repl")
|
||||
.subcommand(App::new(CMD_REPL)
|
||||
.about("Launch the interactive Read Eval Print Loop (REPL)")
|
||||
)
|
||||
.subcommand(App::new("edit")
|
||||
.subcommand(App::new(CMD_EDIT)
|
||||
.about("Launch the Roc editor")
|
||||
.arg(Arg::with_name(DIRECTORY_OR_FILES)
|
||||
.index(1)
|
||||
|
@ -77,7 +91,7 @@ pub fn build_app<'a>() -> App<'a> {
|
|||
)
|
||||
)
|
||||
.subcommand(
|
||||
App::new("docs")
|
||||
App::new(CMD_DOCS)
|
||||
.about("Generate documentation for Roc modules")
|
||||
.arg(Arg::with_name(DIRECTORY_OR_FILES)
|
||||
.index(1)
|
||||
|
@ -97,10 +111,18 @@ pub fn docs(files: Vec<PathBuf>) {
|
|||
)
|
||||
}
|
||||
|
||||
pub fn build(target: &Triple, matches: &ArgMatches, run_after_build: bool) -> io::Result<()> {
|
||||
let arena = Bump::new();
|
||||
let filename = matches.value_of(FLAG_ROC_FILE).unwrap();
|
||||
pub enum BuildConfig {
|
||||
BuildOnly,
|
||||
BuildAndRun { roc_file_arg_index: usize },
|
||||
}
|
||||
|
||||
pub fn build(target: &Triple, matches: &ArgMatches, config: BuildConfig) -> io::Result<i32> {
|
||||
use BuildConfig::*;
|
||||
|
||||
let arena = Bump::new();
|
||||
let filename = matches.value_of(ROC_FILE).unwrap();
|
||||
|
||||
let original_cwd = std::env::current_dir()?;
|
||||
let opt_level = if matches.is_present(FLAG_OPTIMIZE) {
|
||||
OptLevel::Optimize
|
||||
} else {
|
||||
|
@ -130,7 +152,7 @@ pub fn build(target: &Triple, matches: &ArgMatches, run_after_build: bool) -> io
|
|||
}
|
||||
});
|
||||
|
||||
let res_binary_path = build::build_file(
|
||||
let res_binary_path = build_file(
|
||||
&arena,
|
||||
target,
|
||||
src_dir,
|
||||
|
@ -141,23 +163,77 @@ pub fn build(target: &Triple, matches: &ArgMatches, run_after_build: bool) -> io
|
|||
);
|
||||
|
||||
match res_binary_path {
|
||||
Ok(binary_path) => {
|
||||
if run_after_build {
|
||||
// Run the compiled app
|
||||
Command::new(binary_path)
|
||||
.spawn()
|
||||
.unwrap_or_else(|err| panic!("Failed to run app after building it: {:?}", err))
|
||||
.wait()
|
||||
.expect("TODO gracefully handle block_on failing");
|
||||
Ok(BuiltFile {
|
||||
binary_path,
|
||||
outcome,
|
||||
total_time,
|
||||
}) => {
|
||||
match config {
|
||||
BuildOnly => {
|
||||
// If possible, report the generated executable name relative to the current dir.
|
||||
let generated_filename = binary_path
|
||||
.strip_prefix(env::current_dir().unwrap())
|
||||
.unwrap_or(&binary_path);
|
||||
|
||||
println!(
|
||||
"🎉 Built {} in {} ms",
|
||||
generated_filename.to_str().unwrap(),
|
||||
total_time.as_millis()
|
||||
);
|
||||
|
||||
// Return a nonzero exit code if there were problems
|
||||
let status_code = match outcome {
|
||||
BuildOutcome::NoProblems => 0,
|
||||
BuildOutcome::OnlyWarnings => 1,
|
||||
BuildOutcome::Errors => 2,
|
||||
};
|
||||
|
||||
Ok(status_code)
|
||||
}
|
||||
BuildAndRun { roc_file_arg_index } => {
|
||||
let mut cmd = Command::new(binary_path);
|
||||
|
||||
// Forward all the arguments after the .roc file argument
|
||||
// to the new process. This way, you can do things like:
|
||||
//
|
||||
// roc run app.roc foo bar baz
|
||||
//
|
||||
// ...and have it so that app.roc will receive only `foo`,
|
||||
// `bar`, and `baz` as its arguments.
|
||||
for (index, arg) in std::env::args().enumerate() {
|
||||
if index > roc_file_arg_index {
|
||||
cmd.arg(arg);
|
||||
}
|
||||
}
|
||||
|
||||
// Run the compiled app
|
||||
let exit_status = cmd
|
||||
.current_dir(original_cwd)
|
||||
.spawn()
|
||||
.unwrap_or_else(|err| panic!("Failed to run app after building it: {:?}", err))
|
||||
.wait()
|
||||
.expect("TODO gracefully handle block_on failing when roc run spawns a subprocess for the compiled app");
|
||||
|
||||
// `roc run` exits with the same status code as the app it ran.
|
||||
//
|
||||
// If you want to know whether there were compilation problems
|
||||
// via status code, use either `roc build` or `roc check` instead!
|
||||
match exit_status.code() {
|
||||
Some(code) => Ok(code),
|
||||
None => {
|
||||
todo!("TODO gracefully handle the roc run subprocess terminating with a signal.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(LoadingProblem::FormattedReport(report)) => {
|
||||
print!("{}", report);
|
||||
|
||||
Ok(1)
|
||||
}
|
||||
Err(other) => {
|
||||
panic!("build_file failed with error:\n{:?}", other);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue