Restore CLI

This commit is contained in:
Richard Feldman 2020-08-02 22:04:31 -04:00
parent 87630e1d69
commit 9e0cf4cd35
3 changed files with 161 additions and 32 deletions

View file

@ -2,19 +2,17 @@
extern crate clap;
use bumpalo::Bump;
use clap::{App, Arg, ArgMatches};
use roc_build::program::gen;
use roc_collections::all::MutMap;
use roc_gen::llvm::build::OptLevel;
use roc_load::file::LoadingProblem;
use std::time::SystemTime;
use clap::{App, Arg, ArgMatches};
use std::io::{self, ErrorKind};
use std::path::{Path, PathBuf};
use std::process;
use std::process::Command;
use std::time::SystemTime;
use target_lexicon::Triple;
use tokio::process::Command;
use tokio::runtime::Builder;
pub mod repl;
@ -105,14 +103,6 @@ pub fn build(matches: &ArgMatches, run_after_build: bool) -> io::Result<()> {
let path = Path::new(filename).canonicalize().unwrap();
let src_dir = path.parent().unwrap().canonicalize().unwrap();
// Create the runtime
let mut rt = Builder::new()
.thread_name("roc")
.threaded_scheduler()
.enable_io()
.build()
.expect("Error spawning initial compiler thread."); // TODO make this error nicer.
// Spawn the root task
let path = path.canonicalize().unwrap_or_else(|err| {
use ErrorKind::*;
@ -131,22 +121,17 @@ pub fn build(matches: &ArgMatches, run_after_build: bool) -> io::Result<()> {
}
}
});
let binary_path = rt
.block_on(build_file(src_dir, path, opt_level))
.expect("TODO gracefully handle block_on failing");
let binary_path =
build_file(src_dir, path, opt_level).expect("TODO gracefully handle build_file failing");
if run_after_build {
// Run the compiled app
rt.block_on(async {
Command::new(binary_path)
.spawn()
.unwrap_or_else(|err| panic!("Failed to run app after building it: {:?}", err))
.await
.map_err(|_| {
todo!("gracefully handle error after `app` spawned");
})
})
.expect("TODO gracefully handle block_on failing");
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(())
@ -168,7 +153,8 @@ fn build_file(
OptLevel::Normal => roc_builtins::std::standard_stdlib(),
OptLevel::Optimize => roc_builtins::unique::uniq_stdlib(),
};
let loaded = roc_load::file::load(&stdlib, src_dir, filename.clone(), subs_by_module).await?;
let loaded =
roc_load::file::load(filename.clone(), &stdlib, src_dir.as_path(), subs_by_module)?;
let dest_filename = filename.with_extension("o");
gen(
@ -201,7 +187,7 @@ fn build_file(
.map_err(|_| {
todo!("gracefully handle `ar` failing to spawn.");
})?
.await
.wait()
.map_err(|_| {
todo!("gracefully handle error after `ar` spawned");
})?;
@ -224,7 +210,7 @@ fn build_file(
.map_err(|_| {
todo!("gracefully handle `rustc` failing to spawn.");
})?
.await
.wait()
.map_err(|_| {
todo!("gracefully handle error after `rustc` spawned");
})?;