Skip to content

Commit

Permalink
Merge branch 'main' into w/9/cache
Browse files Browse the repository at this point in the history
  • Loading branch information
weezy20 committed Apr 5, 2024
2 parents cb9b13b + 3bbe617 commit 548b7ad
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tempfile = "3.8"
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
url = { version = "2.5", optional = true }
walkdir = "2.4"
regex="1.5.4"

# contracts
contract-build = { version = "4.0.0-rc.3", optional = true }
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,5 @@ pop up parachain -f ./tests/zombienet.toml -p https://github.com/r0gue-io/pop-no

Pop CLI would not be possible without these awesome crates!

- Local network deployment powered by [zombienet-sdk](https://github.com/paritytech/zombienet-sdk)
- Local network deployment powered by [zombienet-sdk](https://github.com/paritytech/zombienet-sdk)
- [cargo contract](https://github.com/paritytech/cargo-contract) a setup and deployment tool for developing Wasm based smart contracts via ink!
7 changes: 5 additions & 2 deletions src/commands/new/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use clap::{Args, Parser};
use std::{fs, path::Path};
use strum_macros::{Display, EnumString};

use cliclack::{clear_screen, confirm, intro, outro, outro_cancel, set_theme};
use cliclack::{clear_screen, confirm, intro, log, outro, outro_cancel, set_theme};

#[derive(Clone, Parser, Debug, Display, EnumString, PartialEq)]
pub enum Template {
Expand Down Expand Up @@ -70,7 +70,7 @@ impl NewParachainCommand {
}
let mut spinner = cliclack::spinner();
spinner.start("Generating parachain...");
instantiate_template_dir(
let tag = instantiate_template_dir(
&self.template,
destination_path,
Config {
Expand All @@ -85,6 +85,9 @@ impl NewParachainCommand {
}
}
spinner.stop("Generation complete");
if let Some(tag) = tag {
log::info(format!("Version: {}", tag))?;
}
let pop_db = PopDb::open_or_init();
pop_db.set_parachain_path(destination_path);
outro(format!("cd into \"{}\" and enjoy hacking! 🚀", &self.name))?;
Expand Down
5 changes: 3 additions & 2 deletions src/engines/contract_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ pub fn create_smart_contract(name: String, target: &Option<PathBuf>) -> anyhow::
}

pub fn build_smart_contract(path: &Option<PathBuf>) -> anyhow::Result<()> {
// If the user specifies a path (which is not the current directory), it will have to manually add a Cargo.toml file. If not provided, pop-cli will ask the user for a specific path. or
// ask to the user the specific path (Like cargo-contract does)
// If the user specifies a path (which is not the current directory), it will have to manually
// add a Cargo.toml file. If not provided, pop-cli will ask the user for a specific path. or ask
// to the user the specific path (Like cargo-contract does)
let manifest_path;
if path.is_some() {
let full_path: PathBuf =
Expand Down
18 changes: 11 additions & 7 deletions src/engines/parachain_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ pub struct Config {
}

/// Creates a new template at `target` dir
pub fn instantiate_template_dir(template: &Template, target: &Path, config: Config) -> Result<()> {
pub fn instantiate_template_dir(
template: &Template,
target: &Path,
config: Config,
) -> Result<Option<String>> {
sanitize(target)?;
use Template::*;
let url = match template {
Expand All @@ -30,15 +34,15 @@ pub fn instantiate_template_dir(template: &Template, target: &Path, config: Conf
return instantiate_base_template(target, config);
},
};
clone_and_degit(url, target)?;
let tag = clone_and_degit(url, target)?;
Repository::init(target)?;
Ok(())
Ok(tag)
}

pub fn instantiate_base_template(target: &Path, config: Config) -> Result<()> {
pub fn instantiate_base_template(target: &Path, config: Config) -> Result<Option<String>> {
let temp_dir = ::tempfile::TempDir::new_in(std::env::temp_dir())?;
let source = temp_dir.path();
clone_and_degit("https://github.com/r0gue-io/base-parachain", source)?;
let tag = clone_and_degit("https://github.com/r0gue-io/base-parachain", source)?;

for entry in WalkDir::new(&source) {
let entry = entry?;
Expand Down Expand Up @@ -66,7 +70,7 @@ pub fn instantiate_base_template(target: &Path, config: Config) -> Result<()> {
let network = Network { node: "parachain-template-node".into() };
write_to_file(&target.join("network.toml"), network.render().expect("infallible").as_ref());
Repository::init(target)?;
Ok(())
Ok(tag)
}

pub fn build_parachain(path: Option<PathBuf>) -> anyhow::Result<()> {
Expand All @@ -90,7 +94,7 @@ mod tests {
decimals: 18,
initial_endowment: "1000000".to_string(),
};
let result: anyhow::Result<()> = instantiate_base_template(temp_dir.path(), config);
let result = instantiate_base_template(temp_dir.path(), config);
assert!(result.is_ok());
Ok(temp_dir)
}
Expand Down
24 changes: 22 additions & 2 deletions src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use cliclack::{log, outro_cancel};
use git2::{IndexAddOption, Repository, ResetType};
use regex::Regex;
use std::{
env::current_dir,
fs::{self, OpenOptions},
Expand Down Expand Up @@ -43,11 +44,30 @@ pub(crate) fn write_to_file<'a>(path: &Path, contents: &'a str) {
}

/// Clone `url` into `target` and degit it
pub(crate) fn clone_and_degit(url: &str, target: &Path) -> Result<()> {
pub(crate) fn clone_and_degit(url: &str, target: &Path) -> Result<Option<String>> {
let repo = Repository::clone(url, target)?;

// fetch tags from remote
let release = fetch_latest_tag(&repo);

let git_dir = repo.path();
fs::remove_dir_all(&git_dir)?;
Ok(())
Ok(release)
}

/// Fetch the latest release from a repository
fn fetch_latest_tag(repo: &Repository) -> Option<String> {
let version_reg = Regex::new(r"v\d+\.\d+\.\d+").expect("Valid regex");
let tags = repo.tag_names(None).ok()?;
// Start from latest tags
for tag in tags.iter().rev() {
if let Some(tag) = tag {
if version_reg.is_match(tag) {
return Some(tag.to_string());
}
}
}
None
}

/// Init a new git repo on creation of a parachain
Expand Down

0 comments on commit 548b7ad

Please sign in to comment.