Skip to content

Commit

Permalink
Generation changes, format fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vorlias committed Jan 29, 2024
1 parent 497d140 commit db575ca
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 45 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ tarmac help [<subcommand>]
* When a `tarmac.toml` file is found, Tarmac will include it and its includes and stop traversing that directory.

### InputConfig
* `name`, string, **optional**
* The name of the input
* Used for the TypeScript interface name (if `codegen-typescript-declaration` is specified - otherwise generated TS code will use **TarmacAssets**)
* `glob`, string
* A path glob that should include any files for this input group.
* Tarmac uses the [globset library](https://docs.rs/globset/0.4.5/globset/) and supports any syntax it supports.
Expand All @@ -202,6 +205,8 @@ tarmac help [<subcommand>]
* If defined and `codegen` is true, Tarmac will merge all generated Lua code for this input group into a single file.
* `codegen-base-path`, path, **optional**
* Defines the base path for generating Lua code when `codegen-path` is also defined. Defaults to **the directory containing `tarmac.toml`**.
* `codegen-typescript-declaration`, bool, **optional**
* Will generate a corresponding `.d.ts` file alongside the generated Lua file if true. Defaults to **false**.

## License
Tarmac is available under the MIT license. See [LICENSE.txt](LICENSE.txt) for details.
6 changes: 4 additions & 2 deletions examples/02-spritesheets/pack-these/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** This file was @generated by Tarmac. It is not intended for manual editing. */
// This file was generated by Tarmac. It is not intended for manual editing.
interface ImageSlice {
readonly Image: string;
readonly ImageRectOffset: Vector2;
Expand All @@ -22,6 +22,8 @@ interface PackedSprites {
f: ImageSlice;
g: ImageSlice;
}
/** Tarmac Generated Asset Types */
/**
* Generated Tarmac Asset Interface
**/
declare const PackedSprites: PackedSprites;
export = PackedSprites;
40 changes: 23 additions & 17 deletions src/codegen.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::{path::{Path, self}, io, collections::BTreeMap};
use std::{
collections::BTreeMap,
io,
path::{self, Path},
};

use crate::{data::SyncInput, typescript, lua};
use crate::{data::SyncInput, lua, typescript};

pub fn perform_codegen(output_path: Option<&Path>, inputs: &[&SyncInput]) -> io::Result<()> {
lua::codegen::perform_codegen(output_path, inputs)?;
Expand All @@ -22,7 +26,10 @@ pub enum GroupedItem<'a> {
}

impl GroupedItem<'_> {
pub fn parse_root_folder<'a>(output_path: &Path, inputs: &'a [&SyncInput]) -> BTreeMap<String, GroupedItem<'a>> {
pub fn parse_root_folder<'a>(
output_path: &Path,
inputs: &'a [&SyncInput],
) -> BTreeMap<String, GroupedItem<'a>> {
let mut root_folder: BTreeMap<String, GroupedItem<'a>> = BTreeMap::new();

for &input in inputs {
Expand All @@ -31,18 +38,18 @@ impl GroupedItem<'_> {
if !input.config.codegen {
continue;
}

// The extension portion of the path is not useful for code generation.
// By stripping it off, we generate the names that users expect.
let mut path_without_extension = input.path_without_dpi_scale.clone();
path_without_extension.set_extension("");

// If we can't construct a relative path, there isn't a sensible name
// that we can use to refer to this input.
let relative_path = path_without_extension
.strip_prefix(&input.config.codegen_base_path)
.expect("Input base path was not a base path for input");

// Collapse `..` path segments so that we can map this path onto our
// tree of inputs.
let mut segments = Vec::new();
Expand All @@ -57,14 +64,14 @@ impl GroupedItem<'_> {
path::Component::ParentDir => assert!(segments.pop().is_some()),
}
}

// Navigate down the tree, creating any folder entries that don't exist
// yet.
let mut current_dir = &mut root_folder;
for (i, &segment) in segments.iter().enumerate() {
if i == segments.len() - 1 {
// We assume that the last segment of a path must be a file.

let input_group = match current_dir.get_mut(segment) {
Some(existing) => existing,
None => {
Expand All @@ -75,7 +82,7 @@ impl GroupedItem<'_> {
current_dir.get_mut(segment).unwrap()
}
};

if let GroupedItem::InputGroup {
inputs_by_dpi_scale,
} = input_group
Expand All @@ -85,13 +92,12 @@ impl GroupedItem<'_> {
unreachable!();
}
} else {
let next_entry =
current_dir
.entry(segment.to_owned())
.or_insert_with(|| GroupedItem::Folder {
children_by_name: BTreeMap::new(),
});

let next_entry = current_dir.entry(segment.to_owned()).or_insert_with(|| {
GroupedItem::Folder {
children_by_name: BTreeMap::new(),
}
});

if let GroupedItem::Folder { children_by_name } = next_entry {
current_dir = children_by_name;
} else {
Expand All @@ -103,4 +109,4 @@ impl GroupedItem<'_> {

root_folder
}
}
}
3 changes: 2 additions & 1 deletion src/lua/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ use std::{
use fs_err::File;

use crate::{
codegen::GroupedItem,
data::ImageSlice,
data::{AssetId, SyncInput},
lua::lua_ast::{Block, Expression, Function, IfBlock, Statement, Table}, codegen::GroupedItem,
lua::lua_ast::{Block, Expression, Function, IfBlock, Statement, Table},
};

const CODEGEN_HEADER: &str =
Expand Down
2 changes: 1 addition & 1 deletion src/lua/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod codegen;
pub mod lua_ast;
pub mod lua_ast;
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
mod alpha_bleed;
mod asset_name;
mod auth_cookie;
mod codegen;
mod commands;
mod data;
mod dpi_scale;
mod glob;
mod lua;
mod options;
mod roblox_web_api;
mod sync_backend;
mod lua;
mod typescript;
mod codegen;

use std::{env, panic, process};

Expand Down
39 changes: 19 additions & 20 deletions src/typescript/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use fs_err::File;
use crate::{
codegen::GroupedItem,
data::SyncInput,
typescript::ts_ast::{Expression, VariableDeclaration, VariableKind},
typescript::ts_ast::{Comment, Expression, VariableDeclaration, VariableKind},
};

use super::ts_ast::{
Expand All @@ -19,7 +19,7 @@ use super::ts_ast::{
};

const CODEGEN_HEADER: &str =
"/** This file was @generated by Tarmac. It is not intended for manual editing. */";
"This file was generated by Tarmac. It is not intended for manual editing.";
const CODEGEN_IMAGE_SLICE_INTERFACE: &str = "ImageSlice";

pub fn perform_codegen(output_path: Option<&Path>, inputs: &[&SyncInput]) -> io::Result<()> {
Expand Down Expand Up @@ -181,16 +181,15 @@ fn codegen_grouped(output_path: &Path, inputs: &[&SyncInput]) -> io::Result<()>
}

let first = should_generate_d_ts.unwrap();
let name = &first.config.name;

let root_folder = GroupedItem::parse_root_folder(output_path, inputs);

let root = &GroupedItem::Folder {
children_by_name: root_folder,
};

let mut file = File::create(declaration_path)?;
writeln!(file, "{}", CODEGEN_HEADER)?;
let mut file = File::create(&declaration_path)?;
write!(file, "{}", Comment::singleline(CODEGEN_HEADER.into()))?;

let (properties, prereqs) = get_properties(&root);

Expand Down Expand Up @@ -219,23 +218,23 @@ fn codegen_grouped(output_path: &Path, inputs: &[&SyncInput]) -> io::Result<()>
write!(
file,
"{}",
Statement::InterfaceDeclaration(assets_interface)
)?;

write!(
file,
"/** Tarmac Generated Asset Types */\n{}",
VariableDeclaration::new(
sanitized_name.to_string(),
VariableKind::Const,
Some(Expression::Identifier(sanitized_name.to_string())),
Some(vec![ModifierToken::Declare]),
None,
)
Statement::list(vec![
// interface TarmacAssets { ... }
Statement::InterfaceDeclaration(assets_interface),
Comment::multiline("*\n * Generated Tarmac Asset Interface\n *".into()),
// declare const TarmacAssets: TarmacAssets
VariableDeclaration::new(
sanitized_name.to_string(),
VariableKind::Const,
Some(Expression::Identifier(sanitized_name.to_string())),
Some(vec![ModifierToken::Declare]),
None,
),
// export = TarmacAssets;
export_assignment,
])
)?;

write!(file, "{}", export_assignment)?;

Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/typescript/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod codegen;
pub mod ts_ast;
pub mod codegen;
38 changes: 37 additions & 1 deletion src/typescript/ts_ast.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt::{self, Write};
use std::fmt::{self, Display, Write};

use fs_err::write;

Expand Down Expand Up @@ -384,17 +384,38 @@ pub struct TypeAliasDeclaration {
type_expression: Expression,
}

pub enum Comment {
Single(String),
Multiline(String),
}

impl Comment {
pub fn multiline(text: String) -> Statement {
Statement::Comment(Self::Multiline(text))
}

pub fn singleline(text: String) -> Statement {
Statement::Comment(Self::Single(text))
}
}

pub(crate) enum Statement {
InterfaceDeclaration(InterfaceDeclaration),
TypeAliasDeclaration(TypeAliasDeclaration),
VariableDeclaration(VariableDeclaration),
ExportAssignment(ExportAssignment),
Comment(Comment),
List(Vec<Statement>),
}

impl Statement {
pub fn export_assignment(expression: Expression) -> Statement {
Statement::ExportAssignment(ExportAssignment { expression })
}

pub fn list(statements: Vec<Statement>) -> Self {
Self::List(statements)
}
}

impl FmtTS for Statement {
Expand All @@ -412,6 +433,21 @@ impl FmtTS for Statement {
type_alias.name, type_alias.type_expression
)
}
Self::Comment(comment) => match comment {
Comment::Single(text) => {
writeln!(output, "// {}", text)
}
Comment::Multiline(text) => {
writeln!(output, "/*{}*/", text)
}
},
Self::List(statements) => {
for statement in statements {
statement.fmt_ts(output)?;
}

Ok(())
}
}
}
}
Expand Down

0 comments on commit db575ca

Please sign in to comment.