Skip to content

Commit

Permalink
Merge branch '0.6.x' into add-polling-2
Browse files Browse the repository at this point in the history
  • Loading branch information
Ketasaja committed Feb 15, 2025
2 parents 551e031 + 5624070 commit 5600f8b
Show file tree
Hide file tree
Showing 28 changed files with 330 additions and 25 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ jobs:
override: true
profile: minimal

- uses: Swatinem/rust-cache@v2

- name: Build
run: cargo build --locked --verbose

Expand All @@ -56,6 +58,8 @@ jobs:
override: true
components: rustfmt, clippy

- uses: Swatinem/rust-cache@v2

- name: Rustfmt
run: cargo fmt -- --check

Expand Down
7 changes: 0 additions & 7 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@ name: Docs

on:
workflow_dispatch: {}
push:
branches:
- '0.6.x'
tags-ignore:
- "**"
paths:
- docs/**
release:
types:
- published
Expand Down
20 changes: 20 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ fn main() -> Result<()> {
std::fs::write(file_path, defs)?;
}

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)?;
}

if let Some(defs) = types_output.defs {
let defs_path = if types_path.file_stem().unwrap() == "init" {
types_path.with_file_name("index.d.ts")
} else {
types_path.with_extension("d.ts")
};

std::fs::write(defs_path, defs)?;
}

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

if let Some(tooling) = code.tooling {
let tooling_path = config_path.parent().unwrap().join(tooling.path);

Expand Down
32 changes: 19 additions & 13 deletions docs/config/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ opt client_output = "path/to/client/output.lua"`
const outputExample = `opt server_output = "./network/client.luau"
opt client_output = "src/client/zap.luau"`

const typesOutputExample = `opt types_output = "network/types.luau"`

const asyncLibExample = `opt yield_type = "promise"
opt async_lib = "require(game:GetService('ReplicatedStorage').Promise)"`

Expand Down Expand Up @@ -37,14 +39,24 @@ The paths are relative to the configuration file and should point to a lua(u) fi

<CodeBlock :code="outputExample" />

## `types_output` [`0.6.18+`]

Configures where Luau types will be output.

The path is relative to the configuration file and should point to a lua(u) file.

### Example

<CodeBlock :code="typesOutputExample" />

## `call_default` `[0.6.18+]`

The default `call` field that will be used for events. See [call](events.html#call) for possible options.

### Example

<CodeBlock code = `opt call_default = "ManySync" />
<CodeBlock code = `opt call_default = "Polling" />
<CodeBlock code =`opt call_default = "Polling" />

### Default

Expand All @@ -58,15 +70,12 @@ This option changes the name of the remotes generated by Zap.

<CodeBlock code = 'opt remote_scope = "ZAP"' />


The generated remotes will be `ZAP_RELIABLE` and `ZAP_UNRELIABLE` respectively.


### Example

<CodeBlock code = 'opt remote_scope = "PACKAGE_NAME"' />


The generated remotes will change to be `PACKAGE_NAME_RELIABLE` and `PACKAGE_NAME_UNRELIABLE` respectively.

## `remote_folder`
Expand All @@ -77,15 +86,12 @@ This option changes the name folder that Zap's remotes are placed inside of Repl

<CodeBlock code = 'opt remote_folder = "ZAP"' />


The generated remotes will be in `ReplicatedStorage -> ZAP`.


### Example

<CodeBlock code = 'opt remote_scope = "CHARACTER"' />


The generated remotes be in `ReplicatedStorage -> CHARACTER`

## `casing`
Expand Down Expand Up @@ -179,13 +185,13 @@ This can be mitigated by firing remotes to the server at a timed rate, so as to
local Timer = 0

RunService.Heartbeat:Connect(function(DeltaTime)
Timer += DeltaTime
Timer += DeltaTime

-- Only send events 60 times per second
if Timer >= 1 / 60 then
Timer = 0
Zap.SendEvents()
end
-- Only send events 60 times per second
if Timer >= 1 / 60 then
Timer = 0
Zap.SendEvents()
end
end)
```

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

pub server_output: &'src str,
pub client_output: &'src str,
pub types_output: Option<&'src str>,
pub tooling_output: &'src str,

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

Expand Down Expand Up @@ -79,6 +80,11 @@ pub fn run(input: &str, no_warnings: bool) -> Return {
code: output::luau::client::code(&config),
defs: output::typescript::client::code(&config),
},
types: config.types_output.map(|types_output| Output {
path: types_output.into(),
code: output::luau::types::code(&config),
defs: output::typescript::types::code(&config),
}),
tooling: output::tooling::output(&config),
}),
diagnostics,
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 @@ -5,6 +5,7 @@ use crate::{

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 Output for TypesOutput<'_> {
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()
}
1 change: 1 addition & 0 deletions zap/src/output/typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::config::{Config, Enum, Parameter, Ty};

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

pub trait ConfigProvider {
fn get_config(&self) -> &Config;
Expand Down
76 changes: 76 additions & 0 deletions zap/src/output/typescript/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use crate::config::{Config, TyDecl};

use super::{ConfigProvider, Output};

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

impl Output for TypesOutput<'_> {
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 ConfigProvider for TypesOutput<'_> {
fn get_config(&self) -> &Config {
self.config
}
}

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.buf
}
}

pub fn code(config: &Config) -> Option<String> {
Some(TypesOutput::new(config).output())
}
17 changes: 17 additions & 0 deletions zap/src/parser/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,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: Option<&str> = self.types_output_opt(&config.opts);
let (tooling_output, ..) = self.str_opt("tooling_output", "network/tooling.lua", &config.opts);

let casing = self.casing_opt(&config.opts);
Expand Down Expand Up @@ -152,6 +153,7 @@ impl<'src> Converter<'src> {

server_output,
client_output,
types_output,
tooling_output,

casing,
Expand Down Expand Up @@ -236,6 +238,21 @@ impl<'src> Converter<'src> {
}
}

fn types_output_opt(&mut self, opts: &[SyntaxOpt<'src>]) -> Option<&'src str> {
let opt = opts.iter().find(|opt| opt.name.name == "types_output")?;

if let SyntaxOptValueKind::Str(opt_value) = &opt.value.kind {
Some(self.str(opt_value))
} else {
self.report(Report::AnalyzeInvalidOptValue {
span: opt.value.span(),
expected: "Types output path expected.",
});

None
}
}

fn call_default_opt(&mut self, opts: &[SyntaxOpt<'src>]) -> Option<EvCall> {
let opt = opts.iter().find(|opt| opt.name.name == "call_default")?;

Expand Down
8 changes: 7 additions & 1 deletion zap/tests/lune/base.luau
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local luau = require("@lune/luau")
local process = require("@lune/process")
local roblox = require("@lune/roblox")
local task = require("@lune/task")
local serverCode, clientCode, toolingCode = unpack(process.args)
local serverCode, clientCode, typesCode, toolingCode = unpack(process.args)

local noop = function() end

Expand Down Expand Up @@ -80,6 +80,12 @@ local client = luau.load(clientCode, {
environment = environment
})()

local types = luau.load(typesCode, {
debugName = "Types",
codegenEnabled = true,
environment = environment,
})()

local tooling = luau.load(toolingCode, {
debugName = "Tooling",
codegenEnabled = true,
Expand Down
Loading

0 comments on commit 5600f8b

Please sign in to comment.