Skip to content

Commit f559377

Browse files
committed
refactor: specify default locations at core logic
1 parent 3d107c2 commit f559377

File tree

23 files changed

+136
-164
lines changed

23 files changed

+136
-164
lines changed

crates/pop-chains/src/build/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub fn build_project(
8383
/// # Arguments
8484
/// * `path` - The optional path to the manifest, defaulting to the current directory if not
8585
/// specified.
86-
pub fn is_supported(path: Option<&Path>) -> Result<bool, Error> {
86+
pub fn is_supported(path: &Path) -> Result<bool, Error> {
8787
let manifest = from_path(path)?;
8888
// Simply check for a chain dependency
8989
const DEPENDENCIES: [&str; 4] =
@@ -118,7 +118,7 @@ fn build_binary_path<F>(project_path: &Path, path_builder: F) -> Result<PathBuf,
118118
where
119119
F: Fn(&str) -> PathBuf,
120120
{
121-
let manifest = from_path(Some(project_path))?;
121+
let manifest = from_path(project_path)?;
122122
let project_name = manifest.package().name();
123123
let release = path_builder(project_name);
124124
if !release.exists() {
@@ -1287,13 +1287,13 @@ mod tests {
12871287
assert!(!is_supported(Some(&path.join(name)))?);
12881288

12891289
// Chain
1290-
let mut manifest = from_path(Some(&path.join(name)))?;
1290+
let mut manifest = from_path(&path.join(name))?;
12911291
manifest
12921292
.dependencies
12931293
.insert("cumulus-client-collator".into(), Dependency::Simple("^0.14.0".into()));
12941294
let manifest = toml_edit::ser::to_string_pretty(&manifest)?;
12951295
write(path.join(name).join("Cargo.toml"), manifest)?;
1296-
assert!(is_supported(Some(&path.join(name)))?);
1296+
assert!(is_supported(&path.join(name))?);
12971297
Ok(())
12981298
}
12991299
}

crates/pop-chains/src/build/runtime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl DeterministicBuilder {
127127
/// # Arguments
128128
/// * `path` - The optional path to the manifest, defaulting to the current directory if not
129129
/// specified.
130-
pub fn is_supported(path: Option<&Path>) -> Result<bool, Error> {
130+
pub fn is_supported(path: &Path) -> Result<bool, Error> {
131131
let manifest = from_path(path)?;
132132
// Simply check for a parachain dependency
133133
const DEPENDENCIES: [&str; 3] = ["frame-system", "frame-support", "substrate-wasm-builder"];

crates/pop-cli/src/commands/build/contract.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::path::PathBuf;
77
/// Configuration for building a smart contract.
88
pub struct BuildContract {
99
/// Path of the contract project.
10-
pub(crate) path: Option<PathBuf>,
10+
pub(crate) path: PathBuf,
1111
/// Build profile: `true` for release mode, `false` for debug mode.
1212
pub(crate) release: bool,
1313
}
@@ -25,8 +25,7 @@ impl BuildContract {
2525
fn build(self, cli: &mut impl cli::traits::Cli) -> anyhow::Result<&'static str> {
2626
cli.intro("Building your contract")?;
2727
// Build contract.
28-
let build_result =
29-
build_smart_contract(self.path.as_deref(), self.release, Verbosity::Default)?;
28+
let build_result = build_smart_contract(&self.path, self.release, Verbosity::Default)?;
3029
cli.success(build_result.display())?;
3130
cli.outro("Build completed successfully!")?;
3231
Ok("contract")

crates/pop-cli/src/commands/build/mod.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ impl Command {
107107
#[cfg(any(feature = "polkavm-contracts", feature = "wasm-contracts", feature = "chain"))]
108108
// If only contract feature enabled, build as contract
109109
let project_path =
110-
crate::common::builds::get_project_path(args.path.clone(), args.path_pos.clone());
110+
crate::common::builds::ensure_project_path(args.path.clone(), args.path_pos.clone());
111111

112112
#[cfg(any(feature = "polkavm-contracts", feature = "wasm-contracts"))]
113-
if pop_contracts::is_supported(project_path.as_deref())? {
113+
if pop_contracts::is_supported(&project_path)? {
114114
// All commands originating from root command are valid
115115
let release = match args.profile {
116116
Some(profile) => profile.into(),
@@ -122,17 +122,16 @@ impl Command {
122122

123123
// If project is a parachain runtime, build as parachain runtime
124124
#[cfg(feature = "chain")]
125-
if args.only_runtime || pop_chains::runtime::is_supported(project_path.as_deref())? {
125+
if args.only_runtime || pop_chains::runtime::is_supported(&project_path)? {
126126
let profile = match args.profile {
127127
Some(profile) => profile,
128128
None => args.release.into(),
129129
};
130-
let temp_path = PathBuf::from("./");
131130
let features = args.features.unwrap_or_default();
132131
let feature_list = collect_features(&features, args.benchmark, args.try_runtime);
133132

134133
BuildRuntime {
135-
path: project_path.unwrap_or(temp_path).to_path_buf(),
134+
path: project_path,
136135
profile,
137136
benchmark: feature_list.contains(&Benchmark.as_ref()),
138137
try_runtime: feature_list.contains(&TryRuntime.as_ref()),
@@ -144,17 +143,16 @@ impl Command {
144143

145144
// If project is a parachain runtime, build as parachain runtime
146145
#[cfg(feature = "chain")]
147-
if pop_chains::is_supported(project_path.as_deref())? {
146+
if pop_chains::is_supported(&project_path)? {
148147
let profile = match args.profile {
149148
Some(profile) => profile,
150149
None => args.release.into(),
151150
};
152-
let temp_path = PathBuf::from("./");
153151
let features = args.features.unwrap_or_default();
154152
let feature_list = collect_features(&features, args.benchmark, args.try_runtime);
155153

156154
BuildChain {
157-
path: project_path.unwrap_or(temp_path).to_path_buf(),
155+
path: project_path,
158156
package: args.package,
159157
profile,
160158
benchmark: feature_list.contains(&Benchmark.as_ref()),

crates/pop-cli/src/commands/build/runtime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ impl BuildRuntime {
3333
pub(crate) fn execute(self) -> anyhow::Result<()> {
3434
let cli = &mut cli::Cli;
3535
let current_dir = current_dir().unwrap_or(PathBuf::from("./"));
36-
let is_parachain = pop_chains::is_supported(Some(&current_dir))?;
37-
let is_runtime = pop_chains::runtime::is_supported(Some(&current_dir))?;
36+
let is_parachain = pop_chains::is_supported(&current_dir)?;
37+
let is_runtime = pop_chains::runtime::is_supported(&current_dir)?;
3838
// `pop build runtime` must be run inside a parachain project or a specific runtime folder.
3939
if !is_parachain && !is_runtime {
4040
return display_message(

crates/pop-cli/src/commands/build/spec.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ pub(crate) enum RelayChain {
141141
#[derive(Args, Default)]
142142
pub struct BuildSpecCommand {
143143
/// Directory path for your project [default: current directory]
144-
#[arg(long)]
145-
pub(crate) path: Option<PathBuf>,
144+
#[arg(long, default_value = "./Cargo.toml")]
145+
pub(crate) path: PathBuf,
146146
/// File name for the resulting spec. If a path is given,
147147
/// the necessary directories will be created
148148
#[arg(short, long = "output")]
@@ -194,7 +194,7 @@ impl BuildSpecCommand {
194194
let mut cli = Cli;
195195
cli.intro("Generate your chain spec")?;
196196
// Checks for appchain project.
197-
if is_supported(self.path.as_deref())? {
197+
if is_supported(self.path.as_path())? {
198198
let build_spec = self.configure_build_spec(&mut cli).await?;
199199
if let Err(e) = build_spec.build(&mut cli) {
200200
cli.outro_cancel(e.to_string())?;
@@ -234,9 +234,6 @@ impl BuildSpecCommand {
234234
..
235235
} = self;
236236

237-
// Project path.
238-
let path = path.unwrap_or(PathBuf::from("./"));
239-
240237
// Chain.
241238
let chain = match chain {
242239
Some(chain) => chain,
@@ -252,9 +249,7 @@ impl BuildSpecCommand {
252249
// Output file.
253250
let maybe_chain_spec_file = PathBuf::from(&chain);
254251
// Check if the provided chain specification is a file.
255-
let (output_file, prompt) = if maybe_chain_spec_file.exists() &&
256-
maybe_chain_spec_file.is_file()
257-
{
252+
let (output_file, prompt) = if maybe_chain_spec_file.is_file() {
258253
if output_file.is_some() {
259254
cli.warning("NOTE: If an existing chain spec file is provided it will be used for the output path.")?;
260255
}
@@ -450,7 +445,7 @@ impl BuildSpecCommand {
450445
let package = if deterministic {
451446
package
452447
.or_else(|| {
453-
from_path(Some(&runtime_dir))
448+
from_path(&runtime_dir)
454449
.ok()
455450
.and_then(|manifest| manifest.package.map(|pkg| pkg.name))
456451
})
@@ -745,13 +740,14 @@ mod tests {
745740
let deterministic = true;
746741
let package = "runtime-name";
747742
let runtime_dir = PathBuf::from("./new-runtime-dir");
743+
let path = PathBuf::from("./");
748744

749745
for build_spec_cmd in [
750746
// No flags used.
751747
BuildSpecCommand::default(),
752748
// All flags used.
753749
BuildSpecCommand {
754-
path: None,
750+
path,
755751
output_file: Some(PathBuf::from(output_file)),
756752
profile: Some(profile.clone()),
757753
id: Some(para_id),
@@ -839,6 +835,7 @@ mod tests {
839835
let deterministic = true;
840836
let package = "runtime-name";
841837
let runtime_dir = PathBuf::from("./new-runtime-dir");
838+
let path = PathBuf::from("./");
842839

843840
// Create a temporary file to act as the existing chain spec file.
844841
let temp_dir = tempdir()?;
@@ -855,7 +852,7 @@ mod tests {
855852
},
856853
// All flags used.
857854
BuildSpecCommand {
858-
path: None,
855+
path: path.clone(),
859856
output_file: Some(PathBuf::from(output_file)),
860857
profile: Some(profile.clone()),
861858
id: Some(para_id),

crates/pop-cli/src/commands/call/contract.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::{
44
cli::{self, traits::*},
55
common::{
6-
builds::get_project_path,
6+
builds::{ensure_project_path, get_project_path},
77
contracts::{has_contract_been_built, normalize_call_args, request_contract_function_args},
88
prompt::display_message,
99
urls,
@@ -186,12 +186,12 @@ impl CallContractCommand {
186186

187187
/// If the contract has not been built, build it in release mode.
188188
async fn ensure_contract_built(&self, cli: &mut impl Cli) -> Result<()> {
189-
let project_path = get_project_path(self.path.clone(), self.path_pos.clone());
189+
let project_path = ensure_project_path(self.path.clone(), self.path_pos.clone());
190190
// Build the contract in release mode
191191
cli.warning("NOTE: contract has not yet been built.")?;
192192
let spinner = spinner();
193193
spinner.start("Building contract in RELEASE mode...");
194-
let result = match build_smart_contract(project_path.as_deref(), true, Verbosity::Quiet) {
194+
let result = match build_smart_contract(&project_path, true, Verbosity::Quiet) {
195195
Ok(result) => result,
196196
Err(e) => {
197197
return Err(anyhow!(format!(
@@ -225,7 +225,7 @@ impl CallContractCommand {
225225

226226
project_path
227227
.as_ref()
228-
.map(|p| p.is_dir() && !has_contract_been_built(Some(p)))
228+
.map(|p| p.is_dir() && !has_contract_been_built(p))
229229
.unwrap_or_default()
230230
}
231231

@@ -401,7 +401,7 @@ impl CallContractCommand {
401401
cli: &mut impl Cli,
402402
prompt_to_repeat_call: bool,
403403
) -> Result<()> {
404-
let project_path = get_project_path(self.path.clone(), self.path_pos.clone());
404+
let project_path = ensure_project_path(self.path.clone(), self.path_pos.clone());
405405

406406
let message = match &self.message {
407407
Some(message) => message.to_string(),
@@ -410,9 +410,7 @@ impl CallContractCommand {
410410
},
411411
};
412412
// Disable wallet signing and display warning if the call is read-only.
413-
let path = PathBuf::from("./");
414-
let message_metadata =
415-
get_message(project_path.as_deref().unwrap_or_else(|| &path), &message)?;
413+
let message_metadata = get_message(project_path.clone(), &message)?;
416414
if !message_metadata.mutates && self.use_wallet {
417415
cli.warning("NOTE: Signing is not required for this read-only call. The '--use-wallet' flag will be ignored.")?;
418416
self.use_wallet = false;

crates/pop-cli/src/commands/call/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ impl CallArgs {
3939
let current_dir = std::env::current_dir()?;
4040

4141
#[cfg(any(feature = "polkavm-contracts", feature = "wasm-contracts"))]
42-
if pop_contracts::is_supported(Some(&current_dir))? {
42+
if pop_contracts::is_supported(&current_dir)? {
4343
let mut cmd = contract::CallContractCommand::default();
4444
cmd.path_pos = Some(current_dir);
4545
return Ok(Command::Contract(cmd));
4646
}
4747

4848
#[cfg(feature = "chain")]
49-
if pop_chains::is_supported(Some(&current_dir))? {
49+
if pop_chains::is_supported(&current_dir)? {
5050
return Ok(Command::Chain(Default::default()));
5151
}
5252

crates/pop-cli/src/commands/test/contract.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const HELP_HEADER: &str = "Smart contract testing options";
1818
#[clap(next_help_heading = HELP_HEADER)]
1919
pub(crate) struct TestContractCommand {
2020
#[clap(skip)]
21-
pub(crate) path: Option<PathBuf>,
21+
pub(crate) path: PathBuf,
2222
/// Run end-to-end tests
2323
#[arg(short, long)]
2424
e2e: bool,
@@ -52,12 +52,12 @@ impl TestContractCommand {
5252
},
5353
};
5454

55-
test_e2e_smart_contract(self.path.as_deref(), self.node.as_deref())?;
55+
test_e2e_smart_contract(&self.path, self.node.as_deref())?;
5656
cli.outro("End-to-end testing complete")?;
5757
Ok(E2e)
5858
} else {
5959
cli.intro("Starting unit tests")?;
60-
test_project(self.path.as_deref())?;
60+
test_project(&self.path)?;
6161
cli.outro("Unit testing complete")?;
6262
Ok(Unit)
6363
}

crates/pop-cli/src/commands/test/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#[cfg(any(feature = "polkavm-contracts", feature = "wasm-contracts"))]
44
use crate::cli;
55
use crate::common::{
6-
builds::get_project_path,
6+
builds::ensure_project_path,
77
Project::{self, *},
88
TestFeature::{self, Unit},
99
};
@@ -75,20 +75,20 @@ impl Command {
7575
#[cfg(any(feature = "polkavm-contracts", feature = "wasm-contracts"))]
7676
cli: &mut impl cli::traits::Cli,
7777
) -> anyhow::Result<(Project, TestFeature)> {
78-
let project_path = get_project_path(args.path.clone(), args.path_pos.clone());
78+
let project_path = ensure_project_path(args.path.clone(), args.path_pos.clone());
7979

8080
#[cfg(any(feature = "polkavm-contracts", feature = "wasm-contracts"))]
81-
if pop_contracts::is_supported(project_path.as_deref())? {
81+
if pop_contracts::is_supported(&project_path)? {
8282
let mut cmd = args.contract;
83-
cmd.path = project_path;
83+
cmd.path = project_path.clone();
8484
let feature = contract::TestContractCommand::execute(cmd, cli).await?;
8585
return Ok((Contract, feature));
8686
}
8787

88-
test_project(project_path.as_deref())?;
88+
test_project(&project_path)?;
8989

9090
#[cfg(feature = "chain")]
91-
if pop_chains::is_supported(project_path.as_deref())? {
91+
if pop_chains::is_supported(&project_path)? {
9292
return Ok((Chain, Unit));
9393
}
9494
Ok((Unknown, Unit))

0 commit comments

Comments
 (0)