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 all 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-rc2
tags: fernapi/fern-ruby-sdk:0.9.0-rc2
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 "hey@buildwithfern.com"

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"]
122 changes: 105 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,62 @@ export function generateService(
packagePath,
syncClientClassReference,
artifactRegistry
)
),
initializerOverride: new Function_({
name: "initialize",
invocationName: "new",
// Initialize each subpackage
functionBody: [
new Expression({
leftSide: requestClientProperty.toVariable(),
rightSide: requestClientProperty.name,
isAssignment: true
}),
...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 +692,43 @@ export function generateService(
packagePath,
undefined,
undefined
)
),
initializerOverride: new Function_({
name: "initialize",
invocationName: "new",
// Initialize each subpackage
functionBody: [
new Expression({
leftSide: requestClientProperty.toVariable(),
rightSide: requestClientProperty.name,
isAssignment: true
}),
...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
Loading
Loading