mirror of
https://github.com/rust-lang/cargo.git
synced 2025-09-25 11:14:46 +00:00
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:
parent
521d93a576
commit
5fb7a4f1cc
4
Makefile
4
Makefile
@ -31,9 +31,9 @@ $(HAMCREST): $(wildcard libs/hamcrest-rust/src/hamcrest/*.rs)
|
||||
|
||||
# === Cargo
|
||||
|
||||
$(LIBCARGO): $(SRC)
|
||||
$(LIBCARGO): $(SRC) $(HAMMER)
|
||||
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)
|
||||
|
||||
libcargo: $(LIBCARGO)
|
||||
|
@ -1 +1 @@
|
||||
Subproject commit d8463257a28af989fc899920401d9bd17e063ea6
|
||||
Subproject commit 60ec16a732abfc4dd6a090e4f47e9e2779fd9ba5
|
@ -6,11 +6,11 @@ extern crate hammer;
|
||||
extern crate serialize;
|
||||
extern crate toml;
|
||||
|
||||
use hammer::{FlagDecoder,FlagConfig,FlagConfiguration};
|
||||
use serialize::{Decoder,Decodable};
|
||||
use hammer::FlagConfig;
|
||||
use serialize::Decoder;
|
||||
use serialize::json::Encoder;
|
||||
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;
|
||||
|
||||
#[deriving(Decodable,Encodable,Eq,Clone,Ord)]
|
||||
@ -35,30 +35,13 @@ struct ReadManifestFlags {
|
||||
manifest_path: ~str
|
||||
}
|
||||
|
||||
impl FlagConfig for ReadManifestFlags {
|
||||
fn config(_: Option<ReadManifestFlags>, c: FlagConfiguration) -> FlagConfiguration {
|
||||
c
|
||||
}
|
||||
}
|
||||
impl FlagConfig for ReadManifestFlags {}
|
||||
|
||||
fn main() {
|
||||
match execute() {
|
||||
Err(e) => {
|
||||
println!("{}", e.message);
|
||||
// TODO: Exit with error code
|
||||
},
|
||||
_ => return
|
||||
}
|
||||
execute_main::<ReadManifestFlags>(execute);
|
||||
}
|
||||
|
||||
fn execute() -> 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));
|
||||
}
|
||||
|
||||
fn execute(flags: ReadManifestFlags) -> CargoResult<()> {
|
||||
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));
|
||||
|
||||
@ -82,7 +65,7 @@ fn execute() -> CargoResult<()> {
|
||||
|
||||
fn normalize(lib: &Option<~[SerializedLibTarget]>, bin: &Option<~[SerializedExecTarget]>) -> (~[LibTarget], ~[ExecTarget]) {
|
||||
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));
|
||||
~[LibTarget{ path: path, name: l.name.clone() }]
|
||||
}
|
||||
|
@ -2,16 +2,18 @@
|
||||
#[allow(deprecated_owned_vector)];
|
||||
|
||||
extern crate toml;
|
||||
extern crate hammer;
|
||||
extern crate serialize;
|
||||
extern crate cargo;
|
||||
|
||||
use hammer::FlagConfig;
|
||||
use std::os::args;
|
||||
use std::io;
|
||||
use std::io::process::{Process,ProcessConfig,InheritFd};
|
||||
use serialize::json;
|
||||
use serialize::Decodable;
|
||||
use std::path::Path;
|
||||
use cargo::{Manifest,CargoResult,CargoError,ToCargoError};
|
||||
use cargo::{Manifest,CargoResult,CargoError,ToCargoError,execute_main};
|
||||
|
||||
/**
|
||||
cargo-rustc -- ...args
|
||||
@ -20,16 +22,15 @@ use cargo::{Manifest,CargoResult,CargoError,ToCargoError};
|
||||
*/
|
||||
|
||||
fn main() {
|
||||
match execute() {
|
||||
Err(e) => {
|
||||
write!(&mut std::io::stderr(), "{}", e.message);
|
||||
// TODO: Exit with error code
|
||||
},
|
||||
_ => return
|
||||
}
|
||||
execute_main::<RustcFlags>(execute);
|
||||
}
|
||||
|
||||
fn execute() -> CargoResult<()> {
|
||||
#[deriving(Decodable,Eq,Clone,Ord)]
|
||||
struct RustcFlags;
|
||||
|
||||
impl FlagConfig for RustcFlags {}
|
||||
|
||||
fn execute(_: RustcFlags) -> CargoResult<()> {
|
||||
let mut reader = io::stdin();
|
||||
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 {
|
||||
format!("{}", path.join(part).display())
|
||||
}
|
||||
|
||||
fn vec_idx<T>(v: ~[T], idx: uint) -> Option<T> {
|
||||
if idx < v.len() {
|
||||
Some(v[idx])
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
@ -4,9 +4,12 @@
|
||||
#[allow(deprecated_owned_vector)];
|
||||
|
||||
extern crate serialize;
|
||||
use serialize::{Decoder};
|
||||
extern crate hammer;
|
||||
|
||||
use serialize::{Decoder,Decodable};
|
||||
use std::fmt;
|
||||
use std::fmt::{Show,Formatter};
|
||||
use hammer::{FlagDecoder,FlagConfig};
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
use std;
|
||||
use std::os;
|
||||
use std::path::Path;
|
||||
use std::io::IoResult;
|
||||
use std::io::process::{Process,ProcessConfig,ProcessOutput};
|
||||
use ToCargoError;
|
||||
use CargoResult;
|
||||
|
Loading…
x
Reference in New Issue
Block a user