Skip to content

Commit f22b4b1

Browse files
committed
auto merge of #8697 : kballard/rust/rustpkg-no-args, r=catamorphism
`rustpkg build` et al were only checking one directory up to see if it was in a dir named "src". Ditch that entirely and instead check if the cwd is descended from any of the workspace paths. Besides being more intelligent about whether or not something is a workspace, this also allows for package ids composed of multiple path components. r? @catamorphism
2 parents 578e680 + eafa63f commit f22b4b1

File tree

3 files changed

+35
-36
lines changed

3 files changed

+35
-36
lines changed

src/librustpkg/rustpkg.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use path_util::{U_RWX, in_rust_path};
4343
use path_util::{built_executable_in_workspace, built_library_in_workspace, default_workspace};
4444
use path_util::{target_executable_in_workspace, target_library_in_workspace};
4545
use source_control::is_git_dir;
46-
use workspace::{each_pkg_parent_workspace, pkg_parent_workspaces, in_workspace, cwd_to_workspace};
46+
use workspace::{each_pkg_parent_workspace, pkg_parent_workspaces, cwd_to_workspace};
4747
use context::Ctx;
4848
use package_id::PkgId;
4949
use package_source::PkgSrc;
@@ -190,11 +190,10 @@ impl CtxMethods for Ctx {
190190
match cmd {
191191
"build" => {
192192
if args.len() < 1 {
193-
if !in_workspace(|| { usage::build() } ) {
194-
return;
193+
match cwd_to_workspace() {
194+
None => { usage::build(); return }
195+
Some((ws, pkgid)) => self.build(&ws, &pkgid)
195196
}
196-
let (workspace, pkgid) = cwd_to_workspace();
197-
self.build(&workspace, &pkgid);
198197
}
199198
else {
200199
// The package id is presumed to be the first command-line
@@ -210,13 +209,12 @@ impl CtxMethods for Ctx {
210209
}
211210
"clean" => {
212211
if args.len() < 1 {
213-
if !in_workspace(|| { usage::clean() } ) {
214-
return;
212+
match cwd_to_workspace() {
213+
None => { usage::clean(); return }
214+
// tjc: Maybe clean should clean all the packages in the
215+
// current workspace, though?
216+
Some((ws, pkgid)) => self.clean(&ws, &pkgid)
215217
}
216-
// tjc: Maybe clean should clean all the packages in the
217-
// current workspace, though?
218-
let (workspace, pkgid) = cwd_to_workspace();
219-
self.clean(&workspace, &pkgid);
220218

221219
}
222220
else {
@@ -239,11 +237,10 @@ impl CtxMethods for Ctx {
239237
}
240238
"install" => {
241239
if args.len() < 1 {
242-
if !in_workspace(|| { usage::install() }) {
243-
return;
240+
match cwd_to_workspace() {
241+
None => { usage::install(); return }
242+
Some((ws, pkgid)) => self.install(&ws, &pkgid)
244243
}
245-
let (workspace, pkgid) = cwd_to_workspace();
246-
self.install(&workspace, &pkgid);
247244
}
248245
else {
249246
// The package id is presumed to be the first command-line

src/librustpkg/tests.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,8 @@ fn package_script_with_default_build() {
695695
696696
#[test]
697697
fn rustpkg_build_no_arg() {
698-
let tmp = mkdtemp(&os::tmpdir(), "rustpkg_build_no_arg").expect("rustpkg_build_no_arg failed");
698+
let tmp = mkdtemp(&os::tmpdir(), "rustpkg_build_no_arg").expect("rustpkg_build_no_arg failed")
699+
.push(".rust");
699700
let package_dir = tmp.push("src").push("foo");
700701
assert!(os::mkdir_recursive(&package_dir, U_RWX));
701702
@@ -709,7 +710,8 @@ fn rustpkg_build_no_arg() {
709710
#[test]
710711
fn rustpkg_install_no_arg() {
711712
let tmp = mkdtemp(&os::tmpdir(),
712-
"rustpkg_install_no_arg").expect("rustpkg_install_no_arg failed");
713+
"rustpkg_install_no_arg").expect("rustpkg_install_no_arg failed")
714+
.push(".rust");
713715
let package_dir = tmp.push("src").push("foo");
714716
assert!(os::mkdir_recursive(&package_dir, U_RWX));
715717
writeFile(&package_dir.push("lib.rs"),
@@ -721,7 +723,8 @@ fn rustpkg_install_no_arg() {
721723
722724
#[test]
723725
fn rustpkg_clean_no_arg() {
724-
let tmp = mkdtemp(&os::tmpdir(), "rustpkg_clean_no_arg").expect("rustpkg_clean_no_arg failed");
726+
let tmp = mkdtemp(&os::tmpdir(), "rustpkg_clean_no_arg").expect("rustpkg_clean_no_arg failed")
727+
.push(".rust");
725728
let package_dir = tmp.push("src").push("foo");
726729
assert!(os::mkdir_recursive(&package_dir, U_RWX));
727730

src/librustpkg/workspace.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010

1111
// rustpkg utilities having to do with workspaces
1212

13-
use std::os;
13+
use std::{os,util};
1414
use std::path::Path;
1515
use path_util::workspace_contains_package_id;
1616
use package_id::PkgId;
1717

18-
use rustc::metadata::filesearch::rust_path;
18+
use path_util::rust_path;
1919

2020
pub fn each_pkg_parent_workspace(pkgid: &PkgId, action: &fn(&Path) -> bool) -> bool {
2121
// Using the RUST_PATH, find workspaces that contain
@@ -42,23 +42,22 @@ pub fn pkg_parent_workspaces(pkgid: &PkgId) -> ~[Path] {
4242
.collect()
4343
}
4444

45-
pub fn in_workspace(complain: &fn()) -> bool {
46-
let dir_part = os::getcwd().pop().components.clone();
47-
if *(dir_part.last()) != ~"src" {
48-
complain();
49-
false
50-
}
51-
else {
52-
true
53-
}
54-
}
55-
5645
/// Construct a workspace and package-ID name based on the current directory.
5746
/// This gets used when rustpkg gets invoked without a package-ID argument.
58-
pub fn cwd_to_workspace() -> (Path, PkgId) {
47+
pub fn cwd_to_workspace() -> Option<(Path, PkgId)> {
5948
let cwd = os::getcwd();
60-
let ws = cwd.pop().pop();
61-
let cwd_ = cwd.clone();
62-
let pkgid = cwd_.components.last().to_str();
63-
(ws, PkgId::new(pkgid))
49+
for path in rust_path().move_iter() {
50+
let srcpath = path.push("src");
51+
if srcpath.is_ancestor_of(&cwd) {
52+
// I'd love to use srcpath.get_relative_to(cwd) but it behaves wrong
53+
// I'd say broken, but it has tests enforcing the wrong behavior.
54+
// instead, just hack up the components vec
55+
let mut pkgid = cwd;
56+
pkgid.is_absolute = false;
57+
let comps = util::replace(&mut pkgid.components, ~[]);
58+
pkgid.components = comps.move_iter().skip(srcpath.components.len()).collect();
59+
return Some((path, PkgId::new(pkgid.components.connect("/"))))
60+
}
61+
}
62+
None
6463
}

0 commit comments

Comments
 (0)