Skip to content

Commit

Permalink
feat(cli): add '--watch' argument to the 'build' subcommand to run th…
Browse files Browse the repository at this point in the history
…e compiler_daemon
  • Loading branch information
Wodann committed Oct 6, 2019
1 parent 64d9e04 commit caac5c6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
1 change: 1 addition & 0 deletions crates/mun/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2018"
failure = "0.1.5"
clap = "2.33.0"
mun_compiler = { path = "../mun_compiler" }
mun_compiler_daemon = { path = "../mun_compiler_daemon" }

[build-dependencies]
lazy_static = "1.4.0"
Expand Down
48 changes: 26 additions & 22 deletions crates/mun/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ fn main() -> Result<(), failure::Error> {
.required(true)
.index(1),
)
.arg(Arg::with_name("watch").long("watch").help(
"Run the compiler in watch mode.
Watch input files and trigger recompilation on changes.",
))
.arg(
Arg::with_name("opt-level")
.short("O")
Expand All @@ -35,35 +39,35 @@ fn main() -> Result<(), failure::Error> {
.get_matches();

match matches.subcommand() {
("build", Some(matches)) => {
let optimization_lvl = match matches.value_of("opt-level") {
Some("0") => mun_compiler::OptimizationLevel::None,
Some("1") => mun_compiler::OptimizationLevel::Less,
None | Some("2") => mun_compiler::OptimizationLevel::Default,
Some("3") => mun_compiler::OptimizationLevel::Aggressive,
_ => return Err(format_err!("Only optimization levels 0-3 are supported")),
};

let target = matches.value_of("target").map(|t| t.to_string());

build(matches, optimization_lvl, target)?
}
("build", Some(matches)) => build(matches)?,
_ => unreachable!(),
}

Ok(())
}

/// Build the source file specified
fn build(
matches: &ArgMatches,
optimization_lvl: mun_compiler::OptimizationLevel,
target: Option<String>,
) -> Result<(), failure::Error> {
let options = mun_compiler::CompilerOptions {
fn build(matches: &ArgMatches) -> Result<(), failure::Error> {
let options = compiler_options(matches)?;
if matches.is_present("watch") {
mun_compiler_daemon::main(&options)
} else {
mun_compiler::main(&options)
}
}

fn compiler_options(matches: &ArgMatches) -> Result<mun_compiler::CompilerOptions, failure::Error> {
let optimization_lvl = match matches.value_of("opt-level") {
Some("0") => mun_compiler::OptimizationLevel::None,
Some("1") => mun_compiler::OptimizationLevel::Less,
None | Some("2") => mun_compiler::OptimizationLevel::Default,
Some("3") => mun_compiler::OptimizationLevel::Aggressive,
_ => return Err(format_err!("Only optimization levels 0-3 are supported")),
};

Ok(mun_compiler::CompilerOptions {
input: matches.value_of("INPUT").unwrap().into(), // Safe because its a required arg
target,
target: matches.value_of("target").map(|t| t.to_string()),
optimization_lvl,
};
mun_compiler::main(options)
})
}
2 changes: 1 addition & 1 deletion crates/mun/test/main.mun
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ fn multiply(a:float, b:float):float {

fn main():int {
5
}
}
4 changes: 2 additions & 2 deletions crates/mun_compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ fn diagnostics(db: &CompilerDatabase, file_id: FileId) -> Vec<Diagnostic> {
result.into_inner()
}

pub fn main(options: CompilerOptions) -> Result<(), failure::Error> {
pub fn main(options: &CompilerOptions) -> Result<(), failure::Error> {
let (mut db, file_id) = CompilerDatabase::from_file(&options.input)?;
db.set_optimization_lvl(options.optimization_lvl);
if let Some(target) = options.target {
if let Some(ref target) = options.target {
db.set_target(spec::Target::search(&target).unwrap());
}

Expand Down

0 comments on commit caac5c6

Please sign in to comment.