Skip to content

Commit

Permalink
templates wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Lochlan Wansbrough committed Oct 7, 2024
1 parent 0bbfe26 commit 616aa27
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 53 deletions.
3 changes: 1 addition & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ impl App {
Some(CliCommand::Test) => {
let input_path = Path::new("../test-game/dist/.rune/input/");
let binary = std::fs::read(input_path.join("test-game.wasm")).unwrap();
rune::runtime::test(input_path.to_path_buf(), binary);

rune::runtime::test(input_path.to_path_buf(), binary).await;
},
Some(CliCommand::Run { release }) => {
crate::commands::build::build(release).await?;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/bundle/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ fn create_icns_file(
// }

// Otherwise, read available images and pack them into a new ICNS file.
let mut family = icns::IconFamily::new();
let family = icns::IconFamily::new();

fn add_icon_to_family(
icon: image::DynamicImage,
Expand Down
2 changes: 1 addition & 1 deletion src/commands/bundle/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<()> {
fs::remove_dir_all(&msi_path)?;
}

let mut package = new_empty_package(&msi_path)?;
let package = new_empty_package(&msi_path)?;

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub async fn docs(config: &Config, mode: &Mode) -> Result<()> {
Action::Resize(w, h) => {
tui.resize(Rect::new(0, 0, w, h))?;
tui.draw(|f| {
let r = docs.draw(f, f.size());
let r = docs.draw(f, f.area());
if let Err(e) = r {
action_tx
.send(Action::Error(format!(
Expand All @@ -89,7 +89,7 @@ pub async fn docs(config: &Config, mode: &Mode) -> Result<()> {
}
Action::Render => {
tui.draw(|f| {
let r = docs.draw(f, f.size());
let r = docs.draw(f, f.area());
if let Err(e) = r {
action_tx
.send(Action::Error(format!(
Expand Down
10 changes: 5 additions & 5 deletions src/commands/docs/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl CurrentItem {
format!("{}: {}", type_def.kind.as_str(), type_def.name.clone().unwrap_or_default())
},
CurrentItem::Function(func) => {
let name = func.item_name().clone();
let name = func.item_name();
let params = func.params.iter().skip(1).map(|(param_name, param_type)| {
format!("{param_name}")
}).collect::<Vec<_>>().join(", ");
Expand Down Expand Up @@ -126,7 +126,7 @@ fn render_function_list_item<'a>(package: &UnresolvedPackage, func: &Function) -
let item = CurrentItem::Function(func.clone());
match func.kind {
wit_parser::FunctionKind::Freestanding => {
let name = func.item_name().clone();
let name = func.item_name();
let params = func.params.iter().skip(1).map(|(param_name, param_type)| {
format!("{param_name}")
}).collect::<Vec<_>>().join(", ");
Expand All @@ -141,7 +141,7 @@ fn render_function_list_item<'a>(package: &UnresolvedPackage, func: &Function) -
)
},
wit_parser::FunctionKind::Method(type_id) => {
let name = func.item_name().clone();
let name = func.item_name();
let params = func.params.iter().skip(1).map(|(param_name, param_type)| {
format!("{param_name}")
}).collect::<Vec<_>>().join(", ");
Expand All @@ -156,7 +156,7 @@ fn render_function_list_item<'a>(package: &UnresolvedPackage, func: &Function) -
)
},
wit_parser::FunctionKind::Static(type_id) => {
let name = func.item_name().clone();
let name = func.item_name();
let params = func.params.iter().skip(1).map(|(param_name, param_type)| {
format!("{param_name}")
}).collect::<Vec<_>>().join(", ");
Expand All @@ -171,7 +171,7 @@ fn render_function_list_item<'a>(package: &UnresolvedPackage, func: &Function) -
)
},
wit_parser::FunctionKind::Constructor(type_id) => {
let name = func.item_name().clone();
let name = func.item_name();
let params = func.params.iter().skip(1).map(|(param_name, param_type)| {
format!("{param_name}")
}).collect::<Vec<_>>().join(", ");
Expand Down
1 change: 0 additions & 1 deletion src/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::cli::NewSubcommand;

use crate::Result;

pub mod common;
pub mod game;

pub async fn new(new: &NewSubcommand) -> Result<()> {
Expand Down
36 changes: 0 additions & 36 deletions src/commands/new/common.rs

This file was deleted.

41 changes: 36 additions & 5 deletions src/commands/new/game.rs
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(())
}

0 comments on commit 616aa27

Please sign in to comment.