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

feat(new parachain): show polkadot version #89

Merged
merged 8 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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: 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 @@ -24,6 +24,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
7 changes: 5 additions & 2 deletions src/commands/new/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,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 @@ -69,7 +69,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 @@ -84,6 +84,9 @@ impl NewParachainCommand {
}
}
spinner.stop("Generation complete");
if let Some(tag) = tag {
log::info(format!("Version: {}", tag))?;
}
outro(format!("cd into \"{}\" and enjoy hacking! 🚀", &self.name))?;
Ok(())
}
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)?;
weezy20 marked this conversation as resolved.
Show resolved Hide resolved

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: anyhow::Result<String> = 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> {
AlexD10S marked this conversation as resolved.
Show resolved Hide resolved
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
Loading