Skip to content

Commit

Permalink
add sylvia config and prompts for selecting it
Browse files Browse the repository at this point in the history
  • Loading branch information
iboss-ptk committed Nov 16, 2023
1 parent 93b5a57 commit 8dda811
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ contract_dir = "whatever""#;
contract_name: "counter".to_string(),
target_dir: None,
version: None,
template: Some("classic".into()),
},
})
.unwrap();
Expand Down
20 changes: 17 additions & 3 deletions packages/cli/src/modules/wasm/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use data_doc_derive::GetDataDocs;
use serde::Deserialize;
use serde::Serialize;
Expand All @@ -8,18 +10,30 @@ pub struct WasmConfig {
pub contract_dir: String,

/// Reference to contract template repository
pub template_repo: String,
pub template_repos: HashMap<String, String>,

/// Version of rust-optimizer
pub optimizer_version: String,
}

impl Default for WasmConfig {
fn default() -> Self {
let mut template_repo = HashMap::new();

template_repo.insert(
"classic".to_string(),
"https://github.com/osmosis-labs/cw-minimal-template".to_string(),
);

template_repo.insert(
"sylvia".to_string(),
"https://github.com/osmosis-labs/cw-sylvia-template".to_string(),
);

Self {
contract_dir: "contracts".to_string(),
template_repo: "https://github.com/osmosis-labs/cw-minimal-template".to_string(),
optimizer_version: "0.12.8".to_string(),
template_repos: template_repo,
optimizer_version: "0.14.0".to_string(),
}
}
}
32 changes: 29 additions & 3 deletions packages/cli/src/modules/wasm/entrypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ pub enum WasmCmd {
/// Template's version, using main branch if not specified
#[clap(short, long)]
version: Option<String>,

/// Template name, prompt for template if not specified
#[clap(short, long)]
template: Option<String>,
},
/// Build .wasm for storing contract code on the blockchain
Build {
Expand Down Expand Up @@ -367,7 +371,14 @@ impl<'a> Module<'a, WasmConfig, WasmCmd, anyhow::Error> for WasmModule {
contract_name: name,
target_dir, // TODO: Rremove this
version,
} => ops::new(&ctx, name, version.to_owned(), target_dir.to_owned()),
template,
} => ops::new(
&ctx,
name,
version.to_owned(),
target_dir.to_owned(),
template.to_owned(),
),
cmd @ WasmCmd::Build { .. } => build(ctx, cmd),
cmd @ WasmCmd::StoreCode { .. } => store_code(ctx, cmd).map(|_| ()),
cmd @ WasmCmd::UpdateAdmin { .. } => update_admin(ctx, cmd).map(|_| ()),
Expand Down Expand Up @@ -816,7 +827,7 @@ pub(crate) fn execute<'a>(

#[cfg(test)]
mod tests {
use std::{env, fs, path::Path};
use std::{collections::HashMap, env, fs, path::Path};

use assert_fs::{prelude::*, TempDir};
use cargo_toml::{Dependency, DependencyDetail, Manifest};
Expand Down Expand Up @@ -845,6 +856,7 @@ mod tests {
contract_name: "counter-1".to_string(),
version: None,
target_dir: None,
template: Some("classic".into()),
},
)
.unwrap();
Expand All @@ -860,6 +872,7 @@ mod tests {
contract_name: "counter-2".to_string(),
target_dir: None,
version: None,
template: Some("classic".into()),
},
)
.unwrap();
Expand All @@ -886,6 +899,7 @@ mod tests {
contract_name: "counter-1".to_string(),
target_dir: None,
version: None,
template: Some("classic".into()),
},
)
.unwrap();
Expand All @@ -898,6 +912,7 @@ mod tests {
contract_name: "counter-2".to_string(),
target_dir: None,
version: None,
template: Some("classic".into()),
},
)
.unwrap();
Expand All @@ -921,8 +936,15 @@ mod tests {
struct WasmContext {}
impl<'a> Context<'a, WasmConfig> for WasmContext {
fn config(&self) -> Result<WasmConfig> {
let mut template_repos = HashMap::new();

template_repos.insert(
"classic".to_string(),
"https://github.com/CosmWasm/cw-template.git".to_string(),
);

Ok(WasmConfig {
template_repo: "https://github.com/CosmWasm/cw-template.git".to_string(),
template_repos,
..Default::default()
})
}
Expand All @@ -934,6 +956,7 @@ mod tests {
contract_name: "counter-1".to_string(),
target_dir: None,
version: Some("0.16".into()),
template: Some("classic".into()),
},
)
.unwrap();
Expand All @@ -947,6 +970,7 @@ mod tests {
contract_name: "counter-2".to_string(),
target_dir: None,
version: Some("0.16".into()),
template: Some("classic".into()),
},
)
.unwrap();
Expand Down Expand Up @@ -975,6 +999,7 @@ mod tests {
contract_name: "counter-1".to_string(),
target_dir: Some("custom-path".into()),
version: None,
template: Some("classic".into()),
},
)
.unwrap();
Expand All @@ -987,6 +1012,7 @@ mod tests {
contract_name: "counter-2".to_string(),
target_dir: Some("custom-path".into()),
version: None,
template: Some("classic".into()),
},
)
.unwrap();
Expand Down
38 changes: 35 additions & 3 deletions packages/cli/src/modules/wasm/ops/new.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,53 @@
use crate::framework::Context;
use crate::modules::wasm::config::WasmConfig;
use crate::support::template::Template;
use anyhow::Result;
use anyhow::{Context as _, Result};
use console::{style, Emoji};
use dialoguer::{theme::ColorfulTheme, Select};
use std::path::PathBuf;

pub static SHRUG: Emoji<'_, '_> = Emoji("🤷 ", "");

pub fn new<'a, Ctx: Context<'a, WasmConfig>>(
ctx: &Ctx,
name: &str,
version: Option<String>,
target_dir: Option<PathBuf>,
template: Option<String>,
) -> Result<()> {
let cfg = ctx.config()?;
let repo = &cfg.template_repo;
let templates = cfg.template_repos.keys().collect::<Vec<_>>();
let version = version.unwrap_or_else(|| "main".to_string());
let target_dir =
target_dir.unwrap_or(ctx.root()?.join(PathBuf::from(cfg.contract_dir.as_str())));

let cw_template = Template::new(name.to_string(), repo.to_owned(), version, None, target_dir);
let repo = if let Some(template) = template {
cfg.template_repos
.get(&template)
.ok_or_else(|| anyhow::anyhow!("Unable to get template repository"))?
} else {
let template_idx = Select::with_theme(&ColorfulTheme::default())
.with_prompt(format!(
"{} {}",
SHRUG,
style("Which template would you like to use?").bold()
))
.items(&templates)
.default(0)
.interact()
.with_context(|| "Canceled")?;

cfg.template_repos
.get(templates[template_idx].as_str())
.ok_or_else(|| anyhow::anyhow!("Unable to get template repository"))?
};

let cw_template = Template::new(
name.to_string(),
repo.to_string(),
version,
None,
target_dir,
);
cw_template.generate()
}

0 comments on commit 8dda811

Please sign in to comment.