8000 feat: types generator command by loks0n · Pull Request #784 · appwrite/sdk-generator · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
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 47 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 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
8000
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
eb8bd87
fix: type generation for java with class based approach
ChiragAgg5k May 30, 2025
65a27c3
fix: compilation error in java
ChiragAgg5k May 30, 2025
1eab080
fix: php and swift
ChiragAgg5k May 30, 2025
59a6674
chore: return relationship type in dart
ChiragAgg5k May 30, 2025
4cabce3
chore: add javascript jsdocs
ChiragAgg5k May 30, 2025
0d46a41
chore: add undefined for options in js
ChiragAgg5k May 30, 2025
70b0e93
fix: add constructor for php lang
ChiragAgg5k May 30, 2025
4690460
fix: required
ChiragAgg5k May 30, 2025
2e4b643
feat: add getters and setters for php
ChiragAgg5k May 30, 2025
b8f07c0
feat: complete codable for swift
ChiragAgg5k May 30, 2025
ea25a56
chore: fix naming with spaces
ChiragAgg5k May 30, 2025
7a74cc6
feat: fix relationship attribute mapping
ChiragAgg5k May 30, 2025
47e51e1
fix: missing relationship, use primitives in java
ChiragAgg5k May 30, 2025
0f60155
feat: add support for enum
ChiragAgg5k May 30, 2025
41282ce
feat: add java enum
ChiragAgg5k May 30, 2025
d9ca85a
chore: complete num for js and kotlin
ChiragAgg5k May 31, 2025
5ffd5fa
chore: finish enum for php ts and swift
ChiragAgg5k May 31, 2025
41efeed
Update templates/cli/lib/type-generation/languages/java.js.twig
ChiragAgg5k May 31, 2025
8a759ca
feat: add factory and toMap methods for Dart model generation
ChiragAgg5k May 31, 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
50 changes: 50 additions & 0 deletions src/SDK/Language/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,51 @@ 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/javascript.js',
'template' => 'cli/lib/type-generation/languages/javascript.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 +320,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
126 ch 8000 anges: 126 additions & 0 deletions templates/cli/lib/commands/types.js.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
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");
const { JavaScript } = require("../type-generation/languages/javascript");

/**
* @param {string} language
* @returns {import("../type-generation/languages/language").LanguageMeta}
*/
function createLanguageMeta(language) {
switch (language) {
case "ts":
return new TypeScript();
case "js":
return new JavaScript();
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", "js", "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(`Generated types for all the listed collections`);
});

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,
};
152 changes: 152 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,152 @@
/** @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:
type = "String";
if (attribute.format === AttributeType.ENUM) {
type = LanguageMeta.toPascalCase(attribute.key);
}
break;
case AttributeType.INTEGER:
type = "int";
break;
case AttributeType.FLOAT:
type = "double";
break;
case AttributeType.BOOLEAN:
type = "bool";
break;
case AttributeType.RELATIONSHIP:
type = LanguageMeta.toPascalCase(attribute.relatedCollection);
if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany') {
type = `List<${type}>`;
}
break;
default:
throw new Error(`Unknown attribute type: ${attribute.type}`);
}
if (attribute.array) {
type = `List<${type}>`;
}
if (!attribute.required) {
type += "?";
}
return type;
}

getTemplate() {
return `<% for (const attribute of collection.attributes) { -%>
<% if (attribute.type === 'relationship') { -%>
import '<%- attribute.relatedCollection.toLowerCase() %>.dart';

<% } -%>
<% } -%>
<% for (const attribute of collection.attributes) { -%>
<% if (attribute.format === 'enum') { -%>
enum <%- toPascalCase(attribute.key) %> {
<% for (const element of attribute.elements) { -%>
<%- element %>,
<% } -%>
}

<% } -%>
<% } -%>
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) %>,
<% } -%>
});

factory <%= toPascalCase(collection.name) %>.fromMap(Map<String, dynamic> map) {
return <%= toPascalCase(collection.name) %>(
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
<%= toCamelCase(attribute.key) %>: <% if (attribute.type === 'string' || attribute.type === 'email' || attribute.type === 'datetime') { -%>
<% if (attribute.format === 'enum') { -%>
<% if (attribute.array) { -%>
(map['<%= attribute.key %>'] as List<dynamic>?)?.map((e) =&g E060 t; <%- toPascalCase(attribute.key) %>.values.firstWhere((element) => element.name == e)).toList()<% if (!attribute.required) { %> ?? []<% } -%>
<% } else { -%>
<% if (!attribute.required) { -%>
map['<%= attribute.key %>'] != null ? <%- toPascalCase(attribute.key) %>.values.where((e) => e.name == map['<%= attribute.key %>']).firstOrNull : null<% } else { -%>
<%- toPascalCase(attribute.key) %>.values.firstWhere((e) => e.name == map['<%= attribute.key %>'])<% } -%>
<% } -%>
<% } else { -%>
<% if (attribute.array) { -%>
List<String>.from(map['<%= attribute.key %>'] ?? [])<% if (!attribute.required) { %> ?? []<% } -%>
<% } else { -%>
map['<%= attribute.key %>']<% if (!attribute.required) { %>?<% } %>.toString()<% if (!attribute.required) { %> ?? null<% } -%>
<% } -%>
<% } -%>
<% } else if (attribute.type === 'integer') { -%>
<% if (attribute.array) { -%>
List<int>.from(map['<%= attribute.key %>'] ?? [])<% if (!attribute.required) { %> ?? []<% } -%>
<% } else { -%>
map['<%= attribute.key %>']<% if (!attribute.required) { %> ?? null<% } -%>
<% } -%>
<% } else if (attribute.type === 'float') { -%>
<% if (attribute.array) { -%>
List<double>.from(map['<%= attribute.key %>'] ?? [])<% if (!attribute.required) { %> ?? []<% } -%>
<% } else { -%>
map['<%= attribute.key %>']<% if (!attribute.required) { %> ?? null<% } -%>
<% } -%>
<% } else if (attribute.type === 'boolean') { -%>
<% if (attribute.array) { -%>
List<bool>.from(map['<%= attribute.key %>'] ?? [])<% if (!attribute.required) { %> ?? []<% } -%>
<% } else { -%>
map['<%= attribute.key %>']<% if (!attribute.required) { %> ?? null<% } -%>
<% } -%>
<% } else if (attribute.type === 'relationship') { -%>
<% if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany') { -%>
(map['<%= attribute.key %>'] as List<dynamic>?)?.map((e) => <%- toPascalCase(attribute.relatedCollection) %>.fromMap(e)).toList()<% if (!attribute.required) { %> ?? []<% } -%>
<% } else { -%>
<% if (!attribute.required) { -%>
map['<%= attribute.key %>'] != null ? <%- toPascalCase(attribute.relatedCollection) %>.fromMap(map['<%= attribute.key %>']) : null<% } else { -%>
<%- toPascalCase(attribute.relatedCollection) %>.fromMap(map['<%= attribute.key %>'])<% } -%>
<% } -%>
<% } -%>,
<% } -%>
);
}

Map<String, dynamic> toMap() {
return {
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
"<%= attribute.key %>": <% if (attribute.type === 'relationship') { -%>
<% if ((attribute.relationType === 'oneToMany' && attribute.side === 'parent') || (attribute.relationType === 'manyToOne' && attribute.side === 'child') || attribute.relationType === 'manyToMany') { -%>
<%= toCamelCase(attribute.key) %><% if (!attribute.required) { %>?<% } %>.map((e) => e.toMap()).toList()<% if (!attribute.required) { %> ?? []<% } -%>
<% } else { -%>
<%= toCamelCase(attribute.key) %><% if (!attribute.required) { %>?<% } %>.toMap()<% if (!attribute.required) { %> ?? {}<% } -%>
<% } -%>
<% } else if (attribute.format === 'enum') { -%>
<% if (attribute.array) { -%>
<%= toCamelCase(attribute.key) %><% if (!attribute.required) { %>?<% } %>.map((e) => e.name).toList()<% if (!attribute.required) { %> ?? []<% } -%>
<% } else { -%>
<%= toCamelCase(attribute.key) %><% if (!attribute.required) { %>?<% } %>.name<% if (!attribute.required) { %> ?? null<% } -%>
<% } -%>
<% } else { -%>
<%= toCamelCase(attribute.key) -%>
<% } -%>,
<% } -%>
};
}
}
`;
}

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

module.exports = { Dart };
Loading
0