Skip to content

Commit

Permalink
update blueprint template
Browse files Browse the repository at this point in the history
  • Loading branch information
yuval-hazaz committed Feb 17, 2025
1 parent 1c5ca20 commit 407fcf1
Show file tree
Hide file tree
Showing 25 changed files with 113 additions and 127 deletions.
8 changes: 4 additions & 4 deletions blueprint-plugins/blueprint-plugin-template/package-lock.json

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

2 changes: 1 addition & 1 deletion blueprint-plugins/blueprint-plugin-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"author": "Yuval Hazaz",
"license": "Apache-2.0",
"devDependencies": {
"@amplication/code-gen-types": "3.2.0",
"@amplication/code-gen-types": "3.2.1",
"@amplication/code-gen-utils": "^0.0.9",
"@babel/parser": "^7.23.0",
"@babel/types": "^7.23.0",
Expand Down
50 changes: 26 additions & 24 deletions blueprint-plugins/blueprint-plugin-template/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,32 @@ import {
blueprintPluginEventsParams as blueprint,
blueprintPluginEventsTypes,
blueprintTypes,
FileMap,
IFile,
} from "@amplication/code-gen-types";
import { CodeBlock } from "@amplication/csharp-ast";
import { camelCase, kebabCase } from "lodash";
import { resolve, join } from "path";
import { REPLACEMENTS } from "./constants";
import { replacePlaceholders, getPluginSettings } from "./utils";
import { getPluginSettings } from "./utils";

class BlueprintPluginTemplatePlugin
implements blueprintTypes.AmplicationPlugin
{
register(): blueprintPluginEventsTypes.BlueprintEvents {
return {
createBlueprint: {
after: this.afterLoadStaticFiles,
before: this.beforeCreateBlueprint,
},
};
}
async afterLoadStaticFiles(
async beforeCreateBlueprint(
context: blueprintTypes.DsgContext,
eventParams: blueprint.CreateBlueprintParams,
files: FileMap<CodeBlock>,
): Promise<FileMap<CodeBlock>> {
): Promise<blueprint.CreateBlueprintParams> {
context.logger.info("Generating Static Files ...");

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

const kebabCasePluginName = kebabCase(pluginName.trim());
Expand All @@ -51,33 +49,37 @@ class BlueprintPluginTemplatePlugin
2,
);

const stringReplacements = {
PluginName: REPLACEMENTS.PLUGIN_CAMEL_CASE_NAME,
};

const basePluginPath = `./plugins/${kebabCasePluginName}`;

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

// set the path to the static files and fetch them for manipulation
const staticPath = resolve(__dirname, "./static");
const staticFiles = await context.utils.importStaticFiles(staticPath, "./");

for (const item of staticFiles.getAll()) {
item.code = replacePlaceholders(item.code, REPLACEMENTS);

item.path = join(
basePluginPath,
replacePlaceholders(item.path, REPLACEMENTS),
);

context.logger.info(`generating file at path: ${item.path}`);
const files = await context.utils.importStaticFilesWithReplacements(
staticPath,
".",
REPLACEMENTS,
stringReplacements,
);

const file: IFile<CodeBlock> = {
path: item.path,
for (const file of files.getAll()) {
const codeBlock: IFile<CodeBlock> = {
path: join(
basePluginPath,
context.utils.replacePlaceholders(file.path, REPLACEMENTS),
),
code: new CodeBlock({
code: item.code,
code: file.code,
}),
};
files.set(file);
context.files.set(codeBlock);
}
return files;

return eventParams;
}
}

Expand Down

This file was deleted.

This file was deleted.

17 changes: 2 additions & 15 deletions blueprint-plugins/blueprint-plugin-template/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { Settings } from "./types";
import defaultSettings from "../.amplicationrc.json";

export const getPluginSettings = (
pluginInstallations: PluginInstallation[]
pluginInstallations: PluginInstallation[],
): Settings => {
const plugin = pluginInstallations.find(
(plugin) => plugin.npm === PackageName
(plugin) => plugin.npm === PackageName,
);

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

return settings;
};

export function replacePlaceholders(
template: string,
replacements: Record<string, string>
): string {
return template.replace(/{{(.*?)}}/g, (match, key) => {
// Return the replacement value if it exists (even if it's an empty string)
// Otherwise, keep the placeholder
return Object.prototype.hasOwnProperty.call(replacements, key.trim())
? replacements[key.trim()]
: match;
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
"author": "{{PLUGIN_AUTHOR}}",
"license": "{{PLUGIN_LICENSE}}",
"devDependencies": {
"@amplication/code-gen-types": "^3.2.0",
"@amplication/code-gen-types": "^3.2.1",
"@amplication/code-gen-utils": "^0.0.9",
"@amplication/csharp-ast": "^0.0.7",
"@babel/parser": "^7.23.0",
"@babel/types": "^7.23.0",
"@types/lodash": "^4.14.200",
Expand All @@ -34,6 +35,7 @@
"ts-loader": "^9.5.0",
"typescript": "^5.2.2",
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4"
"webpack-cli": "^5.1.4",
"pascal-case": "^3.1.2"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const PLUGIN_ID = "{{PLUGIN_KEBAB_CASE_NAME}}";
69 changes: 69 additions & 0 deletions blueprint-plugins/blueprint-plugin-template/static/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {
blueprintPluginEventsParams as blueprint,
blueprintPluginEventsTypes,
blueprintTypes,
IFile,
} from "@amplication/code-gen-types";
import { CodeBlock } from "@amplication/csharp-ast";
import { resolve } from "path";
import { pascalCase } from "pascal-case";

class PluginName implements blueprintTypes.AmplicationPlugin {
register(): blueprintPluginEventsTypes.BlueprintEvents {
return {
createBlueprint: {
before: this.beforeCreateBlueprint,
},
};
}
async beforeCreateBlueprint(
context: blueprintTypes.DsgContext,
eventParams: blueprint.CreateBlueprintParams,
): Promise<blueprint.CreateBlueprintParams> {
context.logger.info("Generating Files from PluginName...");

const params = {} as Record<string, string>;

params.SERVICE_DISPLAY_NAME = context.resourceInfo?.name || "Service Name";
params.SERVICE_NAME = pascalCase(params.SERVICE_DISPLAY_NAME);

//resource catalog properties
const resourceCatalogProperties = (context.resourceInfo?.properties ||
{}) as Record<string, string>;
const resourceSetting = (context.resourceSettings?.properties ||
({} as Record<string, string>)) as Record<string, string>;

//all catalog and resource settings are available for use in the template
const placeholders = {
...params,
...resourceCatalogProperties,
...resourceSetting,
};

const stringReplacements = {
ServiceName: params.SERVICE_NAME,
};

// set the path to the static files and fetch them for manipulation
const staticPath = resolve(__dirname, "./static");
const files = await context.utils.importStaticFilesWithReplacements(
staticPath,
".",
placeholders,
stringReplacements,
);

for (const file of files.getAll()) {
const codeBlock: IFile<CodeBlock> = {
path: file.path,
code: new CodeBlock({
code: file.code,
}),
};
context.files.set(codeBlock);
}
return eventParams;
}
}

export default PluginName;
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { PluginInstallation } from "@amplication/code-gen-types";
import { name as PackageName } from "../package.json";
import { Settings } from "./types";
import defaultSettings from "../.amplicationrc.json";
import { PLUGIN_ID } from "./constants";

export const getPluginSettings = (
pluginInstallations: PluginInstallation[],
): Settings => {
const plugin = pluginInstallations.find(
(plugin) => plugin.npm === PackageName,
(plugin) => plugin.pluginId === PLUGIN_ID,
);

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

return settings;
};

export function replacePlaceholders(
template: string,
replacements: Record<string, string>,
): string {
return template.replace(/{{(.*?)}}/g, (match, key) => {
return replacements[key.trim()] || match; // Use placeholder if no replacement found
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
"noFallthroughCasesInSwitch": true // Prevent fallthrough in switch statements
},
"include": ["src/**/*"], // Include all files in the src directory
"exclude": ["node_modules", "dist", "src/static"] // Exclude unnecessary and template files
"exclude": ["node_modules", "dist", "src/static", "static"] // Exclude unnecessary and template files
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
}),
new CopyWebpackPlugin({
patterns: [
{ from: "static", to: "static", noErrorOnMissing: true },
{ from: "src/static", to: "static", noErrorOnMissing: true },
{ from: "src/templates", to: "templates", noErrorOnMissing: true },
],
Expand Down
2 changes: 1 addition & 1 deletion blueprint-plugins/blueprint-plugin-template/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
"noFallthroughCasesInSwitch": true // Prevent fallthrough in switch statements
},
"include": ["src/**/*"], // Include all files in the src directory
"exclude": ["node_modules", "dist", "src/static"] // Exclude unnecessary and template files
"exclude": ["node_modules", "dist", "src/static", "static/**/*"] // Exclude unnecessary and template files
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
}),
new CopyWebpackPlugin({
patterns: [
{ from: "static", to: "static", noErrorOnMissing: true },
{ from: "src/static", to: "static", noErrorOnMissing: true },
{ from: "src/templates", to: "templates", noErrorOnMissing: true },
],
Expand Down

0 comments on commit 407fcf1

Please sign in to comment.