Lots of cleanup and more generic commands

Started to extract some of the generic behavior across all commands into
`execute_main` so that the commands themselves just need to operate
against structs (for JSON in and Flags)
This commit is contained in:
Yehuda Katz 2014-03-20 14:02:51 -07:00
parent 521d93a576
commit 5fb7a4f1cc
6 changed files with 49 additions and 47 deletions

View File

@ -31,9 +31,9 @@ $(HAMCREST): $(wildcard libs/hamcrest-rust/src/hamcrest/*.rs)
# === Cargo # === Cargo
$(LIBCARGO): $(SRC) $(LIBCARGO): $(SRC) $(HAMMER)
mkdir -p target mkdir -p target
$(RUSTC) $(RUSTC_FLAGS) --out-dir target src/cargo/mod.rs $(RUSTC) $(RUSTC_FLAGS) $(DEPS) --out-dir target src/cargo/mod.rs
touch $(LIBCARGO) touch $(LIBCARGO)
libcargo: $(LIBCARGO) libcargo: $(LIBCARGO)

@ -1 +1 @@
Subproject commit d8463257a28af989fc899920401d9bd17e063ea6 Subproject commit 60ec16a732abfc4dd6a090e4f47e9e2779fd9ba5

View File

@ -6,11 +6,11 @@ extern crate hammer;
extern crate serialize; extern crate serialize;
extern crate toml; extern crate toml;
use hammer::{FlagDecoder,FlagConfig,FlagConfiguration}; use hammer::FlagConfig;
use serialize::{Decoder,Decodable}; use serialize::Decoder;
use serialize::json::Encoder; use serialize::json::Encoder;
use toml::from_toml; use toml::from_toml;
use cargo::{Manifest,LibTarget,ExecTarget,Project,CargoResult,CargoError,ToCargoError}; use cargo::{Manifest,LibTarget,ExecTarget,Project,CargoResult,ToCargoError,execute_main};
use std::path::Path; use std::path::Path;
#[deriving(Decodable,Encodable,Eq,Clone,Ord)] #[deriving(Decodable,Encodable,Eq,Clone,Ord)]
@ -35,30 +35,13 @@ struct ReadManifestFlags {
manifest_path: ~str manifest_path: ~str
} }
impl FlagConfig for ReadManifestFlags { impl FlagConfig for ReadManifestFlags {}
fn config(_: Option<ReadManifestFlags>, c: FlagConfiguration) -> FlagConfiguration {
c
}
}
fn main() { fn main() {
match execute() { execute_main::<ReadManifestFlags>(execute);
Err(e) => {
println!("{}", e.message);
// TODO: Exit with error code
},
_ => return
}
} }
fn execute() -> CargoResult<()> { fn execute(flags: ReadManifestFlags) -> CargoResult<()> {
let mut decoder = FlagDecoder::new::<ReadManifestFlags>(std::os::args().tail());
let flags: ReadManifestFlags = Decodable::decode(&mut decoder);
if decoder.error.is_some() {
return Err(CargoError::new(decoder.error.unwrap(), 1));
}
let manifest_path = flags.manifest_path; let manifest_path = flags.manifest_path;
let root = try!(toml::parse_from_file(manifest_path.clone()).to_cargo_error(format!("Couldn't parse Toml file: {}", manifest_path), 1)); let root = try!(toml::parse_from_file(manifest_path.clone()).to_cargo_error(format!("Couldn't parse Toml file: {}", manifest_path), 1));
@ -82,7 +65,7 @@ fn execute() -> CargoResult<()> {
fn normalize(lib: &Option<~[SerializedLibTarget]>, bin: &Option<~[SerializedExecTarget]>) -> (~[LibTarget], ~[ExecTarget]) { fn normalize(lib: &Option<~[SerializedLibTarget]>, bin: &Option<~[SerializedExecTarget]>) -> (~[LibTarget], ~[ExecTarget]) {
fn lib_targets(libs: &[SerializedLibTarget]) -> ~[LibTarget] { fn lib_targets(libs: &[SerializedLibTarget]) -> ~[LibTarget] {
let l = &lib[0]; let l = &libs[0];
let path = l.path.clone().unwrap_or_else(|| format!("src/{}.rs", l.name)); let path = l.path.clone().unwrap_or_else(|| format!("src/{}.rs", l.name));
~[LibTarget{ path: path, name: l.name.clone() }] ~[LibTarget{ path: path, name: l.name.clone() }]
} }

View File

@ -2,16 +2,18 @@
#[allow(deprecated_owned_vector)]; #[allow(deprecated_owned_vector)];
extern crate toml; extern crate toml;
extern crate hammer;
extern crate serialize; extern crate serialize;
extern crate cargo; extern crate cargo;
use hammer::FlagConfig;
use std::os::args; use std::os::args;
use std::io; use std::io;
use std::io::process::{Process,ProcessConfig,InheritFd}; use std::io::process::{Process,ProcessConfig,InheritFd};
use serialize::json; use serialize::json;
use serialize::Decodable; use serialize::Decodable;
use std::path::Path; use std::path::Path;
use cargo::{Manifest,CargoResult,CargoError,ToCargoError}; use cargo::{Manifest,CargoResult,CargoError,ToCargoError,execute_main};
/** /**
cargo-rustc -- ...args cargo-rustc -- ...args
@ -20,16 +22,15 @@ use cargo::{Manifest,CargoResult,CargoError,ToCargoError};
*/ */
fn main() { fn main() {
match execute() { execute_main::<RustcFlags>(execute);
Err(e) => {
write!(&mut std::io::stderr(), "{}", e.message);
// TODO: Exit with error code
},
_ => return
}
} }
fn execute() -> CargoResult<()> { #[deriving(Decodable,Eq,Clone,Ord)]
struct RustcFlags;
impl FlagConfig for RustcFlags {}
fn execute(_: RustcFlags) -> CargoResult<()> {
let mut reader = io::stdin(); let mut reader = io::stdin();
let input = try!(reader.read_to_str().to_cargo_error(~"Cannot read stdin to a string", 1)); let input = try!(reader.read_to_str().to_cargo_error(~"Cannot read stdin to a string", 1));
@ -83,11 +84,3 @@ fn execute() -> CargoResult<()> {
fn join(path: &Path, part: ~str) -> ~str { fn join(path: &Path, part: ~str) -> ~str {
format!("{}", path.join(part).display()) format!("{}", path.join(part).display())
} }
fn vec_idx<T>(v: ~[T], idx: uint) -> Option<T> {
if idx < v.len() {
Some(v[idx])
} else {
None
}
}

View File

@ -4,9 +4,12 @@
#[allow(deprecated_owned_vector)]; #[allow(deprecated_owned_vector)];
extern crate serialize; extern crate serialize;
use serialize::{Decoder}; extern crate hammer;
use serialize::{Decoder,Decodable};
use std::fmt; use std::fmt;
use std::fmt::{Show,Formatter}; use std::fmt::{Show,Formatter};
use hammer::{FlagDecoder,FlagConfig};
pub mod util; pub mod util;
@ -80,3 +83,28 @@ impl<T> ToCargoError<T> for Option<T> {
} }
} }
} }
pub fn execute_main<T: FlagConfig + Decodable<FlagDecoder>>(exec: fn(T) -> CargoResult<()>) {
fn call<T: FlagConfig + Decodable<FlagDecoder>>(exec: fn(T) -> CargoResult<()>) -> CargoResult<()> {
let flags = try!(flags_from_args::<T>());
exec(flags)
}
match call(exec) {
Err(e) => {
let _ = write!(&mut std::io::stderr(), "{}", e.message);
std::os::set_exit_status(e.exit_code as int);
},
Ok(_) => ()
}
}
fn flags_from_args<T: FlagConfig + Decodable<FlagDecoder>>() -> CargoResult<T> {
let mut decoder = FlagDecoder::new::<T>(std::os::args().tail());
let flags: T = Decodable::decode(&mut decoder);
match decoder.error {
Some(err) => Err(CargoError::new(err, 1)),
None => Ok(flags)
}
}

View File

@ -1,7 +1,5 @@
use std;
use std::os; use std::os;
use std::path::Path; use std::path::Path;
use std::io::IoResult;
use std::io::process::{Process,ProcessConfig,ProcessOutput}; use std::io::process::{Process,ProcessConfig,ProcessOutput};
use ToCargoError; use ToCargoError;
use CargoResult; use CargoResult;