Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Commit

Permalink
packages and targets can be have a custom sort value in settings.json…
Browse files Browse the repository at this point in the history
… now
  • Loading branch information
mkloubert committed Dec 10, 2016
1 parent b6b20d7 commit b562188
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Add the subsection `packages` and add one or more entry:
| `exclude` | Files to exclude (s. [node-glob](https://github.com/isaacs/node-glob)). |
| `files` | Files to include (s. [node-glob](https://github.com/isaacs/node-glob)). Default: `**` |
| `name` | The name of the package. |
| `sortOrder` | An optional number to sort the package elements. |

#### Targets

Expand Down Expand Up @@ -131,6 +132,7 @@ Add the subsection `targets` and add one or more entry:
| `deployed` | The operations that should be invoked AFTER ALL files have been deployed successfully. |
| `description` | The description of the target. |
| `name` | The name of the target. |
| `sortOrder` | An optional number to sort the target elements. |
| `type` | The type. |

##### deployed
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vs-deploy",
"displayName": "Deploy",
"description": "Commands for deploying files of your workspace to a destination.",
"version": "1.3.0",
"version": "1.4.0",
"publisher": "mkloubert",
"engines": {
"vscode": "^1.5.0"
Expand Down
20 changes: 20 additions & 0 deletions src/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ export interface DeployContext {
* @param {DeployPackage[]} The packages.
*/
packages(): DeployPackage[];
/**
* Returns the list of (other) plugins.
*
* @param {DeployPlugin[]} The list of (other) plugins.
*/
plugins(): DeployPlugin[];
/**
* Shows a warning message.
*
Expand Down Expand Up @@ -256,6 +262,10 @@ export interface DeployPackage {
* The name.
*/
name?: string;
/**
* The sort order.
*/
sortOrder: number;
}

/**
Expand All @@ -278,6 +288,12 @@ export interface DeployPlugin {
* Gets the filename of the plugin.
*/
__file?: string;
/**
* [INTERNAL] DO NOT DEFINE OR OVERWRITE THIS PROPERTY BY YOUR OWN!
*
* Gets the index of the plugin.
*/
__index?: number;
/**
* [INTERNAL] DO NOT DEFINE OR OVERWRITE THIS PROPERTY BY YOUR OWN!
*
Expand Down Expand Up @@ -340,6 +356,10 @@ export interface DeployTarget {
* The name.
*/
name?: string;
/**
* The sort order.
*/
sortOrder: number;
/**
* The type.
*/
Expand Down
12 changes: 10 additions & 2 deletions src/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ export class Deployer {
packages = [];
}

return packages.filter(x => x);
return deploy_helpers.sortPackages(packages);
}

/**
Expand All @@ -530,7 +530,7 @@ export class Deployer {
targets = [];
}

return targets.filter(x => x);
return deploy_helpers.sortTargets(targets);
}

/**
Expand Down Expand Up @@ -609,6 +609,7 @@ export class Deployer {
}).filter(x => x)
.map(x => Path.join(pluginDir, x));

let nextPluginIndex = -1;
moduleFiles.forEach(x => {
try {
let pluginModule: deploy_contracts.DeployPluginModule = require(x);
Expand Down Expand Up @@ -636,6 +637,7 @@ export class Deployer {
},
outputChannel: () => me.outputChannel,
packages: () => me.getPackages(),
plugins: null,
targets: () => me.getTargets(),
warn: function(msg) {
if (msg) {
Expand All @@ -660,7 +662,13 @@ export class Deployer {

let newPlugin = pluginModule.createPlugin(ctx);
if (newPlugin) {
let pluginIndex = ++nextPluginIndex;
ctx.plugins = function() {
return loadedPlugins.filter(x => x.__index != pluginIndex);
};

newPlugin.__file = deploy_helpers.parseTargetType(Path.basename(x));
newPlugin.__index = pluginIndex;
newPlugin.__type = deploy_helpers.parseTargetType(Path.basename(x, '.js'));

loadedPlugins.push(newPlugin);
Expand Down
92 changes: 92 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ import * as Path from 'path';
import * as vscode from 'vscode';


/**
* Compares two values for a sort operation.
*
* @param {T} x The left value.
* @param {T} y The right value.
*
* @return {number} The "sort value".
*/
export function compareValues<T>(x: T, y: T): number {
if (x > y) {
return 1;
}

if (x < y) {
return -1;
}

return 0;
}

/**
* Creates a quick pick for deploying a single file.
*
Expand Down Expand Up @@ -155,6 +175,78 @@ export function replaceAllStrings(str: string, searchValue: string, replaceValue
.join(replaceValue);
}

/**
* Sorts a list of packages.
*
* @param {deploy_contracts.DeployPackage[]} pkgs The input list.
*
* @return {deploy_contracts.DeployPackage[]} The sorted list.
*/
export function sortPackages(pkgs: deploy_contracts.DeployPackage[]): deploy_contracts.DeployPackage[] {
if (!pkgs) {
pkgs = [];
}

return pkgs.filter(x => x)
.map(x => {
return {
level0: x.sortOrder, // first sort by "sortOrder"
level1: toStringSafe(x.name).toLowerCase().trim(), // then by "name"
value: x,
};
})
.sort((x, y) => {
let comp0 = compareValues(x.level0, y.level0);
if (0 != comp0) {
return comp0;
}

let comp1 = compareValues(x.level1, y.level1);
if (0 != comp1) {
return comp1;
}

return 0;
})
.map(x => x.value);
}

/**
* Sorts a list of targets.
*
* @param {deploy_contracts.DeployTarget[]} pkgs The input list.
*
* @return {deploy_contracts.DeployTarget[]} The sorted list.
*/
export function sortTargets(targets: deploy_contracts.DeployTarget[]): deploy_contracts.DeployTarget[] {
if (!targets) {
targets = [];
}

return targets.filter(x => x)
.map(x => {
return {
level0: x.sortOrder, // first sort by "sortOrder"
level1: toStringSafe(x.name).toLowerCase().trim(), // then by "name"
value: x,
};
})
.sort((x, y) => {
let comp0 = compareValues(x.level0, y.level0);
if (0 != comp0) {
return comp0;
}

let comp1 = compareValues(x.level1, y.level1);
if (0 != comp1) {
return comp1;
}

return 0;
})
.map(x => x.value);
}

/**
* Tries to convert a file path to a relative path.
*
Expand Down

0 comments on commit b562188

Please sign in to comment.