Skip to content

Commit

Permalink
Implement organization of codegen, and add style argument
Browse files Browse the repository at this point in the history
  • Loading branch information
bibi-reden committed Apr 25, 2024
1 parent 50d3a28 commit 1757304
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 115 deletions.
111 changes: 0 additions & 111 deletions src/codegen.rs

This file was deleted.

49 changes: 49 additions & 0 deletions src/codegen/flat.rs
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
))
}
89 changes: 89 additions & 0 deletions src/codegen/mod.rs
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.
4 changes: 3 additions & 1 deletion src/nested.rs → src/codegen/nested/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
mod ast;

use anyhow::{bail, Context};
use std::collections::BTreeMap;
use std::{path::Component as PathComponent, path::Path};

use crate::ast::{AstTarget, Expression, ReturnStatement};
use crate::LockFile;
use ast::{AstTarget, Expression, ReturnStatement};
use std::fmt::Write;

use self::types::NestedTable;
Expand Down
5 changes: 2 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ use upload::upload_asset;
use crate::config::Config;

pub mod args;
mod ast;
mod codegen;
pub mod config;
pub mod lockfile;
mod nested;
pub mod state;
mod svg;
mod upload;
Expand Down Expand Up @@ -180,7 +178,7 @@ async fn main() -> anyhow::Result<()> {
}));

let lua_filename = format!("{}.{}", state.output_name, state.lua_extension);
let lua_output = generate_lua(&state.new_lockfile, asset_dir_str);
let lua_output = generate_lua(&state.new_lockfile, asset_dir_str, &state.style);

write(Path::new(&state.write_dir).join(lua_filename), lua_output?)
.await
Expand All @@ -192,6 +190,7 @@ async fn main() -> anyhow::Result<()> {
&state.new_lockfile,
asset_dir_str,
state.output_name.as_str(),
&state.style,
);

write(Path::new(&state.write_dir).join(ts_filename), ts_output?)
Expand Down

0 comments on commit 1757304

Please sign in to comment.