Skip to content

Commit

Permalink
Auto update extension.json (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
anchpop authored Jul 5, 2024
1 parent f75778c commit 3dee446
Show file tree
Hide file tree
Showing 10 changed files with 627 additions and 407 deletions.
891 changes: 498 additions & 393 deletions Cargo.lock

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license = "Apache-2.0"
repository = "https://github.com/dfinity/dfx-extensions"

[workspace.dependencies]
dfx-core = { git = "https://github.com/dfinity/sdk", rev = "cb67f24b7acb9aaa4609f38a34527a9436fb6b72" }
dfx-core = { git = "https://github.com/dfinity/sdk", rev = "d8c1ede88d1978653d2e7d7005095fe38499e023" }
dfx-extensions-utils.path = "./extensions-utils"

anyhow = "^1"
Expand All @@ -21,8 +21,8 @@ flate2 = { version = "1.0.25", default-features = false, features = [
] }
fn-error-context = "0.2.1"
futures-util = "0.3.28"
ic-agent = { git = "https://github.com/dfinity/agent-rs", rev = "8273d321e9a09fd8373bd4e38b0676ec6ad9c260" }
ic-utils = { git = "https://github.com/dfinity/agent-rs", rev = "8273d321e9a09fd8373bd4e38b0676ec6ad9c260" }
ic-agent = { git = "https://github.com/dfinity/agent-rs", rev = "be929fd7967249c879f48f2f494cbfc5805a7d98" }
ic-utils = { git = "https://github.com/dfinity/agent-rs", rev = "be929fd7967249c879f48f2f494cbfc5805a7d98" }
reqwest = { version = "^0.11.22", default-features = false, features = [
"blocking",
"json",
Expand All @@ -34,10 +34,11 @@ slog = "^2.7.0"
tempfile = "3.5.0"
tokio = { version = "^1.36.0", features = ["rt-multi-thread"] }
url = "^2.4.1"
ic-icp-index = { git = "https://github.com/dfinity/ic", rev = "f174aa82788f2dfc39b1cdb81fd19d691e77ac0b" }
ic-icrc1-index-ng = { git = "https://github.com/dfinity/ic", rev = "f174aa82788f2dfc39b1cdb81fd19d691e77ac0b" }
ic-icrc1-ledger = { git = "https://github.com/dfinity/ic", rev = "f174aa82788f2dfc39b1cdb81fd19d691e77ac0b" }
ic-sns-cli = { git = "https://github.com/dfinity/ic", rev = "f174aa82788f2dfc39b1cdb81fd19d691e77ac0b" }
ic-icp-index = { git = "https://github.com/dfinity/ic", rev = "cc4ea40e8571b80043013e4e74bd2b89844230c7" }
ic-icrc1-index-ng = { git = "https://github.com/dfinity/ic", rev = "cc4ea40e8571b80043013e4e74bd2b89844230c7" }
ic-icrc1-ledger = { git = "https://github.com/dfinity/ic", rev = "cc4ea40e8571b80043013e4e74bd2b89844230c7" }
ic-sns-cli = { git = "https://github.com/dfinity/ic", rev = "cc4ea40e8571b80043013e4e74bd2b89844230c7" }
serde_json = "1.0.79"

# Config for 'cargo dist'
[workspace.metadata.dist]
Expand Down
11 changes: 7 additions & 4 deletions extensions-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ reqwest.workspace = true
dfx-core.workspace = true

anyhow.workspace = true
backoff = { version = "0.4.0", features = [ "futures", "tokio" ] }
flate2 = { version = "1.0.25", default-features = false, features = ["zlib-ng"] }
backoff = { version = "0.4.0", features = ["futures", "tokio"] }
flate2 = { version = "1.0.25", default-features = false, features = [
"zlib-ng",
] }
fn-error-context.workspace = true
futures-util.workspace = true
hyper-rustls = { version = "0.23.0", features = ["webpki-roots", "http2"] }
reqwest.workspace = true
rustls = "0.20.4"
semver = "1.0.17"
serde = "1.0.159"
serde_json = "1.0.95"
serde.workspace = true
serde_json.workspace = true
slog-async = "2.4.0"
slog-term = "2.9.0"
slog.workspace = true
Expand All @@ -35,3 +37,4 @@ thiserror = "1.0.40"
tokio.workspace = true
url.workspace = true
candid.workspace = true
clap.workspace = true
1 change: 1 addition & 0 deletions extensions-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pub mod dependencies;
mod error;
mod logger;
pub mod manifest;
mod project;

pub use dependencies::{
Expand Down
98 changes: 98 additions & 0 deletions extensions-utils/src/manifest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use anyhow::{bail, Context};
use dfx_core::extension::manifest::*;
use extension::*;
use std::{collections::BTreeMap, path::Path};

fn generate_extension_manifest(
cmd: &clap::Command,
old_manifest: ExtensionManifest,
) -> ExtensionManifest {
ExtensionManifest {
name: cmd.get_name().to_string(),
summary: cmd.get_about().map(|a| a.to_string()).unwrap_or_default(),
description: cmd.get_long_about().map(|a| a.to_string()),
subcommands: Some(generate_subcommands(cmd)),
..old_manifest
}
}

fn generate_subcommands(cmd: &clap::Command) -> ExtensionSubcommandsOpts {
let mut subcommands = BTreeMap::new();

for subcmd in cmd.get_subcommands() {
subcommands.insert(
subcmd.get_name().to_string(),
ExtensionSubcommandOpts {
about: subcmd.get_about().map(|a| a.to_string()),
args: Some(generate_args(subcmd)),
subcommands: if subcmd.has_subcommands() {
Some(generate_subcommands(subcmd))
} else {
None
},
},
);
}

ExtensionSubcommandsOpts(subcommands)
}

fn generate_args(cmd: &clap::Command) -> BTreeMap<String, ExtensionSubcommandArgOpts> {
let mut args = BTreeMap::new();

for arg in cmd.get_arguments() {
args.insert(
arg.get_id().to_string(),
#[allow(deprecated)]
ExtensionSubcommandArgOpts {
about: arg.get_help().map(|h| h.to_string()),
long: arg.get_long().map(|l| l.to_string()),
short: arg.get_short(),
multiple: false, // Deprecated, set to false
values: match arg.get_num_args() {
None => ArgNumberOfValues::Number(if arg.get_action().takes_values() {
1
} else {
0
}),
Some(value_range) => {
let min = value_range.min_values();
let max = value_range.max_values();
if min == 0 && max == usize::MAX {
ArgNumberOfValues::Unlimited
} else if min == max {
ArgNumberOfValues::Number(min as usize)
} else {
// max is inclusive, but ArgNumberOfValues::Range wants an exclusive range
ArgNumberOfValues::Range(min..(max.saturating_add(1)))
}
}
},
},
);
}

args
}

pub fn verify_extension_manifest<Command: clap::CommandFactory>(path: &Path) -> anyhow::Result<()> {
// read the mainfest from the path and deserizlize it
let current_manifest_string = std::fs::read_to_string(path).context(format!(
"Could not read the extension manifest at {}",
path.display(),
))?;
let current_manifest: ExtensionManifest = serde_json::from_str(&current_manifest_string)?;

let command_info = Command::command();
let updated_manifest = generate_extension_manifest(&command_info, current_manifest);

let updated_manifest_string = serde_json::to_string_pretty(&updated_manifest)?;
// write the json to the path
if updated_manifest_string != current_manifest_string {
std::fs::write(path, updated_manifest_string)?;
bail!(
"Extension manifest at {} was out of date. This has been fixed. Please commit the changes.", path.display()
);
}
Ok(())
}
2 changes: 1 addition & 1 deletion extensions/nns/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::env;
use std::path::PathBuf;

const REPLICA_REV: &str = "f174aa82788f2dfc39b1cdb81fd19d691e77ac0b";
const REPLICA_REV: &str = "cc4ea40e8571b80043013e4e74bd2b89844230c7";

const BINARY_DEPENDENCIES: &[(&str, &str)] = &[
// (downloaded binary name, renamed binary name)
Expand Down
1 change: 1 addition & 0 deletions extensions/sns/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ build = "build.rs"
dfx-extensions-utils.workspace = true

[dependencies]
serde_json.workspace = true
dfx-core.workspace = true
dfx-extensions-utils.workspace = true
ic-sns-cli.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion extensions/sns/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::env;
use std::path::PathBuf;

const REPLICA_REV: &str = "f174aa82788f2dfc39b1cdb81fd19d691e77ac0b";
const REPLICA_REV: &str = "cc4ea40e8571b80043013e4e74bd2b89844230c7";

const BINARY_DEPENDENCIES: &[(&str, &str)] = &[
// (downloaded binary name, renamed binary name)
Expand Down
2 changes: 1 addition & 1 deletion extensions/sns/e2e/assets/sns/valid/sns_init.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Swap:
- CH

VestingSchedule:
events: 83
events: 5
interval: 17 days

start_time: 12:00 UTC
Expand Down
11 changes: 11 additions & 0 deletions extensions/sns/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,14 @@ fn main() -> anyhow::Result<()> {
}?;
Ok(())
}

#[test]
fn verify_extension_manifest() {
let project_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
println!("Project root: {:?}", project_root);

let dest_path = project_root.join("extension.json");

dfx_extensions_utils::manifest::verify_extension_manifest::<SubCommand>(dest_path.as_path())
.unwrap();
}

0 comments on commit 3dee446

Please sign in to comment.