-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1198 from multiversx/template-tag
Template tags, refactor, improved tests
- Loading branch information
Showing
23 changed files
with
468 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
pull_request: | ||
|
||
permissions: | ||
checks: write | ||
pull-requests: write | ||
|
||
jobs: | ||
template_test: | ||
name: Template tool test - current (unreleased) templates | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions-rs/toolchain@v1 | ||
with: | ||
default: true | ||
toolchain: nightly-2023-08-08 | ||
- name: Run template tool test | ||
run: | | ||
cd framework/meta | ||
cargo test --features template-test-current |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
mod contract_creator; | ||
mod contract_creator_target; | ||
mod repo_source; | ||
mod repo_temp_download; | ||
mod repo_version; | ||
mod template_adjuster; | ||
mod template_download; | ||
mod template_list; | ||
mod template_metadata; | ||
mod template_source; | ||
|
||
pub use repo_temp_download::{RepoSource, RepoTempDownload}; | ||
pub use contract_creator::{create_contract, ContractCreator}; | ||
pub use contract_creator_target::ContractCreatorTarget; | ||
pub use repo_source::RepoSource; | ||
pub use repo_temp_download::RepoTempDownload; | ||
pub use repo_version::RepoVersion; | ||
pub use template_adjuster::TemplateAdjuster; | ||
pub use template_download::{template_download, TemplateDownloader}; | ||
pub use template_list::{print_template_names, template_names_from_repo}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use crate::{ | ||
cli_args::TemplateArgs, | ||
version_history::{validate_template_tag, LAST_TEMPLATE_VERSION}, | ||
}; | ||
|
||
use super::{ | ||
template_source::{template_sources, TemplateSource}, | ||
ContractCreatorTarget, RepoSource, RepoVersion, TemplateAdjuster, | ||
}; | ||
|
||
/// Creates a new contract on disk, from a template, given a name. | ||
pub async fn create_contract(args: &TemplateArgs) { | ||
let version = get_repo_version(&args.tag); | ||
let repo_temp_download = RepoSource::download_from_github(version, std::env::temp_dir()).await; | ||
let target = target_from_args(args); | ||
|
||
let creator = ContractCreator::new(&repo_temp_download, args.template.clone(), target, false); | ||
|
||
creator.create_contract(); | ||
} | ||
|
||
fn target_from_args(args: &TemplateArgs) -> ContractCreatorTarget { | ||
let new_name = args.name.clone().unwrap_or_else(|| args.template.clone()); | ||
let target_path = args.path.clone().unwrap_or_default(); | ||
ContractCreatorTarget { | ||
target_path, | ||
new_name, | ||
} | ||
} | ||
|
||
pub(crate) fn get_repo_version(args_tag: &Option<String>) -> RepoVersion { | ||
if let Some(tag) = args_tag { | ||
assert!(validate_template_tag(tag), "invalid template tag"); | ||
RepoVersion::Tag(tag.clone()) | ||
} else { | ||
RepoVersion::Tag(LAST_TEMPLATE_VERSION.to_string()) | ||
} | ||
} | ||
|
||
/// Coordinates the creation of a new contract from a template. | ||
pub struct ContractCreator<'a> { | ||
pub repo_source: &'a RepoSource, | ||
pub template_source: TemplateSource<'a>, | ||
pub target: ContractCreatorTarget, | ||
pub adjuster: TemplateAdjuster, | ||
} | ||
|
||
impl<'a> ContractCreator<'a> { | ||
pub fn new( | ||
repo_source: &'a RepoSource, | ||
template_name: String, | ||
target: ContractCreatorTarget, | ||
keep_paths: bool, | ||
) -> Self { | ||
let template_sources = template_sources(repo_source); | ||
let template_source = template_sources | ||
.into_iter() | ||
.find(|source| source.metadata.name == template_name) | ||
.unwrap_or_else(|| panic!("Unknown template {template_name}")); | ||
|
||
let metadata = template_source.metadata.clone(); | ||
ContractCreator { | ||
repo_source, | ||
template_source, | ||
target: target.clone(), | ||
adjuster: TemplateAdjuster { | ||
metadata, | ||
target, | ||
keep_paths, | ||
}, | ||
} | ||
} | ||
|
||
pub fn create_contract(&self) { | ||
self.copy_template(); | ||
self.update_dependencies(); | ||
self.rename_template(); | ||
} | ||
|
||
pub fn copy_template(&self) { | ||
self.template_source | ||
.copy_template(self.target.contract_dir()); | ||
} | ||
|
||
pub fn update_dependencies(&self) { | ||
self.adjuster.update_dependencies(); | ||
} | ||
|
||
pub fn rename_template(&self) { | ||
self.adjuster.rename_template_to(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use std::path::PathBuf; | ||
|
||
#[derive(Clone)] | ||
pub struct ContractCreatorTarget { | ||
pub target_path: PathBuf, | ||
pub new_name: String, | ||
} | ||
|
||
impl ContractCreatorTarget { | ||
pub fn contract_dir(&self) -> PathBuf { | ||
self.target_path.join(&self.new_name) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use std::{ | ||
fs, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
use super::{RepoTempDownload, RepoVersion}; | ||
|
||
pub enum RepoSource { | ||
Downloaded(RepoTempDownload), | ||
LocalPath(PathBuf), | ||
} | ||
|
||
impl RepoSource { | ||
pub async fn download_from_github(version: RepoVersion, temp_dir_path: PathBuf) -> Self { | ||
fs::create_dir_all(&temp_dir_path).unwrap(); | ||
RepoSource::Downloaded(RepoTempDownload::download_from_github(version, temp_dir_path).await) | ||
} | ||
|
||
pub fn from_local_path(repo_local_path: impl AsRef<Path>) -> Self { | ||
RepoSource::LocalPath(repo_local_path.as_ref().to_path_buf()) | ||
} | ||
|
||
pub fn repo_path(&self) -> PathBuf { | ||
match self { | ||
RepoSource::Downloaded(repo_temp_download) => { | ||
repo_temp_download.repository_temp_dir_path() | ||
}, | ||
RepoSource::LocalPath(local_path) => local_path.clone(), | ||
} | ||
} | ||
} |
Oops, something went wrong.