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

use near-abi-rs #23

Merged
merged 17 commits into from
Aug 26, 2022
4 changes: 2 additions & 2 deletions cargo-near/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ cargo_metadata = "0.14"
clap = { version = "3.2", features = ["derive", "env"] }
colored = "2.0"
env_logger = "0.9"
near-sdk = { version = "4.1.0-pre.1", features = ["abi"] }
log = "0.4"
prettyplease = "0.1"
toml = "0.5"
serde_json = "1.0"
symbolic-debuginfo = "8.8"
syn = "1.0"
quote = "1.0"
schemars = "0.8"
near-abi = { version = "0.1.0-pre.0", features = ["__chunked-entries"] }

[dev-dependencies]
function_name = "0.3"
proc-macro2 = "1.0"
schemars = "0.8"
7 changes: 3 additions & 4 deletions cargo-near/src/abi/generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub(crate) fn generate_main_rs(dylib_path: &Path) -> anyhow::Result<String> {
let near_abi_function_defs = near_abi_symbols.iter().map(|s| {
let name = format_ident!("{}", s);
quote! {
fn #name() -> near_sdk::__private::AbiRoot;
fn #name() -> near_sdk::__private::ChunkedAbiEntry;
}
});
let near_abi_function_invocations = near_abi_symbols.iter().map(|s| {
Expand All @@ -141,9 +141,8 @@ pub(crate) fn generate_main_rs(dylib_path: &Path) -> anyhow::Result<String> {
}

fn main() -> Result<(), std::io::Error> {
let root_abis = vec![#(#near_abi_function_invocations),*];
let combined_root_abi = near_sdk::__private::AbiRoot::combine(root_abis);
let contents = serde_json::to_string_pretty(&combined_root_abi)?;
let abi_entries = vec![#(#near_abi_function_invocations),*];
let contents = serde_json::to_string_pretty(&abi_entries)?;
print!("{}", contents);
Ok(())
}
Expand Down
25 changes: 12 additions & 13 deletions cargo-near/src/abi/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::cargo::{manifest::CargoManifestPath, metadata::CrateMetadata};
use crate::util;
use near_sdk::__private::schemars::schema::{Schema, SchemaObject};
use near_sdk::__private::{AbiMetadata, AbiRoot};
use std::collections::HashMap;
use std::{fs, path::PathBuf};

Expand Down Expand Up @@ -50,35 +48,36 @@ pub(crate) fn execute(
)],
)?;

let mut near_abi: AbiRoot = serde_json::from_slice(&stdout)?;
let metadata = extract_metadata(&crate_metadata);
near_abi.metadata = metadata;
let mut contract_abi = near_abi::__private::ChunkedAbiEntry::combine(
serde_json::from_slice::<Vec<_>>(&stdout)?.into_iter(),
)?
.into_abi_root(extract_metadata(&crate_metadata));
if !generate_docs {
strip_docs(&mut near_abi);
strip_docs(&mut contract_abi);
}
let near_abi_json = serde_json::to_string_pretty(&near_abi)?;
let near_abi_json = serde_json::to_string_pretty(&contract_abi)?;
let out_path_abi = crate_metadata.target_directory.join(ABI_FILE);
fs::write(&out_path_abi, near_abi_json)?;

Ok(AbiResult { path: out_path_abi })
}

fn extract_metadata(crate_metadata: &CrateMetadata) -> AbiMetadata {
fn extract_metadata(crate_metadata: &CrateMetadata) -> near_abi::AbiMetadata {
let package = &crate_metadata.root_package;
AbiMetadata {
near_abi::AbiMetadata {
name: Some(package.name.clone()),
version: Some(package.version.to_string()),
authors: package.authors.clone(),
other: HashMap::new(),
}
}

fn strip_docs(abi_root: &mut AbiRoot) {
for function in &mut abi_root.abi.functions {
fn strip_docs(abi_root: &mut near_abi::AbiRoot) {
for function in &mut abi_root.body.functions {
function.doc = None;
}
for schema in &mut abi_root.abi.root_schema.definitions.values_mut() {
if let Schema::Object(SchemaObject {
for schema in &mut abi_root.body.root_schema.definitions.values_mut() {
if let schemars::schema::Schema::Object(schemars::schema::SchemaObject {
metadata: Some(metadata),
..
}) = schema
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ publish = false

[dev-dependencies]
anyhow = "1.0"
borsh = "0.9"
cargo-near = { path = "../cargo-near" }
function_name = "0.3"
git2 = "0.14"
near-sdk = { version = "4.1.0-pre.1", features = ["abi"] }
prettyplease = "0.1"
proc-macro2 = "1.0"
schemars = "0.8"
serde_json = "1.0"
syn = "1.0"
tempfile = "3.3"
quote = "1.0"
near-abi = "0.1.0-pre.0"
2 changes: 1 addition & 1 deletion integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ macro_rules! generate_abi {
doc: false,
}))?;

let abi_root: near_sdk::__private::AbiRoot =
let abi_root: near_abi::AbiRoot =
serde_json::from_slice(&fs::read(workspace_dir.join(function_name!()).join("target").join("near").join("abi.json"))?)?;
abi_root
}};
Expand Down
6 changes: 5 additions & 1 deletion integration-tests/templates/_Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
near-sdk = { version = "4.1.0-pre.1", features = ["abi"] }
serde = { version = "1", features = ["derive"] }
schemars = "0.8"

[dependencies.near-sdk]
git = "https://github.com/near/near-sdk-rs.git"
rev = "97a0d01cafe1cd7dceb2774f5840052e577edf40"
features = ["abi"]

[workspace]
members = []

Expand Down
5 changes: 4 additions & 1 deletion integration-tests/templates/_Cargo_no_abi_feature.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
near-sdk = { version = "4.1.0-pre.1" }
serde = { version = "1", features = ["derive"] }
schemars = "0.8"

[dependencies.near-sdk]
git = "https://github.com/near/near-sdk-rs.git"
rev = "97a0d01cafe1cd7dceb2774f5840052e577edf40"

[workspace]
members = []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ serde = { version = "1", features = ["derive"] }
schemars = "0.8"

[dependencies.near-sdk]
version = "4.1.0-pre.1"
git = "https://github.com/near/near-sdk-rs.git"
rev = "97a0d01cafe1cd7dceb2774f5840052e577edf40"
features = ["abi"]

[workspace]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
near-sdk = { version = "4.1.0-pre.1", features = ["abi", "unstable"] }
serde = { version = "1", features = ["derive"] }
schemars = "0.8"

[dependencies.near-sdk]
git = "https://github.com/near/near-sdk-rs.git"
rev = "97a0d01cafe1cd7dceb2774f5840052e577edf40"
features = ["abi", "unstable"]

[workspace]
members = []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
near-sdk = { version = "4.1.0-pre.1", default-features = false, features = ["abi"] }
serde = { version = "1", features = ["derive"] }
schemars = "0.8"

[dependencies.near-sdk]
git = "https://github.com/near/near-sdk-rs.git"
rev = "97a0d01cafe1cd7dceb2774f5840052e577edf40"
default-features = false
features = ["abi"]

[workspace]
members = []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ serde = { version = "1", features = ["derive"] }
schemars = "0.8"

[patch.crates-io]
near-sdk = { git = "https://github.com/near/near-sdk-rs.git", rev = "aa151597033872d44d5639933540ce6f1eca505a" }
near-sdk = { git = "https://github.com/near/near-sdk-rs.git", rev = "97a0d01cafe1cd7dceb2774f5840052e577edf40" }

[workspace]
members = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ crate-type = ["cdylib"]
serde = { version = "1", features = ["derive"] }
schemars = "0.8"

[target.'cfg(windows)'.dependencies]
near-sdk = { version = "4.1.0-pre.1", features = ["abi"] }
[target.'cfg(windows)'.dependencies.near-sdk]
git = "https://github.com/near/near-sdk-rs.git"
rev = "97a0d01cafe1cd7dceb2774f5840052e577edf40"
features = ["abi"]

[target.'cfg(unix)'.dependencies]
near-sdk = { version = "4.1.0-pre.1", features = ["abi"] }
[target.'cfg(unix)'.dependencies.near-sdk]
git = "https://github.com/near/near-sdk-rs.git"
rev = "97a0d01cafe1cd7dceb2774f5840052e577edf40"
features = ["abi"]

[workspace]
members = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
near = { version = "4.1.0-pre.1", package = "near-sdk", features = ["abi"] }
serde = { version = "1", features = ["derive"] }
schemars = "0.8"

[dependencies.near]
package = "near-sdk"
git = "https://github.com/near/near-sdk-rs.git"
rev = "97a0d01cafe1cd7dceb2774f5840052e577edf40"
features = ["abi"]

[workspace]
members = []

Expand Down
38 changes: 19 additions & 19 deletions integration-tests/tests/cargo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn clone_git_repo(version: &str) -> anyhow::Result<TempDir> {
#[test]
#[named]
fn test_dependency_local_path() -> anyhow::Result<()> {
let near_sdk_dir = clone_git_repo("aa151597033872d44d5639933540ce6f1eca505a")?;
let near_sdk_dir = clone_git_repo("97a0d01cafe1cd7dceb2774f5840052e577edf40")?;
let near_sdk_dep_path = near_sdk_dir.path().join("near-sdk");

// near-sdk = { path = "::path::", features = ["abi"] }
Expand All @@ -30,8 +30,8 @@ fn test_dependency_local_path() -> anyhow::Result<()> {
pub fn foo(&self, a: bool, b: u32) {}
};

assert_eq!(abi_root.abi.functions.len(), 1);
let function = &abi_root.abi.functions[0];
assert_eq!(abi_root.body.functions.len(), 1);
let function = &abi_root.body.functions[0];
assert_eq!(function.params.len(), 2);

Ok(())
Expand All @@ -40,7 +40,7 @@ fn test_dependency_local_path() -> anyhow::Result<()> {
#[test]
#[named]
fn test_dependency_local_path_with_version() -> anyhow::Result<()> {
let near_sdk_dir = clone_git_repo("aa151597033872d44d5639933540ce6f1eca505a")?;
let near_sdk_dir = clone_git_repo("97a0d01cafe1cd7dceb2774f5840052e577edf40")?;
let near_sdk_dep_path = near_sdk_dir.path().join("near-sdk");

// near-sdk = { path = "::path::", version = "4.1.0-pre.1", features = ["abi"] }
Expand All @@ -51,8 +51,8 @@ fn test_dependency_local_path_with_version() -> anyhow::Result<()> {
pub fn foo(&self, a: bool, b: u32) {}
};

assert_eq!(abi_root.abi.functions.len(), 1);
let function = &abi_root.abi.functions[0];
assert_eq!(abi_root.body.functions.len(), 1);
let function = &abi_root.body.functions[0];
assert_eq!(function.params.len(), 2);

Ok(())
Expand All @@ -70,8 +70,8 @@ fn test_dependency_explicit() -> anyhow::Result<()> {
pub fn foo(&self, a: bool, b: u32) {}
};

assert_eq!(abi_root.abi.functions.len(), 1);
let function = &abi_root.abi.functions[0];
assert_eq!(abi_root.body.functions.len(), 1);
let function = &abi_root.body.functions[0];
assert_eq!(function.params.len(), 2);

Ok(())
Expand All @@ -87,8 +87,8 @@ fn test_dependency_no_default_features() -> anyhow::Result<()> {
pub fn foo(&self, a: bool, b: u32) {}
};

assert_eq!(abi_root.abi.functions.len(), 1);
let function = &abi_root.abi.functions[0];
assert_eq!(abi_root.body.functions.len(), 1);
let function = &abi_root.body.functions[0];
assert_eq!(function.params.len(), 2);

Ok(())
Expand All @@ -104,8 +104,8 @@ fn test_dependency_multiple_features() -> anyhow::Result<()> {
pub fn foo(&self, a: bool, b: u32) {}
};

assert_eq!(abi_root.abi.functions.len(), 1);
let function = &abi_root.abi.functions[0];
assert_eq!(abi_root.body.functions.len(), 1);
let function = &abi_root.body.functions[0];
assert_eq!(function.params.len(), 2);

Ok(())
Expand All @@ -127,8 +127,8 @@ fn test_dependency_platform_specific() -> anyhow::Result<()> {
pub fn foo(&self, a: bool, b: u32) {}
};

assert_eq!(abi_root.abi.functions.len(), 1);
let function = &abi_root.abi.functions[0];
assert_eq!(abi_root.body.functions.len(), 1);
let function = &abi_root.body.functions[0];
assert_eq!(function.params.len(), 2);

Ok(())
Expand Down Expand Up @@ -156,8 +156,8 @@ fn test_dependency_renamed() -> anyhow::Result<()> {
}
};

assert_eq!(abi_root.abi.functions.len(), 1);
let function = &abi_root.abi.functions[0];
assert_eq!(abi_root.body.functions.len(), 1);
let function = &abi_root.body.functions[0];
assert_eq!(function.params.len(), 2);

Ok(())
Expand All @@ -172,15 +172,15 @@ fn test_dependency_patch() -> anyhow::Result<()> {
// near-sdk = { version = "4.1.0-pre.1", features = ["abi"] }
//
// [patch.crates-io]
// near-sdk = { git = "https://github.com/near/near-sdk-rs.git", rev = "aa151597033872d44d5639933540ce6f1eca505a" }
// near-sdk = { git = "https://github.com/near/near-sdk-rs.git", rev = "97a0d01cafe1cd7dceb2774f5840052e577edf40" }
let abi_root = generate_abi_fn! {
with Cargo "/templates/sdk-dependency/_Cargo_patch.toml";

pub fn foo(&self, a: bool, b: u32) {}
};

assert_eq!(abi_root.abi.functions.len(), 1);
let function = &abi_root.abi.functions[0];
assert_eq!(abi_root.body.functions.len(), 1);
let function = &abi_root.body.functions[0];
assert_eq!(function.params.len(), 2);

Ok(())
Expand Down
Loading