@@ -27,13 +27,12 @@ extern mod syntax(vers = "0.6");
2727
2828use core::*;
2929use io::{ReaderUtil, WriterUtil};
30- use std::getopts;
30+ use std::{json, semver, getopts} ;
3131use std::net::url;
3232use send_map::linear::LinearMap;
3333use rustc::metadata::filesearch;
3434use rustc::driver::{driver, session};
3535use syntax::{ast, attr, codemap, diagnostic, parse, visit};
36- use std::semver;
3736
3837mod usage;
3938mod util;
@@ -251,6 +250,7 @@ impl PackageScript {
251250
252251struct Ctx {
253252 cfgs: ~[~str],
253+ json: bool,
254254 mut dep_cache: LinearMap<~str, bool>
255255}
256256
@@ -294,6 +294,9 @@ impl Ctx {
294294
295295 self.do_cmd(args[0]);
296296 }
297+ ~"info" => {
298+ self.info();
299+ }
297300 ~"install" => {
298301 self.install(if args.len() >= 1 { Some(args[0]) }
299302 else { None },
@@ -470,6 +473,58 @@ impl Ctx {
470473 true
471474 }
472475
476+ fn info() {
477+ if self.json {
478+ match PackageScript::parse(&os::getcwd()) {
479+ result::Ok(script) => {
480+ let mut map = ~LinearMap();
481+
482+ map.insert(~"id", json::String(script.id));
483+ map.insert(~"name", json::String(script.name));
484+ map.insert(~"vers", json::String(script.vers.to_str()));
485+ map.insert(~"deps", json::List(do script.deps.map |&dep| {
486+ let (url, target) = dep;
487+ let mut inner = ~LinearMap();
488+
489+ inner.insert(~"url", json::String(url));
490+
491+ if !target.is_none() {
492+ inner.insert(~"target", json::String(target.get()));
493+ }
494+
495+ json::Object(inner)
496+ }));
497+
498+ io::println(json::to_pretty_str(&json::Object(map)));
499+ }
500+ result::Err(_) => io::println(~"{}")
501+ }
502+ } else {
503+ let script = match PackageScript::parse(&os::getcwd()) {
504+ result::Ok(script) => script,
505+ result::Err(err) => {
506+ util::error(err);
507+
508+ return;
509+ }
510+ };
511+
512+ util::note(fmt!("id: %s", script.id));
513+ util::note(fmt!("name: %s", script.name));
514+ util::note(fmt!("vers: %s", script.vers.to_str()));
515+ util::note(fmt!("deps: %s", if script.deps.len() > 0 { ~"" } else { ~"none" }));
516+
517+ for script.deps.each |&dep| {
518+ let (url, target) = dep;
519+
520+ util::note(fmt!(" <%s> (%s)", url, match target {
521+ Some(target) => target,
522+ None => ~""
523+ }));
524+ }
525+ }
526+ }
527+
473528 fn install(url: Option<~str>, target: Option<~str>, cache: bool) -> bool {
474529 let mut success;
475530 let mut dir;
@@ -783,6 +838,7 @@ impl Ctx {
783838pub fn main() {
784839 let args = os::args();
785840 let opts = ~[getopts::optflag(~"h"), getopts::optflag(~"help"),
841+ getopts::optflag(~"j"), getopts::optflag(~"json"),
786842 getopts::optmulti(~"c"), getopts::optmulti(~"cfg")];
787843 let matches = &match getopts::getopts(args, opts) {
788844 result::Ok(m) => m,
@@ -794,6 +850,8 @@ pub fn main() {
794850 };
795851 let help = getopts::opt_present(matches, ~"h") ||
796852 getopts::opt_present(matches, ~"help");
853+ let json = getopts::opt_present(matches, ~"j") ||
854+ getopts::opt_present(matches, ~"json");
797855 let cfgs = vec::append(getopts::opt_strs(matches, ~"cfg"),
798856 getopts::opt_strs(matches, ~"c"));
799857 let mut args = copy matches.free;
@@ -813,6 +871,7 @@ pub fn main() {
813871 ~"build" => usage::build(),
814872 ~"clean" => usage::clean(),
815873 ~"do" => usage::do_cmd(),
874+ ~"info" => usage::info(),
816875 ~"install" => usage::install(),
817876 ~"prefer" => usage::prefer(),
818877 ~"test" => usage::test(),
@@ -824,6 +883,7 @@ pub fn main() {
824883
825884 Ctx {
826885 cfgs: cfgs,
886+ json: json,
827887 mut dep_cache: LinearMap()
828888 }.run(cmd, args);
829889}
@@ -906,7 +966,7 @@ pub fn Crate(file: ~str) -> Crate {
906966 * Assumes that the package script has been compiled
907967 * in is the working directory.
908968 */
909- fn work_dir() -> Path {
969+ pub fn work_dir() -> Path {
910970 os::self_exe_path().get()
911971}
912972
@@ -916,7 +976,7 @@ fn work_dir() -> Path {
916976 * that the cwd is changed to it before
917977 * running this executable.
918978 */
919- fn src_dir() -> Path {
979+ pub fn src_dir() -> Path {
920980 os::getcwd()
921981}
922982
0 commit comments