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

feat: Implement print plan and basic checks for publish command #154

Merged
merged 3 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 0.3.5

### Enhancements
* Allow error on no changes detected
* List crates to be published
* Add dry-run mode for publishing

## 0.3.4

### Bug Fixes
Expand Down
45 changes: 37 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ But this will also work on single crates because by default every individual cra
1. [Fixed or Independent](#fixed-or-independent)
7. [Publish](#publish)
8. [Rename](#rename)
9. [Plan](#plan)
3. [Config](#config)
4. [Changelog](#changelog)

Expand Down Expand Up @@ -83,8 +84,10 @@ USAGE:
cargo workspaces list [OPTIONS]

OPTIONS:
-a, --all Show private crates that are normally hidden
-h, --help Print help information

LIST OPTIONS:
-a, --all Show private crates that are normally hidden
--json Show information as a JSON array
-l, --long Show extended information
```
Expand All @@ -105,15 +108,17 @@ USAGE:
cargo workspaces changed [OPTIONS]

OPTIONS:
-a, --all Show private crates that are normally hidden
--error-on-empty Return non-zero exit code if no changes detected
--force <pattern> Always include targeted crates matched by glob even when there are no changes
-h, --help Print help information
--ignore-changes <pattern> Ignore changes in files matched by glob
--include-merged-tags Include tags from merged branches
--json Show information as a JSON array
-l, --long Show extended information
--since <SINCE> Use this git reference instead of the last tag

LIST OPTIONS:
-a, --all Show private crates that are normally hidden
--json Show information as a JSON array
-l, --long Show extended information
```

### Exec
Expand Down Expand Up @@ -216,8 +221,8 @@ OPTIONS:
-h, --help Print help information

VERSION ARGS:
<BUMP> Increment all versions by the given explicit semver keyword while skipping the prompts for them [possible values: major, minor, patch,
premajor, preminor, prepatch, prerelease, custom]
<BUMP> Increment all versions by the given explicit semver keyword while skipping the prompts for them
[possible values: major, minor, patch, premajor, preminor, prepatch, skip, prerelease, custom]
<CUSTOM> Specify custom version value when 'bump' is set to 'custom'

VERSION OPTIONS:
Expand All @@ -244,11 +249,14 @@ GIT OPTIONS:

PUBLISH OPTIONS:
--allow-dirty Allow dirty working directories to be published
--dry-run Runs in dry-run mode
--no-remove-dev-deps Don't remove dev-dependencies while publishing
--no-verify Skip crate verification (not recommended)
--publish-as-is Publish crates from the current commit without versioning
--registry <REGISTRY> The Cargo registry to use for publishing
--token <TOKEN> The token to use for publishing

REGISTRY OPTIONS:
--registry <REGISTRY> The Cargo registry to use
--token <TOKEN> The token to use for accessing the registry
```

### Rename
Expand All @@ -269,6 +277,27 @@ OPTIONS:
--ignore <pattern> Ignore the crates matched by glob
```

### Plan
popzxc marked this conversation as resolved.
Show resolved Hide resolved

List the crates in publishing order. This does not check for changes or try to version. It takes the crates as-is.

```
USAGE:
cargo workspaces plan [OPTIONS]

OPTIONS:
-h, --help Print help information
--skip-published Skip already published crate versions

REGISTRY OPTIONS:
--registry <REGISTRY> The Cargo registry to use
--token <TOKEN> The token to use for accessing the registry

LIST OPTIONS:
--json Show information as a JSON array
-l, --long Show extended information
```

## Config

There are two kind of options.
Expand Down
5 changes: 3 additions & 2 deletions cargo-workspaces/Cargo.lock

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

1 change: 1 addition & 0 deletions cargo-workspaces/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ tame-index = { version = "0.9.0", features = ["git", "sparse"] }
dunce = "1.0.4"
ctrlc = "3.4.1"
toml_edit = "0.19.10"
url = "2.5.2"

[dev-dependencies]
assert_cmd = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion cargo-workspaces/src/changed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ impl Changed {
return Err(Error::NoChanges);
}

return Ok(());
Ok(())
}
}
12 changes: 6 additions & 6 deletions cargo-workspaces/src/exec.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::{dag, info, is_private, Error, Result, INTERNAL_ERR};
use crate::utils::{dag, filter_private, info, Error, Result, INTERNAL_ERR};

use cargo_metadata::Metadata;
use clap::Parser;
Expand Down Expand Up @@ -34,7 +34,11 @@ impl Exec {
.map(|x| (x.clone(), x.version.to_string()))
.collect::<Vec<_>>();

let (names, visited) = dag(&pkgs);
let (names, mut visited) = dag(&pkgs);

if self.ignore_private {
visited = filter_private(visited, &pkgs);
}

let ignore = self
.ignore
Expand All @@ -45,10 +49,6 @@ impl Exec {
for p in &visited {
let (pkg, _) = names.get(p).expect(INTERNAL_ERR);

if self.ignore_private && is_private(pkg) {
continue;
}

if let Some(pattern) = &ignore {
if pattern.compile_matcher().is_match(&pkg.name) {
continue;
Expand Down
3 changes: 1 addition & 2 deletions cargo-workspaces/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ impl List {

let pkg_ids = visited
.into_iter()
.map(|p| names.get(&p).expect(INTERNAL_ERR).0.id.clone())
.collect::<Vec<_>>();
.map(|p| names.get(&p).expect(INTERNAL_ERR).0.id.clone());

let pkgs = get_pkgs(&metadata, self.list.all)?;

Expand Down
3 changes: 3 additions & 0 deletions cargo-workspaces/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod create;
mod exec;
mod init;
mod list;
mod plan;
mod publish;
mod rename;
mod version;
Expand All @@ -24,6 +25,7 @@ enum Subcommand {
Create(create::Create),
Rename(rename::Rename),
Init(init::Init),
Plan(plan::Plan),
}

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -83,6 +85,7 @@ fn main() {
Subcommand::Exec(x) => x.run(metadata),
Subcommand::Create(x) => x.run(metadata),
Subcommand::Rename(x) => x.run(metadata),
Subcommand::Plan(x) => x.run(metadata),
_ => unreachable!(),
}
};
Expand Down
70 changes: 70 additions & 0 deletions cargo-workspaces/src/plan.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use crate::utils::{
create_http_client, dag, filter_private, get_pkgs, is_published, list, package_registry,
ListOpt, ListPublicOpt, RegistryOpt, Result, INTERNAL_ERR,
};

use cargo_metadata::Metadata;
use clap::Parser;

/// List the crates in publishing order
#[derive(Debug, Parser)]
pub struct Plan {
/// Skip already published crate versions
#[clap(long)]
skip_published: bool,

#[clap(flatten)]
registry: RegistryOpt,

#[clap(flatten)]
list: ListPublicOpt,
}

impl Plan {
pub fn run(self, metadata: Metadata) -> Result {
let pkgs = metadata
.packages
.iter()
.map(|x| (x.clone(), x.version.to_string()))
.collect::<Vec<_>>();

let (names, visited) = dag(&pkgs);

let http_client = create_http_client(&metadata.workspace_root, &self.registry.token)?;

let pkg_ids = filter_private(visited, &pkgs)
.into_iter()
.map(|p| {
let (pkg, version) = names.get(&p).expect(INTERNAL_ERR);

let published = if self.skip_published {
let index_url =
package_registry(&metadata, self.registry.registry.as_ref(), pkg)?;
is_published(&http_client, index_url, &pkg.name, version)?
} else {
false
};

Ok((pkg.id.clone(), published))
})
.collect::<Result<Vec<_>>>()?
.into_iter()
.filter_map(|(id, published)| (!published).then_some(id));

let pkgs = get_pkgs(&metadata, false)?;

let ordered_pkgs = pkg_ids
.into_iter()
.filter_map(|id| pkgs.iter().find(|p| p.id == id))
.cloned()
.collect::<Vec<_>>();

list(
&ordered_pkgs,
ListOpt {
all: false,
list: self.list,
},
)
}
}
Loading