Skip to content

Commit

Permalink
feat: rewrite sort plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
eyolas committed Oct 2, 2017
1 parent 536405a commit fb8ecd6
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 45 deletions.
5 changes: 3 additions & 2 deletions packages/core/package.json
Expand Up @@ -33,14 +33,15 @@
"dependencies": {
"caller": "^1.0.1",
"debug": "^3.0.0",
"gert": "^2.0.0",
"gert-topo-sort": "^1.0.0",
"glob": "^7.1.1",
"inversify": "^4.3.0",
"inversify-logger-middleware": "^3.0.0",
"js-yaml": "^3.8.1",
"lodash": "^4.17.4",
"shortstop": "^1.0.3",
"shortstop-handlers": "^1.0.1",
"topo": "^2.0.2"
"shortstop-handlers": "^1.0.1"
},
"devDependencies": {
"@types/debug": "^0.0.30",
Expand Down
59 changes: 43 additions & 16 deletions packages/core/src/plugin-list.ts
Expand Up @@ -6,7 +6,8 @@ import {
} from './interfaces';
import { METADATA_KEY, ERRORS_MSGS } from './constants';
import * as _ from 'lodash';
import * as Topo from 'topo';
import { Graph } from 'gert';
import * as TopoSort from 'gert-topo-sort';

export class PluginList {
private _plugins: GabliamPluginDefinition[] = [];
Expand All @@ -25,35 +26,61 @@ export class PluginList {
}

sort() {
const treePlugin = new Topo();

const vertices: { [k: string]: string[] } = {};
for (const plugin of this._plugins) {
const after = [];
const before = [];
const orderedPlugin = [plugin.name];
if (plugin.dependencies) {
for (const dep of plugin.dependencies) {
if (!this.has(dep.name)) {
for (const deps of plugin.dependencies) {
if (!this.has(deps.name)) {
throw new Error(
`The plugin ${plugin.name} need the plugin ${dep.name}}`
`The plugin ${plugin.name} need the plugin ${deps.name}}`
);
}
switch (dep.order) {
case 'before':
before.unshift(dep.name);
break;
switch (deps.order) {
case 'after':
after.push(dep.name);
orderedPlugin.unshift(deps.name);
break;
case 'before':
orderedPlugin.push(deps.name);
break;
}
}
}
treePlugin.add(plugin.name, { after, before });
while (orderedPlugin.length && orderedPlugin[0] !== plugin.name) {
const name = orderedPlugin.shift()!;
const index =
orderedPlugin.indexOf(plugin.name) === orderedPlugin.length
? orderedPlugin.length
: orderedPlugin.indexOf(plugin.name) + 1;
const toAdd = _.without(orderedPlugin.slice(0, index), name);
if (!vertices[name]) {
vertices[name] = [];
}

vertices[name].push(...toAdd);
vertices[name] = _.uniq(vertices[name]);
}

if (!vertices[plugin.name]) {
vertices[plugin.name] = [];
}

vertices[plugin.name].push(
..._.without(orderedPlugin.slice(1), plugin.name)
);
vertices[plugin.name] = _.uniq(vertices[plugin.name]);
}

const sortedPlugins = <string[]>treePlugin.nodes;
const listPlugin = sortedPlugins.map<GabliamPluginDefinition>(
const graph = new Graph({
directed: true,
vertices
});

// Rewrite listPlugin
const listPlugin = TopoSort(graph).map<GabliamPluginDefinition>(
p => this.findByName(p)!
);

this._plugins = listPlugin;
}

Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/typings/gert.d.ts
@@ -0,0 +1,18 @@
declare module 'gert' {
interface GraphOptions {
directed: boolean;
vertices: { [k: string]: string[] };
}
export class Graph {
constructor(options: GraphOptions);
}
}

declare module 'gert-topo-sort' {
import gert = require('gert');

type Gts = (graph: gert.Graph) => string[];
const gts: Gts;

export = gts;
}
25 changes: 0 additions & 25 deletions packages/core/src/typings/topo.d.ts

This file was deleted.

15 changes: 13 additions & 2 deletions packages/core/yarn.lock
Expand Up @@ -1540,6 +1540,17 @@ gauge@~2.7.3:
strip-ansi "^3.0.1"
wide-align "^1.1.0"

gert-topo-sort@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/gert-topo-sort/-/gert-topo-sort-1.0.0.tgz#e63d78cb134576c43cda2116f1689bce93a8dac0"

gert@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/gert/-/gert-2.0.0.tgz#634c649751ddd4b6f1aefb46d31f8057a1a62b71"
dependencies:
hoek "^2.16.3"
joi "^6.9.1"

get-caller-file@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-1.0.2.tgz#f702e63127e7e231c160a80c1554acb70d5047e5"
Expand Down Expand Up @@ -1836,7 +1847,7 @@ hawk@~3.1.3:
hoek "2.x.x"
sntp "1.x.x"

hoek@2.x.x:
hoek@2.x.x, hoek@^2.16.3:
version "2.16.3"
resolved "https://registry.yarnpkg.com/hoek/-/hoek-2.16.3.tgz#20bb7403d3cea398e91dc4710a8ff1b8274a25ed"

Expand Down Expand Up @@ -4359,7 +4370,7 @@ topo@1.x.x:
dependencies:
hoek "2.x.x"

topo@2.x.x, topo@^2.0.2:
topo@2.x.x:
version "2.0.2"
resolved "https://registry.yarnpkg.com/topo/-/topo-2.0.2.tgz#cd5615752539057c0dc0491a621c3bc6fbe1d182"
dependencies:
Expand Down

0 comments on commit fb8ecd6

Please sign in to comment.