Skip to content

Commit 3ad49a0

Browse files
committed
init helpers: is_contract & is_parachain
1 parent 07c8414 commit 3ad49a0

File tree

4 files changed

+43
-15
lines changed

4 files changed

+43
-15
lines changed

Cargo.lock

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ reqwest = { version = "0.11", optional = true }
4343
serde_json = { version = "1.0", optional = true }
4444
serde = { version = "1.0", features = ["derive"], optional = true }
4545
symlink = { version = "0.1", optional = true }
46-
toml_edit = { version = "0.22", optional = true }
46+
toml_edit = { version = "0.22.9" }
4747
tracing-subscriber = { version = "0.3", optional = true }
4848
zombienet-sdk = { git = "https://github.com/r0gue-io/zombienet-sdk", branch = "pop", optional = true }
4949
zombienet-support = { git = "https://github.com/r0gue-io/zombienet-sdk", branch = "pop", optional = true }
@@ -72,7 +72,6 @@ parachain = [
7272
"dep:reqwest",
7373
"dep:serde_json",
7474
"dep:symlink",
75-
"dep:toml_edit",
7675
"dep:tracing-subscriber",
7776
"dep:url",
7877
"dep:zombienet-sdk",

src/helpers.rs

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::Result;
1+
use anyhow::{Context, Result};
22
use cliclack::{log, outro_cancel};
33
use git2::{IndexAddOption, Repository, ResetType};
44
use regex::Regex;
@@ -119,7 +119,36 @@ pub(crate) fn resolve_pallet_path(path: Option<String>) -> PathBuf {
119119
}
120120
}
121121
}
122-
122+
/// Checks if `path` is a ink contract project directory by searching its dependencies
123+
pub(crate) fn is_contract(path: &Path) -> Result<bool> {
124+
let manifest_path = path.join("Cargo.toml");
125+
Ok(if manifest_path.exists() {
126+
let manifest =
127+
fs::read_to_string(manifest_path).context("is_contract: Failed to read Cargo.toml")?;
128+
let manifest: toml_edit::DocumentMut =
129+
manifest.parse().context("is_contract: Cargo.toml is not well formed")?;
130+
let dependencies =
131+
manifest["dependencies"].as_table().expect("dependencies is not a table");
132+
dependencies.contains_key("ink") && dependencies.contains_key("scale")
133+
} else {
134+
false
135+
})
136+
}
137+
/// Checks if `path` is a substrate parachain project directory by searching its dependencies
138+
pub(crate) fn is_parachain(path: &Path) -> Result<bool> {
139+
let workspace_manifest = path.join("Cargo.toml");
140+
if workspace_manifest.exists() {
141+
let workspace_manifest = fs::read_to_string(workspace_manifest)
142+
.context("is_parachain: Failed to read Cargo.toml")?;
143+
let workspace_manifest: toml_edit::DocumentMut = workspace_manifest
144+
.parse()
145+
.context("is_parachain: Cargo.toml is not well formed")?;
146+
todo!("Check if workspace keys are present");
147+
Ok(false)
148+
} else {
149+
Ok(false)
150+
}
151+
}
123152
#[cfg(test)]
124153
mod tests {
125154
use super::*;

src/parachains/zombienet.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::{
1515
};
1616
use symlink::{remove_symlink_file, symlink_file};
1717
use tempfile::{Builder, NamedTempFile};
18-
use toml_edit::{value, Document, Formatted, Item, Table, Value};
18+
use toml_edit::{value, DocumentMut, Formatted, Item, Table, Value};
1919
use url::Url;
2020
use zombienet_sdk::{Network, NetworkConfig, NetworkConfigExt};
2121
use zombienet_support::fs::local::LocalFileSystem;
@@ -26,7 +26,7 @@ pub struct Zombienet {
2626
/// The cache location, used for caching binaries.
2727
cache: PathBuf,
2828
/// The config to be used to launch a network.
29-
network_config: (PathBuf, Document),
29+
network_config: (PathBuf, DocumentMut),
3030
/// The binary required to launch the relay chain.
3131
relay_chain: Binary,
3232
/// The binaries required to launch parachains.
@@ -43,7 +43,7 @@ impl Zombienet {
4343
) -> Result<Self> {
4444
// Parse network config
4545
let network_config_path = PathBuf::from(network_config);
46-
let config = std::fs::read_to_string(&network_config_path)?.parse::<Document>()?;
46+
let config = std::fs::read_to_string(&network_config_path)?.parse::<DocumentMut>()?;
4747
// Determine binaries
4848
let relay_chain_binary = Self::relay_chain(relay_chain_version, &config, &cache).await?;
4949
let mut parachain_binaries = IndexMap::new();
@@ -257,7 +257,7 @@ impl Zombienet {
257257

258258
async fn relay_chain(
259259
version: Option<&String>,
260-
network_config: &Document,
260+
network_config: &DocumentMut,
261261
cache: &PathBuf,
262262
) -> Result<Binary> {
263263
const BINARY: &str = "polkadot";
@@ -602,7 +602,7 @@ mod tests {
602602
let cache = PathBuf::from(temp_dir.path());
603603

604604
let network_config_path = PathBuf::from(CONFIG_FILE_PATH);
605-
let config = std::fs::read_to_string(&network_config_path)?.parse::<Document>()?;
605+
let config = std::fs::read_to_string(&network_config_path)?.parse::<DocumentMut>()?;
606606

607607
let binary_relay_chain =
608608
Zombienet::relay_chain(Some(&TESTING_POLKADOT_VERSION.to_string()), &config, &cache)
@@ -627,7 +627,7 @@ mod tests {
627627
let cache = PathBuf::from(temp_dir.path());
628628

629629
let network_config_path = PathBuf::from(CONFIG_FILE_PATH);
630-
let config = std::fs::read_to_string(&network_config_path)?.parse::<Document>()?;
630+
let config = std::fs::read_to_string(&network_config_path)?.parse::<DocumentMut>()?;
631631

632632
// Ideally here we will Mock GitHub struct and its get_latest_release function response
633633
let binary_relay_chain = Zombienet::relay_chain(None, &config, &cache).await?;
@@ -651,7 +651,7 @@ mod tests {
651651
let network_config_path = generate_wrong_config_no_relay(&temp_dir)
652652
.expect("Error generating the testing toml file");
653653

654-
let config = std::fs::read_to_string(&network_config_path)?.parse::<Document>()?;
654+
let config = std::fs::read_to_string(&network_config_path)?.parse::<DocumentMut>()?;
655655

656656
let result_error =
657657
Zombienet::relay_chain(Some(&TESTING_POLKADOT_VERSION.to_string()), &config, &cache)

0 commit comments

Comments
 (0)