Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional types_output option #79

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ fn main() -> Result<()> {
let server_path = config_path.parent().unwrap().join(code.server.path);
let client_path = config_path.parent().unwrap().join(code.client.path);

if let Some(types_output) = code.types {
let types_path = config_path.parent().unwrap().join(types_output.path);

if let Some(parent) = types_path.parent() {
std::fs::create_dir_all(parent)?;
}

std::fs::write(types_path.clone(), types_output.code)?;
}

if let Some(parent) = server_path.parent() {
std::fs::create_dir_all(parent)?;
}
Expand Down
9 changes: 6 additions & 3 deletions docs/config/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const outputDefaults = `opt server_output = "./network/server.lua"
opt client_output = "path/to/client/output.lua"`

const outputExample = `opt server_output = "./network/client.luau"
opt client_output = "src/client/zap.luau"`
opt client_output = "src/client/zap.luau"
opt types_output = "src/shared/types.luau"`

const asyncLibExample = `opt yield_type = "promise"
opt async_lib = "require(game:GetService('ReplicatedStorage').Promise)"`
Expand All @@ -13,12 +14,14 @@ opt async_lib = "require(game:GetService('ReplicatedStorage').Promise)"`

Options are placed at the beginning of Zap config files and allow you to configure the way Zap generates code.

## `server_output` & `client_output`
## `server_output`, `client_output` & `types_output`

These two options allow you to configure where Zap will output generated code. If you're not using the CLI these options can be ignored.
These three options allow you to configure where Zap will output generated code. If you're not using the CLI these options can be ignored.

The paths are relative to the configuration file and should point to a lua(u) file.

The `types_output` option does not have a default value and is optional.

### Default

<CodeBlock :code="outputDefaults" />
Expand Down
1 change: 1 addition & 0 deletions zap/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Config<'src> {

pub server_output: &'src str,
pub client_output: &'src str,
pub types_output: &'src str,

pub casing: Casing,
pub yield_type: YieldType,
Expand Down
10 changes: 10 additions & 0 deletions zap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct Output {
pub struct Code {
pub server: Output,
pub client: Output,
pub types: Option<Output>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -71,6 +72,15 @@ pub fn run(input: &str) -> Return {
code: output::luau::client::code(&config),
defs: output::typescript::client::code(&config),
},
types: if !config.types_output.is_empty() {
Some(Output {
path: config.types_output.into(),
code: output::luau::types::code(&config),
defs: None,
})
} else {
None
},
}),
diagnostics: reports.into_iter().map(|report| report.into()).collect(),
}
Expand Down
1 change: 1 addition & 0 deletions zap/src/output/luau/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::config::{Enum, NumTy, Range, Ty};

pub mod client;
pub mod server;
pub mod types;

pub trait Output {
fn push(&mut self, s: &str);
Expand Down
71 changes: 71 additions & 0 deletions zap/src/output/luau/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use crate::config::{Config, TyDecl};

use super::Output;

struct TypesOutput<'src> {
config: &'src Config<'src>,
tabs: u32,
buf: String,
}

impl<'a> Output for TypesOutput<'a> {
fn push(&mut self, s: &str) {
self.buf.push_str(s);
}

fn indent(&mut self) {
self.tabs += 1;
}

fn dedent(&mut self) {
self.tabs -= 1;
}

fn push_indent(&mut self) {
for _ in 0..self.tabs {
self.push("\t");
}
}
}

impl<'a> TypesOutput<'a> {
pub fn new(config: &'a Config) -> Self {
Self {
config,
tabs: 0,
buf: String::new(),
}
}

fn push_tydecl(&mut self, tydecl: &TyDecl) {
let name = &tydecl.name;
let ty = &tydecl.ty;

self.push_indent();
self.push(&format!("export type {name} = "));
self.push_ty(ty);
self.push("\n");
}

fn push_tydecls(&mut self) {
for tydecl in self.config.tydecls.iter() {
self.push_tydecl(tydecl);
}
}

pub fn output(mut self) -> String {
self.push_line(&format!(
"-- Types generated by Zap v{} (https://github.com/red-blox/zap)",
env!("CARGO_PKG_VERSION")
));

self.push_tydecls();
self.push_line("return nil");

self.buf
}
}

pub fn code(config: &Config) -> String {
TypesOutput::new(config).output()
}
2 changes: 2 additions & 0 deletions zap/src/parser/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl<'src> Converter<'src> {

let (server_output, ..) = self.str_opt("server_output", "network/server.lua", &config.opts);
let (client_output, ..) = self.str_opt("client_output", "network/client.lua", &config.opts);
let (types_output, ..) = self.str_opt("types_output", "", &config.opts);

let casing = self.casing_opt(&config.opts);
let yield_type = self.yield_type_opt(typescript, &config.opts);
Expand All @@ -107,6 +108,7 @@ impl<'src> Converter<'src> {

server_output,
client_output,
types_output,

casing,
yield_type,
Expand Down
Loading