Skip to content

Handle rustpkg build, etc. when given no args properly #8697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions src/librustpkg/rustpkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions src/librustpkg/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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"),
Expand All @@ -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));

Expand Down
35 changes: 17 additions & 18 deletions src/librustpkg/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}