2011-11-30 17:57:46 -06:00
|
|
|
// cargo.rs - Rust package manager
|
|
|
|
|
2011-12-16 19:33:39 -06:00
|
|
|
use rustc;
|
|
|
|
use std;
|
|
|
|
|
2011-11-30 17:57:46 -06:00
|
|
|
import rustc::syntax::{ast, codemap, visit};
|
|
|
|
import rustc::syntax::parse::parser;
|
2012-01-05 18:03:28 -06:00
|
|
|
import rustc::util::filesearch::get_cargo_root;
|
2012-01-13 19:33:16 -06:00
|
|
|
import rustc::driver::diagnostic;
|
2011-11-30 17:57:46 -06:00
|
|
|
|
2011-12-01 21:37:56 -06:00
|
|
|
import std::fs;
|
2011-12-08 22:41:29 -06:00
|
|
|
import std::generic_os;
|
2011-11-30 17:57:46 -06:00
|
|
|
import std::io;
|
2012-01-11 08:15:54 -06:00
|
|
|
import io::writer_util;
|
2011-12-15 19:27:55 -06:00
|
|
|
import std::json;
|
2011-12-13 18:25:51 -06:00
|
|
|
import option;
|
|
|
|
import option::{none, some};
|
2011-12-16 19:33:39 -06:00
|
|
|
import result;
|
|
|
|
import std::map;
|
2011-12-08 22:41:29 -06:00
|
|
|
import std::os;
|
2011-11-30 17:57:46 -06:00
|
|
|
import std::run;
|
2011-12-13 18:25:51 -06:00
|
|
|
import str;
|
2011-11-30 17:57:46 -06:00
|
|
|
import std::tempfile;
|
2011-12-13 18:25:51 -06:00
|
|
|
import vec;
|
2011-11-30 17:57:46 -06:00
|
|
|
|
2011-12-16 19:33:39 -06:00
|
|
|
tag _src {
|
|
|
|
/* Break cycles in package <-> source */
|
|
|
|
_source(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
type package = {
|
2011-12-16 21:08:25 -06:00
|
|
|
// source: _src,
|
2011-12-16 19:33:39 -06:00
|
|
|
name: str,
|
|
|
|
uuid: str,
|
2011-12-16 21:08:25 -06:00
|
|
|
url: str,
|
2011-12-20 19:41:23 -06:00
|
|
|
method: str,
|
2011-12-20 22:41:22 -06:00
|
|
|
ref: option::t<str>,
|
2011-12-20 19:41:23 -06:00
|
|
|
tags: [str]
|
2011-12-16 19:33:39 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
type source = {
|
|
|
|
name: str,
|
|
|
|
url: str,
|
2011-12-20 18:59:37 -06:00
|
|
|
sig: option::t<str>,
|
|
|
|
key: option::t<str>,
|
|
|
|
keyfp: option::t<str>,
|
2011-12-16 19:33:39 -06:00
|
|
|
mutable packages: [package]
|
|
|
|
};
|
|
|
|
|
2011-12-08 22:50:25 -06:00
|
|
|
type cargo = {
|
2011-12-20 18:59:37 -06:00
|
|
|
pgp: bool,
|
2011-12-08 22:50:25 -06:00
|
|
|
root: str,
|
|
|
|
bindir: str,
|
|
|
|
libdir: str,
|
|
|
|
workdir: str,
|
2011-12-16 19:33:39 -06:00
|
|
|
sourcedir: str,
|
2012-01-18 21:16:14 -06:00
|
|
|
sources: map::hashmap<str, source>,
|
|
|
|
mutable test: bool
|
2011-12-08 22:50:25 -06:00
|
|
|
};
|
|
|
|
|
2011-11-30 17:57:46 -06:00
|
|
|
type pkg = {
|
|
|
|
name: str,
|
|
|
|
vers: str,
|
|
|
|
uuid: str,
|
|
|
|
desc: option::t<str>,
|
2011-12-08 22:41:29 -06:00
|
|
|
sigs: option::t<str>,
|
|
|
|
crate_type: option::t<str>
|
2011-11-30 17:57:46 -06:00
|
|
|
};
|
|
|
|
|
2011-12-16 19:33:39 -06:00
|
|
|
fn info(msg: str) {
|
2012-01-18 21:16:14 -06:00
|
|
|
io::stdout().write_line("info: " + msg);
|
2011-12-16 19:33:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn warn(msg: str) {
|
|
|
|
io::stdout().write_line("warning: " + msg);
|
|
|
|
}
|
|
|
|
|
2011-12-16 21:08:25 -06:00
|
|
|
fn error(msg: str) {
|
|
|
|
io::stdout().write_line("error: " + msg);
|
|
|
|
}
|
|
|
|
|
2011-11-30 17:57:46 -06:00
|
|
|
fn load_link(mis: [@ast::meta_item]) -> (option::t<str>,
|
|
|
|
option::t<str>,
|
|
|
|
option::t<str>) {
|
|
|
|
let name = none;
|
|
|
|
let vers = none;
|
|
|
|
let uuid = none;
|
|
|
|
for a: @ast::meta_item in mis {
|
|
|
|
alt a.node {
|
|
|
|
ast::meta_name_value(v, {node: ast::lit_str(s), span: _}) {
|
|
|
|
alt v {
|
|
|
|
"name" { name = some(s); }
|
|
|
|
"vers" { vers = some(s); }
|
|
|
|
"uuid" { uuid = some(s); }
|
|
|
|
_ { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(name, vers, uuid)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load_pkg(filename: str) -> option::t<pkg> {
|
2012-01-13 19:33:16 -06:00
|
|
|
let cm = codemap::new_codemap();
|
|
|
|
let sess = @{
|
|
|
|
cm: cm,
|
|
|
|
mutable next_id: 0,
|
2012-01-14 01:27:28 -06:00
|
|
|
diagnostic: diagnostic::mk_handler(cm, none)
|
2012-01-13 19:33:16 -06:00
|
|
|
};
|
2011-11-30 17:57:46 -06:00
|
|
|
let c = parser::parse_crate_from_crate_file(filename, [], sess);
|
|
|
|
|
|
|
|
let name = none;
|
|
|
|
let vers = none;
|
|
|
|
let uuid = none;
|
|
|
|
let desc = none;
|
|
|
|
let sigs = none;
|
2011-12-08 22:41:29 -06:00
|
|
|
let crate_type = none;
|
2011-11-30 17:57:46 -06:00
|
|
|
|
|
|
|
for a in c.node.attrs {
|
|
|
|
alt a.node.value.node {
|
|
|
|
ast::meta_name_value(v, {node: ast::lit_str(s), span: _}) {
|
|
|
|
alt v {
|
|
|
|
"desc" { desc = some(v); }
|
|
|
|
"sigs" { sigs = some(v); }
|
2011-12-08 22:41:29 -06:00
|
|
|
"crate_type" { crate_type = some(v); }
|
2011-11-30 17:57:46 -06:00
|
|
|
_ { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast::meta_list(v, mis) {
|
|
|
|
if v == "link" {
|
|
|
|
let (n, v, u) = load_link(mis);
|
|
|
|
name = n;
|
|
|
|
vers = v;
|
|
|
|
uuid = u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
alt (name, vers, uuid) {
|
|
|
|
(some(name0), some(vers0), some(uuid0)) {
|
|
|
|
some({
|
|
|
|
name: name0,
|
|
|
|
vers: vers0,
|
|
|
|
uuid: uuid0,
|
|
|
|
desc: desc,
|
2011-12-08 22:41:29 -06:00
|
|
|
sigs: sigs,
|
|
|
|
crate_type: crate_type})
|
2011-11-30 17:57:46 -06:00
|
|
|
}
|
|
|
|
_ { ret none; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print(s: str) {
|
|
|
|
io::stdout().write_line(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rest(s: str, start: uint) -> str {
|
|
|
|
if (start >= str::char_len(s)) {
|
|
|
|
""
|
|
|
|
} else {
|
|
|
|
str::char_slice(s, start, str::char_len(s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-08 22:41:29 -06:00
|
|
|
fn need_dir(s: str) {
|
2011-12-16 16:32:09 -06:00
|
|
|
if fs::path_is_dir(s) { ret; }
|
2011-12-08 22:41:29 -06:00
|
|
|
if !fs::make_dir(s, 0x1c0i32) {
|
|
|
|
fail #fmt["can't make_dir %s", s];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-16 19:33:39 -06:00
|
|
|
fn parse_source(name: str, j: json::json) -> source {
|
|
|
|
alt j {
|
|
|
|
json::dict(_j) {
|
2011-12-20 18:59:37 -06:00
|
|
|
let url = alt _j.find("url") {
|
2011-12-16 19:33:39 -06:00
|
|
|
some(json::string(u)) {
|
2011-12-20 18:59:37 -06:00
|
|
|
u
|
2011-12-16 19:33:39 -06:00
|
|
|
}
|
|
|
|
_ { fail "Needed 'url' field in source."; }
|
|
|
|
};
|
2011-12-20 18:59:37 -06:00
|
|
|
let sig = alt _j.find("sig") {
|
|
|
|
some(json::string(u)) {
|
|
|
|
some(u)
|
|
|
|
}
|
|
|
|
_ { none }
|
|
|
|
};
|
|
|
|
let key = alt _j.find("key") {
|
|
|
|
some(json::string(u)) {
|
|
|
|
some(u)
|
|
|
|
}
|
|
|
|
_ { none }
|
|
|
|
};
|
|
|
|
let keyfp = alt _j.find("keyfp") {
|
|
|
|
some(json::string(u)) {
|
|
|
|
some(u)
|
|
|
|
}
|
|
|
|
_ { none }
|
|
|
|
};
|
|
|
|
ret { name: name, url: url, sig: sig, key: key, keyfp: keyfp,
|
|
|
|
mutable packages: [] };
|
2011-12-16 19:33:39 -06:00
|
|
|
}
|
|
|
|
_ { fail "Needed dict value in source."; }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn try_parse_sources(filename: str, sources: map::hashmap<str, source>) {
|
|
|
|
if !fs::path_exists(filename) { ret; }
|
|
|
|
let c = io::read_whole_file_str(filename);
|
|
|
|
let j = json::from_str(result::get(c));
|
|
|
|
alt j {
|
|
|
|
some(json::dict(_j)) {
|
|
|
|
_j.items { |k, v|
|
|
|
|
sources.insert(k, parse_source(k, v));
|
2011-12-22 16:42:52 -06:00
|
|
|
#debug("source: %s", k);
|
2011-12-16 19:33:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ { fail "malformed sources.json"; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-16 21:27:04 -06:00
|
|
|
fn load_one_source_package(&src: source, p: map::hashmap<str, json::json>) {
|
2011-12-16 19:33:39 -06:00
|
|
|
let name = alt p.find("name") {
|
|
|
|
some(json::string(_n)) { _n }
|
|
|
|
_ {
|
|
|
|
warn("Malformed source json: " + src.name + " (missing name)");
|
|
|
|
ret;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let uuid = alt p.find("uuid") {
|
|
|
|
some(json::string(_n)) { _n }
|
|
|
|
_ {
|
|
|
|
warn("Malformed source json: " + src.name + " (missing uuid)");
|
|
|
|
ret;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let url = alt p.find("url") {
|
|
|
|
some(json::string(_n)) { _n }
|
|
|
|
_ {
|
|
|
|
warn("Malformed source json: " + src.name + " (missing url)");
|
|
|
|
ret;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-16 21:08:25 -06:00
|
|
|
let method = alt p.find("method") {
|
|
|
|
some(json::string(_n)) { _n }
|
|
|
|
_ {
|
|
|
|
warn("Malformed source json: " + src.name + " (missing method)");
|
|
|
|
ret;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-20 22:41:22 -06:00
|
|
|
let ref = alt p.find("ref") {
|
|
|
|
some(json::string(_n)) { some(_n) }
|
|
|
|
_ { none }
|
|
|
|
};
|
|
|
|
|
2011-12-20 19:41:23 -06:00
|
|
|
let tags = [];
|
|
|
|
alt p.find("tags") {
|
|
|
|
some(json::list(js)) {
|
|
|
|
for j in *js {
|
|
|
|
alt j {
|
|
|
|
json::string(_j) { vec::grow(tags, 1u, _j); }
|
|
|
|
_ { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ { }
|
|
|
|
}
|
2011-12-16 19:33:39 -06:00
|
|
|
vec::grow(src.packages, 1u, {
|
2011-12-16 21:08:25 -06:00
|
|
|
// source: _source(src),
|
2011-12-16 19:33:39 -06:00
|
|
|
name: name,
|
|
|
|
uuid: uuid,
|
2011-12-16 21:08:25 -06:00
|
|
|
url: url,
|
2011-12-20 19:41:23 -06:00
|
|
|
method: method,
|
2011-12-20 22:41:22 -06:00
|
|
|
ref: ref,
|
2011-12-20 19:41:23 -06:00
|
|
|
tags: tags
|
2011-12-16 19:33:39 -06:00
|
|
|
});
|
2011-12-22 19:53:53 -06:00
|
|
|
log(debug, " Loaded package: " + src.name + "/" + name);
|
2011-12-16 19:33:39 -06:00
|
|
|
}
|
|
|
|
|
2011-12-16 21:08:25 -06:00
|
|
|
fn load_source_packages(&c: cargo, &src: source) {
|
2011-12-22 19:53:53 -06:00
|
|
|
log(debug, "Loading source: " + src.name);
|
2011-12-16 19:33:39 -06:00
|
|
|
let dir = fs::connect(c.sourcedir, src.name);
|
|
|
|
let pkgfile = fs::connect(dir, "packages.json");
|
|
|
|
if !fs::path_exists(pkgfile) { ret; }
|
|
|
|
let pkgstr = io::read_whole_file_str(pkgfile);
|
|
|
|
let j = json::from_str(result::get(pkgstr));
|
|
|
|
alt j {
|
|
|
|
some(json::list(js)) {
|
|
|
|
for _j: json::json in *js {
|
|
|
|
alt _j {
|
|
|
|
json::dict(_p) {
|
2011-12-16 21:27:04 -06:00
|
|
|
load_one_source_package(src, _p);
|
2011-12-16 19:33:39 -06:00
|
|
|
}
|
|
|
|
_ {
|
2011-12-16 22:15:13 -06:00
|
|
|
warn("Malformed source json: " + src.name +
|
|
|
|
" (non-dict pkg)");
|
2011-12-16 19:33:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ {
|
|
|
|
warn("Malformed source json: " + src.name);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-12-08 22:50:25 -06:00
|
|
|
fn configure() -> cargo {
|
2012-01-05 18:03:28 -06:00
|
|
|
let p = alt get_cargo_root() {
|
|
|
|
result::ok(p) { p }
|
|
|
|
result::err(e) { fail e }
|
2011-12-08 22:41:29 -06:00
|
|
|
};
|
|
|
|
|
2011-12-16 19:33:39 -06:00
|
|
|
let sources = map::new_str_hash::<source>();
|
|
|
|
try_parse_sources(fs::connect(p, "sources.json"), sources);
|
|
|
|
try_parse_sources(fs::connect(p, "local-sources.json"), sources);
|
2011-12-08 22:50:25 -06:00
|
|
|
let c = {
|
2011-12-20 18:59:37 -06:00
|
|
|
pgp: pgp::supported(),
|
2011-12-08 22:50:25 -06:00
|
|
|
root: p,
|
|
|
|
bindir: fs::connect(p, "bin"),
|
|
|
|
libdir: fs::connect(p, "lib"),
|
|
|
|
workdir: fs::connect(p, "work"),
|
2011-12-16 19:33:39 -06:00
|
|
|
sourcedir: fs::connect(p, "sources"),
|
2012-01-18 21:16:14 -06:00
|
|
|
sources: sources,
|
|
|
|
mutable test: false
|
2011-12-08 22:50:25 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
need_dir(c.root);
|
2011-12-16 19:33:39 -06:00
|
|
|
need_dir(c.sourcedir);
|
2011-12-08 22:50:25 -06:00
|
|
|
need_dir(c.workdir);
|
|
|
|
need_dir(c.libdir);
|
|
|
|
need_dir(c.bindir);
|
2011-12-08 22:41:29 -06:00
|
|
|
|
2011-12-16 21:08:25 -06:00
|
|
|
sources.keys { |k|
|
|
|
|
let s = sources.get(k);
|
|
|
|
load_source_packages(c, s);
|
|
|
|
sources.insert(k, s);
|
2011-12-16 19:33:39 -06:00
|
|
|
};
|
|
|
|
|
2011-12-20 18:59:37 -06:00
|
|
|
if c.pgp {
|
|
|
|
pgp::init(c.root);
|
|
|
|
}
|
|
|
|
|
2011-12-08 22:50:25 -06:00
|
|
|
c
|
2011-12-08 22:41:29 -06:00
|
|
|
}
|
|
|
|
|
2011-12-16 21:08:25 -06:00
|
|
|
fn for_each_package(c: cargo, b: block(source, package)) {
|
2011-12-16 19:33:39 -06:00
|
|
|
c.sources.values({ |v|
|
2011-12-16 21:31:49 -06:00
|
|
|
for p in copy v.packages {
|
2011-12-16 21:08:25 -06:00
|
|
|
b(v, p);
|
2011-12-16 19:33:39 -06:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2012-01-18 21:16:14 -06:00
|
|
|
// FIXME: deduplicate code with install_one_crate
|
2012-01-19 15:46:28 -06:00
|
|
|
fn test_one_crate(_c: cargo, _path: str, cf: str, _p: pkg) {
|
2012-01-18 21:24:07 -06:00
|
|
|
let buildpath = fs::connect(_path, "/test");
|
|
|
|
need_dir(buildpath);
|
|
|
|
#debug("Testing: %s -> %s", cf, buildpath);
|
|
|
|
let p = run::program_output("rustc", ["--out-dir", buildpath, "--test",
|
|
|
|
cf]);
|
|
|
|
if p.status != 0 {
|
|
|
|
error(#fmt["rustc failed: %d\n%s\n%s", p.status, p.err, p.out]);
|
|
|
|
ret;
|
2011-12-08 22:41:29 -06:00
|
|
|
}
|
2012-01-18 21:24:07 -06:00
|
|
|
let new = fs::list_dir(buildpath);
|
|
|
|
let exec_suffix = os::exec_suffix();
|
|
|
|
for ct: str in new {
|
|
|
|
if (exec_suffix != "" && str::ends_with(ct, exec_suffix)) ||
|
|
|
|
(exec_suffix == "" && !str::starts_with(ct, "./lib")) {
|
|
|
|
run::run_program(ct, []);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-18 21:16:14 -06:00
|
|
|
fn install_one_crate(c: cargo, _path: str, cf: str, _p: pkg) {
|
|
|
|
let buildpath = fs::connect(_path, "/build");
|
|
|
|
need_dir(buildpath);
|
|
|
|
#debug("Installing: %s -> %s", cf, buildpath);
|
|
|
|
let p = run::program_output("rustc", ["--out-dir", buildpath, cf]);
|
|
|
|
if p.status != 0 {
|
|
|
|
error(#fmt["rustc failed: %d\n%s\n%s", p.status, p.err, p.out]);
|
|
|
|
ret;
|
|
|
|
}
|
|
|
|
let new = fs::list_dir(buildpath);
|
|
|
|
let exec_suffix = os::exec_suffix();
|
|
|
|
for ct: str in new {
|
2011-12-16 03:08:24 -06:00
|
|
|
if (exec_suffix != "" && str::ends_with(ct, exec_suffix)) ||
|
2011-12-16 21:39:33 -06:00
|
|
|
(exec_suffix == "" && !str::starts_with(ct, "./lib")) {
|
2011-12-22 16:42:52 -06:00
|
|
|
#debug(" bin: %s", ct);
|
2011-12-08 22:41:29 -06:00
|
|
|
// FIXME: need libstd fs::copy or something
|
2011-12-08 22:50:25 -06:00
|
|
|
run::run_program("cp", [ct, c.bindir]);
|
2011-12-08 22:41:29 -06:00
|
|
|
} else {
|
2011-12-22 16:42:52 -06:00
|
|
|
#debug(" lib: %s", ct);
|
2011-12-08 22:50:25 -06:00
|
|
|
run::run_program("cp", [ct, c.libdir]);
|
2011-12-08 22:41:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-08 22:50:25 -06:00
|
|
|
fn install_source(c: cargo, path: str) {
|
2011-12-22 16:42:52 -06:00
|
|
|
#debug("source: %s", path);
|
2011-12-01 21:37:56 -06:00
|
|
|
fs::change_dir(path);
|
|
|
|
let contents = fs::list_dir(".");
|
|
|
|
|
2011-12-22 16:42:52 -06:00
|
|
|
#debug("contents: %s", str::connect(contents, ", "));
|
2011-12-01 21:37:56 -06:00
|
|
|
|
2011-12-16 03:08:24 -06:00
|
|
|
let cratefiles =
|
2011-12-16 08:27:50 -06:00
|
|
|
vec::filter::<str>(contents, { |n| str::ends_with(n, ".rc") });
|
2011-12-01 21:37:56 -06:00
|
|
|
|
2011-12-08 22:41:29 -06:00
|
|
|
if vec::is_empty(cratefiles) {
|
|
|
|
fail "This doesn't look like a rust package (no .rc files).";
|
2011-12-01 21:37:56 -06:00
|
|
|
}
|
|
|
|
|
2011-12-08 22:41:29 -06:00
|
|
|
for cf: str in cratefiles {
|
|
|
|
let p = load_pkg(cf);
|
|
|
|
alt p {
|
2012-01-19 00:37:22 -06:00
|
|
|
none { cont; }
|
2011-12-08 22:41:29 -06:00
|
|
|
some(_p) {
|
2012-01-18 21:16:14 -06:00
|
|
|
if c.test {
|
|
|
|
test_one_crate(c, path, cf, _p);
|
|
|
|
}
|
2011-12-08 22:50:25 -06:00
|
|
|
install_one_crate(c, path, cf, _p);
|
2011-12-08 22:41:29 -06:00
|
|
|
}
|
|
|
|
}
|
2011-12-01 21:37:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 22:41:22 -06:00
|
|
|
fn install_git(c: cargo, wd: str, url: str, ref: option::t<str>) {
|
2011-12-16 21:08:25 -06:00
|
|
|
run::run_program("git", ["clone", url, wd]);
|
2011-12-20 22:41:22 -06:00
|
|
|
if option::is_some::<str>(ref) {
|
|
|
|
let r = option::get::<str>(ref);
|
|
|
|
fs::change_dir(wd);
|
|
|
|
run::run_program("git", ["checkout", r]);
|
|
|
|
}
|
|
|
|
|
2011-12-08 23:39:41 -06:00
|
|
|
install_source(c, wd);
|
2011-12-08 23:34:06 -06:00
|
|
|
}
|
|
|
|
|
2011-12-16 21:08:25 -06:00
|
|
|
fn install_curl(c: cargo, wd: str, url: str) {
|
|
|
|
let tarpath = fs::connect(wd, "pkg.tar");
|
|
|
|
let p = run::program_output("curl", ["-f", "-s", "-o",
|
|
|
|
tarpath, url]);
|
|
|
|
if p.status != 0 {
|
|
|
|
fail #fmt["Fetch of %s failed: %s", url, p.err];
|
|
|
|
}
|
2011-12-08 23:39:41 -06:00
|
|
|
run::run_program("tar", ["-x", "--strip-components=1",
|
2011-12-16 21:08:25 -06:00
|
|
|
"-C", wd, "-f", tarpath]);
|
2011-12-16 21:27:04 -06:00
|
|
|
install_source(c, wd);
|
2011-12-16 21:08:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn install_file(c: cargo, wd: str, path: str) {
|
|
|
|
run::run_program("tar", ["-x", "--strip-components=1",
|
|
|
|
"-C", wd, "-f", path]);
|
2011-12-08 23:39:41 -06:00
|
|
|
install_source(c, wd);
|
2011-11-30 17:57:46 -06:00
|
|
|
}
|
|
|
|
|
2011-12-16 21:08:25 -06:00
|
|
|
fn install_package(c: cargo, wd: str, pkg: package) {
|
|
|
|
info("Installing with " + pkg.method + " from " + pkg.url + "...");
|
|
|
|
if pkg.method == "git" {
|
2011-12-20 22:41:22 -06:00
|
|
|
install_git(c, wd, pkg.url, pkg.ref);
|
2011-12-16 21:08:25 -06:00
|
|
|
} else if pkg.method == "http" {
|
|
|
|
install_curl(c, wd, pkg.url);
|
|
|
|
} else if pkg.method == "file" {
|
|
|
|
install_file(c, wd, pkg.url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-15 19:27:55 -06:00
|
|
|
fn install_uuid(c: cargo, wd: str, uuid: str) {
|
2011-12-16 19:33:39 -06:00
|
|
|
let ps = [];
|
2011-12-16 21:08:25 -06:00
|
|
|
for_each_package(c, { |s, p|
|
|
|
|
info(#fmt["%s ? %s", p.uuid, uuid]);
|
2011-12-16 19:33:39 -06:00
|
|
|
if p.uuid == uuid {
|
2011-12-16 21:08:25 -06:00
|
|
|
vec::grow(ps, 1u, (s, p));
|
2011-12-16 19:33:39 -06:00
|
|
|
}
|
|
|
|
});
|
2011-12-16 21:08:25 -06:00
|
|
|
if vec::len(ps) == 1u {
|
2011-12-16 21:27:04 -06:00
|
|
|
let (_, p) = ps[0];
|
2011-12-16 21:08:25 -06:00
|
|
|
install_package(c, wd, p);
|
|
|
|
ret;
|
|
|
|
} else if vec::len(ps) == 0u {
|
|
|
|
error("No packages.");
|
|
|
|
ret;
|
|
|
|
}
|
|
|
|
error("Found multiple packages:");
|
|
|
|
for (s,p) in ps {
|
|
|
|
info(" " + s.name + "/" + p.uuid + " (" + p.name + ")");
|
2011-12-16 19:33:39 -06:00
|
|
|
}
|
2011-12-15 19:27:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn install_named(c: cargo, wd: str, name: str) {
|
2011-12-16 19:33:39 -06:00
|
|
|
let ps = [];
|
2011-12-16 21:08:25 -06:00
|
|
|
for_each_package(c, { |s, p|
|
2011-12-16 19:33:39 -06:00
|
|
|
if p.name == name {
|
2011-12-16 21:08:25 -06:00
|
|
|
vec::grow(ps, 1u, (s, p));
|
2011-12-16 19:33:39 -06:00
|
|
|
}
|
|
|
|
});
|
2011-12-16 21:08:25 -06:00
|
|
|
if vec::len(ps) == 1u {
|
2011-12-16 21:27:04 -06:00
|
|
|
let (_, p) = ps[0];
|
2011-12-16 21:08:25 -06:00
|
|
|
install_package(c, wd, p);
|
|
|
|
ret;
|
|
|
|
} else if vec::len(ps) == 0u {
|
|
|
|
error("No packages.");
|
|
|
|
ret;
|
|
|
|
}
|
|
|
|
error("Found multiple packages:");
|
|
|
|
for (s,p) in ps {
|
|
|
|
info(" " + s.name + "/" + p.uuid + " (" + p.name + ")");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn install_uuid_specific(c: cargo, wd: str, src: str, uuid: str) {
|
|
|
|
alt c.sources.find(src) {
|
|
|
|
some(s) {
|
2011-12-16 21:31:49 -06:00
|
|
|
if vec::any(copy s.packages, { |p|
|
2011-12-16 21:08:25 -06:00
|
|
|
if p.uuid == uuid {
|
|
|
|
install_package(c, wd, p);
|
|
|
|
ret true;
|
|
|
|
}
|
|
|
|
ret false;
|
|
|
|
}) { ret; }
|
|
|
|
}
|
|
|
|
_ { }
|
2011-12-16 19:33:39 -06:00
|
|
|
}
|
2011-12-16 21:08:25 -06:00
|
|
|
error("Can't find package " + src + "/" + uuid);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn install_named_specific(c: cargo, wd: str, src: str, name: str) {
|
|
|
|
alt c.sources.find(src) {
|
|
|
|
some(s) {
|
2011-12-16 21:31:49 -06:00
|
|
|
if vec::any(copy s.packages, { |p|
|
2011-12-16 21:08:25 -06:00
|
|
|
if p.name == name {
|
|
|
|
install_package(c, wd, p);
|
|
|
|
ret true;
|
|
|
|
}
|
|
|
|
ret false;
|
|
|
|
}) { ret; }
|
|
|
|
}
|
|
|
|
_ { }
|
|
|
|
}
|
|
|
|
error("Can't find package " + src + "/" + name);
|
2011-12-15 19:27:55 -06:00
|
|
|
}
|
|
|
|
|
2011-12-08 22:50:25 -06:00
|
|
|
fn cmd_install(c: cargo, argv: [str]) {
|
2011-11-30 17:57:46 -06:00
|
|
|
// cargo install <pkg>
|
|
|
|
if vec::len(argv) < 3u {
|
|
|
|
cmd_usage();
|
|
|
|
ret;
|
|
|
|
}
|
|
|
|
|
2012-01-18 21:16:14 -06:00
|
|
|
let target = argv[2];
|
|
|
|
// TODO: getopts
|
|
|
|
if vec::len(argv) > 3u && argv[2] == "--test" {
|
|
|
|
c.test = true;
|
|
|
|
target = argv[3];
|
|
|
|
}
|
|
|
|
|
2011-12-08 23:39:41 -06:00
|
|
|
let wd = alt tempfile::mkdtemp(c.workdir + fs::path_sep(), "") {
|
|
|
|
some(_wd) { _wd }
|
2012-01-19 00:37:22 -06:00
|
|
|
none { fail "needed temp dir"; }
|
2011-12-08 23:39:41 -06:00
|
|
|
};
|
|
|
|
|
2012-01-18 21:16:14 -06:00
|
|
|
if str::starts_with(target, "uuid:") {
|
|
|
|
let uuid = rest(target, 5u);
|
2011-12-16 21:08:25 -06:00
|
|
|
let idx = str::index(uuid, '/' as u8);
|
|
|
|
if idx != -1 {
|
|
|
|
let source = str::slice(uuid, 0u, idx as uint);
|
|
|
|
uuid = str::slice(uuid, idx as uint + 1u, str::byte_len(uuid));
|
|
|
|
install_uuid_specific(c, wd, source, uuid);
|
|
|
|
} else {
|
|
|
|
install_uuid(c, wd, uuid);
|
|
|
|
}
|
2011-12-15 19:27:55 -06:00
|
|
|
} else {
|
2012-01-18 21:16:14 -06:00
|
|
|
let name = target;
|
2011-12-16 21:08:25 -06:00
|
|
|
let idx = str::index(name, '/' as u8);
|
|
|
|
if idx != -1 {
|
|
|
|
let source = str::slice(name, 0u, idx as uint);
|
|
|
|
name = str::slice(name, idx as uint + 1u, str::byte_len(name));
|
|
|
|
install_named_specific(c, wd, source, name);
|
|
|
|
} else {
|
|
|
|
install_named(c, wd, name);
|
|
|
|
}
|
2011-12-01 21:37:56 -06:00
|
|
|
}
|
2011-11-30 17:57:46 -06:00
|
|
|
}
|
|
|
|
|
2011-12-16 19:33:39 -06:00
|
|
|
fn sync_one(c: cargo, name: str, src: source) {
|
|
|
|
let dir = fs::connect(c.sourcedir, name);
|
2011-12-20 18:59:37 -06:00
|
|
|
let pkgfile = fs::connect(dir, "packages.json.new");
|
|
|
|
let destpkgfile = fs::connect(dir, "packages.json");
|
|
|
|
let sigfile = fs::connect(dir, "packages.json.sig");
|
|
|
|
let keyfile = fs::connect(dir, "key.gpg");
|
2011-12-16 19:33:39 -06:00
|
|
|
let url = src.url;
|
|
|
|
need_dir(dir);
|
2011-12-16 21:08:25 -06:00
|
|
|
info(#fmt["fetching source %s...", name]);
|
2011-12-16 19:33:39 -06:00
|
|
|
let p = run::program_output("curl", ["-f", "-s", "-o", pkgfile, url]);
|
|
|
|
if p.status != 0 {
|
|
|
|
warn(#fmt["fetch for source %s (url %s) failed", name, url]);
|
|
|
|
} else {
|
|
|
|
info(#fmt["fetched source: %s", name]);
|
|
|
|
}
|
2011-12-20 18:59:37 -06:00
|
|
|
alt src.sig {
|
|
|
|
some(u) {
|
2011-12-20 20:52:50 -06:00
|
|
|
let p = run::program_output("curl", ["-f", "-s", "-o", sigfile,
|
|
|
|
u]);
|
2011-12-20 18:59:37 -06:00
|
|
|
if p.status != 0 {
|
|
|
|
warn(#fmt["fetch for source %s (sig %s) failed", name, u]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ { }
|
|
|
|
}
|
|
|
|
alt src.key {
|
|
|
|
some(u) {
|
2011-12-20 20:52:50 -06:00
|
|
|
let p = run::program_output("curl", ["-f", "-s", "-o", keyfile,
|
|
|
|
u]);
|
2011-12-20 18:59:37 -06:00
|
|
|
if p.status != 0 {
|
|
|
|
warn(#fmt["fetch for source %s (key %s) failed", name, u]);
|
|
|
|
}
|
|
|
|
pgp::add(c.root, keyfile);
|
|
|
|
}
|
|
|
|
_ { }
|
|
|
|
}
|
|
|
|
alt (src.sig, src.key, src.keyfp) {
|
|
|
|
(some(_), some(_), some(f)) {
|
|
|
|
let r = pgp::verify(c.root, pkgfile, sigfile, f);
|
|
|
|
if !r {
|
2011-12-20 20:52:50 -06:00
|
|
|
warn(#fmt["signature verification failed for source %s",
|
|
|
|
name]);
|
2011-12-20 18:59:37 -06:00
|
|
|
ret;
|
|
|
|
} else {
|
|
|
|
info(#fmt["signature ok for source %s", name]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ {
|
|
|
|
info(#fmt["no signature for source %s", name]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
run::run_program("cp", [pkgfile, destpkgfile]);
|
2011-12-16 19:33:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn cmd_sync(c: cargo, argv: [str]) {
|
|
|
|
if vec::len(argv) == 3u {
|
|
|
|
sync_one(c, argv[2], c.sources.get(argv[2]));
|
|
|
|
} else {
|
|
|
|
c.sources.items { |k, v|
|
|
|
|
sync_one(c, k, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 20:52:50 -06:00
|
|
|
fn cmd_init(c: cargo) {
|
2011-12-20 18:59:37 -06:00
|
|
|
let srcurl = "http://www.rust-lang.org/cargo/sources.json";
|
|
|
|
let sigurl = "http://www.rust-lang.org/cargo/sources.json.sig";
|
|
|
|
|
|
|
|
let srcfile = fs::connect(c.root, "sources.json.new");
|
|
|
|
let sigfile = fs::connect(c.root, "sources.json.sig");
|
|
|
|
let destsrcfile = fs::connect(c.root, "sources.json");
|
|
|
|
|
|
|
|
let p = run::program_output("curl", ["-f", "-s", "-o", srcfile, srcurl]);
|
|
|
|
if p.status != 0 {
|
|
|
|
warn(#fmt["fetch of sources.json failed: %s", p.out]);
|
|
|
|
ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
let p = run::program_output("curl", ["-f", "-s", "-o", sigfile, sigurl]);
|
|
|
|
if p.status != 0 {
|
|
|
|
warn(#fmt["fetch of sources.json.sig failed: %s", p.out]);
|
|
|
|
ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
let r = pgp::verify(c.root, srcfile, sigfile, pgp::signing_key_fp());
|
|
|
|
if !r {
|
|
|
|
warn(#fmt["signature verification failed for sources.json"]);
|
|
|
|
ret;
|
|
|
|
}
|
|
|
|
info(#fmt["signature ok for sources.json"]);
|
|
|
|
run::run_program("cp", [srcfile, destsrcfile]);
|
|
|
|
}
|
|
|
|
|
2011-12-20 19:41:23 -06:00
|
|
|
fn print_pkg(s: source, p: package) {
|
|
|
|
let m = s.name + "/" + p.name + " (" + p.uuid + ")";
|
|
|
|
if vec::len(p.tags) > 0u {
|
|
|
|
m = m + " [" + str::connect(p.tags, ", ") + "]";
|
|
|
|
}
|
|
|
|
info(m);
|
|
|
|
}
|
|
|
|
fn cmd_list(c: cargo, argv: [str]) {
|
|
|
|
for_each_package(c, { |s, p|
|
|
|
|
if vec::len(argv) <= 2u || argv[2] == s.name {
|
|
|
|
print_pkg(s, p);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cmd_search(c: cargo, argv: [str]) {
|
|
|
|
if vec::len(argv) < 3u {
|
|
|
|
cmd_usage();
|
|
|
|
ret;
|
|
|
|
}
|
|
|
|
let n = 0;
|
|
|
|
let name = argv[2];
|
|
|
|
let tags = vec::slice(argv, 3u, vec::len(argv));
|
|
|
|
for_each_package(c, { |s, p|
|
|
|
|
if (str::contains(p.name, name) || name == "*") &&
|
|
|
|
vec::all(tags, { |t| vec::member(t, p.tags) }) {
|
|
|
|
print_pkg(s, p);
|
|
|
|
n += 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
info(#fmt["Found %d packages.", n]);
|
|
|
|
}
|
|
|
|
|
2011-11-30 17:57:46 -06:00
|
|
|
fn cmd_usage() {
|
|
|
|
print("Usage: cargo <verb> [args...]");
|
2012-01-18 21:26:19 -06:00
|
|
|
print(" init Set up ~/.cargo");
|
2012-01-18 21:16:14 -06:00
|
|
|
print(" install [--test] [source/]package-name Install by name");
|
|
|
|
print(" install [--test] uuid:[source/]package-uuid Install by uuid");
|
|
|
|
print(" list [source] List packages");
|
|
|
|
print(" search <name | '*'> [tags...] Search packages");
|
|
|
|
print(" sync Sync all sources");
|
|
|
|
print(" usage This");
|
2011-11-30 17:57:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main(argv: [str]) {
|
|
|
|
if vec::len(argv) < 2u {
|
|
|
|
cmd_usage();
|
|
|
|
ret;
|
|
|
|
}
|
2011-12-08 22:50:25 -06:00
|
|
|
let c = configure();
|
2011-11-30 17:57:46 -06:00
|
|
|
alt argv[1] {
|
2011-12-20 20:52:50 -06:00
|
|
|
"init" { cmd_init(c); }
|
2011-12-08 22:50:25 -06:00
|
|
|
"install" { cmd_install(c, argv); }
|
2011-12-20 19:41:23 -06:00
|
|
|
"list" { cmd_list(c, argv); }
|
|
|
|
"search" { cmd_search(c, argv); }
|
2011-12-16 19:33:39 -06:00
|
|
|
"sync" { cmd_sync(c, argv); }
|
2011-11-30 17:57:46 -06:00
|
|
|
"usage" { cmd_usage(); }
|
|
|
|
_ { cmd_usage(); }
|
|
|
|
}
|
|
|
|
}
|