-
Notifications
You must be signed in to change notification settings - Fork 613
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22d4f94
commit 419396f
Showing
18 changed files
with
491 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
import '../../../public/components/app'; | ||
import '@console/internal/components/app'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "@console/demo-plugin", | ||
"version": "0.0.0-fixed", | ||
"description": "Demo plugin for Console web application", | ||
"private": true, | ||
"dependencies": { | ||
"@console/plugin-sdk": "0.0.0-fixed", | ||
"@console/shared": "0.0.0-fixed" | ||
}, | ||
"consolePlugin": { | ||
"entry": "src/plugin.ts" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { | ||
Plugin, | ||
HrefNavItem, | ||
ResourceNSNavItem, | ||
ResourceListPage, | ||
ResourceDetailPage, | ||
} from '@console/plugin-sdk'; | ||
|
||
// TODO(vojtech): internal code needed by plugins should be moved to console-shared package | ||
import { PodModel } from '@console/internal/models'; | ||
|
||
type ConsumedExtensions = | ||
| HrefNavItem | ||
| ResourceNSNavItem | ||
| ResourceListPage | ||
| ResourceDetailPage; | ||
|
||
const plugin: Plugin<ConsumedExtensions> = [ | ||
{ | ||
type: 'NavItem/Href', | ||
properties: { | ||
section: 'Home', | ||
componentProps: { | ||
name: 'Test Href Link', | ||
href: '/test', | ||
}, | ||
}, | ||
}, | ||
{ | ||
type: 'NavItem/ResourceNS', | ||
properties: { | ||
section: 'Workloads', | ||
componentProps: { | ||
name: 'Test ResourceNS Link', | ||
resource: 'pods', | ||
}, | ||
}, | ||
}, | ||
{ | ||
type: 'ResourcePage/List', | ||
properties: { | ||
model: PodModel, | ||
loader: () => import('@console/internal/components/pod' /* webpackChunkName: "pod" */).then(m => m.PodsPage), | ||
}, | ||
}, | ||
{ | ||
type: 'ResourcePage/Detail', | ||
properties: { | ||
model: PodModel, | ||
loader: () => import('@console/internal/components/pod' /* webpackChunkName: "pod" */).then(m => m.PodsDetailsPage), | ||
}, | ||
}, | ||
]; | ||
|
||
export default plugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "@console/plugin-sdk", | ||
"version": "0.0.0-fixed", | ||
"description": "Console static plugin SDK", | ||
"private": true, | ||
"main": "src/index.ts", | ||
"scripts": { | ||
"test": "yarn --cwd ../.. run test packages/console-plugin-sdk" | ||
}, | ||
"dependencies": { | ||
"@console/shared": "0.0.0-fixed" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* eslint-env node */ | ||
|
||
import * as path from 'path'; | ||
import * as readPkg from 'read-pkg'; | ||
|
||
type Package = readPkg.NormalizedPackageJson; | ||
|
||
interface PluginPackage extends Package { | ||
consolePlugin: { | ||
entry: string; | ||
} | ||
} | ||
|
||
function isValidPluginPackage(pkg: Package): pkg is PluginPackage { | ||
return (pkg as PluginPackage).consolePlugin && typeof (pkg as PluginPackage).consolePlugin.entry === 'string'; | ||
} | ||
|
||
function readPackages(packageFiles: string[]) { | ||
const pkgList: Package[] = packageFiles.map(file => readPkg.sync({ cwd: path.dirname(file), normalize: true })); | ||
|
||
return { | ||
appPackage: pkgList.find(pkg => pkg.name === '@console/app'), | ||
pluginPackages: pkgList.filter(isValidPluginPackage), | ||
}; | ||
} | ||
|
||
/** | ||
* Generate the "active plugins" module source. | ||
* | ||
* @param packageFiles Paths to `package.json` files (all the monorepo packages). | ||
*/ | ||
export function getActivePluginsModule(packageFiles: string[]): string { | ||
const { appPackage, pluginPackages } = readPackages(packageFiles); | ||
let output = ` | ||
var activePlugins = []; | ||
`; | ||
|
||
if (appPackage) { | ||
for (const depName of Object.keys(appPackage.dependencies)) { | ||
const depVersion = appPackage.dependencies[depName]; | ||
const foundPluginPackage = pluginPackages.find(pkg => pkg.name === depName && pkg.version === depVersion); | ||
|
||
if (foundPluginPackage) { | ||
const importName = `plugin_${pluginPackages.indexOf(foundPluginPackage)}`; | ||
const importPath = `${foundPluginPackage.name}/${foundPluginPackage.consolePlugin.entry}`; | ||
output = ` | ||
${output} | ||
import ${importName} from '${importPath}'; | ||
activePlugins.push(${importName}); | ||
`; | ||
} | ||
} | ||
} | ||
|
||
output = ` | ||
${output} | ||
export default activePlugins; | ||
`; | ||
|
||
return output.replace(/^\s+/gm, ''); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './typings'; | ||
export * from './registry'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import * as _ from 'lodash-es'; | ||
import { Extension, PluginList, isNavItem, isResourcePage } from './typings'; | ||
|
||
/** | ||
* Registry used to query for Console extensions. | ||
*/ | ||
export class ExtensionRegistry { | ||
|
||
private readonly extensions: Extension<any>[]; | ||
|
||
public constructor(plugins: PluginList) { | ||
this.extensions = _.flatMap(plugins); | ||
} | ||
|
||
public getNavItems(section: string) { | ||
return this.extensions.filter(isNavItem).filter(e => e.properties.section === section); | ||
} | ||
|
||
public getResourcePages() { | ||
return this.extensions.filter(isResourcePage); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* An extension of the Console web application. | ||
* | ||
* Each extension is a realization (instance) of an extension `type` using the | ||
* parameters provided via the `properties` object. | ||
* | ||
* Core extension types should follow `Category` or `Category/Specialization` | ||
* format, e.g. `NavItem/Href`. | ||
* | ||
* @todo(vojtech) write ESLint rule to guard against extension type duplicity | ||
*/ | ||
export interface Extension<P> { | ||
type: string; | ||
properties: P; | ||
} | ||
|
||
/** | ||
* A plugin is simply a list of extensions. | ||
* | ||
* Plugin metadata is stored in the `package.json` file of the corresponding | ||
* monorepo package. The `consolePlugin.entry` path should point to a module | ||
* that exports the plugin object. | ||
* | ||
* ```json | ||
* { | ||
* "name": "@console/demo-plugin", | ||
* "version": "0.0.0-fixed", | ||
* // scripts, dependencies, etc. | ||
* "consolePlugin": { | ||
* "entry": "src/plugin" | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* For better type checking and code completion, use a type parameter that | ||
* represents the union of all the extension types consumed by the plugin: | ||
* | ||
* ```ts | ||
* // packages/console-demo-plugin/src/plugin.ts | ||
* import { Plugin } from '@console/plugin-sdk'; | ||
* | ||
* const plugin: Plugin<FooExtension | BarExtension> = [ | ||
* { | ||
* type: 'Foo', | ||
* properties: {} // Foo extension specific properties | ||
* }, | ||
* { | ||
* type: 'Bar', | ||
* properties: {} // Bar extension specific properties | ||
* } | ||
* ]; | ||
* | ||
* export default plugin; | ||
* ``` | ||
*/ | ||
export type Plugin<E extends Extension<any>> = E[]; | ||
|
||
/** | ||
* A list of arbitrary plugins. | ||
*/ | ||
export type PluginList = Plugin<Extension<any>>[]; | ||
|
||
// TODO(vojtech): internal code needed by plugin SDK should be moved to console-shared package | ||
|
||
export * from './nav'; | ||
export * from './pages'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Extension } from '.'; | ||
import { K8sKind } from '@console/internal/module/k8s'; | ||
|
||
export interface NavItemProperties { | ||
// TODO(vojtech): link to existing nav sections by value | ||
section: 'Home' | 'Workloads'; | ||
componentProps: { | ||
name: string; | ||
required?: string; | ||
disallowed?: string; | ||
startsWith?: string[]; | ||
} | ||
} | ||
|
||
export interface HrefProperties extends NavItemProperties { | ||
componentProps: NavItemProperties['componentProps'] & { | ||
href: string; | ||
activePath?: string; | ||
} | ||
} | ||
|
||
export interface ResourceNSProperties extends NavItemProperties { | ||
componentProps: NavItemProperties['componentProps'] & { | ||
resource: string; | ||
model?: K8sKind; | ||
} | ||
} | ||
|
||
export interface HrefNavItem extends Extension<HrefProperties> { | ||
type: 'NavItem/Href'; | ||
} | ||
|
||
export interface ResourceNSNavItem extends Extension<ResourceNSProperties> { | ||
type: 'NavItem/ResourceNS'; | ||
} | ||
|
||
// TODO(vojtech): add ResourceClusterNavItem | ||
export type NavItem = HrefNavItem | ResourceNSNavItem; | ||
|
||
export function isHrefNavItem(e: Extension<any>): e is HrefNavItem { | ||
return e.type === 'NavItem/Href'; | ||
} | ||
|
||
export function isResourceNSNavItem(e: Extension<any>): e is ResourceNSNavItem { | ||
return e.type === 'NavItem/ResourceNS'; | ||
} | ||
|
||
export function isNavItem(e: Extension<any>): e is NavItem { | ||
return isHrefNavItem(e) || isResourceNSNavItem(e); | ||
} |
Oops, something went wrong.