Skip to content

Commit

Permalink
Merge pull request #358 from r0gue-io/al3mart/daan-changes
Browse files Browse the repository at this point in the history
chore: feedback and rebase
  • Loading branch information
Daanvdplas authored Nov 29, 2024
2 parents 7e46a67 + 73e6eb2 commit 67d3474
Show file tree
Hide file tree
Showing 8 changed files with 597 additions and 144 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ tar = "0.4.40"
tempfile = "3.10"
thiserror = "1.0.58"
tokio-test = "0.4.4"
toml = "0.5.0"

# networking
reqwest = { version = "0.12", features = ["json"] }
Expand Down
88 changes: 39 additions & 49 deletions crates/pop-cli/src/commands/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use clap::{Args, Subcommand};
#[cfg(feature = "contract")]
use contract::BuildContractCommand;
use duct::cmd;
use pop_common::Profile;
use std::path::PathBuf;
#[cfg(feature = "parachain")]
use {parachain::BuildParachainCommand, spec::BuildSpecCommand};
use pop_common::Profile;

#[cfg(feature = "contract")]
pub(crate) mod contract;
Expand All @@ -29,6 +29,9 @@ pub(crate) struct BuildArgs {
/// The package to be built.
#[arg(short = 'p', long)]
pub(crate) package: Option<String>,
/// For production, always build in release mode to exclude debug features.
#[clap(short = 'r', long, conflicts_with = "profile")]
pub(crate) release: bool,
/// Build profile [default: debug].
#[clap(long, value_enum)]
pub(crate) profile: Option<Profile>,
Expand Down Expand Up @@ -63,23 +66,26 @@ impl Command {
#[cfg(feature = "contract")]
if pop_contracts::is_supported(args.path.as_deref())? {
// All commands originating from root command are valid
let release = match args.profile.unwrap_or(Profile::Debug) {
Profile::Debug => false,
_ => true,
let release = match args.profile {
Some(profile) => profile.into(),
None => args.release,
};
BuildContractCommand { path: args.path, release, valid: true }
.execute()?;
BuildContractCommand { path: args.path, release, valid: true }.execute()?;
return Ok("contract");
}

// If only parachain feature enabled, build as parachain
#[cfg(feature = "parachain")]
if pop_parachains::is_supported(args.path.as_deref())? {
let profile = match args.profile {
Some(profile) => profile,
None => args.release.into(),
};
// All commands originating from root command are valid
BuildParachainCommand {
path: args.path,
package: args.package,
profile: args.profile,
profile: Some(profile),
id: args.id,
valid: true,
}
Expand Down Expand Up @@ -122,29 +128,11 @@ impl Command {

#[cfg(test)]
mod tests {
use std::fs;
use std::fs::write;
use std::path::Path;
use super::*;
use cli::MockCli;
use pop_common::manifest::add_production_profile;
use strum::VariantArray;

fn add_production_profile(project: &Path) -> anyhow::Result<()> {
let root_toml_path = project.join("Cargo.toml");
let mut root_toml_content = fs::read_to_string(&root_toml_path)?;
root_toml_content.push_str(
r#"
[profile.production]
codegen-units = 1
inherits = "release"
lto = true
"#,
);
// Write the updated content back to the file
write(&root_toml_path, root_toml_content)?;
Ok(())
}

#[test]
fn build_works() -> anyhow::Result<()> {
let name = "hello_world";
Expand All @@ -155,31 +143,33 @@ mod tests {
add_production_profile(&project_path)?;

for package in [None, Some(name.to_string())] {
for profile in Profile::VARIANTS {
let project = if package.is_some() { "package" } else { "project" };
let mut cli = MockCli::new()
.expect_intro(format!("Building your {project}"))
.expect_info(format!("The {project} was built in {profile} mode."))
.expect_outro("Build completed successfully!");

assert_eq!(
Command::build(
BuildArgs {
command: None,
path: Some(project_path.clone()),
package: package.clone(),
profile: Some(profile.clone()),
id: None,
},
&mut cli,
)?,
project
);

cli.verify()?;
for release in [true, false] {
for profile in Profile::VARIANTS {
let profile = if release { Profile::Release } else { Profile::Debug };
let project = if package.is_some() { "package" } else { "project" };
let mut cli = MockCli::new()
.expect_intro(format!("Building your {project}"))
.expect_info(format!("The {project} was built in {profile} mode."))
.expect_outro("Build completed successfully!");

assert_eq!(
Command::build(
BuildArgs {
command: None,
path: Some(project_path.clone()),
package: package.clone(),
release,
profile: Some(profile.clone()),
id: None,
},
&mut cli,
)?,
project
);
cli.verify()?;
}
}
}

Ok(())
}
}
18 changes: 1 addition & 17 deletions crates/pop-cli/src/commands/build/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ mod tests {
use super::*;
use cli::MockCli;
use duct::cmd;
use pop_common::manifest::add_production_profile;
use std::{fs, io::Write, path::Path};
use std::fs::write;
use strum::VariantArray;

// Function that generates a Cargo.toml inside node directory for testing.
Expand All @@ -103,22 +103,6 @@ mod tests {
Ok(())
}

fn add_production_profile(project: &Path) -> anyhow::Result<()> {
let root_toml_path = project.join("Cargo.toml");
let mut root_toml_content = fs::read_to_string(&root_toml_path)?;
root_toml_content.push_str(
r#"
[profile.production]
codegen-units = 1
inherits = "release"
lto = true
"#,
);
// Write the updated content back to the file
write(&root_toml_path, root_toml_content)?;
Ok(())
}

#[test]
fn build_works() -> anyhow::Result<()> {
let name = "hello_world";
Expand Down
Loading

0 comments on commit 67d3474

Please sign in to comment.