Skip to content

Commit

Permalink
Split codegen into a separate struct and config section
Browse files Browse the repository at this point in the history
  • Loading branch information
jackTabsCode committed Apr 26, 2024
2 parents 81e85d1 + ee43445 commit 90af12e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ Asphalt is configured with a project file called `asphalt.toml`. It is required
```toml
asset_dir = "test/"
write_dir = "output/"

[codegen]
typescript = true
luau = true
style = "flat"
output_name = "assets"

[creator]
type = "user"
Expand All @@ -58,21 +61,26 @@ id = 9670971
- The directory to output the generated code to. This should probably be somewhere in your game's source folder.
- `creator`: Creator
- The Roblox creator to upload the assets under.
- `typescript`: boolean (optional)
- Generate a Typescript definition file.
- `luau`: boolean (optional)
- Use the `luau` file extension.
- `style`: string (optional)
- The code-generation style to use. Defaults to `flat`.
- `output_name`: string (optional)
- The name for the generated files. Defaults to `assets`.
- `codegen`: Codegen
- Code generation options.
- `existing`: map<string, ExistingAsset> (optional)

#### Creator

- `type`: "user" or "group"
- `id`: number

#### Codegen

- `typescript`: boolean (optional)
- Generate a Typescript definition file.
- `luau`: boolean (optional)
- Use the `luau` file extension.
- `style`: "flat" | "nested" (optional)
- The code-generation style to use. Defaults to `flat`. If you would like to have an experience similar to [Tarmac](https://github.com/rojo-rbx/tarmac), use `nested`.
- `output_name`: string (optional)
- The name for the generated files. Defaults to `assets`.

#### ExistingAsset

- `id`: number
Expand Down
3 changes: 2 additions & 1 deletion src/codegen/nested/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ impl AstFormat for ReturnStatement {
AstTarget::Typescript { output_dir } => {
write!(output, "declare const {output_dir}: ")
}
}?;
}
.expect("Failed to write to output");
let result = self.0.fmt_ast(output);
if let AstTarget::Typescript { output_dir } = output.target {
write!(output, "\nexport = {output_dir}")?
Expand Down
12 changes: 8 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ pub struct ExistingAsset {
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Config {
pub asset_dir: String,
pub write_dir: String,
pub creator: Creator,
pub struct CodegenConfig {
pub output_name: Option<String>,
pub typescript: Option<bool>,
pub luau: Option<bool>,
pub style: Option<StyleType>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Config {
pub asset_dir: String,
pub write_dir: String,
pub creator: Creator,
pub codegen: CodegenConfig,
pub existing: Option<HashMap<String, ExistingAsset>>,
}
9 changes: 5 additions & 4 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ pub struct State {

pub api_key: String,
pub creator: AssetCreator,

pub typescript: bool,
pub output_name: String,
pub lua_extension: String,

pub style: StyleType,

pub font_db: Database,
Expand Down Expand Up @@ -68,15 +68,16 @@ impl State {
let write_dir = PathBuf::from(write_dir);

let output_name = config
.codegen
.output_name
.as_ref()
.unwrap_or(&"assets".to_string())
.to_string();

let typescript = config.typescript.unwrap_or(false);
let style = config.style.unwrap_or(StyleType::Flat);
let typescript = config.codegen.typescript.unwrap_or(false);
let style = config.codegen.style.unwrap_or(StyleType::Flat);

let lua_extension = String::from(if config.luau.unwrap_or(false) {
let lua_extension = String::from(if config.codegen.luau.unwrap_or(false) {
"luau"
} else {
"lua"
Expand Down

0 comments on commit 90af12e

Please sign in to comment.