Skip to content

Commit 82765a0

Browse files
committed
rustpkg: Implement clean; replace boolean result flags with fail!()
1. Implemented the `clean` command 2. The methods implementing rustpkg commands all returned `bool`. Since most of not all of the error situations seem unrecoverable, I changed the methods to return unit (and also stubbed out several more methods that were assuming a package script existed, to be re-implemented in the future)
1 parent e6c3c37 commit 82765a0

File tree

2 files changed

+67
-159
lines changed

2 files changed

+67
-159
lines changed

src/librustpkg/rustpkg.rc

Lines changed: 61 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ use core::hashmap::HashMap;
3333
use core::io::WriterUtil;
3434
use rustc::driver::{driver, session};
3535
use rustc::metadata::filesearch;
36-
use std::net::url;
3736
use std::{getopts};
3837
use syntax::{ast, diagnostic};
3938
use util::*;
@@ -244,16 +243,21 @@ impl Ctx {
244243
src.build(&dst_dir, cfgs);
245244
}
246245
~"clean" => {
247-
self.clean();
246+
if args.len() < 1 {
247+
return usage::build();
248+
}
249+
// The package id is presumed to be the first command-line
250+
// argument
251+
let pkgid = PkgId::new(args[0]);
252+
253+
self.clean(pkgid);
248254
}
249255
~"do" => {
250256
if args.len() < 2 {
251257
return usage::do_cmd();
252258
}
253259

254-
if !self.do_cmd(args[0], args[1]) {
255-
fail!(~"a command failed!");
256-
}
260+
self.do_cmd(args[0], args[1]);
257261
}
258262
~"info" => {
259263
self.info();
@@ -298,12 +302,11 @@ impl Ctx {
298302
}
299303
}
300304

301-
fn do_cmd(&self, cmd: ~str, pkgname: ~str) -> bool {
305+
fn do_cmd(&self, cmd: ~str, pkgname: ~str) {
302306
match cmd {
303307
~"build" | ~"test" => {
304308
util::error(~"that command cannot be manually called");
305-
306-
return false;
309+
fail!(~"do_cmd");
307310
}
308311
_ => {}
309312
}
@@ -319,16 +322,15 @@ impl Ctx {
319322
Some(script_path) => {
320323
let script = PkgScript::parse(script_path, pkgid);
321324
let (_, status) = script.run_custom(cmd); // Ignore cfgs?
322-
if status == 42 { // ???
325+
if status == 42 {
323326
util::error(~"no fns are listening for that cmd");
324-
return false;
327+
fail!(~"do_cmd");
325328
}
326-
status == 0
327329
}
328330
None => {
329331
util::error(fmt!("invoked `do`, but there is no package script in %s",
330332
cwd.to_str()));
331-
false
333+
fail!(~"do_cmd");
332334
}
333335
}
334336
}
@@ -341,128 +343,44 @@ impl Ctx {
341343
}
342344

343345
fn compile(&self, _crate: &Path, _dir: &Path, _flags: ~[~str],
344-
_cfgs: ~[~str], _opt: bool, _test: bool) -> bool {
346+
_cfgs: ~[~str], _opt: bool, _test: bool) {
345347
// What's the difference between build and compile?
346348
fail!(~"compile not yet implemented");
347349
}
348350

349-
fn clean(&self) -> bool {
350-
// stub
351-
fail!();
351+
fn clean(&self, id: PkgId) {
352+
// Could also support a custom build hook in the pkg
353+
// script for cleaning files rustpkg doesn't know about.
354+
// Do something reasonable for now
355+
356+
let dir = dest_dir(id);
357+
util::note(fmt!("Cleaning package %s (removing directory %s)",
358+
id.to_str(), dir.to_str()));
359+
if os::path_exists(&dir) {
360+
util::remove_dir_r(&dir);
361+
util::note(fmt!("Removed directory %s", dir.to_str()));
362+
}
363+
364+
util::note(fmt!("Cleaned package %s", id.to_str()));
352365
}
353366

354367
fn info(&self) {
355368
// stub
356-
fail!();
369+
fail!(~"info not yet implemented");
357370
}
358371

359-
fn install(&self, url: Option<~str>,
360-
target: Option<~str>, cache: bool) -> bool {
361-
let dir = match url {
362-
None => {
363-
util::note(~"installing from the cwd");
364-
os::getcwd()
365-
}
366-
Some(url) => {
367-
let hash = util::hash(if !target.is_none() {
368-
url + target.get()
369-
}
370-
else { url });
371-
372-
if self.dep_cache.contains_key(&hash) {
373-
util::warn(~"already installed dep this run");
374-
return true;
375-
}
376-
377-
self.dep_cache.insert(hash, true);
378-
379-
let dir = util::root().push(~"tmp").push(hash);
380-
381-
if cache && os::path_exists(&dir) {
382-
return true;
383-
}
384-
385-
if !self.fetch(&dir, url, target) {
386-
return false;
387-
}
388-
dir
389-
}
390-
};
391-
392-
let script = match self.build(&dir, false, true, false) {
393-
Some(script) => script,
394-
None => {
395-
return false;
396-
}
397-
};
398-
let work_dir = script.build_dir;
399-
let from_bin_dir = work_dir.push(~"bin");
400-
let from_lib_dir = work_dir.push(~"lib");
401-
let to_bin_dir = util::root().push(~"bin");
402-
let to_lib_dir = util::root().push(~"lib");
403-
let mut bins = ~[];
404-
let mut libs = ~[];
405-
406-
for os::walk_dir(&from_bin_dir) |bin| {
407-
let to = to_bin_dir.push_rel(&bin.file_path());
408-
409-
os::copy_file(bin, &to);
410-
bins.push(to.to_str());
411-
}
412-
413-
for os::walk_dir(&from_lib_dir) |lib| {
414-
let to = to_lib_dir.push_rel(&lib.file_path());
415-
416-
os::copy_file(lib, &to);
417-
libs.push(to.to_str());
418-
}
419-
420-
let package = Pkg {
421-
id: script.id,
422-
bins: bins,
423-
libs: libs
424-
};
425-
426-
util::note(fmt!("installed %s", script.id.to_str()));
427-
util::add_pkg(&package);
428-
429-
true
372+
fn install(&self, _url: Option<~str>,
373+
_target: Option<~str>, _cache: bool) {
374+
// stub
375+
fail!(~"install not yet implemented");
430376
}
431377

432-
fn fetch(&self, dir: &Path, url: ~str, target: Option<~str>) -> bool {
433-
let url = if str::find_str(url, "://").is_none() {
434-
~"http://" + url }
435-
else { url };
436-
let url = match url::from_str(url) {
437-
result::Ok(url) => url,
438-
result::Err(err) => {
439-
util::error(fmt!("failed parsing %s", err.to_lower()));
440-
441-
return false;
442-
}
443-
};
444-
let str = url.to_str();
445-
446-
match Path(url.path).filetype() {
447-
Some(ext) => {
448-
if ext == ~".git" {
449-
return self.fetch_git(dir, str, target);
450-
}
451-
}
452-
None => {}
453-
}
454-
455-
match url.scheme {
456-
~"git" => self.fetch_git(dir, str, target),
457-
~"http" | ~"ftp" | ~"file" => self.fetch_curl(dir, str),
458-
_ => {
459-
util::warn(~"unknown url scheme to fetch, using curl");
460-
self.fetch_curl(dir, str)
461-
}
462-
}
378+
fn fetch(&self, _dir: &Path, _url: ~str, _target: Option<~str>) {
379+
// stub
380+
fail!(~"fetch not yet implemented");
463381
}
464382

465-
fn fetch_curl(&self, dir: &Path, url: ~str) -> bool {
383+
fn fetch_curl(&self, dir: &Path, url: ~str) {
466384
util::note(fmt!("fetching from %s using curl", url));
467385

468386
let tar = dir.dir_path().push(&dir.file_path().to_str() + ~".tar");
@@ -472,7 +390,7 @@ impl Ctx {
472390
url]).status != 0 {
473391
util::error(~"fetching failed: downloading using curl failed");
474392

475-
return false;
393+
fail!();
476394
}
477395

478396
if run::program_output(~"tar", ~[~"-x", ~"--strip-components=1",
@@ -481,13 +399,11 @@ impl Ctx {
481399
util::error(~"fetching failed: extracting using tar failed" +
482400
~"(is it a valid tar archive?)");
483401

484-
return false;
402+
fail!();
485403
}
486-
487-
true
488404
}
489405

490-
fn fetch_git(&self, dir: &Path, url: ~str, target: Option<~str>) -> bool {
406+
fn fetch_git(&self, dir: &Path, url: ~str, target: Option<~str>) {
491407
util::note(fmt!("fetching from %s using git", url));
492408

493409
// Git can't clone into a non-empty directory
@@ -496,8 +412,7 @@ impl Ctx {
496412
if run::program_output(~"git", ~[~"clone", url,
497413
dir.to_str()]).status != 0 {
498414
util::error(~"fetching failed: can't clone repository");
499-
500-
return false;
415+
fail!();
501416
}
502417

503418
if !target.is_none() {
@@ -511,21 +426,17 @@ impl Ctx {
511426

512427
if !success {
513428
util::error(~"fetching failed: can't checkout target");
514-
515-
return false;
429+
fail!();
516430
}
517431
}
518-
519-
true
520432
}
521433

522-
fn prefer(&self, id: ~str, vers: Option<~str>) -> bool {
434+
fn prefer(&self, id: ~str, vers: Option<~str>) {
523435
let package = match util::get_pkg(id, vers) {
524436
result::Ok(package) => package,
525437
result::Err(err) => {
526438
util::error(err);
527-
528-
return false;
439+
fail!(); // Condition?
529440
}
530441
};
531442
let name = package.id.path.to_str(); // ???
@@ -548,29 +459,18 @@ impl Ctx {
548459
}
549460

550461
util::note(fmt!("preferred %s v%s", name, package.id.version.to_str()));
551-
552-
true
553462
}
554463

555-
fn test(&self) -> bool {
556-
let script = match self.build(&os::getcwd(), false, false, true) {
557-
Some(script) => script,
558-
None => {
559-
return false;
560-
}
561-
};
562-
563-
// To do
564-
util::note(fmt!("Would test %s, but this is a dry run",
565-
script.id.to_str()));
566-
false
464+
fn test(&self) {
465+
// stub
466+
fail!(~"test not yet implemented");
567467
}
568468

569-
fn uninstall(&self, _id: ~str, _vers: Option<~str>) -> bool {
469+
fn uninstall(&self, _id: ~str, _vers: Option<~str>) {
570470
fail!(~"uninstall not yet implemented");
571471
}
572472

573-
fn unprefer(&self, _id: ~str, _vers: Option<~str>) -> bool {
473+
fn unprefer(&self, _id: ~str, _vers: Option<~str>) {
574474
fail!(~"unprefer not yet implemented");
575475
}
576476
}
@@ -839,14 +739,19 @@ impl PkgSrc {
839739
prefix, pth),
840740
Some(~"bench.rs") => push_crate(&mut self.benchs,
841741
prefix, pth),
842-
_ => {
843-
util::note(~"Couldn't infer any crates to build.\n\
844-
Try naming a crate `main.rs`, `lib.rs`, \
845-
`test.rs`, or `bench.rs`.");
846-
fail!(~"Failed to infer crates to build");
847-
}
742+
_ => ()
848743
}
849744
}
745+
746+
if self.libs.is_empty() && self.mains.is_empty()
747+
&& self.tests.is_empty() && self.benchs.is_empty() {
748+
749+
util::note(~"Couldn't infer any crates to build.\n\
750+
Try naming a crate `main.rs`, `lib.rs`, \
751+
`test.rs`, or `bench.rs`.");
752+
fail!(~"Failed to infer crates to build");
753+
}
754+
850755
debug!("found %u libs, %u mains, %u tests, %u benchs",
851756
self.libs.len(),
852757
self.mains.len(),

src/librustpkg/testsuite/pass/hello-world/main.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010

1111
/*
1212
The test runner should check that, after `rustpkg build hello-world`:
13-
* testsuite/hello-world/build/ exists
14-
* testsuite/hello-world/build/ contains an executable named hello-world
15-
* testsuite/hello-world/build/ does not contain a library
13+
* testsuite/pass/hello-world/build/ exists
14+
* testsuite/pass/hello-world/build/ contains an executable named hello-world
15+
* testsuite/pass/hello-world/build/ does not contain a library
16+
17+
It should also check that after `rustpkg clean hello-world`:
18+
* testsuite/pass/hello-world/build is empty
1619
*/
1720

1821
use core::io;

0 commit comments

Comments
 (0)