Skip to content

Commit

Permalink
add --dev flag to add and rm
Browse files Browse the repository at this point in the history
add info in docs

fix docs

commit-id:2ccf4af8
  • Loading branch information
tomek0123456789 committed Dec 1, 2023
1 parent 7fdff63 commit 2d1611e
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 14 deletions.
41 changes: 41 additions & 0 deletions scarb/src/bin/scarb/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use url::Url;
use scarb::compiler::Profile;
use scarb::core::PackageName;
use scarb::manifest_editor::DepId;
use scarb::manifest_editor::SectionArgs;
use scarb::version;
use scarb_ui::args::PackagesFilter;
use scarb_ui::OutputFormat;
Expand Down Expand Up @@ -277,6 +278,10 @@ pub struct AddArgs {
/// _Source_ section.
#[command(flatten, next_help_heading = "Source")]
pub source: AddSourceArgs,

/// _Section_ section.
#[command(flatten, next_help_heading = "Section")]
pub section: AddSectionArgs,
}

/// _Source_ section of [`AddArgs`].
Expand All @@ -297,6 +302,24 @@ pub struct AddSourceArgs {
pub git_ref: GitRefGroup,
}

/// _Section_ section of [`AddArgs`].
#[derive(Parser, Clone, Debug)]
pub struct AddSectionArgs {
/// Add as development dependency.
///
/// Dev-dependencies are only used when compiling tests.
///
/// These dependencies are not propagated to other packages which depend on this package.
#[arg(long)]
pub dev: bool,
}

impl SectionArgs for AddSectionArgs {
fn dev(&self) -> bool {
self.dev
}
}

/// Arguments accepted by the `remove` command.
#[derive(Parser, Clone, Debug)]
pub struct RemoveArgs {
Expand All @@ -310,6 +333,24 @@ pub struct RemoveArgs {

#[command(flatten)]
pub packages_filter: PackagesFilter,

/// _Section_ section.
#[command(flatten, next_help_heading = "Section")]
pub section: RemoveSectionArgs,
}

/// _Section_ section of [`RemoveArgs`].
#[derive(Parser, Clone, Debug)]
pub struct RemoveSectionArgs {
/// Remove as development dependency.
#[arg(long)]
pub dev: bool,
}

impl SectionArgs for RemoveSectionArgs {
fn dev(&self) -> bool {
self.dev
}
}

/// Arguments accepted by the `test` command.
Expand Down
15 changes: 11 additions & 4 deletions scarb/src/bin/scarb/commands/add.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use anyhow::Result;

use scarb::core::Config;
use scarb::manifest_editor::{AddDependency, DepId, EditManifestOptions, Op};
use scarb::manifest_editor::{AddDependency, DepId, DepType, EditManifestOptions, Op};
use scarb::{manifest_editor, ops};

use crate::args::{AddArgs, AddSourceArgs};
use crate::args::{AddArgs, AddSectionArgs, AddSourceArgs};

#[tracing::instrument(skip_all, level = "info")]
pub fn run(args: AddArgs, config: &mut Config) -> Result<()> {
Expand All @@ -14,7 +14,7 @@ pub fn run(args: AddArgs, config: &mut Config) -> Result<()> {

manifest_editor::edit(
package.manifest_path(),
build_ops(args.packages, args.source),
build_ops(args.packages, args.source, args.section),
EditManifestOptions {
config,
dry_run: args.dry_run,
Expand All @@ -34,14 +34,21 @@ pub fn run(args: AddArgs, config: &mut Config) -> Result<()> {
Ok(())
}

fn build_ops(packages: Vec<DepId>, source: AddSourceArgs) -> Vec<Box<dyn Op>> {
fn build_ops(
packages: Vec<DepId>,
source: AddSourceArgs,
section: AddSectionArgs,
) -> Vec<Box<dyn Op>> {
let dep_type = DepType::from_section(&section);

let template = AddDependency {
dep: DepId::unspecified(),
path: source.path,
git: source.git,
branch: source.git_ref.branch,
tag: source.git_ref.tag,
rev: source.git_ref.rev,
dep_type,
};

if packages.is_empty() {
Expand Down
15 changes: 11 additions & 4 deletions scarb/src/bin/scarb/commands/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use scarb::core::{Config, PackageName};
use scarb::manifest_editor::{EditManifestOptions, Op, RemoveDependency};
use scarb::{manifest_editor, ops};

use crate::args::RemoveArgs;
use crate::args::{RemoveArgs, RemoveSectionArgs};
use scarb::manifest_editor::DepType;

#[tracing::instrument(skip_all, level = "info")]
pub fn run(args: RemoveArgs, config: &mut Config) -> Result<()> {
Expand All @@ -14,7 +15,7 @@ pub fn run(args: RemoveArgs, config: &mut Config) -> Result<()> {

manifest_editor::edit(
package.manifest_path(),
build_ops(args.packages),
build_ops(args.packages, args.section),
EditManifestOptions {
config,
dry_run: args.dry_run,
Expand All @@ -34,9 +35,15 @@ pub fn run(args: RemoveArgs, config: &mut Config) -> Result<()> {
Ok(())
}

fn build_ops(packages: Vec<PackageName>) -> Vec<Box<dyn Op>> {
fn build_ops(packages: Vec<PackageName>, section: RemoveSectionArgs) -> Vec<Box<dyn Op>> {
let dep_type = DepType::from_section(&section);
packages
.into_iter()
.map(|dep| -> Box<dyn Op> { Box::new(RemoveDependency { dep }) })
.map(|dep| -> Box<dyn Op> {
Box::new(RemoveDependency {
dep,
dep_type: dep_type.clone(),
})
})
.collect()
}
5 changes: 3 additions & 2 deletions scarb/src/manifest_editor/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::internal::fsx;
use crate::sources::canonical_url::CanonicalUrl;

use super::tomlx::get_table_mut;
use super::{DepId, Op, OpCtx};
use super::{DepId, DepType, Op, OpCtx};

#[derive(Clone, Debug, Default)]
pub struct AddDependency {
Expand All @@ -20,6 +20,7 @@ pub struct AddDependency {
pub branch: Option<String>,
pub tag: Option<String>,
pub rev: Option<String>,
pub dep_type: DepType,
}

struct Dep {
Expand Down Expand Up @@ -49,7 +50,7 @@ struct GitSource {
impl Op for AddDependency {
#[tracing::instrument(level = "trace", skip(doc, ctx))]
fn apply_to(self: Box<Self>, doc: &mut Document, ctx: OpCtx<'_>) -> Result<()> {
let tab = get_table_mut(doc, &["dependencies"])?;
let tab = get_table_mut(doc, &[self.dep_type.toml_section_str()])?;

let dep = Dep::resolve(*self, ctx)?;

Expand Down
28 changes: 28 additions & 0 deletions scarb/src/manifest_editor/dep_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
pub trait SectionArgs {
fn dev(&self) -> bool;
}

#[derive(Clone, Debug, Default)]
pub enum DepType {
#[default]
Normal,
Dev,
}

impl DepType {
pub fn toml_section_str(&self) -> &str {
match self {
DepType::Normal => "dependencies",
DepType::Dev => "dev-dependencies"
}
}
}
impl DepType {
pub fn from_section(section_args: &impl SectionArgs) -> DepType {
if section_args.dev() {
DepType::Dev
} else {
DepType::Normal
}
}
}
2 changes: 2 additions & 0 deletions scarb/src/manifest_editor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ use toml_edit::Document;

pub use add::AddDependency;
pub use dep_id::DepId;
pub use dep_type::{DepType, SectionArgs};
pub use remove::RemoveDependency;

use crate::core::Config;
use crate::internal::fsx;

mod add;
mod dep_id;
mod dep_type;
mod remove;
mod tomlx;

Expand Down
11 changes: 7 additions & 4 deletions scarb/src/manifest_editor/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,36 @@ use toml_edit::Document;
use scarb_ui::components::Status;

use crate::core::PackageName;
use crate::manifest_editor::DepType;

use super::tomlx::get_table_mut;
use super::{Op, OpCtx};

#[derive(Debug)]
pub struct RemoveDependency {
pub dep: PackageName,
pub dep_type: DepType,
}

impl Op for RemoveDependency {
#[tracing::instrument(level = "trace", skip(doc, ctx))]
fn apply_to(self: Box<Self>, doc: &mut Document, ctx: OpCtx<'_>) -> Result<()> {
let tab = get_table_mut(doc, &["dependencies"])?;
let tab = get_table_mut(doc, &[self.dep_type.toml_section_str()])?;

// section is hardcoded as there's no support for other section types yet
ctx.opts.config.ui().print(Status::new(
"Removing",
&format!("{} from dependencies", self.dep),
&format!("{} from {}", self.dep, self.dep_type.toml_section_str()),
));

tab.as_table_like_mut()
.unwrap()
.remove(self.dep.as_str())
.ok_or_else(|| {
anyhow!(
"the dependency `{}` could not be found in `dependencies`",
self.dep
"the dependency `{}` could not be found in `{}`",
self.dep,
self.dep_type.toml_section_str(),
)
})?;

Expand Down
20 changes: 20 additions & 0 deletions scarb/tests/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,3 +464,23 @@ fn should_not_sort_if_already_unsorted() {
"#})
.run();
}

#[test]
fn add_dev_dep() {
ManifestEditHarness::offline()
.args(["add", "--dev", "[email protected]"])
.input(indoc! {r#"
[package]
name = "hello"
version = "1.0.0"
"#})
.output(indoc! {r#"
[package]
name = "hello"
version = "1.0.0"
[dev-dependencies]
foo = "1.0.0"
"#})
.run();
}
56 changes: 56 additions & 0 deletions scarb/tests/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,59 @@ fn dry_run() {
"#})
.run();
}

#[test]
fn remove_dev_dep() {
ManifestEditHarness::offline()
.args(["rm", "--dev", "foo"])
.input(indoc! {r#"
[package]
name = "hello"
version = "1.0.0"
[dev-dependencies]
dep = "1.0.0"
foo = "1.0.0"
bar = "1.0.0"
"#})
.output(indoc! {r#"
[package]
name = "hello"
version = "1.0.0"
[dev-dependencies]
dep = "1.0.0"
bar = "1.0.0"
"#})
.stdout_matches(" Removing foo from dev-dependencies\n")
.run();
}

#[test]
fn remove_undefined_dev_dep() {
ManifestEditHarness::offline()
.args(["rm", "--dev", "foo"])
.input(indoc! {r#"
[package]
name = "hello"
version = "1.0.0"
[dev-dependencies]
dep = "1.0.0"
bar = "1.0.0"
"#})
.output(indoc! {r#"
[package]
name = "hello"
version = "1.0.0"
[dev-dependencies]
dep = "1.0.0"
bar = "1.0.0"
"#})
.failure()
.stdout_matches(indoc! {r#" Removing foo from dev-dependencies
error: the dependency `foo` could not be found in `dev-dependencies`
"#})
.run();
}
8 changes: 8 additions & 0 deletions website/docs/cheatsheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ You can add `branch`, `tag` and `rev` fields to Git dependencies.
You can use `ssh://` URLs, Scarb uses local `git` installation for all network operations.
:::

::: info
You can add development dependencies in `[dev-dependencies]` section.
:::

### Via `scarb add`

Add dependency hosted on a Git repository:
Expand All @@ -110,6 +114,10 @@ You can specify package version like this: `[email protected]`, but see rema
`scarb rm` removes a dependency.
:::

::: info
`--dev` flag adds/removes a dev-dependency.
:::

## Formatting

Format Cairo code:
Expand Down
Loading

0 comments on commit 2d1611e

Please sign in to comment.