core-compat-api: spit out explicit legacy extension converter

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-08-16 13:01:27 +02:00
parent 8925f18f7c
commit 71fb0269e3
2 changed files with 58 additions and 42 deletions
@@ -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<any, any> {
overrides?: {
name?: string;
defaultPath?: string;
},
): ExtensionDefinition<any> {
const element = <LegacyExtension />;
const plugin = getComponentData<LegacyBackstagePlugin>(
element,
'core.plugin',
);
if (legacyPlugin !== plugin) {
throw new Error(
`The extension does not belong to the same plugin, got ${plugin?.getId()}`,
);
}
const name = getComponentData<string>(element, 'core.extensionName');
if (!name) {
const extName = getComponentData<string>(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<any, any>[] },
): NewBackstagePlugin {
return createPlugin({
id: legacyPlugin.getId(),
featureFlags: [...legacyPlugin.getFeatureFlags()],
routes: convertLegacyRouteRefs(legacyPlugin.routes ?? {}),
externalRoutes: convertLegacyRouteRefs(legacyPlugin.externalRoutes ?? {}),
extensions: options.extensions,
},
});
}
@@ -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<any, any>[] },
): NewBackstagePlugin {
return createFrontendPlugin({
id: legacyPlugin.getId(),
featureFlags: [...legacyPlugin.getFeatureFlags()],
routes: convertLegacyRouteRefs(legacyPlugin.routes ?? {}),
externalRoutes: convertLegacyRouteRefs(legacyPlugin.externalRoutes ?? {}),
extensions: options.extensions,
});
}