Skip to content

Commit

Permalink
rustpkg: Add info command for probing a pkg.rs and expose work_dir/sr…
Browse files Browse the repository at this point in the history
…c_dir in librustpkg
  • Loading branch information
z0w0 authored and graydon committed Feb 16, 2013
1 parent efe5a0a commit 15440f4
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 6 deletions.
68 changes: 64 additions & 4 deletions src/librustpkg/rustpkg.rc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -251,6 +250,7 @@ impl PackageScript {

struct Ctx {
cfgs: ~[~str],
json: bool,
mut dep_cache: LinearMap<~str, bool>
}

Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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(),
Expand All @@ -824,6 +883,7 @@ pub fn main() {

Ctx {
cfgs: cfgs,
json: json,
mut dep_cache: LinearMap()
}.run(cmd, args);
}
Expand Down Expand Up @@ -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()
}

Expand All @@ -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()
}

Expand Down
11 changes: 10 additions & 1 deletion src/librustpkg/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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]

Expand Down
3 changes: 2 additions & 1 deletion src/librustpkg/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 15440f4

Please sign in to comment.