-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(go): Integrate TypeScript generator (#5816)
- Loading branch information
Showing
32 changed files
with
526 additions
and
31 deletions.
There are no files selected for viewing
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,28 @@ | ||
import { AstNode } from "./core/AstNode"; | ||
import { Writer } from "./core/Writer"; | ||
|
||
export declare namespace File { | ||
interface Args { | ||
/* The list of nodes in the file. */ | ||
nodes?: AstNode[]; | ||
} | ||
} | ||
|
||
export class File extends AstNode { | ||
public readonly nodes: AstNode[]; | ||
|
||
constructor({ nodes }: File.Args = { nodes: [] }) { | ||
super(); | ||
this.nodes = nodes ?? []; | ||
} | ||
|
||
public add(...nodes: AstNode[]): void { | ||
this.nodes.push(...nodes); | ||
} | ||
|
||
public write(writer: Writer): void { | ||
for (const node of this.nodes) { | ||
node.write(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
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
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
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
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
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
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
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
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
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,24 @@ | ||
import { AbstractGeneratorCli, parseIR } from "@fern-api/base-generator"; | ||
import { AbsoluteFilePath } from "@fern-api/fs-utils"; | ||
import { AbstractGoGeneratorContext } from "@fern-api/go-ast"; | ||
import { BaseGoCustomConfigSchema } from "@fern-api/go-ast"; | ||
|
||
import { IntermediateRepresentation } from "@fern-fern/ir-sdk/api"; | ||
import * as IrSerialization from "@fern-fern/ir-sdk/serialization"; | ||
|
||
export abstract class AbstractGoGeneratorCli< | ||
CustomConfig extends BaseGoCustomConfigSchema, | ||
GoGeneratorContext extends AbstractGoGeneratorContext<CustomConfig> | ||
> extends AbstractGeneratorCli<CustomConfig, IntermediateRepresentation, GoGeneratorContext> { | ||
/** | ||
* Parses the IR for the PHP generators | ||
* @param irFilepath | ||
* @returns | ||
*/ | ||
protected async parseIntermediateRepresentation(irFilepath: string): Promise<IntermediateRepresentation> { | ||
return await parseIR<IntermediateRepresentation>({ | ||
absolutePathToIR: AbsoluteFilePath.of(irFilepath), | ||
parse: IrSerialization.IntermediateRepresentation.parse | ||
}); | ||
} | ||
} |
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 { AbstractGoGeneratorCli } from "./AbstractGoGeneratorCli"; |
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,6 +1,4 @@ | ||
void runCli(); | ||
|
||
export async function runCli(): Promise<void> { | ||
// eslint-disable-next-line no-console | ||
console.log("Noop..."); | ||
} | ||
export { AbstractGoGeneratorCli } from "./cli/AbstractGoGeneratorCli"; | ||
export { GoFile } from "./project/GoFile"; | ||
export { GoProject } from "./project/GoProject"; | ||
export { FileGenerator } from "./FileGenerator"; |
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,49 @@ | ||
import { AbstractFormatter, File } from "@fern-api/base-generator"; | ||
import { RelativeFilePath } from "@fern-api/fs-utils"; | ||
import { BaseGoCustomConfigSchema, go } from "@fern-api/go-ast"; | ||
|
||
export declare namespace GoFile { | ||
interface Args { | ||
/* The node to be written to the Go source file */ | ||
node: go.AstNode; | ||
/* Directory of the file */ | ||
directory: RelativeFilePath; | ||
/* Filename of the file */ | ||
filename: string; | ||
/* The package name of the file */ | ||
packageName: string; | ||
/* The root import path of the module */ | ||
rootImportPath: string; | ||
/* The import path of the file */ | ||
importPath: string; | ||
/* Custom generator config */ | ||
customConfig: BaseGoCustomConfigSchema; | ||
/* Optional formatter */ | ||
formatter?: AbstractFormatter; | ||
} | ||
} | ||
|
||
export class GoFile extends File { | ||
constructor({ | ||
node, | ||
directory, | ||
filename, | ||
packageName, | ||
rootImportPath, | ||
importPath, | ||
customConfig, | ||
formatter | ||
}: GoFile.Args) { | ||
super( | ||
filename, | ||
directory, | ||
node.toString({ | ||
packageName, | ||
rootImportPath, | ||
importPath, | ||
customConfig, | ||
formatter | ||
}) | ||
); | ||
} | ||
} |
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,2 +1,56 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-extraneous-class | ||
export class GoProject {} | ||
import { mkdir } from "fs/promises"; | ||
|
||
import { AbstractProject, File } from "@fern-api/base-generator"; | ||
import { AbsoluteFilePath } from "@fern-api/fs-utils"; | ||
import { AbstractGoGeneratorContext, BaseGoCustomConfigSchema } from "@fern-api/go-ast"; | ||
import { loggingExeca } from "@fern-api/logging-execa"; | ||
|
||
/** | ||
* In memory representation of a Go project. | ||
*/ | ||
export class GoProject extends AbstractProject<AbstractGoGeneratorContext<BaseGoCustomConfigSchema>> { | ||
private sourceFiles: File[] = []; | ||
|
||
public constructor({ context }: { context: AbstractGoGeneratorContext<BaseGoCustomConfigSchema> }) { | ||
super(context); | ||
} | ||
|
||
public addGoFiles(file: File): void { | ||
this.sourceFiles.push(file); | ||
} | ||
|
||
public async persist(): Promise<void> { | ||
await this.writeGoFiles({ | ||
absolutePathToDirectory: this.absolutePathToOutputDirectory, | ||
files: this.sourceFiles | ||
}); | ||
} | ||
|
||
private async writeGoFiles({ | ||
absolutePathToDirectory, | ||
files | ||
}: { | ||
absolutePathToDirectory: AbsoluteFilePath; | ||
files: File[]; | ||
}): Promise<AbsoluteFilePath> { | ||
await this.mkdir(absolutePathToDirectory); | ||
await Promise.all(files.map(async (file) => await file.write(absolutePathToDirectory))); | ||
if (files.length > 0) { | ||
// TODO: Uncomment this once the go-v2 generator is responsible for producing the go.mod file. | ||
// Otherwise, we get a "directory prefix . does not contain main module or its selected dependencies" error. | ||
// | ||
// --- | ||
// | ||
// await loggingExeca(this.context.logger, "go", ["fmt", "./..."], { | ||
// doNotPipeOutput: true, | ||
// cwd: absolutePathToDirectory | ||
// }); | ||
} | ||
return absolutePathToDirectory; | ||
} | ||
|
||
private async mkdir(absolutePathToDirectory: AbsoluteFilePath): Promise<void> { | ||
this.context.logger.debug(`mkdir ${absolutePathToDirectory}`); | ||
await mkdir(absolutePathToDirectory, { recursive: true }); | ||
} | ||
} |
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 +1,2 @@ | ||
export { GoFile } from "./GoFile"; | ||
export { GoProject } from "./GoProject"; |
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
Oops, something went wrong.