From eafa63f787d04857268743e3941e7caa8c128bc4 Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Thu, 22 Aug 2013 19:18:38 -0700 Subject: [PATCH] Handle `rustpkg build`, etc. when given no args properly `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. --- src/librustpkg/rustpkg.rs | 27 ++++++++++++--------------- src/librustpkg/tests.rs | 9 ++++++--- src/librustpkg/workspace.rs | 35 +++++++++++++++++------------------ 3 files changed, 35 insertions(+), 36 deletions(-) diff --git a/src/librustpkg/rustpkg.rs b/src/librustpkg/rustpkg.rs index 768ecc41218..79836bcc555 100644 --- a/src/librustpkg/rustpkg.rs +++ b/src/librustpkg/rustpkg.rs @@ -43,7 +43,7 @@ use path_util::{U_RWX, in_rust_path}; use path_util::{built_executable_in_workspace, built_library_in_workspace, default_workspace}; use path_util::{target_executable_in_workspace, target_library_in_workspace}; use source_control::is_git_dir; -use workspace::{each_pkg_parent_workspace, pkg_parent_workspaces, in_workspace, cwd_to_workspace}; +use workspace::{each_pkg_parent_workspace, pkg_parent_workspaces, cwd_to_workspace}; use context::Ctx; use package_id::PkgId; use package_source::PkgSrc; @@ -190,11 +190,10 @@ impl CtxMethods for Ctx { match cmd { "build" => { if args.len() < 1 { - if !in_workspace(|| { usage::build() } ) { - return; + match cwd_to_workspace() { + None => { usage::build(); return } + Some((ws, pkgid)) => self.build(&ws, &pkgid) } - let (workspace, pkgid) = cwd_to_workspace(); - self.build(&workspace, &pkgid); } else { // The package id is presumed to be the first command-line @@ -210,13 +209,12 @@ impl CtxMethods for Ctx { } "clean" => { if args.len() < 1 { - if !in_workspace(|| { usage::clean() } ) { - return; + match cwd_to_workspace() { + None => { usage::clean(); return } + // tjc: Maybe clean should clean all the packages in the + // current workspace, though? + Some((ws, pkgid)) => self.clean(&ws, &pkgid) } - // tjc: Maybe clean should clean all the packages in the - // current workspace, though? - let (workspace, pkgid) = cwd_to_workspace(); - self.clean(&workspace, &pkgid); } else { @@ -239,11 +237,10 @@ impl CtxMethods for Ctx { } "install" => { if args.len() < 1 { - if !in_workspace(|| { usage::install() }) { - return; + match cwd_to_workspace() { + None => { usage::install(); return } + Some((ws, pkgid)) => self.install(&ws, &pkgid) } - let (workspace, pkgid) = cwd_to_workspace(); - self.install(&workspace, &pkgid); } else { // The package id is presumed to be the first command-line diff --git a/src/librustpkg/tests.rs b/src/librustpkg/tests.rs index 3b73d9d11df..98999da41c8 100644 --- a/src/librustpkg/tests.rs +++ b/src/librustpkg/tests.rs @@ -695,7 +695,8 @@ fn package_script_with_default_build() { #[test] fn rustpkg_build_no_arg() { - let tmp = mkdtemp(&os::tmpdir(), "rustpkg_build_no_arg").expect("rustpkg_build_no_arg failed"); + let tmp = mkdtemp(&os::tmpdir(), "rustpkg_build_no_arg").expect("rustpkg_build_no_arg failed") + .push(".rust"); let package_dir = tmp.push("src").push("foo"); assert!(os::mkdir_recursive(&package_dir, U_RWX)); @@ -709,7 +710,8 @@ fn rustpkg_build_no_arg() { #[test] fn rustpkg_install_no_arg() { let tmp = mkdtemp(&os::tmpdir(), - "rustpkg_install_no_arg").expect("rustpkg_install_no_arg failed"); + "rustpkg_install_no_arg").expect("rustpkg_install_no_arg failed") + .push(".rust"); let package_dir = tmp.push("src").push("foo"); assert!(os::mkdir_recursive(&package_dir, U_RWX)); writeFile(&package_dir.push("lib.rs"), @@ -721,7 +723,8 @@ fn rustpkg_install_no_arg() { #[test] fn rustpkg_clean_no_arg() { - let tmp = mkdtemp(&os::tmpdir(), "rustpkg_clean_no_arg").expect("rustpkg_clean_no_arg failed"); + let tmp = mkdtemp(&os::tmpdir(), "rustpkg_clean_no_arg").expect("rustpkg_clean_no_arg failed") + .push(".rust"); let package_dir = tmp.push("src").push("foo"); assert!(os::mkdir_recursive(&package_dir, U_RWX)); diff --git a/src/librustpkg/workspace.rs b/src/librustpkg/workspace.rs index 6ac959e4a32..1afe5d513cc 100644 --- a/src/librustpkg/workspace.rs +++ b/src/librustpkg/workspace.rs @@ -10,12 +10,12 @@ // rustpkg utilities having to do with workspaces -use std::os; +use std::{os,util}; use std::path::Path; use path_util::workspace_contains_package_id; use package_id::PkgId; -use rustc::metadata::filesearch::rust_path; +use path_util::rust_path; pub fn each_pkg_parent_workspace(pkgid: &PkgId, action: &fn(&Path) -> bool) -> bool { // Using the RUST_PATH, find workspaces that contain @@ -42,23 +42,22 @@ pub fn pkg_parent_workspaces(pkgid: &PkgId) -> ~[Path] { .collect() } -pub fn in_workspace(complain: &fn()) -> bool { - let dir_part = os::getcwd().pop().components.clone(); - if *(dir_part.last()) != ~"src" { - complain(); - false - } - else { - true - } -} - /// Construct a workspace and package-ID name based on the current directory. /// This gets used when rustpkg gets invoked without a package-ID argument. -pub fn cwd_to_workspace() -> (Path, PkgId) { +pub fn cwd_to_workspace() -> Option<(Path, PkgId)> { let cwd = os::getcwd(); - let ws = cwd.pop().pop(); - let cwd_ = cwd.clone(); - let pkgid = cwd_.components.last().to_str(); - (ws, PkgId::new(pkgid)) + for path in rust_path().move_iter() { + let srcpath = path.push("src"); + if srcpath.is_ancestor_of(&cwd) { + // I'd love to use srcpath.get_relative_to(cwd) but it behaves wrong + // I'd say broken, but it has tests enforcing the wrong behavior. + // instead, just hack up the components vec + let mut pkgid = cwd; + pkgid.is_absolute = false; + let comps = util::replace(&mut pkgid.components, ~[]); + pkgid.components = comps.move_iter().skip(srcpath.components.len()).collect(); + return Some((path, PkgId::new(pkgid.components.connect("/")))) + } + } + None }