From 71fb0269e3454dff644b78ff4556dbf73eee5b5f Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 16 Aug 2024 13:01:27 +0200 Subject: [PATCH] core-compat-api: spit out explicit legacy extension converter Signed-off-by: Patrik Oldsberg --- ...gin.tsx => convertLegacyPageExtension.tsx} | 63 +++++++------------ .../src/convertLegacyPlugin.ts | 37 +++++++++++ 2 files changed, 58 insertions(+), 42 deletions(-) rename packages/core-compat-api/src/{convertLegacyPlugin.tsx => convertLegacyPageExtension.tsx} (50%) create mode 100644 packages/core-compat-api/src/convertLegacyPlugin.ts diff --git a/packages/core-compat-api/src/convertLegacyPlugin.tsx b/packages/core-compat-api/src/convertLegacyPageExtension.tsx similarity index 50% rename from packages/core-compat-api/src/convertLegacyPlugin.tsx rename to packages/core-compat-api/src/convertLegacyPageExtension.tsx index 052888d53c..2ff5e0c574 100644 --- a/packages/core-compat-api/src/convertLegacyPlugin.tsx +++ b/packages/core-compat-api/src/convertLegacyPageExtension.tsx @@ -15,44 +15,31 @@ */ import { - BackstagePlugin as LegacyBackstagePlugin, getComponentData, RouteRef as LegacyRouteRef, } from '@backstage/core-plugin-api'; import { ExtensionDefinition, - BackstagePlugin as NewBackstagePlugin, - createPageExtension, - createPlugin, + PageBlueprint, } from '@backstage/frontend-plugin-api'; import kebabCase from 'lodash/kebabCase'; -import { - convertLegacyRouteRef, - convertLegacyRouteRefs, -} from './convertLegacyRouteRef'; +import { convertLegacyRouteRef } from './convertLegacyRouteRef'; import { ComponentType } from 'react'; import React from 'react'; import { compatWrapper } from './compatWrapper'; -/** @internal */ -export function convertLegacyExtension( +/** @public */ +export function convertLegacyPageExtension( LegacyExtension: ComponentType<{}>, - legacyPlugin: LegacyBackstagePlugin, -): ExtensionDefinition { + overrides?: { + name?: string; + defaultPath?: string; + }, +): ExtensionDefinition { const element = ; - const plugin = getComponentData( - element, - 'core.plugin', - ); - if (legacyPlugin !== plugin) { - throw new Error( - `The extension does not belong to the same plugin, got ${plugin?.getId()}`, - ); - } - - const name = getComponentData(element, 'core.extensionName'); - if (!name) { + const extName = getComponentData(element, 'core.extensionName'); + if (!extName) { throw new Error('Extension has no name'); } @@ -61,25 +48,17 @@ export function convertLegacyExtension( 'core.mountPoint', ); - if (name.endsWith('Page')) { - return createPageExtension({ - defaultPath: kebabCase(name.slice(0, -'Page'.length)), + const name = extName.endsWith('Page') + ? extName.slice(0, -'Page'.length) + : extName; + const kebabName = kebabCase(name); + + return PageBlueprint.make({ + name: overrides?.name ?? kebabName, + params: { + defaultPath: overrides?.defaultPath ?? `/${kebabName}`, routeRef: mountPoint && convertLegacyRouteRef(mountPoint), loader: () => Promise.resolve(compatWrapper(element)), - }); - } -} - -/** @public */ -export function convertLegacyPlugin( - legacyPlugin: LegacyBackstagePlugin, - options: { extensions: ExtensionDefinition[] }, -): NewBackstagePlugin { - return createPlugin({ - id: legacyPlugin.getId(), - featureFlags: [...legacyPlugin.getFeatureFlags()], - routes: convertLegacyRouteRefs(legacyPlugin.routes ?? {}), - externalRoutes: convertLegacyRouteRefs(legacyPlugin.externalRoutes ?? {}), - extensions: options.extensions, + }, }); } diff --git a/packages/core-compat-api/src/convertLegacyPlugin.ts b/packages/core-compat-api/src/convertLegacyPlugin.ts new file mode 100644 index 0000000000..05beb4e469 --- /dev/null +++ b/packages/core-compat-api/src/convertLegacyPlugin.ts @@ -0,0 +1,37 @@ +/* + * Copyright 2024 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { BackstagePlugin as LegacyBackstagePlugin } from '@backstage/core-plugin-api'; +import { + ExtensionDefinition, + BackstagePlugin as NewBackstagePlugin, + createFrontendPlugin, +} from '@backstage/frontend-plugin-api'; +import { convertLegacyRouteRefs } from './convertLegacyRouteRef'; + +/** @public */ +export function convertLegacyPlugin( + legacyPlugin: LegacyBackstagePlugin, + options: { extensions: ExtensionDefinition[] }, +): NewBackstagePlugin { + return createFrontendPlugin({ + id: legacyPlugin.getId(), + featureFlags: [...legacyPlugin.getFeatureFlags()], + routes: convertLegacyRouteRefs(legacyPlugin.routes ?? {}), + externalRoutes: convertLegacyRouteRefs(legacyPlugin.externalRoutes ?? {}), + extensions: options.extensions, + }); +}