Skip to content

Commit

Permalink
chore: refactor zmodel-code-generator and move it to sdk package (#836)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Nov 17, 2023
1 parent cccbc3c commit b962e05
Show file tree
Hide file tree
Showing 23 changed files with 691 additions and 354 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zenstack-monorepo",
"version": "1.3.0",
"version": "1.3.1",
"description": "",
"scripts": {
"build": "pnpm -r build",
Expand Down
2 changes: 1 addition & 1 deletion packages/language/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/language",
"version": "1.3.0",
"version": "1.3.1",
"displayName": "ZenStack modeling language compiler",
"description": "ZenStack modeling language compiler",
"homepage": "https://zenstack.dev",
Expand Down
3 changes: 2 additions & 1 deletion packages/language/src/generated/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,8 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel
"rule": {
"$ref": "#/rules@15"
},
"arguments": []
"arguments": [],
"cardinality": "?"
},
{
"$type": "Keyword",
Expand Down
4 changes: 1 addition & 3 deletions packages/language/src/zmodel.langium
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ ArrayExpr:
'[' (items+=Expression (',' items+=Expression)*)? ']';

ConfigInvocationExpr:
name=ID ('(' ConfigInvocationArgList ')')?;
name=ID ('(' ConfigInvocationArgList? ')')?;

fragment ConfigInvocationArgList:
args+=ConfigInvocationArg (',' args+=ConfigInvocationArg)*;
Expand All @@ -67,7 +67,6 @@ ConfigExpr:
LiteralExpr | InvocationExpr | ConfigArrayExpr;

type ReferenceTarget = FunctionParam | DataModelField | EnumField;

ThisExpr:
value=THIS;

Expand Down Expand Up @@ -259,7 +258,6 @@ AttributeParamType:
(type=(ExpressionType | 'FieldReference' | 'TransitiveFieldReference' | 'ContextType') | reference=[TypeDeclaration:RegularID]) (array?='[' ']')? (optional?='?')?;

type TypeDeclaration = DataModel | Enum;

DataModelFieldAttribute:
decl=[Attribute:DataModelFieldAttributeName] ('(' AttributeArgList? ')')?;

Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/openapi/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/openapi",
"displayName": "ZenStack Plugin and Runtime for OpenAPI",
"version": "1.3.0",
"version": "1.3.1",
"description": "ZenStack plugin and runtime supporting OpenAPI",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/swr/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/swr",
"displayName": "ZenStack plugin for generating SWR hooks",
"version": "1.3.0",
"version": "1.3.1",
"description": "ZenStack plugin for generating SWR hooks",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/tanstack-query/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/tanstack-query",
"displayName": "ZenStack plugin for generating tanstack-query hooks",
"version": "1.3.0",
"version": "1.3.1",
"description": "ZenStack plugin for generating tanstack-query hooks",
"main": "index.js",
"exports": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/trpc/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/trpc",
"displayName": "ZenStack plugin for tRPC",
"version": "1.3.0",
"version": "1.3.1",
"description": "ZenStack plugin for tRPC",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/runtime",
"displayName": "ZenStack Runtime Library",
"version": "1.3.0",
"version": "1.3.1",
"description": "Runtime of ZenStack for both client-side and server-side environments.",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publisher": "zenstack",
"displayName": "ZenStack Language Tools",
"description": "Build scalable web apps with minimum code by defining authorization and validation rules inside the data schema that closer to the database",
"version": "1.3.0",
"version": "1.3.1",
"author": {
"name": "ZenStack Team"
},
Expand Down
11 changes: 7 additions & 4 deletions packages/schema/src/cli/plugin-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ export class PluginRunner {
const plugins: PluginInfo[] = [];
const pluginDecls = options.schema.declarations.filter((d): d is Plugin => isPlugin(d));

let prismaOutput = resolvePath('./prisma/schema.prisma', { schemaPath: options.schemaPath, name: '' });
let prismaOutput = resolvePath('./prisma/schema.prisma', { schemaPath: options.schemaPath });

for (const pluginDecl of pluginDecls) {
const pluginProvider = this.getPluginProvider(pluginDecl);
if (!pluginProvider) {
console.error(`Plugin ${pluginDecl.name} has invalid provider option`);
throw new PluginError('', `Plugin ${pluginDecl.name} has invalid provider option`);
}
const pluginModulePath = this.getPluginModulePath(pluginProvider);
const pluginModulePath = this.getPluginModulePath(pluginProvider, options);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let pluginModule: any;
try {
Expand Down Expand Up @@ -117,7 +117,7 @@ export class PluginRunner {
plugins.unshift(existing);
} else {
// synthesize a plugin and insert front
const pluginModule = require(this.getPluginModulePath(corePlugin.provider));
const pluginModule = require(this.getPluginModulePath(corePlugin.provider, options));
const pluginName = this.getPluginName(pluginModule, corePlugin.provider);
plugins.unshift({
name: pluginName,
Expand Down Expand Up @@ -300,7 +300,10 @@ export class PluginRunner {
}
}

private getPluginModulePath(provider: string) {
private getPluginModulePath(provider: string, options: Pick<PluginOptions, 'schemaPath'>) {
if (path.isAbsolute(provider) || provider.startsWith('.')) {
return resolvePath(provider, options);
}
let pluginModulePath = provider;
if (pluginModulePath.startsWith('@core/')) {
pluginModulePath = pluginModulePath.replace(/^@core/, path.join(__dirname, '../plugins'));
Expand Down
10 changes: 5 additions & 5 deletions packages/schema/src/plugins/prisma/schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import {
PassThroughAttribute as PrismaPassThroughAttribute,
SimpleField,
} from './prisma-builder';
import ZModelCodeGenerator from './zmodel-code-generator';
import { ZModelCodeGenerator } from '@zenstackhq/sdk';

const MODEL_PASSTHROUGH_ATTR = '@@prisma.passthrough';
const FIELD_PASSTHROUGH_ATTR = '@prisma.passthrough';
Expand Down Expand Up @@ -266,7 +266,7 @@ export default class PrismaSchemaGenerator {

decl.attributes
.filter((attr) => attr.decl.ref && !this.isPrismaAttribute(attr))
.forEach((attr) => model.addComment('/// ' + this.zModelGenerator.generateAttribute(attr)));
.forEach((attr) => model.addComment('/// ' + this.zModelGenerator.generate(attr)));

// user defined comments pass-through
decl.comments.forEach((c) => model.addComment(c));
Expand Down Expand Up @@ -313,7 +313,7 @@ export default class PrismaSchemaGenerator {

const nonPrismaAttributes = field.attributes.filter((attr) => attr.decl.ref && !this.isPrismaAttribute(attr));

const documentations = nonPrismaAttributes.map((attr) => '/// ' + this.zModelGenerator.generateAttribute(attr));
const documentations = nonPrismaAttributes.map((attr) => '/// ' + this.zModelGenerator.generate(attr));

const result = model.addField(field.name, type, attributes, documentations);

Expand Down Expand Up @@ -413,7 +413,7 @@ export default class PrismaSchemaGenerator {

decl.attributes
.filter((attr) => attr.decl.ref && !this.isPrismaAttribute(attr))
.forEach((attr) => _enum.addComment('/// ' + this.zModelGenerator.generateAttribute(attr)));
.forEach((attr) => _enum.addComment('/// ' + this.zModelGenerator.generate(attr)));

// user defined comments pass-through
decl.comments.forEach((c) => _enum.addComment(c));
Expand All @@ -426,7 +426,7 @@ export default class PrismaSchemaGenerator {

const nonPrismaAttributes = field.attributes.filter((attr) => attr.decl.ref && !this.isPrismaAttribute(attr));

const documentations = nonPrismaAttributes.map((attr) => '/// ' + this.zModelGenerator.generateAttribute(attr));
const documentations = nonPrismaAttributes.map((attr) => '/// ' + this.zModelGenerator.generate(attr));
_enum.addField(field.name, attributes, documentations);
}
}
177 changes: 0 additions & 177 deletions packages/schema/src/plugins/prisma/zmodel-code-generator.ts

This file was deleted.

Loading

0 comments on commit b962e05

Please sign in to comment.