Have cargo-compile automatically find the root
authorYehuda Katz <wycats@gmail.com>
Wed, 7 May 2014 23:46:43 +0000 (16:46 -0700)
committerYehuda Katz <wycats@gmail.com>
Wed, 7 May 2014 23:46:43 +0000 (16:46 -0700)
TODO: Deal with Paths vs. Strings (which come from serialized forms like
flags and manifests)

src/bin/cargo-compile.rs

index 07d4ccc19ae9a1e1cb0974aaa25181cac270aa1b..dca60173c8a1345f8b74ceb958cbd4d8e9bc022d 100644 (file)
@@ -7,12 +7,14 @@ extern crate serialize;
 
 use cargo::ops::cargo_compile::compile;
 use cargo::core::errors::{CLIResult,CLIError,ToResult};
+use cargo::util::important_paths::find_project;
 use hammer::{FlagDecoder,FlagConfig,HammerError};
 use serialize::Decodable;
+use std::os;
 
 #[deriving(Eq,Clone,Decodable,Encodable)]
 pub struct Options {
-    manifest_path: ~str
+    manifest_path: Option<~str>
 }
 
 impl FlagConfig for Options {}
@@ -23,7 +25,17 @@ fn flags<T: FlagConfig + Decodable<FlagDecoder, HammerError>>() -> CLIResult<T>
 }
 
 fn execute() -> CLIResult<()> {
-    compile(try!(flags::<Options>()).manifest_path.as_slice()).to_result(|err|
+    let options = try!(flags::<Options>());
+
+    let root = match options.manifest_path {
+        Some(path) => Path::new(path),
+        None => try!(find_project(os::getcwd(), "Cargo.toml".to_owned())
+                    .map(|path| path.join("Cargo.toml"))
+                    .to_result(|err|
+                        CLIError::new("Could not find Cargo.toml in this directory or any parent directory", Some(err.to_str()), 1)))
+    };
+
+    compile(root.as_str().unwrap().as_slice()).to_result(|err|
         CLIError::new(format!("Compilation failed: {}", err), None, 1))
 }