Skip to content

Commit

Permalink
fixes: review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
weezy20 committed Apr 2, 2024
1 parent 3906821 commit 307b4ee
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/commands/new/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ impl NewParachainCommand {
}
}
spinner.stop("Generation complete");
log::info(format!("Version: {}", tag))?;
if let Some(tag) = tag {
log::info(format!("Version: {}", tag))?;
}
outro(format!("cd into \"{}\" and enjoy hacking! 🚀", &self.name))?;
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions src/engines/parachain_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn instantiate_template_dir(
template: &Template,
target: &Path,
config: Config,
) -> Result<String> {
) -> Result<Option<String>> {
sanitize(target)?;
use Template::*;
let url = match template {
Expand All @@ -39,7 +39,7 @@ pub fn instantiate_template_dir(
Ok(tag)
}

pub fn instantiate_base_template(target: &Path, config: Config) -> Result<String> {
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();
let tag = clone_and_degit("https://github.com/r0gue-io/base-parachain", source)?;
Expand Down
24 changes: 11 additions & 13 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,32 +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<String> {
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_tag(&repo);
let release = fetch_latest_tag(&repo);

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

/// Fetch the latest release from a repository
fn fetch_tag(repo: &Repository) -> String {
let mut release = String::new();

let version_reg = Regex::new(r"v\d+\.\d+\.\d+").unwrap();

for tag in repo.tag_names(None).unwrap().iter() {
let tag = tag.unwrap();
if version_reg.is_match(tag) {
if release.is_empty() || *tag > *release {
release = tag.to_string();
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());
}
}
}
release
None
}

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

0 comments on commit 307b4ee

Please sign in to comment.