Skip to content

Commit 407fcf1

Browse files
committed
update blueprint template
1 parent 1c5ca20 commit 407fcf1

25 files changed

+113
-127
lines changed

blueprint-plugins/blueprint-plugin-template/package-lock.json

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

blueprint-plugins/blueprint-plugin-template/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"author": "Yuval Hazaz",
1717
"license": "Apache-2.0",
1818
"devDependencies": {
19-
"@amplication/code-gen-types": "3.2.0",
19+
"@amplication/code-gen-types": "3.2.1",
2020
"@amplication/code-gen-utils": "^0.0.9",
2121
"@babel/parser": "^7.23.0",
2222
"@babel/types": "^7.23.0",

blueprint-plugins/blueprint-plugin-template/src/index.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,32 @@ import {
22
blueprintPluginEventsParams as blueprint,
33
blueprintPluginEventsTypes,
44
blueprintTypes,
5-
FileMap,
65
IFile,
76
} from "@amplication/code-gen-types";
87
import { CodeBlock } from "@amplication/csharp-ast";
98
import { camelCase, kebabCase } from "lodash";
109
import { resolve, join } from "path";
1110
import { REPLACEMENTS } from "./constants";
12-
import { replacePlaceholders, getPluginSettings } from "./utils";
11+
import { getPluginSettings } from "./utils";
1312

1413
class BlueprintPluginTemplatePlugin
1514
implements blueprintTypes.AmplicationPlugin
1615
{
1716
register(): blueprintPluginEventsTypes.BlueprintEvents {
1817
return {
1918
createBlueprint: {
20-
after: this.afterLoadStaticFiles,
19+
before: this.beforeCreateBlueprint,
2120
},
2221
};
2322
}
24-
async afterLoadStaticFiles(
23+
async beforeCreateBlueprint(
2524
context: blueprintTypes.DsgContext,
2625
eventParams: blueprint.CreateBlueprintParams,
27-
files: FileMap<CodeBlock>,
28-
): Promise<FileMap<CodeBlock>> {
26+
): Promise<blueprint.CreateBlueprintParams> {
2927
context.logger.info("Generating Static Files ...");
3028

31-
// determine the name of the service which will be used as the name for the workflow
32-
// workflow names must be lower case letters and numbers. words may be separated with dashes (-):
29+
// determine the name of the plugin which will be used as the ID for the plugin
30+
// plugin ID must be in kebab case (lowercase letters and numbers. words may be separated with dashes (-))
3331
const pluginName = context.resourceInfo?.name || "plugin";
3432

3533
const kebabCasePluginName = kebabCase(pluginName.trim());
@@ -51,33 +49,37 @@ class BlueprintPluginTemplatePlugin
5149
2,
5250
);
5351

52+
const stringReplacements = {
53+
PluginName: REPLACEMENTS.PLUGIN_CAMEL_CASE_NAME,
54+
};
55+
5456
const basePluginPath = `./plugins/${kebabCasePluginName}`;
5557

5658
context.logger.info(`base plugin path: ${basePluginPath}`);
5759

5860
// set the path to the static files and fetch them for manipulation
5961
const staticPath = resolve(__dirname, "./static");
60-
const staticFiles = await context.utils.importStaticFiles(staticPath, "./");
61-
62-
for (const item of staticFiles.getAll()) {
63-
item.code = replacePlaceholders(item.code, REPLACEMENTS);
64-
65-
item.path = join(
66-
basePluginPath,
67-
replacePlaceholders(item.path, REPLACEMENTS),
68-
);
69-
70-
context.logger.info(`generating file at path: ${item.path}`);
62+
const files = await context.utils.importStaticFilesWithReplacements(
63+
staticPath,
64+
".",
65+
REPLACEMENTS,
66+
stringReplacements,
67+
);
7168

72-
const file: IFile<CodeBlock> = {
73-
path: item.path,
69+
for (const file of files.getAll()) {
70+
const codeBlock: IFile<CodeBlock> = {
71+
path: join(
72+
basePluginPath,
73+
context.utils.replacePlaceholders(file.path, REPLACEMENTS),
74+
),
7475
code: new CodeBlock({
75-
code: item.code,
76+
code: file.code,
7677
}),
7778
};
78-
files.set(file);
79+
context.files.set(codeBlock);
7980
}
80-
return files;
81+
82+
return eventParams;
8183
}
8284
}
8385

blueprint-plugins/blueprint-plugin-template/src/static/src/constants.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

blueprint-plugins/blueprint-plugin-template/src/static/src/index.ts

Lines changed: 0 additions & 63 deletions
This file was deleted.

blueprint-plugins/blueprint-plugin-template/src/utils.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { Settings } from "./types";
44
import defaultSettings from "../.amplicationrc.json";
55

66
export const getPluginSettings = (
7-
pluginInstallations: PluginInstallation[]
7+
pluginInstallations: PluginInstallation[],
88
): Settings => {
99
const plugin = pluginInstallations.find(
10-
(plugin) => plugin.npm === PackageName
10+
(plugin) => plugin.npm === PackageName,
1111
);
1212

1313
const userSettings = plugin?.settings ?? {};
@@ -19,16 +19,3 @@ export const getPluginSettings = (
1919

2020
return settings;
2121
};
22-
23-
export function replacePlaceholders(
24-
template: string,
25-
replacements: Record<string, string>
26-
): string {
27-
return template.replace(/{{(.*?)}}/g, (match, key) => {
28-
// Return the replacement value if it exists (even if it's an empty string)
29-
// Otherwise, keep the placeholder
30-
return Object.prototype.hasOwnProperty.call(replacements, key.trim())
31-
? replacements[key.trim()]
32-
: match;
33-
});
34-
}

blueprint-plugins/blueprint-plugin-template/src/static/package.json renamed to blueprint-plugins/blueprint-plugin-template/static/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
"author": "{{PLUGIN_AUTHOR}}",
1717
"license": "{{PLUGIN_LICENSE}}",
1818
"devDependencies": {
19-
"@amplication/code-gen-types": "^3.2.0",
19+
"@amplication/code-gen-types": "^3.2.1",
2020
"@amplication/code-gen-utils": "^0.0.9",
21+
"@amplication/csharp-ast": "^0.0.7",
2122
"@babel/parser": "^7.23.0",
2223
"@babel/types": "^7.23.0",
2324
"@types/lodash": "^4.14.200",
@@ -34,6 +35,7 @@
3435
"ts-loader": "^9.5.0",
3536
"typescript": "^5.2.2",
3637
"webpack": "^5.89.0",
37-
"webpack-cli": "^5.1.4"
38+
"webpack-cli": "^5.1.4",
39+
"pascal-case": "^3.1.2"
3840
}
3941
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const PLUGIN_ID = "{{PLUGIN_KEBAB_CASE_NAME}}";
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import {
2+
blueprintPluginEventsParams as blueprint,
3+
blueprintPluginEventsTypes,
4+
blueprintTypes,
5+
IFile,
6+
} from "@amplication/code-gen-types";
7+
import { CodeBlock } from "@amplication/csharp-ast";
8+
import { resolve } from "path";
9+
import { pascalCase } from "pascal-case";
10+
11+
class PluginName implements blueprintTypes.AmplicationPlugin {
12+
register(): blueprintPluginEventsTypes.BlueprintEvents {
13+
return {
14+
createBlueprint: {
15+
before: this.beforeCreateBlueprint,
16+
},
17+
};
18+
}
19+
async beforeCreateBlueprint(
20+
context: blueprintTypes.DsgContext,
21+
eventParams: blueprint.CreateBlueprintParams,
22+
): Promise<blueprint.CreateBlueprintParams> {
23+
context.logger.info("Generating Files from PluginName...");
24+
25+
const params = {} as Record<string, string>;
26+
27+
params.SERVICE_DISPLAY_NAME = context.resourceInfo?.name || "Service Name";
28+
params.SERVICE_NAME = pascalCase(params.SERVICE_DISPLAY_NAME);
29+
30+
//resource catalog properties
31+
const resourceCatalogProperties = (context.resourceInfo?.properties ||
32+
{}) as Record<string, string>;
33+
const resourceSetting = (context.resourceSettings?.properties ||
34+
({} as Record<string, string>)) as Record<string, string>;
35+
36+
//all catalog and resource settings are available for use in the template
37+
const placeholders = {
38+
...params,
39+
...resourceCatalogProperties,
40+
...resourceSetting,
41+
};
42+
43+
const stringReplacements = {
44+
ServiceName: params.SERVICE_NAME,
45+
};
46+
47+
// set the path to the static files and fetch them for manipulation
48+
const staticPath = resolve(__dirname, "./static");
49+
const files = await context.utils.importStaticFilesWithReplacements(
50+
staticPath,
51+
".",
52+
placeholders,
53+
stringReplacements,
54+
);
55+
56+
for (const file of files.getAll()) {
57+
const codeBlock: IFile<CodeBlock> = {
58+
path: file.path,
59+
code: new CodeBlock({
60+
code: file.code,
61+
}),
62+
};
63+
context.files.set(codeBlock);
64+
}
65+
return eventParams;
66+
}
67+
}
68+
69+
export default PluginName;
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { PluginInstallation } from "@amplication/code-gen-types";
2-
import { name as PackageName } from "../package.json";
32
import { Settings } from "./types";
43
import defaultSettings from "../.amplicationrc.json";
4+
import { PLUGIN_ID } from "./constants";
55

66
export const getPluginSettings = (
77
pluginInstallations: PluginInstallation[],
88
): Settings => {
99
const plugin = pluginInstallations.find(
10-
(plugin) => plugin.npm === PackageName,
10+
(plugin) => plugin.pluginId === PLUGIN_ID,
1111
);
1212

1313
const userSettings = plugin?.settings ?? {};
@@ -19,12 +19,3 @@ export const getPluginSettings = (
1919

2020
return settings;
2121
};
22-
23-
export function replacePlaceholders(
24-
template: string,
25-
replacements: Record<string, string>,
26-
): string {
27-
return template.replace(/{{(.*?)}}/g, (match, key) => {
28-
return replacements[key.trim()] || match; // Use placeholder if no replacement found
29-
});
30-
}

blueprint-plugins/blueprint-plugin-template/src/static/tsconfig.json renamed to blueprint-plugins/blueprint-plugin-template/static/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
"noFallthroughCasesInSwitch": true // Prevent fallthrough in switch statements
1919
},
2020
"include": ["src/**/*"], // Include all files in the src directory
21-
"exclude": ["node_modules", "dist", "src/static"] // Exclude unnecessary and template files
21+
"exclude": ["node_modules", "dist", "src/static", "static"] // Exclude unnecessary and template files
2222
}

blueprint-plugins/blueprint-plugin-template/src/static/webpack.config.js renamed to blueprint-plugins/blueprint-plugin-template/static/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = {
1414
}),
1515
new CopyWebpackPlugin({
1616
patterns: [
17+
{ from: "static", to: "static", noErrorOnMissing: true },
1718
{ from: "src/static", to: "static", noErrorOnMissing: true },
1819
{ from: "src/templates", to: "templates", noErrorOnMissing: true },
1920
],

blueprint-plugins/blueprint-plugin-template/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
"noFallthroughCasesInSwitch": true // Prevent fallthrough in switch statements
1919
},
2020
"include": ["src/**/*"], // Include all files in the src directory
21-
"exclude": ["node_modules", "dist", "src/static"] // Exclude unnecessary and template files
21+
"exclude": ["node_modules", "dist", "src/static", "static/**/*"] // Exclude unnecessary and template files
2222
}

blueprint-plugins/blueprint-plugin-template/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = {
1414
}),
1515
new CopyWebpackPlugin({
1616
patterns: [
17+
{ from: "static", to: "static", noErrorOnMissing: true },
1718
{ from: "src/static", to: "static", noErrorOnMissing: true },
1819
{ from: "src/templates", to: "templates", noErrorOnMissing: true },
1920
],

0 commit comments

Comments
 (0)