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

chore: support specify contract path input with or without -p flag #361

Merged
merged 16 commits into from
Jan 7, 2025
27 changes: 21 additions & 6 deletions crates/pop-cli/src/commands/build/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// SPDX-License-Identifier: GPL-3.0

use crate::cli::{self, Cli};
use clap::{Args, Subcommand};
use anyhow::ensure;
use clap::{Args, Parser, Subcommand};
#[cfg(feature = "contract")]
use contract::BuildContractCommand;
use duct::cmd;
Expand All @@ -23,9 +24,12 @@ pub(crate) mod spec;
pub(crate) struct BuildArgs {
#[command(subcommand)]
pub command: Option<Command>,
/// Directory path for your project [default: current directory]
/// Directory path with flag for your project [default: current directory]
#[arg(long)]
pub(crate) path: Option<PathBuf>,
/// Directory path without flag for your project [default: current directory]
#[arg(value_name = "PATH", required_unless_present = "path")]
pub(crate) path_pos: Option<PathBuf>,
/// The package to be built.
#[arg(short = 'p', long)]
pub(crate) package: Option<String>,
Expand Down Expand Up @@ -63,27 +67,37 @@ impl Command {
/// Executes the command.
pub(crate) fn execute(args: BuildArgs) -> anyhow::Result<&'static str> {
// If only contract feature enabled, build as contract
let path_flag = args.path.clone();
let path_pos = args.path_pos.clone();
let project_path = match path_flag {
Some(ref path) if path.to_str().unwrap().contains("./") => Some(path.to_owned()),
_ => {
ensure!(path_pos.is_some(), "Failed to get directory!");
path_pos
},
};

#[cfg(feature = "contract")]
if pop_contracts::is_supported(args.path.as_deref())? {
if pop_contracts::is_supported(project_path.as_deref())? {
// All commands originating from root command are valid
let release = match args.profile {
Some(profile) => profile.into(),
None => args.release,
};
BuildContractCommand { path: args.path, release, valid: true }.execute()?;
BuildContractCommand { path: project_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())? {
if pop_parachains::is_supported(project_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,
path: project_path,
package: args.package,
profile: Some(profile),
id: args.id,
Expand Down Expand Up @@ -157,6 +171,7 @@ mod tests {
BuildArgs {
command: None,
path: Some(project_path.clone()),
path_pos: Some(project_path.clone()),
package: package.clone(),
release,
profile: Some(profile.clone()),
Expand Down
11 changes: 11 additions & 0 deletions crates/pop-cli/tests/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ async fn contract_lifecycle() -> Result<()> {
.args(&["build", "--path", "./test_contract", "--release"])
.assert()
.success();

// pop build ./test_contract --release
/*Command::cargo_bin("pop")
.unwrap()
.current_dir(&temp_dir)
.args(&["build", "./test_contract", "--release"])
.assert()
.success();
*/
println!("Contract built!!");
ndkazu marked this conversation as resolved.
Show resolved Hide resolved

// Verify that the directory target has been created
assert!(temp_dir.join("test_contract/target").exists());
// Verify that all the artifacts has been generated
Expand Down
Loading