This repository has been archived by the owner on Jan 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Cli ergonomics #9
Closed
Closed
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
64c1bc8
Travis updates
gibix 2ec7760
update copyright
gibix fac490a
Small code shake-up
gibix e7a61f8
Bump cargo version to 1.27
gibix d94ab48
Use structopt for argument parsing
gibix 58c7335
Better error handling with failure::Error
gibix 3596224
Handle --ebuild-path flag
gibix eb25067
Add basic logging with env_logger
gibix 0ef37ce
Drop cargo for cargo_metadata
gibix 96d7e97
Add first integration test
gibix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
* Copyright 2016-2018 Doug Goldstein <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
* http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
* <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
* option. This file may not be copied, modified, or distributed | ||
* except according to those terms. | ||
*/ | ||
|
||
extern crate cargo; | ||
#[macro_use] | ||
extern crate serde_derive; | ||
extern crate time; | ||
|
||
use cargo::{Config, CliResult}; | ||
use cargo::core::{Package, PackageSet, Resolve, Workspace}; | ||
use cargo::core::registry::PackageRegistry; | ||
use cargo::core::resolver::Method; | ||
use cargo::ops; | ||
use cargo::util::{important_paths, CargoResult, CargoResultExt}; | ||
use std::fs::OpenOptions; | ||
use std::io::Write; | ||
use std::path::PathBuf; | ||
|
||
#[derive(Deserialize)] | ||
pub struct Options { | ||
flag_verbose: u32, | ||
flag_quiet: Option<bool>, | ||
} | ||
|
||
/// Finds the root Cargo.toml of the workspace | ||
fn workspace(config: &Config, manifest_path: Option<String>) -> CargoResult<Workspace> { | ||
let root = important_paths::find_root_manifest_for_wd(manifest_path, config.cwd())?; | ||
Workspace::new(&root, config) | ||
} | ||
|
||
/// Generates a package registry by using the Cargo.lock or creating one as necessary | ||
fn registry<'a>(config: &'a Config, package: &Package) -> CargoResult<PackageRegistry<'a>> { | ||
let mut registry = PackageRegistry::new(config)?; | ||
registry | ||
.add_sources(&[package.package_id().source_id().clone()])?; | ||
Ok(registry) | ||
} | ||
|
||
/// Resolve the packages necessary for the workspace | ||
fn resolve<'a>(registry: &mut PackageRegistry, | ||
workspace: &'a Workspace) | ||
-> CargoResult<(PackageSet<'a>, Resolve)> { | ||
// resolve our dependencies | ||
let (packages, resolve) = ops::resolve_ws(workspace)?; | ||
|
||
// resolve with all features set so we ensure we get all of the depends downloaded | ||
let resolve = ops::resolve_with_previous(registry, | ||
workspace, | ||
/* resolve it all */ | ||
Method::Everything, | ||
/* previous */ | ||
Some(&resolve), | ||
/* don't avoid any */ | ||
None, | ||
/* specs */ | ||
&[])?; | ||
|
||
Ok((packages, resolve)) | ||
} | ||
|
||
pub fn real_main(options: Options, config: &Config) -> CliResult { | ||
config | ||
.configure(options.flag_verbose, | ||
options.flag_quiet, | ||
/* color */ | ||
&None, | ||
/* frozen */ | ||
false, | ||
/* locked */ | ||
false)?; | ||
|
||
// Load the workspace and current package | ||
let workspace = workspace(config, None)?; | ||
let package = workspace.current()?; | ||
|
||
// Resolve all dependencies (generate or use Cargo.lock as necessary) | ||
let mut registry = registry(config, &package)?; | ||
let resolve = resolve(&mut registry, &workspace)?; | ||
|
||
// build the crates the package needs | ||
let mut crates = resolve | ||
.1 | ||
.iter() | ||
.map(|pkg| format!("{}-{}\n", pkg.name(), pkg.version())) | ||
.collect::<Vec<String>>(); | ||
|
||
// sort the crates | ||
crates.sort(); | ||
|
||
// root package metadata | ||
let metadata = package.manifest().metadata(); | ||
|
||
// package description | ||
let desc = metadata | ||
.description | ||
.as_ref() | ||
.cloned() | ||
.unwrap_or_else(|| String::from(package.name())); | ||
|
||
// package homepage | ||
let homepage = | ||
metadata.homepage.as_ref().cloned().unwrap_or(metadata | ||
.repository | ||
.as_ref() | ||
.cloned() | ||
.unwrap_or_else(|| String::from(""))); | ||
|
||
let license = metadata | ||
.license | ||
.as_ref() | ||
.cloned() | ||
.unwrap_or_else(|| String::from("unknown license")); | ||
|
||
// build up the ebuild path | ||
let ebuild_path = PathBuf::from(format!("{}-{}.ebuild", package.name(), package.version())); | ||
|
||
// Open the file where we'll write the ebuild | ||
let mut file = try!(OpenOptions::new() | ||
.write(true) | ||
.create(true) | ||
.truncate(true) | ||
.open(&ebuild_path) | ||
.chain_err(|| "failed to create ebuild")); | ||
|
||
// write the contents out | ||
try!(write!(file, | ||
include_str!("ebuild.template"), | ||
description = desc.trim(), | ||
homepage = homepage.trim(), | ||
license = license.trim(), | ||
crates = crates.join(""), | ||
cargo_ebuild_ver = env!("CARGO_PKG_VERSION"), | ||
this_year = 1900 + time::now().tm_year, | ||
).chain_err(|| "unable to write ebuild to disk")); | ||
|
||
println!("Wrote: {}", ebuild_path.display()); | ||
|
||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just wrapping a
main()
function into the library. Let's work out a real API and havelib.rs
expose that API and then switch the binary to using that. A better way to structure the changes would be commits that work in the following order: