Skip to content
This repository has been archived by the owner on Jan 8, 2022. It is now read-only.

Cli ergonomics #9

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
48 changes: 48 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ serde = "1"
serde_derive = "1"
time = "^0.1"
structopt = "0.2.8"
failure = "0.1.1"
human-panic = "1.0.0"
124 changes: 65 additions & 59 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ extern crate cargo;
extern crate time;
#[macro_use]
extern crate structopt;
extern crate failure;

use cargo::{Config, CliResult};
use cargo::core::{Package, PackageSet, Resolve, Workspace};
use cargo::core::registry::PackageRegistry;
use cargo::core::resolver::Method;
use cargo::core::{Package, PackageSet, Resolve, Workspace};
use cargo::ops;
use cargo::util::{important_paths, CargoResult};
use cargo::Config;
pub use failure::Error;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
use std::path::PathBuf; // re-exported to main

#[derive(Debug, StructOpt)]
pub enum Command {
Expand All @@ -34,15 +36,15 @@ pub enum Command {
}

/// Parse cli commands
pub fn run_cargo_ebuild(config: &mut cargo::Config, cmd: Option<Command>) -> CliResult {
pub fn run_cargo_ebuild(cmd: Option<Command>) -> Result<(), Error> {
// If no command is specified run build with default conf
let cmd = cmd.unwrap_or(Command::Build {
manifest_path: None,
});

// Here will be the match of the commands, now just example
match cmd {
Command::Build { manifest_path } => real_main(config, manifest_path),
Command::Build { manifest_path } => build(manifest_path),
}
}

Expand All @@ -55,51 +57,55 @@ fn workspace(config: &Config) -> CargoResult<Workspace> {
/// 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()])?;
registry.add_sources(&[package.package_id().source_id().clone()])?;
Ok(registry)
}

/// Resolve the packages necessary for the workspace
fn resolve<'a>(registry: &mut PackageRegistry<'a>,
workspace: &Workspace<'a>)
-> CargoResult<(PackageSet<'a>, Resolve)> {
fn resolve<'a>(
registry: &mut PackageRegistry<'a>,
workspace: &Workspace<'a>,
) -> 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 */
&[],
// register patches
true,
// warn
false
)?;
let resolve = ops::resolve_with_previous(
registry,
workspace,
/* resolve it all */
Method::Everything,
/* previous */
Some(&resolve),
/* don't avoid any */
None,
/* specs */
&[],
// register patches
true,
// warn
false,
)?;

Ok((packages, resolve))
}

pub fn real_main(config: &mut cargo::Config, _manifest_path: Option<String>) -> CliResult {
config
.configure(0,
Some(false),
/* color */
&None,
/* frozen */
false,
/* locked */
false,
// unstable flag
&Vec::new()
)?;
pub fn build(_manifest_path: Option<String>) -> Result<(), Error> {
// build the crate URIs
let config = &mut Config::default()?;

config.configure(
0,
Some(false),
/* color */
&None,
/* frozen */
false,
/* locked */
false,
// unstable flag
&Vec::new(),
)?;

// Load the workspace and current package
let workspace = workspace(config)?;
Expand Down Expand Up @@ -130,12 +136,13 @@ pub fn real_main(config: &mut cargo::Config, _manifest_path: Option<String>) ->
.unwrap_or_else(|| package.name().to_string());

// package homepage
let homepage =
metadata.homepage.as_ref().cloned().unwrap_or(metadata
.repository
.as_ref()
.cloned()
.unwrap_or_else(|| String::from("")));
let homepage = metadata.homepage.as_ref().cloned().unwrap_or(
metadata
.repository
.as_ref()
.cloned()
.unwrap_or_else(|| String::from("")),
);

let license = metadata
.license
Expand All @@ -148,25 +155,24 @@ pub fn real_main(config: &mut cargo::Config, _manifest_path: Option<String>) ->

// Open the file where we'll write the ebuild
let mut file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(&ebuild_path)
.expect("failed to create ebuild");
.write(true)
.create(true)
.truncate(true)
.open(&ebuild_path)?;

// write the contents out
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)
.expect("unable to write ebuild to disk");
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
)?;

println!("Wrote: {}", ebuild_path.display());


Ok(())
}
17 changes: 8 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
* except according to those terms.
*/

extern crate cargo_ebuild;
extern crate cargo;
#[macro_use]
extern crate human_panic;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if someone inputted the wrong arguments we're not going to show them the following:

Well, this is embarrassing.

cargo-ebuild had a problem and crashed. To help us diagnose the problem you can send us a crash report.

We have generated a report file at "/var/folders/zw/bpfvmq390lv2c6gn_6byyv0w0000gn/T/report-8351cad6-d2b5-4fe8-accd-1fcbf4538792.toml". Submit an issue or email with the subject of "human-panic Crash Report" and include the report as an attachment.

- Homepage: https://github.com/cardoe/cargo-ebuild
- Authors: Doug Goldstein <[email protected]>

We take privacy seriously, and do not perform any automated error collection. In order to improve the software, we rely on people to submit reports.

Thank you kindly!

This is the wrong crate for the job. Printing out a reasonable error message worked previously and this isn't reasonable.

#[macro_use]
extern crate structopt;
extern crate cargo_ebuild;

use cargo::Config;
use cargo_ebuild::*;
use structopt::StructOpt;

Expand All @@ -31,11 +31,10 @@ pub struct Cli {
pub verbosity: u8,
}

fn main() {
fn main() -> Result<(), Error> {
let args = Cli::from_args();
let mut config = Config::default().unwrap();
let result = run_cargo_ebuild(&mut config, args.cmd);
if let Err(e) = result {
cargo::exit_with_error(e, &mut *config.shell());
}

setup_panic!();

run_cargo_ebuild(args.cmd)
}