-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lochlan Wansbrough
committed
Oct 7, 2024
1 parent
0bbfe26
commit 616aa27
Showing
8 changed files
with
46 additions
and
53 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
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,54 @@ | ||
use std::{env, fs, path::Path}; | ||
use std::{fs, io::{Read, Write}, borrow::Cow, env, path::{Path, PathBuf}}; | ||
use rust_embed::Embed; | ||
|
||
use crate::Result; | ||
|
||
use super::common::template_files_recursively; | ||
use liquid::Object; | ||
|
||
#[derive(Embed)] | ||
#[folder = "src/templates"] | ||
struct Templates; | ||
|
||
pub async fn game(identifier: &String, name: &String, template: &Option<String>) -> Result<()> { | ||
let template_key = template.clone().unwrap_or("hello-world".to_owned()); | ||
|
||
let template_path = format!("src/templates/{template_key}"); | ||
let paths = Templates::iter() | ||
.filter(|p| p.starts_with(&template_key)) | ||
.collect::<Vec<_>>(); | ||
|
||
let template_root_path = env::current_exe().ok().unwrap().parent().unwrap().parent().unwrap().parent().unwrap().join(&template_path); | ||
let project_root_path = env::current_dir().ok().unwrap(); | ||
|
||
let globals = liquid::object!({ | ||
"identifier": identifier, | ||
"name": name | ||
}); | ||
|
||
template_files_recursively(project_root_path.as_path(), &template_root_path, &template_root_path, &globals)?; | ||
template_files(&template_key, project_root_path.as_path(), paths, &globals)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
|
||
pub fn template_files(template_key: &str, project_root: &Path, paths: Vec<Cow<'static, str>>, globals: &Object) -> crate::Result<()> { | ||
for path in paths { | ||
let contents = Templates::get(path.as_ref()).unwrap(); | ||
let relative_path = path.strip_prefix(&template_key).unwrap(); | ||
let destination_path = project_root.join(PathBuf::from(relative_path)); | ||
let mut file = fs::OpenOptions::new() | ||
.create(true) | ||
.read(true) | ||
.write(true) | ||
.open(&destination_path.clone())?; | ||
|
||
let template = liquid::ParserBuilder::with_stdlib() | ||
.build().unwrap() | ||
.parse(&std::str::from_utf8(&contents.data).unwrap()) | ||
.unwrap(); | ||
|
||
let contents = template.render(&globals).unwrap(); | ||
|
||
file.write_all(contents.as_bytes())?; | ||
} | ||
|
||
Ok(()) | ||
} |