Skip to content

Commit

Permalink
Clean up cargo-compile
Browse files Browse the repository at this point in the history
  • Loading branch information
wycats committed May 7, 2014
1 parent adbab76 commit da0ec9a
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
target

.cargo
4 changes: 2 additions & 2 deletions src/bin/cargo-compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ fn flags<T: FlagConfig + Decodable<FlagDecoder, HammerError>>() -> CLIResult<T>
}

fn execute() -> CLIResult<()> {
compile(try!(flags::<Options>()).manifest_path.as_slice()).to_result(|_|
CLIError::new("Compilation failed", None, 1))
compile(try!(flags::<Options>()).manifest_path.as_slice()).to_result(|err|
CLIError::new(format!("Compilation failed: {}", err), None, 1))
}

fn main() {
Expand Down
22 changes: 16 additions & 6 deletions src/cargo/core/dependency.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
use core;
use core::NameVer;

#[deriving(Eq,Clone,Show,Encodable,Decodable)]
pub struct Dependency {
name: core::NameVer
name: NameVer
}

impl Dependency {
pub fn new(name: &str) -> Dependency {
Dependency { name: core::NameVer::new(name.to_owned(), "1.0.0") }
Dependency { name: NameVer::new(name.to_owned(), "1.0.0") }
}

pub fn with_namever(name: &core::NameVer) -> Dependency {
pub fn with_namever(name: &NameVer) -> Dependency {
Dependency { name: name.clone() }
}

pub fn with_name_and_version(name: &str, version: &str) -> Dependency {
Dependency { name: core::NameVer::new(name, version) }
Dependency { name: NameVer::new(name, version) }
}

pub fn get_namever<'a>(&'a self) -> &'a core::NameVer {
pub fn get_namever<'a>(&'a self) -> &'a NameVer {
&self.name
}

pub fn get_name<'a>(&'a self) -> &'a str {
self.name.get_name()
}
}

pub trait DependencyNameVers {
fn namevers(&self) -> Vec<NameVer>;
}

impl DependencyNameVers for Vec<Dependency> {
fn namevers(&self) -> Vec<NameVer> {
self.iter().map(|dep| dep.get_namever().clone()).collect()
}
}
16 changes: 15 additions & 1 deletion src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use collections::HashMap;
use std::fmt;
use std::fmt::{Show,Formatter};
use core::{
Dependency,
NameVer,
Expand All @@ -16,7 +18,13 @@ pub struct Manifest {
target_dir: Path,
}

#[deriving(Clone,Eq)]
impl Show for Manifest {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f.buf, "Manifest({}, authors={}, targets={}, target_dir={})", self.summary, self.authors, self.targets, self.target_dir.display())
}
}

#[deriving(Show,Clone,Eq)]
pub enum TargetKind {
LibTarget,
BinTarget
Expand All @@ -29,6 +37,12 @@ pub struct Target {
path: Path
}

impl Show for Target {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f.buf, "{}(name={}, path={})", self.kind, self.name, self.path.display())
}
}

impl Manifest {
pub fn new(summary: &Summary, targets: &[Target], target_dir: &Path) -> Manifest {
Manifest {
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ pub mod package;
pub mod dependency;
pub mod manifest;
pub mod resolver;
mod summary;
pub mod summary;
mod registry;
10 changes: 9 additions & 1 deletion src/cargo/core/namever.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use semver;
use std::fmt;
use std::fmt::{Show,Formatter};
use serialize::{
Encodable,
Encoder,
Decodable,
Decoder
};

#[deriving(Clone,Eq,Show,Ord)]
#[deriving(Clone,Eq,Ord)]
pub struct NameVer {
name: ~str,
version: semver::Version
Expand All @@ -26,6 +28,12 @@ impl NameVer {
}
}

impl Show for NameVer {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f.buf, "{} v{}", self.name, self.version)
}
}

impl<E, D: Decoder<E>> Decodable<D,E> for NameVer {
fn decode(d: &mut D) -> Result<NameVer, E> {
let vector: Vec<~str> = try!(Decodable::decode(d));
Expand Down
12 changes: 12 additions & 0 deletions src/cargo/core/package.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::slice;
use std::fmt;
use std::fmt::{Show,Formatter};
use std::path::Path;
use core::{
Dependency,
Expand All @@ -25,6 +27,10 @@ impl Package {
}
}

pub fn to_dependency(&self) -> Dependency {
Dependency::with_namever(self.manifest.get_summary().get_name_ver())
}

pub fn get_manifest<'a>(&'a self) -> &'a Manifest {
&self.manifest
}
Expand Down Expand Up @@ -58,6 +64,12 @@ impl Package {
}
}

impl Show for Package {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f.buf, "{}", self.get_summary().get_name_ver())
}
}

pub struct PackageSet {
packages: ~[Package]
}
Expand Down
15 changes: 15 additions & 0 deletions src/cargo/core/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,18 @@ impl Summary {
self.dependencies.as_slice()
}
}

pub trait SummaryVec {
fn names(&self) -> Vec<~str>;
fn deps(&self) -> Vec<Dependency>;
}

impl SummaryVec for Vec<Summary> {
fn names(&self) -> Vec<~str> {
self.iter().map(|summary| summary.name_ver.get_name().to_owned()).collect()
}

fn deps(&self) -> Vec<Dependency> {
self.iter().map(|summary| Dependency::with_namever(summary.get_name_ver())).collect()
}
}
32 changes: 15 additions & 17 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@
* b. Compile each dependency in order, passing in the -L's pointing at each previously compiled dependency
*/

use std::vec::Vec;
use std::os;
use util::config;
use util::config::{all_configs,ConfigValue};
use core::{PackageSet,Source,Dependency,NameVer};
use core::{PackageSet,Source};
use core::resolver::resolve;
use sources::path::PathSource;
use ops::cargo_rustc;
use ops::cargo_read_manifest::read_manifest;
use core::errors::{CargoError,CLIError,CLIResult,ToResult};
use core::summary::SummaryVec;

pub fn compile(manifest_path: &str) -> CLIResult<()> {
// TODO: Fix
let root_dep = try!(read_manifest(manifest_path)).to_dependency();

let configs = try!(all_configs(os::getcwd()).to_result(|err: CargoError|
CLIError::new("Could not load configurations", Some(err.to_str()), 1)));

Expand All @@ -37,25 +39,21 @@ pub fn compile(manifest_path: &str) -> CLIResult<()> {
};

let source = PathSource::new(paths);
let summaries = try!(source.list().to_result(|err| CLIError::new(format!("Unable to list packages from {}", source), Some(err.to_str()), 1)));
let names: Vec<NameVer> = summaries.iter().map(|s| s.get_name_ver().clone()).collect();

// This does not need to happen
// try!(source.download(names.as_slice()).to_result(|err| CLIError::new(format!("Unable to download packages from {}", source), Some(err.to_str()), 1)));
let summaries = try!(source.list().to_result(|err|
CLIError::new(format!("Unable to list packages from {}", source), Some(err.to_str()), 1)));

let deps: Vec<Dependency> = summaries.iter().map(|summary| {
Dependency::with_namever(summary.get_name_ver())
}).collect();
let resolved = try!(resolve([root_dep], &summaries).to_result(|err: CargoError|
CLIError::new("Unable to resolve dependencies", Some(err.to_str()), 1)));

let packages = try!(source.get(names.as_slice()).to_result(|err|
CLIError::new(format!("Unable to get packages from {} for {}", source, names), Some(err.to_str()), 1)));
try!(source.download(resolved.as_slice()).to_result(|err|
CLIError::new(format!("Unable to download packages from {}", source), Some(err.to_str()), 1)));

let registry = PackageSet::new(packages.as_slice());
let packages = try!(source.get(resolved.as_slice()).to_result(|err|
CLIError::new(format!("Unable to get packages from {} for {}", source, summaries.names()), Some(err.to_str()), 1)));

let resolved = try!(resolve(deps.as_slice(), &registry).to_result(|err: CargoError|
CLIError::new("Unable to resolve dependencies", Some(err.to_str()), 1)));
let package_set = PackageSet::new(packages.as_slice());

try!(cargo_rustc::compile(&registry));
try!(cargo_rustc::compile(&package_set));

Ok(())
}
3 changes: 2 additions & 1 deletion src/cargo/ops/cargo_rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub fn compile(pkgs: &core::PackageSet) -> CLIResult<()> {
};

for pkg in sorted.iter() {
println!("Compiling {}", pkg);
try!(compile_pkg(pkg, pkgs));
}

Expand Down Expand Up @@ -52,7 +53,7 @@ fn rustc(root: &Path, target: &core::Target, dest: &Path, deps: &[core::Package]
.args(args.as_slice())
.exec()
.to_result(|err|
CLIError::new(format!("Couldn't execute rustc {}", args.connect(" ")), Some(err.to_str()), 1)));
CLIError::new(format!("Couldn't execute `rustc {}` in `{}`", args.connect(" "), root.display()), Some(err.to_str()), 1)));

Ok(())
}
Expand Down

0 comments on commit da0ec9a

Please sign in to comment.