Skip to content
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

[do not merge] fix(ruby): tweak generation #5162

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
40 changes: 40 additions & 0 deletions .github/workflows/publish-ruby-sdk.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
on:
push:
branches:
- dsinghvi/ruby-tweaks

jobs:
ruby-sdk:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4
with:
fetch-tags: true

- name: 📥 Install
uses: ./.github/actions/install

- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: fernapi
password: ${{ secrets.FERN_API_DOCKERHUB_PASSWORD }}

- name: Build CLI
run: pnpm --filter @fern-api/fern-ruby-sdk dist:cli

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
file: ./generators/ruby/sdk/Dockerfile
platforms: linux/amd64,linux/arm64
# cache-from: type=gha
# cache-to: type=gha,mode=min
push: true
labels: version=0.9.0-rc0
tags: fernapi/fern-ruby-sdk:0.9.0-rc0
31 changes: 17 additions & 14 deletions generators/ruby/codegen/src/ast/Module_.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,24 @@ export class Module_ extends AstNode {
let moduleWrappedItem: Module_ | Class_ | T = child;
let moduleBreadcrumbs: string[] = [locationGenerator.rootModule];
if (path) {
if (!locationGenerator.shouldFlattenModules) {
moduleBreadcrumbs = moduleBreadcrumbs.concat(locationGenerator.getModulePathFromTypeName(path));
const classWrapper = locationGenerator.getClassPathFromTypeName(path);

if (classWrapper !== undefined && includeFilename) {
moduleWrappedItem = new Class_({
classReference: new ClassReference({ name: classWrapper }),
includeInitializer: false,
children: child
});
}
} else {
moduleBreadcrumbs = locationGenerator.getModuleBreadcrumbs({ path, includeFilename, isType });
}
moduleBreadcrumbs = locationGenerator.getModuleBreadcrumbs({ path, includeFilename, isType });
}
// if (path) {
// if (!locationGenerator.shouldFlattenModules) {
// moduleBreadcrumbs = moduleBreadcrumbs.concat(locationGenerator.getModulePathFromTypeName(path));
// const classWrapper = locationGenerator.getClassPathFromTypeName(path);

// if (classWrapper !== undefined && includeFilename) {
// moduleWrappedItem = new Class_({
// classReference: new ClassReference({ name: classWrapper }),
// includeInitializer: false,
// children: child
// });
// }
// } else {
// moduleBreadcrumbs = locationGenerator.getModuleBreadcrumbs({ path, includeFilename, isType });
// }
// }

moduleBreadcrumbs.reverse().forEach(
(mod) =>
Expand Down
20 changes: 15 additions & 5 deletions generators/ruby/sdk/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
# syntax = edrevo/dockerfile-plus
INCLUDE+ packages/generators/docker/Dockerfile.base
FROM node:20.18-alpine3.20 AS node
FROM ruby:3.2.6-alpine3.20

RUN apk --no-cache add bash curl git zip && git config --global user.name "fern" && git config --global user.email "[email protected]"

RUN curl -L https://cs.symfony.com/download/php-cs-fixer-v3.phar -o /usr/local/bin/php-cs-fixer \
&& chmod +x /usr/local/bin/php-cs-fixer
ENV YARN_CACHE_FOLDER=/.yarn

COPY generators/ruby/sdk/dist /dist

# Install Ruby and Rubocop for formatting
RUN apk update && apk add --no-cache build-base ruby ruby-dev && gem install rubocop
# Copy over node contents to be able to run the compiled CLI
COPY --from=node /usr/local/bin/node /usr/local/bin/
COPY --from=node /usr/local/lib/node_modules /usr/local/lib/node_modules
RUN ln -s ../lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm \
&& ln -s ../lib/node_modules/npm/bin/npx-cli.js /usr/local/bin/npx

ENTRYPOINT ["node", "/dist/cli.cjs", "ruby-sdk"]
# Cache generator files and create entry point within an extended Dockerfile
ENTRYPOINT ["node", "/dist/cli.cjs"]
108 changes: 91 additions & 17 deletions generators/ruby/sdk/src/AbstractionUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,9 @@ export function generateService(
fileUploadUtility: FileUploadUtility,
locationGenerator: LocationGenerator,
packagePath: string[],
artifactRegistry: ArtifactRegistry
artifactRegistry: ArtifactRegistry,
subpackages: Map<Name, Class_> = new Map(),
asyncSubpackages: Map<Name, Class_> = new Map()
): ClientClassPair {
const subpackageName = subpackage.name;
const serviceName = subpackageName.pascalCase.unsafeName;
Expand All @@ -587,14 +589,21 @@ export function generateService(
import_,
moduleBreadcrumbs
});
const syncClassReference = new ClassReference({
name: `${serviceName}Client`,
import_,
moduleBreadcrumbs
});
const syncClientClass = new Class_({
classReference: new ClassReference({
name: `${serviceName}Client`,
import_,
moduleBreadcrumbs
}),
properties: [requestClientProperty],
includeInitializer: true,
classReference: syncClassReference,
properties: [
requestClientProperty,
...Array.from(subpackages.entries()).map(
([spName, sp]) =>
new Property({ name: getSubpackagePropertyNameFromIr(spName), type: sp.classReference })
)
],
includeInitializer: false,
functions: generateEndpoints(
crf,
eg,
Expand All @@ -611,19 +620,55 @@ export function generateService(
packagePath,
syncClientClassReference,
artifactRegistry
)
),
initializerOverride: new Function_({
name: "initialize",
invocationName: "new",
// Initialize each subpackage
functionBody: Array.from(subpackages.entries()).map(([spName, sp]) => {
const subpackageClassVariable = new Variable({
name: getSubpackagePropertyNameFromIr(spName),
type: sp.classReference,
variableType: VariableType.INSTANCE
});
return new Expression({
leftSide: subpackageClassVariable,
rightSide:
sp.initializer !== undefined
? new FunctionInvocation({
onObject: sp.classReference,
baseFunction: sp.initializer,
arguments_: sp.initializer.parameters.map((param) =>
param.toArgument(requestClientProperty.toVariable(VariableType.LOCAL))
)
})
: sp.classReference,
isAssignment: true
});
}),
parameters: [requestClientProperty.toParameter({})],
returnValue: syncClassReference,
documentation: subpackage.docs
})
});

// Add Async Client class
const asyncRequestClientProperty = new Property({ name: "request_client", type: asyncRequestClientCr });
const asyncClassReference = new ClassReference({
name: `Async${serviceName}Client`,
import_,
moduleBreadcrumbs
});
const asyncClientClass = new Class_({
classReference: new ClassReference({
name: `Async${serviceName}Client`,
import_,
moduleBreadcrumbs
}),
properties: [asyncRequestClientProperty],
includeInitializer: true,
classReference: asyncClassReference,
properties: [
asyncRequestClientProperty,
...Array.from(subpackages.entries()).map(
([spName, sp]) =>
new Property({ name: getSubpackagePropertyNameFromIr(spName), type: sp.classReference })
)
],
includeInitializer: false,
functions: generateEndpoints(
crf,
eg,
Expand All @@ -640,7 +685,36 @@ export function generateService(
packagePath,
undefined,
undefined
)
),
initializerOverride: new Function_({
name: "initialize",
invocationName: "new",
// Initialize each subpackage
functionBody: Array.from(asyncSubpackages.entries()).map(([spName, sp]) => {
const subpackageClassVariable = new Variable({
name: getSubpackagePropertyNameFromIr(spName),
type: sp.classReference,
variableType: VariableType.INSTANCE
});
return new Expression({
leftSide: subpackageClassVariable,
rightSide:
sp.initializer !== undefined
? new FunctionInvocation({
onObject: sp.classReference,
baseFunction: sp.initializer,
arguments_: sp.initializer.parameters.map((param) =>
param.toArgument(asyncRequestClientProperty.toVariable(VariableType.LOCAL))
)
})
: sp.classReference,
isAssignment: true
});
}),
parameters: [requestClientProperty.toParameter({})],
returnValue: asyncClassReference,
documentation: subpackage.docs
})
});

return { subpackageName, syncClientClass, asyncClientClass };
Expand Down
60 changes: 47 additions & 13 deletions generators/ruby/sdk/src/ClientsGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class ClientsGenerator {
if (classReferenceFactory !== undefined) {
this.crf = classReferenceFactory;
} else {
this.gc.logger.warn("[Ruby] Client generator was not provided a ClassReferenceFactory, regenerating one.");
this.gc.logger.warn("Client generator was not provided a ClassReferenceFactory, regenerating one.");
const types = new Map();
for (const type of Object.values(intermediateRepresentation.types)) {
types.set(type.name.typeId, type);
Expand Down Expand Up @@ -156,7 +156,7 @@ export class ClientsGenerator {

let environmentClass: Class_ | undefined;
if (this.intermediateRepresentation.environments !== undefined) {
this.gc.logger.debug("[Ruby] Preparing environment files.");
this.gc.logger.debug("Preparing environment files.");
this.defaultEnvironment = getDefaultEnvironmentUrl(
this.clientName,
this.intermediateRepresentation.environments
Expand All @@ -178,15 +178,15 @@ export class ClientsGenerator {
);
}

this.gc.logger.debug("[Ruby] Preparing authorization headers.");
this.gc.logger.debug("Preparing authorization headers.");
const headersGenerator = new HeadersGenerator(
this.intermediateRepresentation.headers,
this.crf,
this.intermediateRepresentation.auth,
this.shouldGenerateOauth
);

this.gc.logger.debug("[Ruby] Preparing request options classes.");
this.gc.logger.debug("Preparing request options classes.");
const requestOptionsClass = new RequestOptions({
headersGenerator,
clientName: this.clientName
Expand All @@ -210,7 +210,7 @@ export class ClientsGenerator {
isOptional: true
});

this.gc.logger.debug("[Ruby] Preparing request clients.");
this.gc.logger.debug("Preparing request clients.");
const [syncClientClass, asyncClientClass] = generateRequestClients(
this.clientName,
this.intermediateRepresentation.sdkConfig,
Expand All @@ -237,7 +237,7 @@ export class ClientsGenerator {
})
);

this.gc.logger.debug("[Ruby] Preparing example snippets.");
this.gc.logger.debug("Preparing example snippets.");
const dummyRootClientDoNotUse = generateDummyRootClient(this.gemName, this.clientName, syncClientClass);
const eg = new ExampleGenerator({
rootClientClass: dummyRootClientDoNotUse,
Expand Down Expand Up @@ -278,7 +278,9 @@ export class ClientsGenerator {
generatedClasses: Map<TypeId, Class_>,
flattenedProperties: Map<TypeId, ObjectProperty[]>,
subpackagePaths: Map<SubpackageId, string[]>,
artifactRegistry: ArtifactRegistry
artifactRegistry: ArtifactRegistry,
subpackages: Map<Name, Class_> = new Map(),
asyncSubpackages: Map<Name, Class_> = new Map()
): ClientClassPair {
if (subpackage.service === undefined) {
throw new Error("Calling getServiceClasses without a service defined within the subpackage.");
Expand All @@ -304,7 +306,9 @@ export class ClientsGenerator {
fileUtilityClass,
locationGenerator,
subpackagePaths.get(packageId) ?? [],
artifactRegistry
artifactRegistry,
subpackages,
asyncSubpackages
);
const serviceModule = Module_.wrapInModules({
locationGenerator,
Expand Down Expand Up @@ -336,7 +340,35 @@ export class ClientsGenerator {
subpackagePaths: Map<SubpackageId, string[]>,
artifactRegistry: ArtifactRegistry
): ClientClassPair | undefined {
if (subpackage.service !== undefined) {
if (subpackage.service != null) {
const classPairs: ClientClassPair[] = subpackage.subpackages
.map((subpackageId) => {
const subpackage = subpackages.get(subpackageId);
if (subpackage === undefined) {
throw new Error(`Subpackage ${subpackageId} was not defined within in the IR`);
}

const classPair = subpackageClassReferences.get(subpackageId);
if (classPair === undefined) {
return getSubpackageClasses(
subpackageName,
subpackageId,
subpackage,
services,
subpackages,
clientName,
crf,
irBasePath,
generatedClasses,
flattenedProperties,
subpackagePaths,
artifactRegistry
);
}
return classPair;
})
.filter((cp) => cp !== undefined) as ClientClassPair[];

return getServiceClasses(
packageId,
subpackage,
Expand All @@ -347,7 +379,9 @@ export class ClientsGenerator {
generatedClasses,
flattenedProperties,
subpackagePaths,
artifactRegistry
artifactRegistry,
new Map(classPairs.map((cp) => [cp.subpackageName, cp.syncClientClass])),
new Map(classPairs.map((cp) => [cp.subpackageName, cp.asyncClientClass]))
);
} else {
// We create these subpackage files to support dot access for service clients,
Expand Down Expand Up @@ -410,7 +444,7 @@ export class ClientsGenerator {
}
}

this.gc.logger.debug("[Ruby] Generating files for subpackages.");
this.gc.logger.debug("Generating files for subpackages.");
Array.from(this.subpackages.entries()).forEach(([packageId, subpackage]) =>
getSubpackageClasses(
subpackage.name,
Expand All @@ -429,7 +463,7 @@ export class ClientsGenerator {
);

// 3. Generate main file, this is what people import while leveraging the gem.
this.gc.logger.debug("[Ruby] Generating files for root package.");
this.gc.logger.debug("Generating files for root package.");
const rootSubpackageClasses = this.intermediateRepresentation.rootPackage.subpackages
.map((sp) => subpackageClassReferences.get(sp))
.filter((cp) => cp !== undefined) as ClientClassPair[];
Expand Down Expand Up @@ -506,7 +540,7 @@ export class ClientsGenerator {
)
);

this.gc.logger.debug("[Ruby] Generating snippets.json file.");
this.gc.logger.debug("Generating snippets.json file.");
if (this.gc.config.output.snippetFilepath !== undefined) {
clientFiles.push(eg.generateSnippetsFile(this.gc.config.output.snippetFilepath));
}
Expand Down
Loading
Loading