]> git.lizzy.rs Git - rust.git/commitdiff
rustpkg: Add info command for probing a pkg.rs and expose work_dir/src_dir in librustpkg
authorZack Corr <zack@z0w0.me>
Sat, 26 Jan 2013 08:35:10 +0000 (18:35 +1000)
committerGraydon Hoare <graydon@mozilla.com>
Sat, 16 Feb 2013 02:04:11 +0000 (18:04 -0800)
src/librustpkg/rustpkg.rc
src/librustpkg/usage.rs
src/librustpkg/util.rs

index 9721fdb2c104eeb5b523c39153cde43b3f051e57..a2741f33099e0c08e60b4978263c54ba31b842d7 100644 (file)
@@ -27,13 +27,12 @@ extern mod syntax(vers = "0.6");
 
 use core::*;
 use io::{ReaderUtil, WriterUtil};
-use std::getopts;
+use std::{json, semver, getopts};
 use std::net::url;
 use send_map::linear::LinearMap;
 use rustc::metadata::filesearch;
 use rustc::driver::{driver, session};
 use syntax::{ast, attr, codemap, diagnostic, parse, visit};
-use std::semver;
 
 mod usage;
 mod util;
@@ -251,6 +250,7 @@ impl PackageScript {
 
 struct Ctx {
     cfgs: ~[~str],
+    json: bool,
     mut dep_cache: LinearMap<~str, bool>
 }
 
@@ -294,6 +294,9 @@ impl Ctx {
 
                 self.do_cmd(args[0]);
             }
+            ~"info" => {
+                self.info();
+            }
             ~"install" => {
                 self.install(if args.len() >= 1 { Some(args[0]) }
                              else { None },
@@ -470,6 +473,58 @@ impl Ctx {
         true
     }
 
+    fn info() {
+        if self.json {
+            match PackageScript::parse(&os::getcwd()) {
+                result::Ok(script) => {
+                    let mut map = ~LinearMap();
+
+                    map.insert(~"id", json::String(script.id));
+                    map.insert(~"name", json::String(script.name));
+                    map.insert(~"vers", json::String(script.vers.to_str()));
+                    map.insert(~"deps", json::List(do script.deps.map |&dep| {
+                        let (url, target) = dep;
+                        let mut inner = ~LinearMap();
+
+                        inner.insert(~"url", json::String(url));
+
+                        if !target.is_none() {
+                            inner.insert(~"target", json::String(target.get()));
+                        }
+
+                        json::Object(inner)
+                    }));
+
+                    io::println(json::to_pretty_str(&json::Object(map)));
+                }
+                result::Err(_) => io::println(~"{}")
+            }
+        } else {
+            let script = match PackageScript::parse(&os::getcwd()) {
+                result::Ok(script) => script,
+                result::Err(err) => {
+                    util::error(err);
+
+                    return;
+                }
+            };
+
+            util::note(fmt!("id: %s", script.id));
+            util::note(fmt!("name: %s", script.name));
+            util::note(fmt!("vers: %s", script.vers.to_str()));
+            util::note(fmt!("deps: %s", if script.deps.len() > 0 { ~"" } else { ~"none" }));
+
+            for script.deps.each |&dep| {
+                let (url, target) = dep;
+
+                util::note(fmt!("  <%s> (%s)", url, match target {
+                    Some(target) => target,
+                    None => ~""
+                }));
+            }
+        }
+    }
+
     fn install(url: Option<~str>, target: Option<~str>, cache: bool) -> bool {
         let mut success;
         let mut dir;
@@ -783,6 +838,7 @@ impl Ctx {
 pub fn main() {
     let args = os::args();
     let opts = ~[getopts::optflag(~"h"), getopts::optflag(~"help"),
+                 getopts::optflag(~"j"), getopts::optflag(~"json"),
                  getopts::optmulti(~"c"), getopts::optmulti(~"cfg")];
     let matches = &match getopts::getopts(args, opts) {
         result::Ok(m) => m,
@@ -794,6 +850,8 @@ pub fn main() {
     };
     let help = getopts::opt_present(matches, ~"h") ||
                getopts::opt_present(matches, ~"help");
+    let json = getopts::opt_present(matches, ~"j") ||
+               getopts::opt_present(matches, ~"json");
     let cfgs = vec::append(getopts::opt_strs(matches, ~"cfg"),
                            getopts::opt_strs(matches, ~"c"));
     let mut args = copy matches.free;
@@ -813,6 +871,7 @@ pub fn main() {
             ~"build" => usage::build(),
             ~"clean" => usage::clean(),
             ~"do" => usage::do_cmd(),
+            ~"info" => usage::info(),
             ~"install" => usage::install(),
             ~"prefer" => usage::prefer(),
             ~"test" => usage::test(),
@@ -824,6 +883,7 @@ pub fn main() {
 
     Ctx {
         cfgs: cfgs,
+        json: json,
         mut dep_cache: LinearMap()
     }.run(cmd, args);
 }
@@ -906,7 +966,7 @@ pub fn Crate(file: ~str) -> Crate {
  * Assumes that the package script has been compiled
  * in is the working directory.
  */
-fn work_dir() -> Path {
+pub fn work_dir() -> Path {
     os::self_exe_path().get()
 }
 
@@ -916,7 +976,7 @@ fn work_dir() -> Path {
  * that the cwd is changed to it before
  * running this executable.
  */
-fn src_dir() -> Path {
+pub fn src_dir() -> Path {
     os::getcwd()
 }
 
index 94fa140e51effea900f1c24cd49a71378420d826..cfda56f777ab2779240573ac6039ec8a826a8cd0 100644 (file)
@@ -14,7 +14,7 @@ pub fn general() {
     io::println(~"Usage: rustpkg [options] <cmd> [args..]
 
 Where <cmd> is one of:
-    build, clean, install, prefer, test, uninstall, unprefer
+    build, clean, do, info, install, prefer, test, uninstall, unprefer
 
 Options:
 
@@ -46,6 +46,15 @@ pub fn do_cmd() {
 by tagging a function with the attribute `#[pkg_do(cmd)]`.");
 }
 
+pub fn info() {
+    io::println(~"rustpkg [options..] info
+
+Probe the package script in the current directory for information.
+
+Options:
+    -j, --json      Output the result as JSON");
+}
+
 pub fn install() {
     io::println(~"rustpkg [options..] install [url] [target]
 
index 150098cb57dffb61392f3f4035b7b1e401f93cae..e19eb5e41d81be9ef679e72dc6a3da93deb28df9 100644 (file)
@@ -35,7 +35,7 @@ pub fn root() -> Path {
 }
 
 pub fn is_cmd(cmd: ~str) -> bool {
-    let cmds = &[~"build", ~"clean", ~"do", ~"install", ~"prefer",
+    let cmds = &[~"build", ~"clean", ~"do", ~"info", ~"install", ~"prefer",
                  ~"test", ~"uninstall", ~"unprefer"];
 
     vec::contains(cmds, &cmd)
@@ -1065,6 +1065,7 @@ fn test_is_cmd() {
     assert is_cmd(~"build");
     assert is_cmd(~"clean");
     assert is_cmd(~"do");
+    assert is_cmd(~"info");
     assert is_cmd(~"install");
     assert is_cmd(~"prefer");
     assert is_cmd(~"test");