Skip to content

Commit

Permalink
Add a publish subcommand to the xtask (which performs a dry-run by …
Browse files Browse the repository at this point in the history
…default) (#2539)

* Update and reorganize xtask dependencies

* Add a publish subcommand (which performs a dry run by default)

* Update xtask/src/main.rs

Co-authored-by: Sergio Gasquez Arcos <[email protected]>

---------

Co-authored-by: Sergio Gasquez Arcos <[email protected]>
  • Loading branch information
jessebraham and SergioGasquez authored Nov 14, 2024
1 parent 52c65dd commit d68550c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 15 deletions.
24 changes: 12 additions & 12 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ edition = "2021"
publish = false

[dependencies]
anyhow = "1.0.89"
basic-toml = "0.1.9"
chrono = "0.4.38"
clap = { version = "4.5.19", features = ["derive"] }
csv = "1.3.0"
env_logger = "0.11.5"
log = "0.4.22"
minijinja = "2.3.1"
semver = { version = "1.0.23", features = ["serde"] }
serde = { version = "1.0.210", features = ["derive"] }
strum = { version = "0.26.3", features = ["derive"] }
toml_edit = "0.22.22"
anyhow = "1.0.93"
basic-toml = "0.1.9"
chrono = "0.4.38"
clap = { version = "4.5.20", features = ["derive", "wrap_help"] }
csv = "1.3.1"
env_logger = "0.11.5"
esp-metadata = { path = "../esp-metadata", features = ["clap"] }
log = "0.4.22"
minijinja = "2.5.0"
semver = { version = "1.0.23", features = ["serde"] }
serde = { version = "1.0.215", features = ["derive"] }
strum = { version = "0.26.3", features = ["derive"] }
toml_edit = "0.22.22"
11 changes: 11 additions & 0 deletions xtask/src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ impl CargoArgsBuilder {
self
}

#[must_use]
pub fn args<S>(mut self, args: &[S]) -> Self
where
S: Clone + Into<String>,
{
for arg in args {
self.args.push(arg.clone().into());
}
self
}

pub fn add_arg<S>(&mut self, arg: S) -> &mut Self
where
S: Into<String>,
Expand Down
54 changes: 51 additions & 3 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ enum Cli {
GenerateEfuseFields(GenerateEfuseFieldsArgs),
/// Lint all packages in the workspace with clippy
LintPackages(LintPackagesArgs),
/// Attempt to publish the specified package.
Publish(PublishArgs),
/// Run doctests for specified chip and package.
#[clap(alias = "run-doc-test")]
RunDocTests(ExampleArgs),
Expand Down Expand Up @@ -151,6 +153,17 @@ struct LintPackagesArgs {
fix: bool,
}

#[derive(Debug, Args)]
struct PublishArgs {
/// Package to publish (performs a dry-run by default).
#[arg(value_enum)]
package: Package,

/// Do not pass the `--dry-run` argument, actually try to publish.
#[arg(long)]
no_dry_run: bool,
}

#[derive(Debug, Args)]
struct RunElfArgs {
/// Which chip to run the tests for.
Expand All @@ -164,9 +177,7 @@ struct RunElfArgs {
// Application

fn main() -> Result<()> {
env_logger::Builder::new()
.filter_module("xtask", log::LevelFilter::Info)
.init();
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();

let workspace = std::env::current_dir()?;

Expand All @@ -180,6 +191,7 @@ fn main() -> Result<()> {
Cli::FmtPackages(args) => fmt_packages(&workspace, args),
Cli::GenerateEfuseFields(args) => generate_efuse_src(&workspace, args),
Cli::LintPackages(args) => lint_packages(&workspace, args),
Cli::Publish(args) => publish(&workspace, args),
Cli::RunDocTests(args) => run_doc_tests(&workspace, args),
Cli::RunElfs(args) => run_elfs(args),
Cli::RunExample(args) => examples(&workspace, args, CargoAction::Run),
Expand Down Expand Up @@ -770,6 +782,42 @@ fn lint_package(path: &Path, args: &[&str], fix: bool) -> Result<()> {
xtask::cargo::run(&cargo_args, path)
}

fn publish(workspace: &Path, args: PublishArgs) -> Result<()> {
let package_name = args.package.to_string();
let package_path = xtask::windows_safe_path(&workspace.join(&package_name));

use Package::*;
let mut publish_args = match args.package {
Examples | HilTest => {
bail!(
"Invalid package '{}' specified, this package should not be published!",
args.package
)
}

EspBacktrace | EspHal | EspHalEmbassy | EspIeee802154 | EspLpHal | EspPrintln
| EspStorage | EspWifi | XtensaLxRt => vec!["--no-verify"],

_ => vec![],
};

if !args.no_dry_run {
publish_args.push("--dry-run");
}

let builder = CargoArgsBuilder::default()
.subcommand("publish")
.args(&publish_args);

let args = builder.build();
log::debug!("{args:#?}");

// Execute `cargo publish` command from the package root:
xtask::cargo::run(&args, &package_path)?;

Ok(())
}

fn run_elfs(args: RunElfArgs) -> Result<()> {
let mut failed: Vec<String> = Vec::new();
for elf in fs::read_dir(&args.path)? {
Expand Down

0 comments on commit d68550c

Please sign in to comment.