Skip to content
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

[WIP]: Add cargo-add (from cargo-edit) to cargo proper #5611

Closed
wants to merge 12 commits into from
Closed
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
Prev Previous commit
Next Next commit
started to move manifest from cargo-edit to cargo
ibaryshnikov committed Sep 11, 2018

Verified

This commit was signed with the committer’s verified signature.
ygrishajev Iaroslav Gryshaiev
commit 04ec76053f34bd855ecac86333464ddbd36afa5d
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -53,6 +53,7 @@ tar = { version = "0.4.15", default-features = false }
tempfile = "3.0"
termcolor = "1.0"
toml = "0.4.2"
toml_edit = "0.1.1"
url = "1.1"
clap = "2.31.2"
unicode-width = "0.1.5"
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/add.rs
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ pub fn cli() -> App {
.about("Add a new dependency")
.arg(Arg::with_name("crate").empty_values(false))
.arg(
opt("version", "Specify a version to install from crates.io")
opt("version", "Specify a version to add from crates.io")
.alias("vers")
.value_name("VERSION"),
)
1 change: 1 addition & 0 deletions src/cargo/lib.rs
Original file line number Diff line number Diff line change
@@ -58,6 +58,7 @@ extern crate tar;
extern crate tempfile;
extern crate termcolor;
extern crate toml;
extern crate toml_edit;
extern crate unicode_width;
extern crate url;

11 changes: 5 additions & 6 deletions src/cargo/ops/cargo_add.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@

use core::{PackageId, SourceId, Workspace};
use core::{SourceId, Workspace};

use util::errors::{CargoResult};

pub fn add(
ws: &Workspace,
krate: &str,
source_id: &SourceId,
vers: Option<&str>,
_ws: &Workspace,
_krate: &str,
_source_id: &SourceId,
_vers: Option<&str>,
) -> CargoResult<()> {

Ok(())
}
106 changes: 106 additions & 0 deletions src/cargo/util/toml/dependency.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use toml_edit;

#[derive(Debug, Hash, PartialEq, Eq, Clone)]
enum DependencySource {
Version(String),
Git(String),
Path(String),
}

/// A dependency handled by Cargo
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
pub struct Dependency {
/// The name of the dependency (as it is set in its `Cargo.toml` and known to crates.io)
pub name: String,
optional: bool,
source: DependencySource,
}

impl Default for Dependency {
fn default() -> Dependency {
Dependency {
name: "".into(),
optional: false,
source: DependencySource::Version("0.1.0".into()),
}
}
}

impl Dependency {
/// Create a new dependency with a name
pub fn new(name: &str) -> Dependency {
Dependency {
name: name.into(),
..Dependency::default()
}
}

/// Set dependency to a given version
pub fn set_version(mut self, version: &str) -> Dependency {
self.source = DependencySource::Version(version.into());
self
}

/// Set dependency to a given repository
pub fn set_git(mut self, repo: &str) -> Dependency {
self.source = DependencySource::Git(repo.into());
self
}

/// Set dependency to a given path
pub fn set_path(mut self, path: &str) -> Dependency {
self.source = DependencySource::Path(path.into());
self
}

/// Set whether the dependency is optional
pub fn set_optional(mut self, opt: bool) -> Dependency {
self.optional = opt;
self
}

/// Get version of dependency
pub fn version(&self) -> Option<&str> {
if let DependencySource::Version(ref version) = self.source {
Some(version)
} else {
None
}
}

/// Convert dependency to TOML
///
/// Returns a tuple with the dependency's name and either the version as a `String`
/// or the path/git repository as an `InlineTable`.
/// (If the dependency is set as `optional`, an `InlineTable` is returned in any case.)
pub fn to_toml(&self) -> (String, toml_edit::Item) {
let data: toml_edit::Item = match (self.optional, self.source.clone()) {
// Extra short when version flag only
(false, DependencySource::Version(v)) => toml_edit::value(v),
// Other cases are represented as an inline table
(optional, source) => {
let mut data = toml_edit::InlineTable::default();

match source {
DependencySource::Version(v) => {
data.get_or_insert("version", v);
}
DependencySource::Git(v) => {
data.get_or_insert("git", v);
}
DependencySource::Path(v) => {
data.get_or_insert("path", v);
}
}
if self.optional {
data.get_or_insert("optional", optional);
}

data.fmt();
toml_edit::value(toml_edit::Value::InlineTable(data))
}
};

(self.name.clone(), data)
}
}
Loading