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

generate types to SpacetimeDB.Types namespace #88

Merged
merged 5 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
28 changes: 26 additions & 2 deletions crates/cli/src/subcommands/generate/csharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1401,7 +1401,7 @@ pub fn autogen_csharp_reducer(ctx: &GenCtx, reducer: &ReducerDef, namespace: &st
output.into_inner()
}

pub fn autogen_csharp_globals(items: &[GenItem], namespace: &str) -> Vec<(String, String)> {
pub fn autogen_csharp_globals(items: &[GenItem], namespace: &str) -> Vec<Vec<(String, String)>> {
let reducers: Vec<&ReducerDef> = items
.iter()
.map(|i| {
Expand Down Expand Up @@ -1551,5 +1551,29 @@ pub fn autogen_csharp_globals(items: &[GenItem], namespace: &str) -> Vec<(String
writeln!(output, "}}").unwrap();
}

vec![("ReducerEvent.cs".into(), output.into_inner())]
let mut result = vec![vec![("ReducerEvent.cs".to_string(), output.into_inner())]];

let mut output = CodeIndenter::new(String::new());

writeln!(output, "using SpacetimeDB;").unwrap();

writeln!(output).unwrap();

if use_namespace {
writeln!(output, "namespace {}", namespace).unwrap();
writeln!(output, "{{").unwrap();
output.indent(1);
}

writeln!(output, "[ReducerClass]").unwrap();
writeln!(output, "public partial class Reducer {{ }}").unwrap();

if use_namespace {
output.dedent(1);
writeln!(output, "}}").unwrap();
}

result.push(vec![("Reducer.cs".into(), output.into_inner())]);

result
}
11 changes: 5 additions & 6 deletions crates/cli/src/subcommands/generate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ pub fn cli() -> clap::Command {
)
.arg(
Arg::new("namespace")
.default_value("SpacetimeDB")
.default_value("SpacetimeDB.Types")
.long("namespace")
.short('n')
.help("The namespace that should be used (default is 'SpacetimeDB')"),
.help("The namespace that should be used (default is 'SpacetimeDB.Types')"),
)
.arg(
Arg::new("lang")
Expand Down Expand Up @@ -158,13 +158,12 @@ pub fn generate<'a>(wasm_file: &'a Path, lang: Language, namespace: &'a str) ->
.iter()
.filter_map(|item| item.generate(&ctx, lang, namespace))
.collect();
for global in generate_globals(&ctx, lang, namespace, &items) {
files.push(global);
}
files.extend(generate_globals(&ctx, lang, namespace, &items).into_iter().flatten());

Ok(files)
}

fn generate_globals(ctx: &GenCtx, lang: Language, namespace: &str, items: &[GenItem]) -> Vec<(String, String)> {
fn generate_globals(ctx: &GenCtx, lang: Language, namespace: &str, items: &[GenItem]) -> Vec<Vec<(String, String)>> {
match lang {
Language::Csharp => csharp::autogen_csharp_globals(items, namespace),
Language::TypeScript => typescript::autogen_typescript_globals(ctx, items),
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/subcommands/generate/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,6 @@ pub fn autogen_python_reducer(ctx: &GenCtx, reducer: &ReducerDef) -> String {
output.into_inner()
}

pub fn autogen_python_globals(_ctx: &GenCtx, _items: &[GenItem]) -> Vec<(String, String)> {
pub fn autogen_python_globals(_ctx: &GenCtx, _items: &[GenItem]) -> Vec<Vec<(String, String)>> {
vec![] //TODO
}
4 changes: 2 additions & 2 deletions crates/cli/src/subcommands/generate/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ pub fn autogen_rust_reducer(ctx: &GenCtx, reducer: &ReducerDef) -> String {
/// to connect to a remote database, and passes the `handle_row_update`
/// and `handle_event` functions so the `BackgroundDbConnection` can spawn workers
/// which use those functions to dispatch on the content of messages.
pub fn autogen_rust_globals(ctx: &GenCtx, items: &[GenItem]) -> Vec<(String, String)> {
pub fn autogen_rust_globals(ctx: &GenCtx, items: &[GenItem]) -> Vec<Vec<(String, String)>> {
let mut output = CodeIndenter::new(String::new());
let out = &mut output;

Expand Down Expand Up @@ -711,7 +711,7 @@ pub fn autogen_rust_globals(ctx: &GenCtx, items: &[GenItem]) -> Vec<(String, Str
// Define `fn connect`.
print_connect_defn(out);

vec![("mod.rs".to_string(), output.into_inner())]
vec![vec![("mod.rs".to_string(), output.into_inner())]]
}

/// Extra imports required by the `mod.rs` file, in addition to the [`SPACETIMEDB_IMPORTS`].
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/subcommands/generate/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,6 @@ pub fn autogen_typescript_reducer(ctx: &GenCtx, reducer: &ReducerDef) -> String
output.into_inner()
}

pub fn autogen_typescript_globals(_ctx: &GenCtx, _items: &[GenItem]) -> Vec<(String, String)> {
pub fn autogen_typescript_globals(_ctx: &GenCtx, _items: &[GenItem]) -> Vec<Vec<(String, String)>> {
vec![] //TODO
}
4 changes: 2 additions & 2 deletions test/tests/namespace-custom-cs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ NAMESPACE=$(random_string)
run_test cargo run generate --out-dir "${TMP_DIR}" --lang cs --namespace "${NAMESPACE}" --project-path "${PROJECT_PATH}"

LINES="$(grep -r -o "namespace ${NAMESPACE}" "${TMP_DIR}" | wc -l | tr -d ' ')"
if [ "${LINES}" != 4 ] ; then
if [ "${LINES}" != 5 ] ; then
echo "FOUND: ${LINES} EXPECTED: "
exit 1
fi

LINES="$(grep -r -o "using SpacetimeDB;" "${TMP_DIR}" | wc -l | tr -d ' ')"
if [ "${LINES}" != 4 ] ; then
if [ "${LINES}" != 5 ] ; then
echo "FOUND: ${LINES} EXPECTED: "
exit 1
fi
Expand Down
2 changes: 1 addition & 1 deletion test/tests/namespace-spacetimedb-cs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ set -euox pipefail
source "./test/lib.include"

TMP_DIR=$(mktemp -d)
NAMESPACE=SpacetimeDB
NAMESPACE=SpacetimeDB.Types

run_test cargo run generate --out-dir "${TMP_DIR}" --lang cs --project-path "${PROJECT_PATH}"

Expand Down
Loading