-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: data-driven backend project templates
- Loading branch information
1 parent
e6942eb
commit 30cd117
Showing
8 changed files
with
165 additions
and
44 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,3 +1,4 @@ | ||
pub mod cache; | ||
pub mod directories; | ||
pub mod model; | ||
pub mod project_templates; |
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,70 @@ | ||
use itertools::Itertools; | ||
use std::collections::BTreeMap; | ||
use std::io; | ||
use std::sync::OnceLock; | ||
|
||
type GetArchiveFn = fn() -> Result<tar::Archive<flate2::read::GzDecoder<&'static [u8]>>, io::Error>; | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum ResourceLocation { | ||
Bundled { get_archive_fn: GetArchiveFn }, | ||
} | ||
|
||
#[derive(Debug, Clone, Eq, PartialEq)] | ||
pub enum Category { | ||
Backend, | ||
} | ||
|
||
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)] | ||
pub struct ProjectTemplateName(pub String); | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct ProjectTemplate { | ||
pub name: ProjectTemplateName, | ||
pub display: String, | ||
pub resource_location: ResourceLocation, | ||
pub category: Category, | ||
pub sort_order: u32, | ||
} | ||
|
||
type ProjectTemplates = BTreeMap<ProjectTemplateName, ProjectTemplate>; | ||
|
||
static PROJECT_TEMPLATES: OnceLock<ProjectTemplates> = OnceLock::new(); | ||
|
||
pub fn populate(builtin_templates: Vec<ProjectTemplate>) { | ||
let templates = builtin_templates | ||
.iter() | ||
.map(|t| (t.name.clone(), t.clone())) | ||
.collect(); | ||
|
||
PROJECT_TEMPLATES.set(templates).unwrap(); | ||
} | ||
|
||
pub fn get_project_template(name: &ProjectTemplateName) -> ProjectTemplate { | ||
PROJECT_TEMPLATES.get().unwrap().get(name).cloned().unwrap() | ||
} | ||
|
||
pub fn get_sorted_templates(category: Category) -> Vec<ProjectTemplate> { | ||
PROJECT_TEMPLATES | ||
.get() | ||
.unwrap() | ||
.values() | ||
.filter(|t| t.category == category) | ||
.cloned() | ||
.sorted_by(|a, b| { | ||
a.sort_order | ||
.cmp(&b.sort_order) | ||
.then_with(|| a.display.cmp(&b.display)) | ||
}) | ||
.collect() | ||
} | ||
|
||
pub fn project_template_cli_names(category: Category) -> Vec<String> { | ||
PROJECT_TEMPLATES | ||
.get() | ||
.unwrap() | ||
.values() | ||
.filter(|t| t.category == category) | ||
.map(|t| t.name.0.clone()) | ||
.collect() | ||
} |
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,2 +1,3 @@ | ||
pub mod import; | ||
pub mod network_mappings; | ||
pub mod templates; |
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,48 @@ | ||
use crate::util::assets; | ||
use dfx_core::config::project_templates::{ | ||
Category, ProjectTemplate, ProjectTemplateName, ResourceLocation, | ||
}; | ||
|
||
pub fn builtin_templates() -> Vec<ProjectTemplate> { | ||
let motoko = ProjectTemplate { | ||
name: ProjectTemplateName("motoko".to_string()), | ||
display: "Motoko".to_string(), | ||
resource_location: ResourceLocation::Bundled { | ||
get_archive_fn: assets::new_project_motoko_files, | ||
}, | ||
category: Category::Backend, | ||
sort_order: 0, | ||
}; | ||
|
||
let rust = ProjectTemplate { | ||
name: ProjectTemplateName("rust".to_string()), | ||
display: "Rust".to_string(), | ||
resource_location: ResourceLocation::Bundled { | ||
get_archive_fn: assets::new_project_rust_files, | ||
}, | ||
category: Category::Backend, | ||
sort_order: 1, | ||
}; | ||
|
||
let azle = ProjectTemplate { | ||
name: ProjectTemplateName("azle".to_string()), | ||
display: "Typescript (Azle)".to_string(), | ||
resource_location: ResourceLocation::Bundled { | ||
get_archive_fn: assets::new_project_azle_files, | ||
}, | ||
category: Category::Backend, | ||
sort_order: 2, | ||
}; | ||
|
||
let kybra = ProjectTemplate { | ||
name: ProjectTemplateName("kybra".to_string()), | ||
display: "Python (Kybra)".to_string(), | ||
resource_location: ResourceLocation::Bundled { | ||
get_archive_fn: assets::new_project_kybra_files, | ||
}, | ||
category: Category::Backend, | ||
sort_order: 2, | ||
}; | ||
|
||
vec![motoko, rust, azle, kybra] | ||
} |
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