Skip to content

Commit

Permalink
Add a publish subcommand (which performs a dry run by default)
Browse files Browse the repository at this point in the history
  • Loading branch information
jessebraham committed Nov 14, 2024
1 parent 84e7214 commit 5daa43d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
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 --dry-run` 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 5daa43d

Please sign in to comment.