Skip to content

Commit 522797c

Browse files
authored
Merge pull request #393 from amplication/dotnet-program-class-refactor
Dotnet-program-class-refactor
2 parents 41fdfa3 + f418d88 commit 522797c

File tree

11 files changed

+146
-83
lines changed

11 files changed

+146
-83
lines changed

package-lock.json

Lines changed: 62 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/dotnet-auth-core-identity/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@amplication/plugin-dotnet-auth-core-identity",
3-
"version": "0.0.3",
3+
"version": "0.1.0",
44
"description": "Add Authentication and Authorization to your .NET Services",
55
"main": "dist/index.js",
66
"nx": {},
@@ -12,9 +12,9 @@
1212
"author": "Mor Hagbi",
1313
"license": "Apache-2.0",
1414
"devDependencies": {
15-
"@amplication/code-gen-types": "2.0.34",
15+
"@amplication/csharp-ast": "^0.0.4",
16+
"@amplication/code-gen-types": "2.0.37",
1617
"@amplication/code-gen-utils": "^0.0.9",
17-
"@amplication/csharp-ast": "0.0.3",
1818
"@babel/parser": "^7.18.11",
1919
"@babel/types": "^7.18.10",
2020
"@types/lodash": "^4.14.182",

plugins/dotnet-auth-core-identity/src/core/create-app-services.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { CodeBlock } from "@amplication/csharp-ast";
1+
import { CodeBlock, ProgramClass } from "@amplication/csharp-ast";
22

3-
export function createAppServices(builderServicesBlocks: CodeBlock[]): void {
4-
builderServicesBlocks.push(
3+
export function createAppServices(programClass: ProgramClass): void {
4+
programClass.builderServicesBlocks.push(
55
new CodeBlock({
66
code: `app.UseApiAuthentication();`,
77
})
88
);
99

10-
builderServicesBlocks.push(
10+
programClass.builderServicesBlocks.push(
1111
new CodeBlock({
1212
code: `using (var scope = app.Services.CreateScope())
1313
{
@@ -17,7 +17,7 @@ export function createAppServices(builderServicesBlocks: CodeBlock[]): void {
1717
})
1818
);
1919

20-
builderServicesBlocks.push(
20+
programClass.builderServicesBlocks.push(
2121
new CodeBlock({
2222
code: `
2323
using (var scope = app.Services.CreateScope())

plugins/dotnet-auth-core-identity/src/core/create-builders-services.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import { CodeBlock, CsharpSupport } from "@amplication/csharp-ast";
1+
import { CodeBlock, CsharpSupport,ProgramClass } from "@amplication/csharp-ast";
22

33
export function createBuildersServices(
44
resourceName: string,
5-
builderServicesBlocks: CodeBlock[]
5+
programClass: ProgramClass
66
): void {
7-
builderServicesBlocks.push(
7+
programClass.builderServicesBlocks.push(
88
new CodeBlock({
99
code: `builder.Services.AddApiAuthentication();`,
1010
})
1111
);
1212

13-
const swaggerBuilderIndex = builderServicesBlocks.findIndex((b) =>
13+
const swaggerBuilderIndex = programClass.builderServicesBlocks.findIndex((b) =>
1414
b.toString().includes("AddSwaggerGen")
1515
);
1616

1717
if (swaggerBuilderIndex === -1) return;
1818

19-
builderServicesBlocks[swaggerBuilderIndex] = new CodeBlock({
19+
programClass.builderServicesBlocks[swaggerBuilderIndex] = new CodeBlock({
2020
references: [
2121
CsharpSupport.classReference({
2222
namespace: `${resourceName}.APIs`,

plugins/dotnet-auth-core-identity/src/index.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
CodeBlock,
1212
CsharpSupport,
1313
MethodType,
14+
ProgramClass,
1415
} from "@amplication/csharp-ast";
1516
import { pascalCase } from "pascal-case";
1617
import {
@@ -22,7 +23,7 @@ import {
2223
createStaticFileFileMap,
2324
getEntityRoleMap,
2425
} from "./core";
25-
import { resolve } from "path";
26+
import { resolve, join } from "path";
2627

2728
class AuthCorePlugin implements dotnetTypes.AmplicationPlugin {
2829
register(): dotnetPluginEventsTypes.DotnetEvents {
@@ -40,7 +41,7 @@ class AuthCorePlugin implements dotnetTypes.AmplicationPlugin {
4041
after: this.afterLoadStaticFiles,
4142
},
4243
CreateProgramFile: {
43-
before: this.beforeCreateProgramFile,
44+
after: this.afterCreateProgramFile,
4445
},
4546
CreateSeedDevelopmentDataFile: {
4647
after: this.afterCreateSeedDevelopmentDataFile,
@@ -69,18 +70,22 @@ class AuthCorePlugin implements dotnetTypes.AmplicationPlugin {
6970
return eventParams;
7071
}
7172

72-
async beforeCreateProgramFile(
73-
context: dotnetTypes.DsgContext,
74-
eventParams: dotnet.CreateProgramFileParams
75-
): Promise<dotnet.CreateProgramFileParams> {
76-
const { builderServicesBlocks, appBlocks } = eventParams;
77-
const { resourceInfo } = context;
78-
if (!resourceInfo) return eventParams;
73+
afterCreateProgramFile(
74+
{ resourceInfo, serverDirectories }: dotnetTypes.DsgContext,
75+
eventParams: dotnet.CreateProgramFileParams,
76+
programClassMap: FileMap<ProgramClass>
77+
): FileMap<ProgramClass> {
78+
if (!resourceInfo) return programClassMap;
7979
const serviceNamespace = pascalCase(resourceInfo.name);
80-
createBuildersServices(serviceNamespace, builderServicesBlocks);
81-
createAppServices(appBlocks);
82-
83-
return eventParams;
80+
const programCsPath = join(serverDirectories.srcDirectory, "Program.cs");
81+
const programClass = programClassMap.get(programCsPath);
82+
if (!programClass)
83+
return programClassMap;
84+
85+
createBuildersServices(serviceNamespace, programClass.code);
86+
createAppServices( programClass.code);
87+
88+
return programClassMap;
8489
}
8590

8691
async afterLoadStaticFiles(

plugins/dotnet-broker-kafka/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@amplication/plugin-dotnet-broker-kafka",
3-
"version": "0.0.6",
3+
"version": "0.1.0",
44
"description": "Use a Kafka message broker to communicate between services generated with Amplication",
55
"main": "dist/index.js",
66
"nx": {},
@@ -12,8 +12,8 @@
1212
"author": "Haim Bell",
1313
"license": "Apache-2.0",
1414
"devDependencies": {
15-
"@amplication/csharp-ast": "^0.0.3",
16-
"@amplication/code-gen-types": "2.0.34",
15+
"@amplication/csharp-ast": "^0.0.4",
16+
"@amplication/code-gen-types": "2.0.37",
1717
"@amplication/code-gen-utils": "^0.0.9",
1818
"@babel/parser": "^7.18.11",
1919
"@babel/types": "^7.18.10",

0 commit comments

Comments
 (0)