Skip to content

Commit

Permalink
refactor: working generated code setup
Browse files Browse the repository at this point in the history
  • Loading branch information
levivannoort committed Sep 28, 2024
1 parent 9e42747 commit 7b73665
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 31 deletions.
22 changes: 19 additions & 3 deletions plugins/backstage-catalog-entity/.amplicationrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
{
"settings": {},
"systemSettings": {
"requireAuthenticationEntity": "false"
"settings": {
"labels": {
"vendor": "amplication"
},
"annotations": {
"github.com/project-slug": "backstage/backstage",
"github.com/team-slug": "backstage/maintainers"
},
"tags": [
"Node.js"
],
"spec": {
"type": "service",
"lifecycle": "production",
"owner": "development"
}
},
"systemSettings": {
"requireAuthenticationEntity": "false"
}
}
21 changes: 20 additions & 1 deletion plugins/backstage-catalog-entity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,29 @@ Documentation:

- register: https://backstage.io/docs/features/software-catalog/#manually-register-components
- documentation: https://backstage.io/docs/features/software-catalog/descriptor-format/
- annotations: https://backstage.io/docs/features/software-catalog/well-known-annotations

## Configuration

If a configuration is required, add it here.
`example`

```json
{
"labels": {
"vendor": "amplication"
},
"annotations": {
"github.com/project-slug": "backstage/backstage",
"github.com/team-slug": "backstage/maintainers"
},
"tags": ["Node.js"],
"spec": {
"type": "service",
"lifecycle": "production",
"owner": "development"
}
}
```

## Scripts

Expand Down
9 changes: 5 additions & 4 deletions plugins/backstage-catalog-entity/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ export const specTypeKey = "${{ SPEC_TYPE }}";
export const specLifecycleKey = "${{ SPEC_LIFECYCLE }}";
export const specOwnerKey = "${{ SPEC_OWNER }}";

export const workspaceIdKey = "${{ WORKSPACE_ID }}";
export const projectIdKey = "${{ PROJECT_ID }}";
export const serviceIdKey = "${{ SERVICE_ID }}";
export const appUrlKey = "${{ APP_URL }}";

export const defaultNamePrefix = "Amplication";
export const defaultDescription =
"Backend service which is auto-generated by Amplication.";
export const defaultLabels = new Map([["vendor", "amplication"]]);
export const defaultAnnotations = new Map([["vendor", "amplication"]]);
export const defaultAnnotations = new Map([
["github.com/project-slug", "amplication"],
]);
export const defaultUrl = "https://app.amplication.com";
export const defaultTags = ["Node.js"];
export const defaultLifecycle = "production";
export const defaultOwner = "development";
52 changes: 38 additions & 14 deletions plugins/backstage-catalog-entity/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import {
metadataLabelsKey,
metadataAnnotationsKey,
metadataTagsKey,
appUrlKey,
specLifecycleKey,
specOwnerKey,
specTypeKey,
defaultNamePrefix,
defaultDescription,
defaultLabels,
defaultAnnotations,
defaultUrl,
defaultTags,
defaultLifecycle,
defaultOwner,
Expand Down Expand Up @@ -47,14 +49,15 @@ class BackstageCatalogEntityPlugin implements AmplicationPlugin {

const serviceName = kebabCase(context.resourceInfo?.name);
const serviceDescription = context.resourceInfo?.description;
const serviceType = context.resourceType.toLowerCase();
const serviceType = "service";
const serviceUrl = context.resourceInfo?.url;

const settings = getPluginSettings(context.pluginInstallations);

const staticPath = resolve(__dirname, "./static/");
const staticFiles = await context.utils.importStaticModules(
staticPath,
"."
"./"
);

const templateFileName = "template.yaml";
Expand All @@ -64,9 +67,9 @@ class BackstageCatalogEntityPlugin implements AmplicationPlugin {
path.replace(templateFileName, destinationFileName)
);

let labels = "";
let annotations = "";
let tags = "";
let labels = `labels:`;
let annotations = `annotations:`;
let tags = `tags:`;

const indentation = " ";

Expand All @@ -78,38 +81,59 @@ class BackstageCatalogEntityPlugin implements AmplicationPlugin {

const l = settings.labels ?? defaultLabels;

l.forEach((value, name) => {
labels = `${labels}\n${indentation}${name}: "${value}"`;
});
if (typeof l === "object" && l !== null) {
Object.entries(l).forEach(([name, value]) => {
labels = `${labels}\n${indentation}${name}: "${value}"`;
});
} else {
context.logger.info(`"Labels should be an object:"`, l);
}

const a = settings.annotations ?? defaultAnnotations;

a.forEach((value, name) => {
annotations = `${annotations}\n${indentation}${name}: "${value}"`;
});
if (typeof a === "object" && a !== null) {
Object.entries(a).forEach(([name, value]) => {
annotations = `${annotations}\n${indentation}${name}: "${value}"`;
});
} else {
context.logger.info(`"Annotations should be an object:"`, a);
}

// Handling tags (Array)
const t = settings.tags ?? defaultTags;

t.forEach((tag) => {
tags = `${tags}\n${indentation}- "${tag}"`;
});
if (Array.isArray(t)) {
t.forEach((tag) => {
tags = `${tags}\n${indentation}- "${tag}"`;
});
} else {
context.logger.info(`"Tags should be an array:"`, t);
}

const appUrl = serviceUrl ?? defaultUrl;

const type = settings.spec?.type ?? serviceType;
const lifecycle = settings.spec?.life_cycle ?? defaultLifecycle;
const owner = settings.spec?.owner ?? defaultOwner;

staticFiles.replaceModulesPath((path) =>
path.replace(templateFileName, destinationFileName)
);

staticFiles.replaceModulesCode((_path, code) =>
code
.replace(metadataNameKey, name)
.replace(metadataDescriptionKey, description)
.replace(metadataLabelsKey, labels)
.replace(metadataAnnotationsKey, annotations)
.replace(metadataTagsKey, tags)
.replace(appUrlKey, appUrl)
.replace(specTypeKey, type)
.replace(specLifecycleKey, lifecycle)
.replace(specOwnerKey, owner)
);

await modules.merge(staticFiles);
context.logger.info(`Generated ${pluginDescription}...`);
return modules;
}
Expand Down
13 changes: 4 additions & 9 deletions plugins/backstage-catalog-entity/src/static/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@ kind: Component
metadata:
name: ${{ METADATA_NAME }}
description: ${{ METADATA_DESCRIPTION }}
labels:
${{ METADATA_LABELS_CUSTOM }}
annotations:
${{ METADATA_ANNOTATIONS }}
github.com/project-slug: [GitHub repo of the service]
tags:
${{ METDATA_TAGS }}
${{ METADATA_LABELS }}
${{ METADATA_ANNOTATIONS }}
${{ METADATA_TAGS }}
links:
- url: https://app.amplication.com/${{ WORKSPACE_ID }}/${{ PROJECT_ID }}/${{ SERVICE_ID }}
- url: ${{ APP_URL }}
title: Amplication Platform
icon: [Amplication icon - The icon field value is meant to be a semantic key that will map to a specific icon that may be provided by an icon library (e.g. material-ui icons)]
spec:
type: ${{ SPEC_TYPE }}
lifecycle: ${{ SPEC_LIFECYCLE }}
Expand Down

0 comments on commit 7b73665

Please sign in to comment.