Skip to content

Commit 15440f4

Browse files
z0w0graydon
authored andcommitted
rustpkg: Add info command for probing a pkg.rs and expose work_dir/src_dir in librustpkg
1 parent efe5a0a commit 15440f4

File tree

3 files changed

+76
-6
lines changed

3 files changed

+76
-6
lines changed

src/librustpkg/rustpkg.rc

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ extern mod syntax(vers = "0.6");
2727

2828
use core::*;
2929
use io::{ReaderUtil, WriterUtil};
30-
use std::getopts;
30+
use std::{json, semver, getopts};
3131
use std::net::url;
3232
use send_map::linear::LinearMap;
3333
use rustc::metadata::filesearch;
3434
use rustc::driver::{driver, session};
3535
use syntax::{ast, attr, codemap, diagnostic, parse, visit};
36-
use std::semver;
3736

3837
mod usage;
3938
mod util;
@@ -251,6 +250,7 @@ impl PackageScript {
251250

252251
struct 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 {
783838
pub 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

src/librustpkg/usage.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn general() {
1414
io::println(~"Usage: rustpkg [options] <cmd> [args..]
1515

1616
Where <cmd> is one of:
17-
build, clean, install, prefer, test, uninstall, unprefer
17+
build, clean, do, info, install, prefer, test, uninstall, unprefer
1818

1919
Options:
2020

@@ -46,6 +46,15 @@ Runs a command in the package script. You can listen to a command
4646
by tagging a function with the attribute `#[pkg_do(cmd)]`.");
4747
}
4848

49+
pub fn info() {
50+
io::println(~"rustpkg [options..] info
51+
52+
Probe the package script in the current directory for information.
53+
54+
Options:
55+
-j, --json Output the result as JSON");
56+
}
57+
4958
pub fn install() {
5059
io::println(~"rustpkg [options..] install [url] [target]
5160

src/librustpkg/util.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn root() -> Path {
3535
}
3636

3737
pub fn is_cmd(cmd: ~str) -> bool {
38-
let cmds = &[~"build", ~"clean", ~"do", ~"install", ~"prefer",
38+
let cmds = &[~"build", ~"clean", ~"do", ~"info", ~"install", ~"prefer",
3939
~"test", ~"uninstall", ~"unprefer"];
4040

4141
vec::contains(cmds, &cmd)
@@ -1065,6 +1065,7 @@ fn test_is_cmd() {
10651065
assert is_cmd(~"build");
10661066
assert is_cmd(~"clean");
10671067
assert is_cmd(~"do");
1068+
assert is_cmd(~"info");
10681069
assert is_cmd(~"install");
10691070
assert is_cmd(~"prefer");
10701071
assert is_cmd(~"test");

0 commit comments

Comments
 (0)