Skip to content

Commit fdbd56c

Browse files
committed
auto merge of #7419 : catamorphism/rust/default-package, r=graydon
r? @brson `rustpkg build`, if executed in a package source directory inside a workspace, will now build that package. By "inside a workspace" I mean that the parent directory has to be called `src`, and rustpkg will create a `build` directory in .. if there isn't already one. Same goes for `rustpkg install` and `rustpkg clean`. For the time being, `rustpkg build` (etc.) will still error out if you run it inside a directory whose parent isn't called `src`. I'm not sure whether or not it's desirable to have it do something in a non-workspace directory.
2 parents 098709a + e39ea2a commit fdbd56c

File tree

7 files changed

+210
-70
lines changed

7 files changed

+210
-70
lines changed

src/librustpkg/installed_packages.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,18 @@ pub fn list_installed_packages(f: &fn(&PkgId) -> bool) -> bool {
1818
for workspaces.iter().advance |p| {
1919
let binfiles = os::list_dir(&p.push("bin"));
2020
for binfiles.iter().advance() |exec| {
21-
f(&PkgId::new(*exec, p));
21+
let exec_path = Path(*exec).filestem();
22+
do exec_path.iter().advance |s| {
23+
f(&PkgId::new(*s, p))
24+
};
2225
}
2326
let libfiles = os::list_dir(&p.push("lib"));
2427
for libfiles.iter().advance() |lib| {
25-
f(&PkgId::new(*lib, p));
28+
debug!("Full name: %s", *lib);
29+
let lib_path = Path(*lib).filestem();
30+
do lib_path.iter().advance |s| {
31+
f(&PkgId::new(*s, p))
32+
};
2633
}
2734
}
2835
true

src/librustpkg/path_util.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,17 @@ pub fn rust_path() -> ~[Path] {
5454
};
5555
let cwd = os::getcwd();
5656
// now add in default entries
57-
env_rust_path.push(cwd.push(".rust"));
5857
env_rust_path.push(cwd.clone());
5958
do cwd.each_parent() |p| { push_if_exists(&mut env_rust_path, p) };
6059
let h = os::homedir();
61-
for h.iter().advance |h| { push_if_exists(&mut env_rust_path, h); }
60+
// Avoid adding duplicates
61+
// could still add dups if someone puts one of these in the RUST_PATH
62+
// manually, though...
63+
for h.iter().advance |hdir| {
64+
if !(cwd.is_ancestor_of(hdir) || hdir.is_ancestor_of(&cwd)) {
65+
push_if_exists(&mut env_rust_path, hdir);
66+
}
67+
}
6268
env_rust_path
6369
}
6470

src/librustpkg/rustpkg.rs

+52-38
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use path_util::{U_RWX, rust_path, in_rust_path};
4242
use path_util::{built_executable_in_workspace, built_library_in_workspace, default_workspace};
4343
use path_util::{target_executable_in_workspace, target_library_in_workspace};
4444
use source_control::is_git_dir;
45-
use workspace::{each_pkg_parent_workspace, pkg_parent_workspaces};
45+
use workspace::{each_pkg_parent_workspace, pkg_parent_workspaces, in_workspace, cwd_to_workspace};
4646
use context::Ctx;
4747
use package_id::PkgId;
4848
use package_source::PkgSrc;
@@ -199,26 +199,41 @@ impl CtxMethods for Ctx {
199199
match cmd {
200200
"build" => {
201201
if args.len() < 1 {
202-
return usage::build();
202+
if !in_workspace(|| { usage::build() } ) {
203+
return;
204+
}
205+
let (workspace, pkgid) = cwd_to_workspace();
206+
self.build(&workspace, &pkgid);
203207
}
204-
// The package id is presumed to be the first command-line
205-
// argument
206-
let pkgid = PkgId::new(args[0].clone(), &os::getcwd());
207-
for each_pkg_parent_workspace(&pkgid) |workspace| {
208-
debug!("found pkg %s in workspace %s, trying to build",
209-
pkgid.to_str(), workspace.to_str());
210-
self.build(workspace, &pkgid);
208+
else {
209+
// The package id is presumed to be the first command-line
210+
// argument
211+
let pkgid = PkgId::new(args[0].clone(), &os::getcwd());
212+
for each_pkg_parent_workspace(&pkgid) |workspace| {
213+
debug!("found pkg %s in workspace %s, trying to build",
214+
pkgid.to_str(), workspace.to_str());
215+
self.build(workspace, &pkgid);
216+
}
211217
}
212218
}
213219
"clean" => {
214220
if args.len() < 1 {
215-
return usage::build();
221+
if !in_workspace(|| { usage::clean() } ) {
222+
return;
223+
}
224+
// tjc: Maybe clean should clean all the packages in the
225+
// current workspace, though?
226+
let (workspace, pkgid) = cwd_to_workspace();
227+
self.clean(&workspace, &pkgid);
228+
229+
}
230+
else {
231+
// The package id is presumed to be the first command-line
232+
// argument
233+
let pkgid = PkgId::new(args[0].clone(), &os::getcwd());
234+
let cwd = os::getcwd();
235+
self.clean(&cwd, &pkgid); // tjc: should use workspace, not cwd
216236
}
217-
// The package id is presumed to be the first command-line
218-
// argument
219-
let pkgid = PkgId::new(args[0].clone(), &os::getcwd());
220-
let cwd = os::getcwd();
221-
self.clean(&cwd, &pkgid); // tjc: should use workspace, not cwd
222237
}
223238
"do" => {
224239
if args.len() < 2 {
@@ -232,37 +247,36 @@ impl CtxMethods for Ctx {
232247
}
233248
"install" => {
234249
if args.len() < 1 {
235-
return usage::install();
236-
}
237-
238-
// The package id is presumed to be the first command-line
239-
// argument
240-
let pkgid = PkgId::new(args[0], &os::getcwd());
241-
let workspaces = pkg_parent_workspaces(&pkgid);
242-
if workspaces.is_empty() {
243-
debug!("install! workspaces was empty");
244-
let rp = rust_path();
245-
assert!(!rp.is_empty());
246-
let src = PkgSrc::new(&rp[0], &build_pkg_id_in_workspace(&pkgid, &rp[0]),
247-
&pkgid);
248-
src.fetch_git();
249-
self.install(&rp[0], &pkgid);
250+
if !in_workspace(|| { usage::install() }) {
251+
return;
252+
}
253+
let (workspace, pkgid) = cwd_to_workspace();
254+
self.install(&workspace, &pkgid);
250255
}
251256
else {
252-
for each_pkg_parent_workspace(&pkgid) |workspace| {
253-
debug!("install: found pkg %s in workspace %s, trying to build",
254-
pkgid.to_str(), workspace.to_str());
255-
256-
self.install(workspace, &pkgid);
257+
// The package id is presumed to be the first command-line
258+
// argument
259+
let pkgid = PkgId::new(args[0], &os::getcwd());
260+
let workspaces = pkg_parent_workspaces(&pkgid);
261+
if workspaces.is_empty() {
262+
let rp = rust_path();
263+
assert!(!rp.is_empty());
264+
let src = PkgSrc::new(&rp[0], &build_pkg_id_in_workspace(&pkgid, &rp[0]),
265+
&pkgid);
266+
src.fetch_git();
267+
self.install(&rp[0], &pkgid);
268+
}
269+
else {
270+
for each_pkg_parent_workspace(&pkgid) |workspace| {
271+
self.install(workspace, &pkgid);
272+
}
257273
}
258274
}
259275
}
260276
"list" => {
261277
io::println("Installed packages:");
262278
for installed_packages::list_installed_packages |pkg_id| {
263-
io::println(fmt!("%s-%s",
264-
pkg_id.local_path.to_str(),
265-
pkg_id.version.to_str()));
279+
io::println(pkg_id.local_path.to_str());
266280
}
267281
}
268282
"prefer" => {

src/librustpkg/tests.rs

+73-23
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ fn command_line_test(args: &[~str], cwd: &Path) -> ProcessOutput {
218218
fn command_line_test_with_env(args: &[~str], cwd: &Path, env: Option<~[(~str, ~str)]>)
219219
-> ProcessOutput {
220220
let cmd = test_sysroot().push("bin").push("rustpkg").to_str();
221-
let cwd = normalize(RemotePath((*cwd).clone()));
222221
debug!("About to run command: %? %? in %s", cmd, args, cwd.to_str());
223222
assert!(os::path_is_dir(&*cwd));
224223
let cwd = (*cwd).clone();
@@ -322,6 +321,14 @@ fn assert_executable_exists(repo: &Path, short_name: &str) {
322321
assert!(is_rwx(&exec));
323322
}
324323

324+
fn assert_built_executable_exists(repo: &Path, short_name: &str) {
325+
debug!("assert_built_executable_exists: repo = %s, short_name = %s", repo.to_str(), short_name);
326+
let exec = built_executable_in_workspace(&PkgId::new(short_name, repo),
327+
repo).expect("assert_built_executable_exists failed");
328+
assert!(os::path_exists(&exec));
329+
assert!(is_rwx(&exec));
330+
}
331+
325332
fn command_line_test_output(args: &[~str]) -> ~[~str] {
326333
let mut result = ~[];
327334
let p_output = command_line_test(args, &os::getcwd());
@@ -490,30 +497,29 @@ fn test_install_git() {
490497
// should have test, bench, lib, and main
491498
command_line_test([~"install", temp_pkg_id.local_path.to_str()], &repo);
492499
// Check that all files exist
493-
let ws = repo.push(".rust");
494-
debug!("Checking for files in %s", ws.to_str());
495-
let exec = target_executable_in_workspace(&temp_pkg_id, &ws);
500+
debug!("Checking for files in %s", repo.to_str());
501+
let exec = target_executable_in_workspace(&temp_pkg_id, &repo);
496502
debug!("exec = %s", exec.to_str());
497503
assert!(os::path_exists(&exec));
498504
assert!(is_rwx(&exec));
499505
let _built_lib =
500506
built_library_in_workspace(&temp_pkg_id,
501-
&ws).expect("test_install_git: built lib should exist");
502-
let lib = target_library_in_workspace(&temp_pkg_id, &ws);
507+
&repo).expect("test_install_git: built lib should exist");
508+
let lib = target_library_in_workspace(&temp_pkg_id, &repo);
503509
debug!("lib = %s", lib.to_str());
504510
assert!(os::path_exists(&lib));
505511
assert!(is_rwx(&lib));
506512
let built_test = built_test_in_workspace(&temp_pkg_id,
507-
&ws).expect("test_install_git: built test should exist");
513+
&repo).expect("test_install_git: built test should exist");
508514
assert!(os::path_exists(&built_test));
509515
let built_bench = built_bench_in_workspace(&temp_pkg_id,
510-
&ws).expect("test_install_git: built bench should exist");
516+
&repo).expect("test_install_git: built bench should exist");
511517
assert!(os::path_exists(&built_bench));
512518
// And that the test and bench executables aren't installed
513-
let test = target_test_in_workspace(&temp_pkg_id, &ws);
519+
let test = target_test_in_workspace(&temp_pkg_id, &repo);
514520
assert!(!os::path_exists(&test));
515521
debug!("test = %s", test.to_str());
516-
let bench = target_bench_in_workspace(&temp_pkg_id, &ws);
522+
let bench = target_bench_in_workspace(&temp_pkg_id, &repo);
517523
debug!("bench = %s", bench.to_str());
518524
assert!(!os::path_exists(&bench));
519525
}
@@ -586,12 +592,12 @@ fn test_package_version() {
586592
command_line_test([~"install", ~"mockgithub.com/catamorphism/test_pkg_version"],
587593
&repo);
588594
assert!(match built_library_in_workspace(&temp_pkg_id,
589-
&repo.push(".rust")) {
595+
&repo) {
590596
Some(p) => p.to_str().ends_with(fmt!("0.4%s", os::consts::DLL_SUFFIX)),
591597
None => false
592598
});
593-
assert!(built_executable_in_workspace(&temp_pkg_id, &repo.push(".rust"))
594-
== Some(repo.push(".rust").push("build").
599+
assert!(built_executable_in_workspace(&temp_pkg_id, &repo)
600+
== Some(repo.push("build").
595601
push("mockgithub.com").
596602
push("catamorphism").
597603
push("test_pkg_version").
@@ -689,7 +695,7 @@ fn rustpkg_library_target() {
689695
690696
add_git_tag(&package_dir, ~"1.0");
691697
command_line_test([~"install", ~"foo"], &foo_repo);
692-
assert_lib_exists(&foo_repo.push(".rust"), "foo", ExactRevision(~"1.0"));
698+
assert_lib_exists(&foo_repo, "foo", ExactRevision(~"1.0"));
693699
}
694700
695701
#[test]
@@ -716,14 +722,55 @@ fn package_script_with_default_build() {
716722
assert!(os::path_exists(&dir.push("build").push("fancy_lib").push("generated.rs")));
717723
}
718724
725+
#[test]
726+
fn rustpkg_build_no_arg() {
727+
let tmp = mkdtemp(&os::tmpdir(), "rustpkg_build_no_arg").expect("rustpkg_build_no_arg failed");
728+
let package_dir = tmp.push("src").push("foo");
729+
assert!(os::mkdir_recursive(&package_dir, U_RWX));
730+
731+
writeFile(&package_dir.push("main.rs"),
732+
"fn main() { let _x = (); }");
733+
debug!("build_no_arg: dir = %s", package_dir.to_str());
734+
command_line_test([~"build"], &package_dir);
735+
assert_built_executable_exists(&tmp, "foo");
736+
}
737+
738+
#[test]
739+
fn rustpkg_install_no_arg() {
740+
let tmp = mkdtemp(&os::tmpdir(),
741+
"rustpkg_install_no_arg").expect("rustpkg_build_no_arg failed");
742+
let package_dir = tmp.push("src").push("foo");
743+
assert!(os::mkdir_recursive(&package_dir, U_RWX));
744+
writeFile(&package_dir.push("lib.rs"),
745+
"fn main() { let _x = (); }");
746+
debug!("install_no_arg: dir = %s", package_dir.to_str());
747+
command_line_test([~"install"], &package_dir);
748+
assert_lib_exists(&tmp, "foo", NoVersion);
749+
}
750+
751+
#[test]
752+
fn rustpkg_clean_no_arg() {
753+
let tmp = mkdtemp(&os::tmpdir(), "rustpkg_clean_no_arg").expect("rustpkg_clean_no_arg failed");
754+
let package_dir = tmp.push("src").push("foo");
755+
assert!(os::mkdir_recursive(&package_dir, U_RWX));
756+
757+
writeFile(&package_dir.push("main.rs"),
758+
"fn main() { let _x = (); }");
759+
debug!("clean_no_arg: dir = %s", package_dir.to_str());
760+
command_line_test([~"build"], &package_dir);
761+
assert_built_executable_exists(&tmp, "foo");
762+
command_line_test([~"clean"], &package_dir);
763+
assert!(!built_executable_in_workspace(&PkgId::new("foo", &package_dir),
764+
&tmp).map_default(false, |m| { os::path_exists(m) }));
765+
}
766+
719767
#[test]
720768
#[ignore (reason = "Un-ignore when #7071 is fixed")]
721769
fn rust_path_test() {
722770
let dir_for_path = mkdtemp(&os::tmpdir(), "more_rust").expect("rust_path_test failed");
723771
let dir = mk_workspace(&dir_for_path, &normalize(RemotePath(Path("foo"))), &NoVersion);
724772
debug!("dir = %s", dir.to_str());
725-
writeFile(&Path("/Users/tjc/more_rust/src/foo-0.1/main.rs"),
726-
"fn main() { let _x = (); }");
773+
writeFile(&dir.push("main.rs"), "fn main() { let _x = (); }");
727774
728775
let cwd = os::getcwd();
729776
debug!("cwd = %s", cwd.to_str());
@@ -781,21 +828,24 @@ fn test_list() {
781828
let quux = PkgId::new("quux", &dir);
782829
create_local_package_in(&quux, &dir);
783830
831+
// NOTE Not really great output, though...
832+
// NOTE do any tests need to be unignored?
784833
command_line_test([~"install", ~"foo"], &dir);
785834
let env_arg = ~[(~"RUST_PATH", dir.to_str())];
835+
debug!("RUST_PATH = %s", dir.to_str());
786836
let list_output = command_line_test_output_with_env([~"list"], env_arg.clone());
787-
assert!(list_output.iter().any(|x| x.starts_with("foo-")));
837+
assert!(list_output.iter().any(|x| x.starts_with("libfoo_")));
788838
789839
command_line_test([~"install", ~"bar"], &dir);
790840
let list_output = command_line_test_output_with_env([~"list"], env_arg.clone());
791-
assert!(list_output.iter().any(|x| x.starts_with("foo-")));
792-
assert!(list_output.iter().any(|x| x.starts_with("bar-")));
841+
assert!(list_output.iter().any(|x| x.starts_with("libfoo_")));
842+
assert!(list_output.iter().any(|x| x.starts_with("libbar_")));
793843
794844
command_line_test([~"install", ~"quux"], &dir);
795845
let list_output = command_line_test_output_with_env([~"list"], env_arg);
796-
assert!(list_output.iter().any(|x| x.starts_with("foo-")));
797-
assert!(list_output.iter().any(|x| x.starts_with("bar-")));
798-
assert!(list_output.iter().any(|x| x.starts_with("quux-")));
846+
assert!(list_output.iter().any(|x| x.starts_with("libfoo_")));
847+
assert!(list_output.iter().any(|x| x.starts_with("libbar_")));
848+
assert!(list_output.iter().any(|x| x.starts_with("libquux_")));
799849
}
800850
801851
#[test]
@@ -836,7 +886,7 @@ fn install_check_duplicates() {
836886
let mut contents = ~[];
837887
let check_dups = |p: &PkgId| {
838888
if contents.contains(p) {
839-
fail!("package database contains duplicate ID");
889+
fail!("package %s appears in `list` output more than once", p.local_path.to_str());
840890
}
841891
else {
842892
contents.push((*p).clone());

src/librustpkg/usage.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ Options:
2323
}
2424

2525
pub fn build() {
26-
io::println("rustpkg [options..] build
26+
io::println("rustpkg [options..] build [package-ID]
2727
28-
Build all targets described in the package script in the current
29-
directory.
28+
Build the given package ID if specified. With no package ID argument,
29+
build the package in the current directory. In that case, the current
30+
directory must be a direct child of an `src` directory in a workspace.
3031
3132
Options:
3233
-c, --cfg Pass a cfg flag to the package script");

0 commit comments

Comments
 (0)