-
Notifications
You must be signed in to change notification settings - Fork 181
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
Template for creating a new SDK generator #4010
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ebf3a54
Simple Unit Test Generator
mikemilla 328fc3d
Generator Code
mikemilla 9594155
Generator LANGUAGE_NAME
mikemilla 2e5b737
Swift Init
mikemilla 25513e7
Writer and Astnode as commons
mikemilla 01f00dd
More function functionality
mikemilla a2b76f3
Lang Gen v2
mikemilla 49fda54
Lang Gen v3
mikemilla 3b40044
Oops
mikemilla 409eff7
Polish
mikemilla 50b4a70
Writer indentation
mikemilla f775291
Polished imports
mikemilla 5fc82d0
Refined imports
mikemilla a7ccd13
Template generator notes
mikemilla ac0f015
Template Generator
mikemilla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Writer } from "./Writer"; | ||
|
||
export abstract class AstNode { | ||
|
||
private indentSize: number; | ||
|
||
constructor(indentSize: number) { | ||
this.indentSize = indentSize; | ||
} | ||
|
||
/** | ||
* Every AST node knows how to write itself to a string. | ||
*/ | ||
public abstract write(writer: Writer): void; | ||
|
||
/** | ||
* Writes the node to a string. | ||
*/ | ||
public toString(): string { | ||
const writer = new Writer(this.indentSize); | ||
this.write(writer); | ||
return writer.toString(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { AstNode } from "./AstNode"; | ||
|
||
export declare namespace Writer { | ||
interface Args { | ||
tabSize?: number; | ||
} | ||
} | ||
|
||
export class Writer { | ||
|
||
private buffer: string; | ||
private indentLevel: number; | ||
private hasWrittenAnything: boolean; | ||
private indentSize: number; | ||
|
||
constructor(indentSize: number) { | ||
this.buffer = ""; | ||
this.indentLevel = 0; | ||
this.hasWrittenAnything = false; | ||
this.indentSize = indentSize; | ||
} | ||
|
||
public write(text: string): void { | ||
|
||
if (this.hasWrittenAnything) { | ||
this.buffer += "\n"; | ||
} | ||
|
||
const indent = this.getIndentString(this.indentSize); | ||
const indentedText = indent + text.replace(/\n/g, `\n${indent}`); | ||
|
||
this.buffer += indentedText; | ||
|
||
this.hasWrittenAnything = true; | ||
|
||
} | ||
|
||
public newLine(): void { | ||
this.buffer += "\n"; | ||
} | ||
|
||
public openIndent(): void { | ||
this.indentLevel++; | ||
} | ||
|
||
public closeIndent(): void { | ||
if (this.indentLevel > 0) { | ||
this.indentLevel--; | ||
} | ||
} | ||
|
||
public writeNode(node: AstNode): void { | ||
node.write(this); | ||
} | ||
|
||
public toString(): string { | ||
return this.buffer; | ||
} | ||
|
||
private getIndentString(tabSize: number): string { | ||
return " ".repeat(this.indentLevel * tabSize); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
export * from "./AbstractGeneratorCli"; | ||
export * from "./AbstractGeneratorContext"; | ||
export * from "./AstNode"; | ||
export * from "./BaseGeneratorConfig"; | ||
export * from "./ConfigUtilities"; | ||
export * from "./AbstractGeneratorContext"; | ||
export * from "./PersistedProject"; | ||
export * from "./GeneratorNotificationService"; | ||
export * from "./parseGeneratorConfig"; | ||
export * from "./AbstractGeneratorCli"; | ||
export * from "./PersistedProject"; | ||
export * from "./Writer"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,25 @@ | ||
{ | ||
"extends": "../../shared/tsconfig.shared.json", | ||
"compilerOptions": { "composite": true, "outDir": "lib", "rootDir": "src" }, | ||
"include": ["./src/**/*"], | ||
"compilerOptions": { | ||
"composite": true, | ||
"outDir": "lib", | ||
"rootDir": "src" | ||
}, | ||
"include": [ | ||
"./src/**/*" | ||
], | ||
"references": [ | ||
{ "path": "../../packages/cli/logger" }, | ||
{ "path": "../../packages/commons/core-utils" }, | ||
{ "path": "../../packages/commons/fs-utils" }, | ||
{ "path": "../../packages/commons/logging-execa" } | ||
{ | ||
"path": "../../packages/cli/logger" | ||
}, | ||
{ | ||
"path": "../../packages/commons/core-utils" | ||
}, | ||
{ | ||
"path": "../../packages/commons/fs-utils" | ||
}, | ||
{ | ||
"path": "../../packages/commons/logging-execa" | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require("fern/.prettierrc"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "../../../shared/jest.config.shared"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ | ||
"name": "@fern-api/LANGUAGE_NAME-codegen", | ||
"version": "0.0.0", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/fern-api/fern.git", | ||
"directory": "generators/LANGUAGE_NAME/codegen" | ||
}, | ||
"files": [ | ||
"lib" | ||
], | ||
"type": "module", | ||
"source": "src/index.ts", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"sideEffects": false, | ||
"scripts": { | ||
"clean": "rm -rf ./lib && tsc --build --clean", | ||
"compile": "tsc --build", | ||
"test": "yarn compile && jest --passWithNoTests", | ||
"lint:eslint": "eslint --max-warnings 0 . --ignore-path=../../../.eslintignore", | ||
"lint:eslint:fix": "yarn lint:eslint --fix", | ||
"format": "prettier --write --ignore-unknown --ignore-path ../../../shared/.prettierignore \"**\"", | ||
"format:check": "prettier --check --ignore-unknown --ignore-path ../../../shared/.prettierignore \"**\"", | ||
"organize-imports": "organize-imports-cli tsconfig.json", | ||
"depcheck": "depcheck" | ||
}, | ||
"dependencies": { | ||
"@fern-api/core-utils": "workspace:*", | ||
"@fern-api/fs-utils": "workspace:*", | ||
"@fern-api/generator-commons": "workspace:*", | ||
"@fern-api/logging-execa": "workspace:*", | ||
"@fern-fern/ir-sdk": "^32", | ||
"lodash-es": "^4.17.21", | ||
"ts-node": "^10.9.2", | ||
"zod": "^3.22.3" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^29.0.3", | ||
"@types/lodash-es": "^4.17.12", | ||
"@types/node": "^18.7.18", | ||
"depcheck": "^1.4.6", | ||
"eslint": "^8.56.0", | ||
"jest": "^29.7.0", | ||
"organize-imports-cli": "^0.10.0", | ||
"typescript": "4.6.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { AstNode, Writer } from "@fern-api/generator-commons"; | ||
import Lang, { Func } from "../lang"; | ||
|
||
export declare namespace Class { | ||
interface Args { | ||
name: string, | ||
functions: Func[] | ||
} | ||
} | ||
|
||
export class Class extends AstNode { | ||
|
||
public readonly name: string; | ||
public readonly functions: Func[]; | ||
|
||
constructor({ | ||
name, | ||
functions, | ||
}: Class.Args) { | ||
super(Lang.indentSize); | ||
this.name = name; | ||
this.functions = functions; | ||
} | ||
|
||
public write(writer: Writer): void { | ||
|
||
writer.write(`class ${this.name} {`); | ||
|
||
writer.newLine(); | ||
|
||
writer.openIndent(); | ||
|
||
this.functions.forEach(func => { | ||
writer.writeNode(func); | ||
writer.newLine(); | ||
}); | ||
|
||
writer.closeIndent(); | ||
|
||
writer.write("}"); | ||
|
||
|
||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be nice to have a