Skip to content

Commit

Permalink
Merge pull request #393 from amplication/dotnet-program-class-refactor
Browse files Browse the repository at this point in the history
Dotnet-program-class-refactor
  • Loading branch information
mulygottlieb authored Aug 26, 2024
2 parents 41fdfa3 + f418d88 commit 522797c
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 83 deletions.
86 changes: 62 additions & 24 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions plugins/dotnet-auth-core-identity/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@amplication/plugin-dotnet-auth-core-identity",
"version": "0.0.3",
"version": "0.1.0",
"description": "Add Authentication and Authorization to your .NET Services",
"main": "dist/index.js",
"nx": {},
Expand All @@ -12,9 +12,9 @@
"author": "Mor Hagbi",
"license": "Apache-2.0",
"devDependencies": {
"@amplication/code-gen-types": "2.0.34",
"@amplication/csharp-ast": "^0.0.4",
"@amplication/code-gen-types": "2.0.37",
"@amplication/code-gen-utils": "^0.0.9",
"@amplication/csharp-ast": "0.0.3",
"@babel/parser": "^7.18.11",
"@babel/types": "^7.18.10",
"@types/lodash": "^4.14.182",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { CodeBlock } from "@amplication/csharp-ast";
import { CodeBlock, ProgramClass } from "@amplication/csharp-ast";

export function createAppServices(builderServicesBlocks: CodeBlock[]): void {
builderServicesBlocks.push(
export function createAppServices(programClass: ProgramClass): void {
programClass.builderServicesBlocks.push(
new CodeBlock({
code: `app.UseApiAuthentication();`,
})
);

builderServicesBlocks.push(
programClass.builderServicesBlocks.push(
new CodeBlock({
code: `using (var scope = app.Services.CreateScope())
{
Expand All @@ -17,7 +17,7 @@ export function createAppServices(builderServicesBlocks: CodeBlock[]): void {
})
);

builderServicesBlocks.push(
programClass.builderServicesBlocks.push(
new CodeBlock({
code: `
using (var scope = app.Services.CreateScope())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { CodeBlock, CsharpSupport } from "@amplication/csharp-ast";
import { CodeBlock, CsharpSupport,ProgramClass } from "@amplication/csharp-ast";

export function createBuildersServices(
resourceName: string,
builderServicesBlocks: CodeBlock[]
programClass: ProgramClass
): void {
builderServicesBlocks.push(
programClass.builderServicesBlocks.push(
new CodeBlock({
code: `builder.Services.AddApiAuthentication();`,
})
);

const swaggerBuilderIndex = builderServicesBlocks.findIndex((b) =>
const swaggerBuilderIndex = programClass.builderServicesBlocks.findIndex((b) =>
b.toString().includes("AddSwaggerGen")
);

if (swaggerBuilderIndex === -1) return;

builderServicesBlocks[swaggerBuilderIndex] = new CodeBlock({
programClass.builderServicesBlocks[swaggerBuilderIndex] = new CodeBlock({
references: [
CsharpSupport.classReference({
namespace: `${resourceName}.APIs`,
Expand Down
31 changes: 18 additions & 13 deletions plugins/dotnet-auth-core-identity/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
CodeBlock,
CsharpSupport,
MethodType,
ProgramClass,
} from "@amplication/csharp-ast";
import { pascalCase } from "pascal-case";
import {
Expand All @@ -22,7 +23,7 @@ import {
createStaticFileFileMap,
getEntityRoleMap,
} from "./core";
import { resolve } from "path";
import { resolve, join } from "path";

class AuthCorePlugin implements dotnetTypes.AmplicationPlugin {
register(): dotnetPluginEventsTypes.DotnetEvents {
Expand All @@ -40,7 +41,7 @@ class AuthCorePlugin implements dotnetTypes.AmplicationPlugin {
after: this.afterLoadStaticFiles,
},
CreateProgramFile: {
before: this.beforeCreateProgramFile,
after: this.afterCreateProgramFile,
},
CreateSeedDevelopmentDataFile: {
after: this.afterCreateSeedDevelopmentDataFile,
Expand Down Expand Up @@ -69,18 +70,22 @@ class AuthCorePlugin implements dotnetTypes.AmplicationPlugin {
return eventParams;
}

async beforeCreateProgramFile(
context: dotnetTypes.DsgContext,
eventParams: dotnet.CreateProgramFileParams
): Promise<dotnet.CreateProgramFileParams> {
const { builderServicesBlocks, appBlocks } = eventParams;
const { resourceInfo } = context;
if (!resourceInfo) return eventParams;
afterCreateProgramFile(
{ resourceInfo, serverDirectories }: dotnetTypes.DsgContext,
eventParams: dotnet.CreateProgramFileParams,
programClassMap: FileMap<ProgramClass>
): FileMap<ProgramClass> {
if (!resourceInfo) return programClassMap;
const serviceNamespace = pascalCase(resourceInfo.name);
createBuildersServices(serviceNamespace, builderServicesBlocks);
createAppServices(appBlocks);

return eventParams;
const programCsPath = join(serverDirectories.srcDirectory, "Program.cs");
const programClass = programClassMap.get(programCsPath);
if (!programClass)
return programClassMap;

createBuildersServices(serviceNamespace, programClass.code);
createAppServices( programClass.code);

return programClassMap;
}

async afterLoadStaticFiles(
Expand Down
6 changes: 3 additions & 3 deletions plugins/dotnet-broker-kafka/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@amplication/plugin-dotnet-broker-kafka",
"version": "0.0.6",
"version": "0.1.0",
"description": "Use a Kafka message broker to communicate between services generated with Amplication",
"main": "dist/index.js",
"nx": {},
Expand All @@ -12,8 +12,8 @@
"author": "Haim Bell",
"license": "Apache-2.0",
"devDependencies": {
"@amplication/csharp-ast": "^0.0.3",
"@amplication/code-gen-types": "2.0.34",
"@amplication/csharp-ast": "^0.0.4",
"@amplication/code-gen-types": "2.0.37",
"@amplication/code-gen-utils": "^0.0.9",
"@babel/parser": "^7.18.11",
"@babel/types": "^7.18.10",
Expand Down
Loading

0 comments on commit 522797c

Please sign in to comment.