Skip to content

Commit

Permalink
feat(infra): Support explicit dry runs for infra publish
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanewok committed Mar 28, 2024
1 parent e0e3509 commit b95debf
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
14 changes: 9 additions & 5 deletions crates/infra/cli/src/commands/publish/cargo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ use infra_utils::github::GitHub;
use infra_utils::paths::PathExtensions;
use itertools::Itertools;

use crate::commands::publish::DryRun;

const USER_FACING_CRATE: &str = "slang_solidity";

pub fn publish_cargo() -> Result<()> {
pub fn publish_cargo(dry_run: DryRun) -> Result<()> {
let local_version = CargoWorkspace::local_version()?;
println!("Local version: {local_version}");

Expand Down Expand Up @@ -49,7 +51,7 @@ pub fn publish_cargo() -> Result<()> {

changeset.commit_changes()?;

run_cargo_publish()?;
run_cargo_publish(dry_run)?;

changeset.revert_changes()?;

Expand Down Expand Up @@ -77,14 +79,16 @@ fn update_cargo_lock() -> Result<()> {
.run()
}

fn run_cargo_publish() -> Result<()> {
fn run_cargo_publish(dry_run: DryRun) -> Result<()> {
let mut command = Command::new("cargo")
.arg("publish")
.property("--package", USER_FACING_CRATE)
.flag("--all-features");

if !GitHub::is_running_in_ci() {
println!("Attempting a dry run, since we are not running in CI.");
if dry_run.is_yes() || !GitHub::is_running_in_ci() {
println!(
"Attempting a dry run, since we are not running in CI or a dry run was requested."
);
command = command.flag("--dry-run");
}

Expand Down
8 changes: 5 additions & 3 deletions crates/infra/cli/src/commands/publish/github_release/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use itertools::Itertools;
use markdown::{Block, Span};
use semver::Version;

pub fn publish_github_release() -> Result<()> {
use crate::commands::publish::DryRun;

pub fn publish_github_release(dry_run: DryRun) -> Result<()> {
let current_version = CargoWorkspace::local_version()?;
println!("Current version: {current_version}");

Expand All @@ -28,8 +30,8 @@ pub fn publish_github_release() -> Result<()> {
println!("{}", notes.lines().map(|l| format!(" │ {l}")).join("\n"));
println!();

if !GitHub::is_running_in_ci() {
println!("Skipping release, since we are not running in CI.");
if dry_run.is_yes() || !GitHub::is_running_in_ci() {
println!("Skipping release, since we are not running in CI or a dry run was requested.");
return Ok(());
}

Expand Down
33 changes: 30 additions & 3 deletions crates/infra/cli/src/commands/publish/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,31 @@ use crate::utils::ClapExtensions;
#[derive(Clone, Debug, Parser)]
pub struct PublishController {
command: PublishCommand,

#[arg(long)]
dry_run: bool,
}

#[derive(Clone, Copy)]
enum DryRun {
Yes,
No,
}

impl DryRun {
fn is_yes(self) -> bool {
matches!(self, DryRun::Yes)
}
}

impl From<bool> for DryRun {
fn from(value: bool) -> Self {
if value {
DryRun::Yes
} else {
DryRun::No
}
}
}

#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
Expand All @@ -34,11 +59,13 @@ impl PublishController {
pub fn execute(&self) -> Result<()> {
Terminal::step(format!("publish {name}", name = self.command.clap_name()));

let dry_run = DryRun::from(self.dry_run);

match self.command {
PublishCommand::Changesets => publish_changesets(),
PublishCommand::Npm => publish_npm(),
PublishCommand::Cargo => publish_cargo(),
PublishCommand::GithubRelease => publish_github_release(),
PublishCommand::Npm => publish_npm(dry_run),
PublishCommand::Cargo => publish_cargo(dry_run),
PublishCommand::GithubRelease => publish_github_release(dry_run),
}
}
}
11 changes: 7 additions & 4 deletions crates/infra/cli/src/commands/publish/npm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use infra_utils::commands::Command;
use infra_utils::github::GitHub;
use infra_utils::paths::PathExtensions;

use crate::commands::publish::DryRun;
use crate::toolchains::napi::{
NapiCompiler, NapiConfig, NapiPackageKind, NapiProfile, NapiResolver,
};

pub fn publish_npm() -> Result<()> {
pub fn publish_npm(dry_run: DryRun) -> Result<()> {
let resolver = NapiResolver::solidity();

NapiCompiler::run(&resolver, NapiProfile::Release)?;
Expand All @@ -22,19 +23,21 @@ pub fn publish_npm() -> Result<()> {
&resolver,
&platform_dir,
&NapiPackageKind::Platform(platform),
dry_run,
)?;
}

// Then publish the main package, that depends on the previously published platform-specific packages:

let package_dir = resolver.main_package_dir();
publish_package(&resolver, &package_dir, &NapiPackageKind::Main)
publish_package(&resolver, &package_dir, &NapiPackageKind::Main, dry_run)
}

fn publish_package(
resolver: &NapiResolver,
package_dir: &Path,
kind: &NapiPackageKind,
dry_run: DryRun,
) -> Result<()> {
println!("Publishing: {package_dir:?}");

Expand All @@ -55,8 +58,8 @@ fn publish_package(
.args(["publish", output_dir.unwrap_str()])
.property("--access", "public");

if !GitHub::is_running_in_ci() {
println!("Doing a dry run, since we are not running in CI.");
if dry_run.is_yes() || !GitHub::is_running_in_ci() {
println!("Doing a dry run, since we are not running in CI or a dry run was requested.");
command = command.flag("--dry-run");
}

Expand Down

0 comments on commit b95debf

Please sign in to comment.