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

add check for supported near-sdk version #49

Merged
merged 5 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cargo-near/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ clap = { version = "3.2", features = ["derive", "env"] }
colored = "2.0"
env_logger = "0.9"
log = "0.4"
toml = "0.5"
serde_json = "1.0"
symbolic-debuginfo = "8.8"
schemars = "0.8"
Expand Down
49 changes: 31 additions & 18 deletions cargo-near/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,35 @@ pub(crate) fn generate_abi(
crate_metadata: &CrateMetadata,
generate_docs: bool,
) -> anyhow::Result<AbiRoot> {
fs::create_dir_all(&crate_metadata.target_directory)?;

let original_cargo_toml: toml::value::Table =
toml::from_slice(&fs::read(&crate_metadata.manifest_path.path)?)?;

if !original_cargo_toml
.get("dependencies")
.ok_or_else(|| anyhow::anyhow!("[dependencies] section not found"))?
.get("near-sdk")
.ok_or_else(|| anyhow::anyhow!("near-sdk dependency not found"))?
.as_table()
.ok_or_else(|| anyhow::anyhow!("near-sdk dependency should be a table"))?
.get("features")
.and_then(|features| features.as_array())
.map(|features| features.contains(&toml::Value::String("abi".to_string())))
.unwrap_or(false)
{
anyhow::bail!("Unable to generate ABI: NEAR SDK \"abi\" feature is not enabled")
let root_node = crate_metadata
.raw_metadata
.resolve
.as_ref()
.and_then(|dep_graph| {
dep_graph
.nodes
.iter()
.find(|node| node.id == crate_metadata.root_package.id)
})
.ok_or_else(|| anyhow::anyhow!("unable to appropriately resolve the dependency graph, perhaps your `Cargo.toml` file is malformed"))?;

let near_sdk_dep = root_node
.deps
.iter()
.find(|dep| dep.name == "near_sdk")
.and_then(|near_sdk| {
crate_metadata
.raw_metadata
.packages
.iter()
.find(|pkg| pkg.id == near_sdk.pkg)
})
.ok_or_else(|| anyhow::anyhow!("`near-sdk` dependency not found"))?;

for required_feature in ["__abi-generate", "__abi-embed"] {
if !near_sdk_dep.features.contains_key(required_feature) {
anyhow::bail!("unsupported `near-sdk` version. expected 4.1.* or higher");
}
miraclx marked this conversation as resolved.
Show resolved Hide resolved
}

let dylib_artifact = util::compile_project(
Expand Down Expand Up @@ -88,6 +99,8 @@ pub(crate) fn write_to_file(
)?,
};

fs::create_dir_all(&crate_metadata.target_directory)?;

let out_path_abi = crate_metadata.target_directory.join(format!(
"{}_abi.{}",
crate_metadata.root_package.name.replace('-', "_"),
Expand Down
2 changes: 2 additions & 0 deletions cargo-near/src/cargo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub(crate) struct CrateMetadata {
pub root_package: Package,
pub target_directory: PathBuf,
pub manifest_path: CargoManifestPath,
pub raw_metadata: cargo_metadata::Metadata,
}

impl CrateMetadata {
Expand All @@ -32,6 +33,7 @@ impl CrateMetadata {
root_package,
target_directory: target_directory.into(),
manifest_path,
raw_metadata: metadata,
};
Ok(crate_metadata)
}
Expand Down