Skip to content

feat: types generator command #784

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

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5bbb5a1
feat: initial draft types generator
loks0n Mar 6, 2024
3a72504
feat: typo in template name
loks0n Mar 6, 2024
bbb4c09
Merge branch '1.5.x' of https://github.com/appwrite/sdk-generator int…
loks0n Mar 6, 2024
3d501a5
fix: use cjs
loks0n Mar 6, 2024
b058588
feat: add ejs
loks0n Mar 6, 2024
1ef2bdc
feat: more stuff
loks0n Mar 7, 2024
ec07a09
feat: python
loks0n Mar 7, 2024
0163bfa
chore: fmt fixes
loks0n Mar 7, 2024
29cbd6b
fix: php, python
loks0n Mar 7, 2024
08689e1
fix: python, php
loks0n Mar 7, 2024
f5ce7c4
fix: python formatting
loks0n Mar 7, 2024
fe9ed0e
fix: formatting
loks0n Mar 7, 2024
d3c471f
fix: formatting
loks0n Mar 7, 2024
731dcf8
fix: formatting 2
loks0n Mar 7, 2024
6f067aa
feat: swift, kotlin
loks0n Mar 7, 2024
d3c2134
feat: java
loks0n Mar 8, 2024
69e401f
feat: php fix
loks0n Mar 8, 2024
b833573
Merge branch 'master' of https://github.com/appwrite/sdk-generator in…
loks0n Mar 8, 2024
61e475d
feat: python models
loks0n Mar 9, 2024
78b5af3
Merge branch 'master' into feat-types-generator-command
ChiragAgg5k May 20, 2025
78ecd2a
chore: whitespace fixes
ChiragAgg5k May 20, 2025
271a4d6
chore: formatting
ChiragAgg5k May 20, 2025
a96ed05
chore: more whitespace stuff
ChiragAgg5k May 20, 2025
734fbc2
chore: remove python generation
ChiragAgg5k May 20, 2025
d4dc6d7
chore: work in progress dart
ChiragAgg5k May 20, 2025
e0ea6ec
chore: fix dart
ChiragAgg5k May 20, 2025
737a136
chore: remove python models
ChiragAgg5k May 20, 2025
a75de52
chore: review changes
ChiragAgg5k May 20, 2025
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
45 changes: 45 additions & 0 deletions src/SDK/Language/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,46 @@ public function getFiles(): array
'destination' => 'lib/sdks.js',
'template' => 'cli/lib/sdks.js.twig',
],
[
'scope' => 'default',
'destination' => 'lib/type-generation/attribute.js',
'template' => 'cli/lib/type-generation/attribute.js.twig',
],
[
'scope' => 'default',
'destination' => 'lib/type-generation/languages/language.js',
'template' => 'cli/lib/type-generation/languages/language.js.twig',
],
[
'scope' => 'default',
'destination' => 'lib/type-generation/languages/php.js',
'template' => 'cli/lib/type-generation/languages/php.js.twig',
],
[
'scope' => 'default',
'destination' => 'lib/type-generation/languages/typescript.js',
'template' => 'cli/lib/type-generation/languages/typescript.js.twig',
],
[
'scope' => 'default',
'destination' => 'lib/type-generation/languages/kotlin.js',
'template' => 'cli/lib/type-generation/languages/kotlin.js.twig',
],
[
'scope' => 'default',
'destination' => 'lib/type-generation/languages/swift.js',
'template' => 'cli/lib/type-generation/languages/swift.js.twig',
],
[
'scope' => 'default',
'destination' => 'lib/type-generation/languages/java.js',
'template' => 'cli/lib/type-generation/languages/java.js.twig',
],
[
'scope' => 'default',
'destination' => 'lib/type-generation/languages/dart.js',
'template' => 'cli/lib/type-generation/languages/dart.js.twig',
],
[
'scope' => 'default',
'destination' => 'lib/questions.js',
Expand Down Expand Up @@ -275,6 +315,11 @@ public function getFiles(): array
'scope' => 'default',
'destination' => 'lib/commands/organizations.js',
'template' => 'cli/lib/commands/organizations.js.twig',
],
[
'scope' => 'default',
'destination' => 'lib/commands/types.js',
'template' => 'cli/lib/commands/types.js.twig',
]
];
}
Expand Down
2 changes: 2 additions & 0 deletions templates/cli/index.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const inquirer = require("inquirer");
{% if sdk.test != "true" %}
const { login, logout, whoami, migrate, register } = require("./lib/commands/generic");
const { init } = require("./lib/commands/init");
const { types } = require("./lib/commands/types");
const { pull } = require("./lib/commands/pull");
const { run } = require("./lib/commands/run");
const { push, deploy } = require("./lib/commands/push");
Expand Down Expand Up @@ -68,6 +69,7 @@ program
.addCommand(init)
.addCommand(pull)
.addCommand(push)
.addCommand(types)
.addCommand(deploy)
.addCommand(run)
.addCommand(logout)
Expand Down
123 changes: 123 additions & 0 deletions templates/cli/lib/commands/types.js.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
const ejs = require("ejs");
const fs = require("fs");
const path = require("path");
const { LanguageMeta, detectLanguage } = require("../type-generation/languages/language");
const { Command, Option, Argument } = require("commander");
const { localConfig } = require("../config");
const { success, log, actionRunner } = require("../parser");
const { PHP } = require("../type-generation/languages/php");
const { TypeScript } = require("../type-generation/languages/typescript");
const { Kotlin } = require("../type-generation/languages/kotlin");
const { Swift } = require("../type-generation/languages/swift");
const { Java } = require("../type-generation/languages/java");
const { Dart } = require("../type-generation/languages/dart");

/**
* @param {string} language
* @returns {import("../type-generation/languages/language").LanguageMeta}
*/
function createLanguageMeta(language) {
switch (language) {
case "ts":
return new TypeScript();
case "php":
return new PHP();
case "kotlin":
return new Kotlin();
case "swift":
return new Swift();
case "java":
return new Java();
case "dart":
return new Dart();
default:
throw new Error(`Language '${language}' is not supported`);
}
}

const templateHelpers = {
toPascalCase: LanguageMeta.toPascalCase,
toCamelCase: LanguageMeta.toCamelCase,
toSnakeCase: LanguageMeta.toSnakeCase,
toKebabCase: LanguageMeta.toKebabCase,
toUpperSnakeCase: LanguageMeta.toUpperSnakeCase
}

const typesOutputArgument = new Argument(
"<output-directory>",
"The directory to write the types to"
);

const typesLanguageOption = new Option(
"-l, --language <language>",
"The language of the types"
)
.choices(["auto", "ts", "php", "kotlin", "swift", "java", "dart"])
.default("auto");

const typesCommand = actionRunner(async (rawOutputDirectory, {language}) => {
if (language === "auto") {
language = detectLanguage();
log(`Detected language: ${language}`);
}

const meta = createLanguageMeta(language);

const outputDirectory = path.resolve(rawOutputDirectory);
if (!fs.existsSync(outputDirectory)) {
log(`Directory: ${outputDirectory} does not exist, creating...`);
fs.mkdirSync(outputDirectory, { recursive: true });
}

if (!fs.existsSync("appwrite.json")) {
throw new Error("appwrite.json not found in current directory");
}

const collections = localConfig.getCollections();
if (collections.length === 0) {
throw new Error("No collections found in appwrite.json");
}

log(`Found ${collections.length} collections: ${collections.map(c => c.name).join(", ")}`);

const totalAttributes = collections.reduce((count, collection) => count + collection.attributes.length, 0);
log(`Found ${totalAttributes} attributes across all collections`);

const templater = ejs.compile(meta.getTemplate());

if (meta.isSingleFile()) {
const content = templater({
collections,
...templateHelpers,
getType: meta.getType
});

const destination = path.join(outputDirectory, meta.getFileName());

fs.writeFileSync(destination, content);
log(`Added types to ${destination}`);
} else {
for (const collection of collections) {
const content = templater({
collection,
...templateHelpers,
getType: meta.getType
});

const destination = path.join(outputDirectory, meta.getFileName(collection));

fs.writeFileSync(destination, content);
log(`Added types for ${collection.name} to ${destination}`);
}
}

success();
});

const types = new Command("types")
.description("Generate types for your Appwrite project")
.addArgument(typesOutputArgument)
.addOption(typesLanguageOption)
.action(actionRunner(typesCommand));

module.exports = { types };
16 changes: 16 additions & 0 deletions templates/cli/lib/type-generation/attribute.js.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const AttributeType = {
STRING: "string",
INTEGER: "integer",
FLOAT: "float",
BOOLEAN: "boolean",
DATETIME: "datetime",
EMAIL: "email",
IP: "ip",
URL: "url",
ENUM: "enum",
RELATIONSHIP: "relationship",
};

module.exports = {
AttributeType,
};
59 changes: 59 additions & 0 deletions templates/cli/lib/type-generation/languages/dart.js.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/** @typedef {import('../attribute').Attribute} Attribute */
const { AttributeType } = require('../attribute');
const { LanguageMeta } = require("./language");

class Dart extends LanguageMeta {
getType(attribute) {
let type = "";
switch (attribute.type) {
case AttributeType.STRING:
case AttributeType.EMAIL:
case AttributeType.DATETIME:
case AttributeType.ENUM:
type = "String";
break;
case AttributeType.INTEGER:
type = "int";
break;
case AttributeType.FLOAT:
type = "double";
break;
case AttributeType.BOOLEAN:
type = "bool";
break;
case AttributeType.RELATIONSHIP:
type = "Map<String, dynamic>";
break;
default:
throw new Error(`Unknown attribute type: ${attribute.type}`);
}
if (attribute.array) {
type = `List<${type}>`;
}
if (!attribute.required) {
type += "?";
}
return type;
}

getTemplate() {
return `class <%= toPascalCase(collection.name) %> {
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
<%= getType(attribute) %> <%= toCamelCase(attribute.key) %>;
<% } -%>

<%= toPascalCase(collection.name) %>({
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
<% if (attribute.required) { %>required <% } %>this.<%= toCamelCase(attribute.key) %>,
<% } -%>
});
}
`;
}

getFileName(collection) {
return LanguageMeta.toSnakeCase(collection.name) + ".dart";
}
}

module.exports = { Dart };
56 changes: 56 additions & 0 deletions templates/cli/lib/type-generation/languages/java.js.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/** @typedef {import('../attribute').Attribute} Attribute */
const { AttributeType } = require('../attribute');
const { LanguageMeta } = require("./language");

class Java extends LanguageMeta {
getType(attribute) {
let type = "";
switch (attribute.type) {
case AttributeType.STRING:
case AttributeType.EMAIL:
case AttributeType.DATETIME:
case AttributeType.ENUM:
type = "String";
break;
case AttributeType.INTEGER:
type = "Int";
break;
case AttributeType.FLOAT:
type = "Float";
break;
case AttributeType.BOOLEAN:
type = "Boolean";
break;
case AttributeType.RELATIONSHIP:
type = "Map<String, Any>";
break;
default:
throw new Error(`Unknown attribute type: ${attribute.type}`);
}
if (attribute.array) {
type = "List<" + type + ">";
}
if (attribute.required) {
type += "?";
}
return type;
}

getTemplate() {
return `package io.appwrite.models;

import java.util.*;

public record <%- toPascalCase(collection.name) %>(
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
<%= getType(attribute) %> <%= toCamelCase(attribute.key) %><%- index < collection.attributes.length - 1 ? ',' : '' %>
<% } -%>
) { }`;
}

getFileName(collection) {
return LanguageMeta.toPascalCase(collection.name) + ".java";
}
}

module.exports = { Java };
54 changes: 54 additions & 0 deletions templates/cli/lib/type-generation/languages/kotlin.js.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/** @typedef {import('../attribute').Attribute} Attribute */
const { AttributeType } = require('../attribute');
const { LanguageMeta } = require("./language");

class Kotlin extends LanguageMeta {
getType(attribute) {
let type = "";
switch (attribute.type) {
case AttributeType.STRING:
case AttributeType.EMAIL:
case AttributeType.DATETIME:
case AttributeType.ENUM:
type = "String";
break;
case AttributeType.INTEGER:
type = "Int";
break;
case AttributeType.FLOAT:
type = "Float";
break;
case AttributeType.BOOLEAN:
type = "Boolean";
break;
case AttributeType.RELATIONSHIP:
type = "Map<String, Any>";
break;
default:
throw new Error(`Unknown attribute type: ${attribute.type}`);
}
if (attribute.array) {
type = "List<" + type + ">";
}
if (attribute.required) {
type += "?";
}
return type;
}

getTemplate() {
return `package io.appwrite.models

data class <%- toPascalCase(collection.name) %>(
<% for (const attribute of collection.attributes) { -%>
val <%- toCamelCase(attribute.key) %>: <%- getType(attribute) %>,
<% } -%>
)`;
}

getFileName(collection) {
return LanguageMeta.toPascalCase(collection.name) + ".kt";
}
}

module.exports = { Kotlin };
Loading