Skip to content

Commit c304e4e

Browse files
committed
yaml patch #2
1 parent db2031a commit c304e4e

File tree

13 files changed

+271
-4
lines changed

13 files changed

+271
-4
lines changed

Common/Node/jsonPatcher.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@ import patch = require('./patch');
33
var jsonPatch = require('fast-json-patch');
44

55
export class JsonPatcher implements patch.IPatcher {
6-
constructor(private patches: patch.IPatch[]) {
7-
6+
constructor(
7+
private patches: patch.IPatch[],
8+
private parse?: (content: string) => any
9+
) {
10+
if (!this.parse) {
11+
this.parse = (content) => JSON.parse(content);
12+
}
813
}
914

1015
apply(content: string): string {
11-
var json = JSON.parse(content);
16+
var json = this.parse(content);
1217
var prevalidate = jsonPatch.validate(this.patches, json);
1318
var result = jsonPatch.apply(json, this.patches, false);
1419
if (result) {

Tasks/YamlPatch/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "jsonpatch",
3+
"private": true,
4+
"version": "0.1.0",
5+
"dependencies": {
6+
"fs-extra": "0.30.0",
7+
"micromatch": "2.3.11",
8+
"fast-json-patch": "1.0.0",
9+
"xregexp": "3.1.1",
10+
"vsts-task-lib": "0.9.6",
11+
"js-yaml": "3.6.1"
12+
}
13+
}

Tasks/YamlPatch/task.json

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"id": "599b994d-c4e1-4e74-b58e-0cd3006e5aac",
3+
"name": "YamlPatch",
4+
"friendlyName": "Patch YAML Files",
5+
"description": "Patch YAML files using JSON patch syntax",
6+
"helpMarkDown": "[More Information](https://github.com/geeklearningio/gl-vsts-tasks-file-patch/wiki/Patch-YAML-Files) (Version #{Version}#)",
7+
"category": "Utility",
8+
"visibility": [
9+
"Build",
10+
"Release"
11+
],
12+
"author": "Geek Learning",
13+
"version": {
14+
"Major": 0,
15+
"Minor": 0,
16+
"Patch": 0
17+
},
18+
"demands": [
19+
"npm"
20+
],
21+
"minimumAgentVersion": "1.91.0",
22+
"instanceNameFormat": "Patch files $(YamlTargetFilters)",
23+
"inputs": [
24+
{
25+
"name": "SyntaxType",
26+
"type": "pickList",
27+
"label": "Syntax type",
28+
"defaultValue": "Standard",
29+
"required": true,
30+
"helpMarkDown": "The syntax used for the patch content. See documentation for more information.",
31+
"options": {
32+
"standard": "Standard Syntax",
33+
"slick": "Slick Syntax"
34+
}
35+
},
36+
{
37+
"name": "YamlWorkingDir",
38+
"type": "filePath",
39+
"label": "Patch working directory",
40+
"defaultValue": "",
41+
"required": true,
42+
"helpMarkDown": "Working directory. Example: $(Build.SourcesDirectory)"
43+
},
44+
{
45+
"name": "YamlTargetFilters",
46+
"type": "multiLine",
47+
"label": "Target files",
48+
"defaultValue": "",
49+
"required": true,
50+
"helpMarkDown": "Patch target file. Example: appsettings*.yaml"
51+
},
52+
{
53+
"name": "YamlPatchContent",
54+
"type": "multiLine",
55+
"label": "Patch Content",
56+
"defaultValue": "",
57+
"required": true,
58+
"helpMarkDown": "Patch content.",
59+
"properties": {
60+
"resizable": "true",
61+
"rows": "10",
62+
"maxLength": "5000"
63+
}
64+
},
65+
{
66+
"name": "OutputPatchFile",
67+
"type": "boolean",
68+
"label": "Output patched file in logs",
69+
"defaultValue": "false",
70+
"helpMarkDown": "Output patched file in logs"
71+
}
72+
],
73+
"execution": {
74+
"Node": {
75+
"target": "yamlPatch.js",
76+
"argumentFormat": ""
77+
}
78+
}
79+
}

Tasks/YamlPatch/yamlPatch.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import path = require('path');
2+
import fs = require('fs-extra');
3+
import tl = require('vsts-task-lib/task');
4+
import micromatch = require('micromatch');
5+
import jsYaml = require('js-yaml');
6+
7+
import patch = require('./common/patch');
8+
import patchProcess = require('./common/patchProcess');
9+
import yamlPatcher = require('./yamlPatcher');
10+
11+
var targetPath = tl.getPathInput("YamlWorkingDir");
12+
var patchContent = tl.getInput("YamlPatchContent");
13+
14+
var patterns: any = tl.getInput("YamlTargetFilters")
15+
var outputPatchedFile = tl.getBoolInput("OutputPatchFile");
16+
var syntax = tl.getInput("SyntaxType");
17+
18+
try {
19+
var patches: patch.IPatch[] = syntax == "slick" ?
20+
patchProcess.expandVariablesAndParseSlickPatch(patchContent) :
21+
patchProcess.expandVariablesAndParseJson(patchContent);
22+
23+
patchProcess.apply(new yamlPatcher.YamlPatcher(patches), targetPath, patterns, outputPatchedFile);
24+
25+
tl.setResult(tl.TaskResult.Succeeded, "Files Patched");
26+
27+
} catch (err) {
28+
console.error(String(err));
29+
tl.setResult(tl.TaskResult.Failed, String(err));
30+
}

Tasks/YamlPatch/yamlPatcher.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import jsYaml = require('js-yaml');
2+
import jsonPatcher = require('./common/jsonPatcher');
3+
import patch = require('./common/patch');
4+
5+
export class YamlPatcher extends jsonPatcher.JsonPatcher {
6+
7+
constructor(patches: patch.IPatch[]) {
8+
super(patches, (content) => jsYaml.safeLoad(content));
9+
}
10+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import yamlPatcher = require("../../Tasks/YamlPatch/yamlPatcher");
2+
var jsYaml = require("../../Tasks/YamlPatch/node_modules/js-yaml");
3+
4+
5+
describe("YANL Patcher", () => {
6+
7+
describe("Operations", () => {
8+
9+
var source: string;
10+
11+
beforeEach(function() {
12+
source = jsYaml.safeDump({
13+
sampleValue : "12"
14+
});
15+
});
16+
17+
describe("Add", () => {
18+
it(": should support basic add.", () => {
19+
var patcher = new yamlPatcher.YamlPatcher([
20+
{
21+
op: "add", path: "/added", value: {}
22+
},{
23+
op: "add", path: "/added/value", value: "42"
24+
}
25+
]);
26+
var result = jsYaml.safeLoad(patcher.apply(source));
27+
28+
expect(result).toBeDefined();
29+
expect(result.sampleValue).toBeDefined();
30+
expect(result.sampleValue).toEqual("12");
31+
expect(result.added.value).toEqual("42");
32+
});
33+
});
34+
});
35+
});

configuration.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"VssExtensionGalleryFlags": [],
1919
"DisplayNamesSuffix": " (Preview)",
2020
"TaskIds": {
21+
"YamlPatch": "790b91da-30c1-4ffb-9d60-43c0fe6096f2",
2122
"JsonPatch": "1309EACE-BCEB-46A5-872A-9D9E33820D1D",
2223
"XmlPatch": "FBDB24F0-CC20-4927-AF82-18421B3DA3CD"
2324
}
@@ -28,6 +29,7 @@
2829
"VssExtensionGalleryFlags": [],
2930
"DisplayNamesSuffix": " (Development)",
3031
"TaskIds": {
32+
"YamlPatch": "14568f85-c21b-490f-b102-568d2a0da4bf",
3133
"JsonPatch": "578A87B3-5A3F-461B-828D-E53DA43A8F3A",
3234
"XmlPatch": "BD8DCFA1-1592-4DA9-8437-871752577791"
3335
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"exclude":[
2020
"node_modules",
2121
"Tasks/JsonPatch/node_modules",
22+
"Tasks/YamlPatch/node_modules",
2223
"Common",
2324
"BuildScripts/node_modules",
2425
".BuildOutput"

typings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"globalDependencies": {
55
"fs-extra": "registry:dt/fs-extra#0.0.0+20160517121359",
66
"jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
7+
"js-yaml": "registry:dt/js-yaml#3.5.2+20160316171810",
78
"micromatch": "registry:dt/micromatch#2.3.7+20160123140905",
89
"node": "registry:dt/node#6.0.0+20160728152422",
910
"parse-glob": "registry:dt/parse-glob#3.0.4+20160110125512",

0 commit comments

Comments
 (0)