Lots of cleanup and more generic commands
authorYehuda Katz <wycats@gmail.com>
Thu, 20 Mar 2014 21:02:51 +0000 (14:02 -0700)
committerYehuda Katz <wycats@gmail.com>
Thu, 20 Mar 2014 21:02:51 +0000 (14:02 -0700)
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)

Makefile
libs/hammer.rs
src/bin/cargo-read-manifest.rs
src/bin/cargo-rustc.rs
src/cargo/mod.rs
src/cargo/util/process_builder.rs

index 7a2100fd8f268fd7294e2496792018ae5dffa7f0..054ee0983e10e970104a2d130509d73e14b5b333 100644 (file)
--- a/Makefile
+++ b/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)
index d8463257a28af989fc899920401d9bd17e063ea6..60ec16a732abfc4dd6a090e4f47e9e2779fd9ba5 160000 (submodule)
@@ -1 +1 @@
-Subproject commit d8463257a28af989fc899920401d9bd17e063ea6
+Subproject commit 60ec16a732abfc4dd6a090e4f47e9e2779fd9ba5
index 17487f48a98a62546547b269bcbab34e4d26a09c..ce2215c8a62bc29d49a7f07bad88413a3c134aa0 100644 (file)
@@ -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() }]
     }
index 4e55d37e9c71c14d43ab5d8177755e5306b4fe9d..8967c6ce2aa53ecdd0e145f744c0041feb7809be 100644 (file)
@@ -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
-    }
-}
index 8ea9d950723f7d461fff70a333c92bdab292ff72..33f5a0169cf1fe550e6aab1a5d02de376fab952b 100644 (file)
@@ -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)
+    }
+}
index 821fa4789a266cc2158cc16b46879dd4e450116c..381b4d5c6525b026b3deb6138a1798f0db389fab 100644 (file)
@@ -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;