From 15440f4236759cd4c65cb2ef7a7df3aac0c79ba7 Mon Sep 17 00:00:00 2001 From: Zack Corr Date: Sat, 26 Jan 2013 18:35:10 +1000 Subject: [PATCH] rustpkg: Add info command for probing a pkg.rs and expose work_dir/src_dir in librustpkg --- src/librustpkg/rustpkg.rc | 68 ++++++++++++++++++++++++++++++++++++--- src/librustpkg/usage.rs | 11 ++++++- src/librustpkg/util.rs | 3 +- 3 files changed, 76 insertions(+), 6 deletions(-) diff --git a/src/librustpkg/rustpkg.rc b/src/librustpkg/rustpkg.rc index 9721fdb2c10..a2741f33099 100644 --- a/src/librustpkg/rustpkg.rc +++ b/src/librustpkg/rustpkg.rc @@ -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() } diff --git a/src/librustpkg/usage.rs b/src/librustpkg/usage.rs index 94fa140e51e..cfda56f777a 100644 --- a/src/librustpkg/usage.rs +++ b/src/librustpkg/usage.rs @@ -14,7 +14,7 @@ pub fn general() { io::println(~"Usage: rustpkg [options] [args..] Where is one of: - build, clean, install, prefer, test, uninstall, unprefer + build, clean, do, info, install, prefer, test, uninstall, unprefer Options: @@ -46,6 +46,15 @@ Runs a command in the package script. You can listen to a command 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] diff --git a/src/librustpkg/util.rs b/src/librustpkg/util.rs index 150098cb57d..e19eb5e41d8 100644 --- a/src/librustpkg/util.rs +++ b/src/librustpkg/util.rs @@ -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");