-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement organization of
codegen
, and add style argument
- Loading branch information
1 parent
50d3a28
commit 1757304
Showing
6 changed files
with
143 additions
and
115 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use crate::lockfile::LockFile; | ||
use anyhow::Context; | ||
use std::path::Path; | ||
|
||
pub fn generate_lua(lockfile: &LockFile, strip_dir: &str) -> anyhow::Result<String> { | ||
let table = lockfile | ||
.entries | ||
.iter() | ||
.map(|(file_path, file_entry)| { | ||
let file_stem = Path::new(file_path) | ||
.to_str() | ||
.context("Failed to convert path to string")? | ||
.strip_prefix(strip_dir) | ||
.context("Failed to strip directory prefix")?; | ||
Ok(format!( | ||
"\t[\"{}\"] = \"rbxassetid://{}\"", | ||
file_stem, file_entry.asset_id | ||
)) | ||
}) | ||
.collect::<Result<Vec<String>, anyhow::Error>>()? | ||
.join(",\n"); | ||
|
||
Ok(format!("return {{\n{}\n}}", table)) | ||
} | ||
|
||
pub fn generate_ts( | ||
lockfile: &LockFile, | ||
strip_dir: &str, | ||
output_dir: &str, | ||
) -> anyhow::Result<String> { | ||
let interface = lockfile | ||
.entries | ||
.keys() | ||
.map(|file_path| { | ||
let file_stem = Path::new(file_path) | ||
.to_str() | ||
.context("Failed to convert path to string")? | ||
.strip_prefix(strip_dir) | ||
.context("Failed to strip directory prefix")?; | ||
Ok(format!("\t\"{}\": string", file_stem)) | ||
}) | ||
.collect::<Result<Vec<String>, anyhow::Error>>()? | ||
.join(",\n"); | ||
|
||
Ok(format!( | ||
"declare const {}: {{\n{}\n}}\nexport = {}", | ||
output_dir, interface, output_dir | ||
)) | ||
} |
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,89 @@ | ||
use crate::{config::StyleType, LockFile}; | ||
|
||
mod flat; | ||
mod nested; | ||
|
||
pub fn generate_lua( | ||
lockfile: &LockFile, | ||
strip_dir: &str, | ||
style: &StyleType, | ||
) -> anyhow::Result<String> { | ||
match style { | ||
StyleType::Flat => flat::generate_lua(lockfile, strip_dir), | ||
StyleType::Nested => nested::generate_lua(lockfile, strip_dir), | ||
} | ||
} | ||
|
||
pub fn generate_ts( | ||
lockfile: &LockFile, | ||
strip_dir: &str, | ||
output_dir: &str, | ||
style: &StyleType, | ||
) -> anyhow::Result<String> { | ||
match style { | ||
StyleType::Flat => flat::generate_ts(lockfile, strip_dir, output_dir), | ||
StyleType::Nested => nested::generate_ts(lockfile, strip_dir, output_dir), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::collections::BTreeMap; | ||
|
||
use crate::{FileEntry, LockFile}; | ||
|
||
fn test_lockfile() -> LockFile { | ||
let mut entries = BTreeMap::new(); | ||
entries.insert( | ||
"assets/foo.png".to_string(), | ||
FileEntry { | ||
asset_id: 1, | ||
hash: "a".to_string(), | ||
}, | ||
); | ||
entries.insert( | ||
"assets/bar/baz.png".to_string(), | ||
FileEntry { | ||
asset_id: 2, | ||
hash: "b".to_string(), | ||
}, | ||
); | ||
LockFile { entries } | ||
} | ||
|
||
#[test] | ||
fn generate_lua() { | ||
let lockfile = test_lockfile(); | ||
|
||
let lua = super::flat::generate_lua(&lockfile, "assets").unwrap(); | ||
assert_eq!(lua, "return {\n\t[\"/bar/baz.png\"] = \"rbxassetid://2\",\n\t[\"/foo.png\"] = \"rbxassetid://1\"\n}"); | ||
} | ||
|
||
#[test] | ||
fn generate_ts() { | ||
let lockfile = test_lockfile(); | ||
|
||
let ts = super::flat::generate_ts(&lockfile, "assets", "assets").unwrap(); | ||
assert_eq!(ts, "declare const assets: {\n\t\"/bar/baz.png\": string,\n\t\"/foo.png\": string\n}\nexport = assets"); | ||
} | ||
|
||
#[test] | ||
fn generate_lua_nested() { | ||
let lockfile = test_lockfile(); | ||
|
||
let lua = super::nested::generate_lua(&lockfile, "assets").unwrap(); | ||
assert_eq!( | ||
lua, | ||
"return {\n bar = {\n [\"baz.png\"] = \"rbxassetid://2\",\n },\n [\"foo.png\"] = \"rbxassetid://1\",\n}"); | ||
} | ||
|
||
#[test] | ||
fn generate_ts_nested() { | ||
let lockfile = test_lockfile(); | ||
|
||
let ts = super::nested::generate_ts(&lockfile, "assets", "assets").unwrap(); | ||
assert_eq!( | ||
ts, | ||
"declare const assets: {\n bar: {\n \"baz.png\": \"rbxassetid://2\",\n },\n \"foo.png\": \"rbxassetid://1\",\n}\nexport = assets"); | ||
} | ||
} |
File renamed without changes.
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