-
Notifications
You must be signed in to change notification settings - Fork 115
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
Showing
30 changed files
with
1,652 additions
and
571 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import PluginApi = require('./PluginApi'); | ||
|
||
export interface InternalExtratermApi extends PluginApi.ExtratermApi { | ||
setTopLevel(el: HTMLElement): void; | ||
addTab(el: HTMLElement): void; | ||
removeTab(el: HTMLElement): void; | ||
} |
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,28 @@ | ||
/* | ||
* Copyright 2017 Simon Edwards <[email protected]> | ||
* | ||
* This source code is licensed under the MIT license which is detailed in the LICENSE.txt file. | ||
*/ | ||
|
||
export interface PluginMetaData { | ||
name: string; | ||
factory: string; | ||
} | ||
|
||
export interface ElementListener { | ||
(element: HTMLElement): void; | ||
} | ||
|
||
export interface ExtratermApi { | ||
addNewTopLevelEventListener(callback: ElementListener): void; | ||
addNewTabEventListener(callback: ElementListener): void; | ||
// registerViewer(): void; | ||
} | ||
|
||
export interface ExtratermPluginFactory { | ||
(api: ExtratermApi): ExtratermPlugin; | ||
} | ||
|
||
export interface ExtratermPlugin { | ||
|
||
} |
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,93 @@ | ||
/* | ||
* Copyright 2017 Simon Edwards <[email protected]> | ||
* | ||
* This source code is licensed under the MIT license which is detailed in the LICENSE.txt file. | ||
*/ | ||
|
||
import fs = require('fs'); | ||
import path = require('path'); | ||
import Logger = require('./logger'); | ||
import PluginApi = require('./PluginApi'); | ||
|
||
const PLUGIN_METADATA = "metadata.json"; | ||
|
||
// Partial because people forget to fill in json files correctly. | ||
type PartialPluginMetaData = { | ||
[P in keyof PluginApi.PluginMetaData]?: PluginApi.PluginMetaData[P]; | ||
} | ||
|
||
interface PluginInfo { | ||
path: string; | ||
name: string; | ||
factoryName: string; | ||
instance: PluginApi.ExtratermPlugin; | ||
} | ||
|
||
export class PluginManager { | ||
|
||
private _log: Logger = null; | ||
|
||
private _pluginDir: string = null; | ||
|
||
private _pluginData: PluginInfo[] = []; | ||
|
||
constructor(pluginDir: string) { | ||
this._log = new Logger("PluginManager", this); | ||
this._pluginDir = pluginDir; | ||
} | ||
|
||
/** | ||
* Load all of the plugins and create instances. | ||
* | ||
* @param api the API instance to pass to the plugins at creation time. | ||
*/ | ||
load(api: PluginApi.ExtratermApi): void { | ||
this._pluginData = this._scan(this._pluginDir); | ||
|
||
for (const pluginData of this._pluginData) { | ||
const factory = this._loadPlugin(pluginData); | ||
pluginData.instance = factory(api); | ||
} | ||
} | ||
|
||
private _loadPlugin(pluginData: PluginInfo): PluginApi.ExtratermPluginFactory { | ||
const factoryPath = path.join(pluginData.path, pluginData.factoryName); | ||
return <PluginApi.ExtratermPluginFactory> require(factoryPath); | ||
} | ||
|
||
/** | ||
* Scan a directory for available plugins. | ||
* | ||
* @param pluginDir the directory to scan. | ||
* @return list of plugin info describing what was found. | ||
*/ | ||
private _scan(pluginDir: string): PluginInfo[] { | ||
const result: PluginInfo[] = []; | ||
|
||
if (fs.existsSync(pluginDir)) { | ||
const contents = fs.readdirSync(pluginDir); | ||
for (const item of contents) { | ||
const metadataPath = path.join(pluginDir, item, PLUGIN_METADATA); | ||
|
||
if (fs.existsSync(metadataPath)) { | ||
const metadataString = fs.readFileSync(metadataPath, "UTF8"); | ||
try { | ||
const metadata = <PartialPluginMetaData> JSON.parse(metadataString); | ||
if (metadata.name == null || metadata.factory == null) { | ||
this._log.warn(`An error occurred while reading the metadata from ${metadataPath}. It is missing 'name' or 'factory' fields.`); | ||
} else { | ||
result.push( { path: path.join(pluginDir, item), name: metadata.name, factoryName: metadata.factory, instance: null } ); | ||
} | ||
|
||
} catch(ex) { | ||
this._log.warn(`An error occurred while processing ${metadataPath}.`, ex); | ||
} | ||
} else { | ||
this._log.warn(`Couldn't find a ${PLUGIN_METADATA} file in ${item}. Ignoring.`); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
} | ||
|
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
Oops, something went wrong.