Skip to content

Commit

Permalink
(feat, csharp): support extra dependencies (#3957)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Jul 1, 2024
1 parent f1c9f08 commit a0b969b
Show file tree
Hide file tree
Showing 48 changed files with 1,157 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ export abstract class AbstractCsharpGeneratorContext<

public abstract getNamespaceForTypeId(typeId: TypeId): string;

public abstract getExtraDependencies(): Record<string, string>;

// STOLEN FROM: ruby/TypesGenerator.ts
// We need a better way to share this sort of ir-processing logic.
//
Expand Down
22 changes: 18 additions & 4 deletions generators/csharp/codegen/src/project/CsharpProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ export class CsharpProject {
github: (github) => github.repoUrl,
publish: () => undefined,
_other: () => undefined
})
}),
context: this.context
});
const templateCsProjContents = csproj.toString();
await writeFile(
Expand Down Expand Up @@ -248,6 +249,7 @@ declare namespace CsProj {
version?: string;
license?: string;
githubUrl?: string;
context: AbstractCsharpGeneratorContext<BaseCsharpCustomConfigSchema>;
}
}

Expand All @@ -257,15 +259,18 @@ class CsProj {
private version: string | undefined;
private license: string | undefined;
private githubUrl: string | undefined;
private context: AbstractCsharpGeneratorContext<BaseCsharpCustomConfigSchema>;

public constructor({ version, license, githubUrl }: CsProj.Args) {
public constructor({ version, license, githubUrl, context }: CsProj.Args) {
this.version = version;
this.license = license;
this.githubUrl = githubUrl;
this.context = context;
}

public toString(): string {
const propertyGroups = this.getPropertyGroups();
const dependencies = this.getDependencies();
return `
<Project Sdk="Microsoft.NET.Sdk">
Expand All @@ -277,8 +282,7 @@ class CsProj {
</PropertyGroup>
<ItemGroup>
<PackageReference Include="OneOf" Version="3.0.263" />
<PackageReference Include="System.Text.Json" Version="8.0.3" />
${dependencies.join(`\n${FOUR_SPACES}${FOUR_SPACES}`)}
</ItemGroup>
<ItemGroup>
Expand All @@ -290,6 +294,16 @@ ${this.getAdditionalItemGroups().join(`\n${FOUR_SPACES}`)}
`;
}

private getDependencies(): string[] {
const result: string[] = [];
result.push('<PackageReference Include="OneOf" Version="3.0.263" />');
result.push('<PackageReference Include="System.Text.Json" Version="8.0.3" />');
for (const [name, version] of Object.entries(this.context.getExtraDependencies())) {
result.push(`<PackageReference Include="${name}" Version="${version}" />`);
}
return result;
}

private getPropertyGroups(): string[] {
const result: string[] = [];
if (this.version != null) {
Expand Down
4 changes: 4 additions & 0 deletions generators/csharp/model/src/ModelGeneratorContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ export class ModelGeneratorContext extends AbstractCsharpGeneratorContext<ModelC
public getAsIsFiles(): string[] {
return [AsIsFiles.StringEnumSerializer, AsIsFiles.OneOfSerializer, AsIsFiles.CollectionItemSerializer];
}

public getExtraDependencies(): Record<string, string> {
return {};
}
}
13 changes: 13 additions & 0 deletions generators/csharp/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.0.33 - 2024-06-21]

- Improvement: The C# generator now supports configuration to specify extra dependencies. Below
is an example of the `generators.yml` configuration:

```yml
- name: fernapi/fern-csharp-sdk
version: 0.0.33
config:
extra-dependencies:
moq: "0.23.4"
```
## [0.0.32 - 2024-06-21]
- Fix: Enum values are JSON serialized before they are sent to the server. For example, if the
Expand Down
2 changes: 1 addition & 1 deletion generators/csharp/sdk/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.32
0.0.33
3 changes: 2 additions & 1 deletion generators/csharp/sdk/src/SdkCustomConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { z } from "zod";

export const SdkCustomConfigSchema = z.strictObject({
namespace: z.string().optional(),
"client-class-name": z.string().optional()
"client-class-name": z.string().optional(),
"extra-dependencies": z.record(z.string()).optional()
});

export type SdkCustomConfigSchema = z.infer<typeof SdkCustomConfigSchema>;
4 changes: 4 additions & 0 deletions generators/csharp/sdk/src/SdkGeneratorContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ export class SdkGeneratorContext extends AbstractCsharpGeneratorContext<SdkCusto
return `${endpoint.name.pascalCase.safeName}Async`;
}

public getExtraDependencies(): Record<string, string> {
return this.customConfig["extra-dependencies"] ?? {};
}

private getNamespaceFromFernFilepath(fernFilepath: FernFilepath): string {
return [this.getNamespace(), ...fernFilepath.packagePath.map((path) => path.pascalCase.safeName)].join(".");
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
25 changes: 25 additions & 0 deletions seed/csharp-sdk/imdb/extra-dependencies/src/SeedApi/SeedApi.csproj

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

26 changes: 26 additions & 0 deletions seed/csharp-sdk/imdb/no-custom-config/.github/workflows/ci.yml

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

Loading

0 comments on commit a0b969b

Please sign in to comment.