Skip to content

Commit

Permalink
feat(python): Introduce Initial Boilerplate for Python V2 Codegen (#4913
Browse files Browse the repository at this point in the history
)
  • Loading branch information
noanflaherty authored Oct 14, 2024
1 parent d397d1b commit 44a67c1
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 0 deletions.
11 changes: 11 additions & 0 deletions generators/pythonv2/codegen/.depcheckrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"ignores": [
"@types/jest",
"globals",
"@types/node",
"@fern-fern/ir-sdk"
],
"ignore-patterns": [
"lib"
]
}
1 change: 1 addition & 0 deletions generators/pythonv2/codegen/.prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("../../../.prettierrc.json");
43 changes: 43 additions & 0 deletions generators/pythonv2/codegen/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "@fern-api/pythonv2-codegen",
"version": "0.0.0",
"repository": {
"type": "git",
"url": "https://github.com/fern-api/fern.git",
"directory": "generators/pythonv2/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": "vitest --run",
"test:update": "vitest --run -u",
"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/generator-commons": "workspace:*",
"@fern-fern/ir-sdk": "^53.7.0"
},
"devDependencies": {
"@types/jest": "^29.5.12",
"@types/node": "^18.7.18",
"depcheck": "^1.4.6",
"eslint": "^8.56.0",
"organize-imports-cli": "^0.10.0",
"prettier": "^2.7.1",
"typescript": "4.6.4",
"vitest": "^2.0.5"
}
}
22 changes: 22 additions & 0 deletions generators/pythonv2/codegen/src/ast/Class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { AstNode } from "./core/AstNode";
import { Writer } from "./core/Writer";

export declare namespace Class {
interface Args {
/* The name of the Python class */
name: string;
}
}

export class Class extends AstNode {
public readonly name: string;

constructor({ name }: Class.Args) {
super();
this.name = name;
}

public write(writer: Writer): void {
writer.write(`class ${this.name}:`);
}
}
10 changes: 10 additions & 0 deletions generators/pythonv2/codegen/src/ast/__test__/Class.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { python } from "../..";

describe("class", () => {
it("basic", async () => {
const clazz = python.class_({
name: "Car"
});
expect(clazz.toString()).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`class > basic 1`] = `"class Car:"`;
13 changes: 13 additions & 0 deletions generators/pythonv2/codegen/src/ast/core/AstNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { AbstractAstNode } from "@fern-api/generator-commons";
import { Writer } from "./Writer";

export abstract class AstNode extends AbstractAstNode {
/**
* Writes the node to a string.
*/
public toString(): string {
const writer = new Writer();
this.write(writer);
return writer.toString();
}
}
9 changes: 9 additions & 0 deletions generators/pythonv2/codegen/src/ast/core/Writer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { AbstractWriter } from "@fern-api/generator-commons";

export declare namespace Writer {}

export class Writer extends AbstractWriter {
public toString(): string {
return this.buffer;
}
}
1 change: 1 addition & 0 deletions generators/pythonv2/codegen/src/ast/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Class } from "./Class";
1 change: 1 addition & 0 deletions generators/pythonv2/codegen/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as python from "./python";
8 changes: 8 additions & 0 deletions generators/pythonv2/codegen/src/python.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Class } from "./ast";

export function class_(args: Class.Args): Class {
return new Class(args);
}

export { Class } from "./ast";
export { AstNode } from "./ast/core/AstNode";
11 changes: 11 additions & 0 deletions generators/pythonv2/codegen/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../../shared/tsconfig.shared.json",
"compilerOptions": { "composite": true, "outDir": "lib", "rootDir": "src" },
"include": ["./src/**/*"],
"references": [
{ "path": "../../../packages/commons/core-utils" },
{ "path": "../../../packages/commons/fs-utils" },
{ "path": "../../../packages/commons/logging-execa" },
{ "path": "../../commons" }
]
}
1 change: 1 addition & 0 deletions generators/pythonv2/codegen/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "../../../shared/vitest.config";
34 changes: 34 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 44a67c1

Please sign in to comment.