Skip to content

more useful rustpkg init #10651

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
58 changes: 49 additions & 9 deletions src/librustpkg/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use std::io::process;
use std::hashmap::HashSet;
use std::io;
use std::io::fs;
use std::io::File;
pub use std::path::Path;

use extra::workcache;
Expand Down Expand Up @@ -79,6 +80,11 @@ mod workspace;

pub mod usage;

enum PkgType {
Bin,
Lib,
}

/// A PkgScript represents user-supplied custom logic for
/// special build hooks. This only exists for packages with
/// an explicit package script.
Expand Down Expand Up @@ -217,7 +223,7 @@ pub trait CtxMethods {
fn test(&self, id: &PkgId, workspace: &Path);
fn uninstall(&self, _id: &str, _vers: Option<~str>);
fn unprefer(&self, _id: &str, _vers: Option<~str>);
fn init(&self);
fn init(&self, name: &str, pkg_type: PkgType);
}

impl CtxMethods for BuildContext {
Expand Down Expand Up @@ -382,10 +388,19 @@ impl CtxMethods for BuildContext {
}
}
"init" => {
if args.len() != 0 {
return usage::init();
if args.len() != 2 {
usage::init();
} else {
self.init();
let pkg_type = match args[0] {
~"bin" => Some(Bin),
~"lib" => Some(Lib),
_ => None,
};
if pkg_type.is_none() {
usage::init();
} else {
self.init(args[1].as_slice(), pkg_type.unwrap());
}
}
}
"uninstall" => {
Expand Down Expand Up @@ -710,11 +725,36 @@ impl CtxMethods for BuildContext {
}
}

fn init(&self) {
fs::mkdir_recursive(&Path::new("src"), io::UserRWX);
fs::mkdir_recursive(&Path::new("bin"), io::UserRWX);
fs::mkdir_recursive(&Path::new("lib"), io::UserRWX);
fs::mkdir_recursive(&Path::new("build"), io::UserRWX);
fn init(&self, name: &str, pkg_type: PkgType) {
let path = Path::new(name);
let path_exists = path.exists();
if path_exists && path.is_file() {
error(format!("rustpkg fails to initialize {0:s}: {0:s} appears to be a file.", name));
} else if path_exists && fs::walk_dir(&path).len() != 0 {
error(format!("rustpkg fails to initialize {0:s}: {0:s} is non-empty.", name));
} else {
let src_dir = format!("{0:s}/src/{0:s}", name);
fs::mkdir_recursive(&Path::new(src_dir.as_slice()), io::UserRWX);
fs::mkdir_recursive(&Path::new(format!("{:s}/bin", name)), io::UserRWX);
fs::mkdir_recursive(&Path::new(format!("{:s}/lib", name)), io::UserRWX);
fs::mkdir_recursive(&Path::new(format!("{:s}/build", name)), io::UserRWX);
match pkg_type {
Bin => match File::create(&Path::new(format!("{:s}/main.rs", src_dir.as_slice()))) {
Some(mut file) => file.write(bytes!("fn main() {
println(\"I don't do much. Yet.\");
}
")),
None => warn("rustpkg fails to create main.rs"),
},
Lib => match File::create(&Path::new(format!("{:s}/lib.rs", src_dir.as_slice()))) {
Some(mut file) => file.write(bytes!("pub fn get_answer() -> int {
42
}
")),
None => warn("rustpkg fails to create lib.rs"),
},
}
}
}

fn uninstall(&self, _id: &str, _vers: Option<~str>) {
Expand Down
6 changes: 3 additions & 3 deletions src/librustpkg/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ Options:
}

pub fn init() {
println("rustpkg init
println("rustpkg init <bin|lib> package-name

This will turn the current working directory into a workspace. The first
command you run when starting off a new project.
This initializes a workspace with the same name as package-name in the current
directory. The first command you run when starting off a new project.
");
}