diff --git a/Tasks/PlistPatch/package.json b/Tasks/PlistPatch/package.json new file mode 100644 index 0000000..6b24a9b --- /dev/null +++ b/Tasks/PlistPatch/package.json @@ -0,0 +1,13 @@ +{ + "name": "plistpatch", + "private": true, + "version": "0.1.0", + "dependencies": { + "fs-extra": "0.30.0", + "micromatch": "2.3.11", + "fast-json-patch": "1.0.0", + "xregexp": "3.1.1", + "vsts-task-lib": "0.9.6", + "plist": "2.0.1" + } +} diff --git a/Tasks/PlistPatch/plistPatch.ts b/Tasks/PlistPatch/plistPatch.ts new file mode 100644 index 0000000..9763d21 --- /dev/null +++ b/Tasks/PlistPatch/plistPatch.ts @@ -0,0 +1,30 @@ +import path = require('path'); +import fs = require('fs-extra'); +import tl = require('vsts-task-lib/task'); +import micromatch = require('micromatch'); +import jsYaml = require('js-yaml'); + +import patch = require('./common/patch'); +import patchProcess = require('./common/patchProcess'); +import plistPatcher = require('./plistPatcher'); + +var targetPath = tl.getPathInput("YamlWorkingDir"); +var patchContent = tl.getInput("YamlPatchContent"); + +var patterns: any = tl.getInput("YamlTargetFilters") +var outputPatchedFile = tl.getBoolInput("OutputPatchFile"); +var syntax = tl.getInput("SyntaxType"); + +try { + var patches: patch.IPatch[] = syntax == "slick" ? + patchProcess.expandVariablesAndParseSlickPatch(patchContent) : + patchProcess.expandVariablesAndParseJson(patchContent); + + patchProcess.apply(new plistPatcher.PlistPatcher(patches), targetPath, patterns, outputPatchedFile); + + tl.setResult(tl.TaskResult.Succeeded, "Files Patched"); + +} catch (err) { + console.error(String(err)); + tl.setResult(tl.TaskResult.Failed, String(err)); +} diff --git a/Tasks/PlistPatch/plistPatcher.ts b/Tasks/PlistPatch/plistPatcher.ts new file mode 100644 index 0000000..41665fa --- /dev/null +++ b/Tasks/PlistPatch/plistPatcher.ts @@ -0,0 +1,14 @@ +import jsonPatcher = require('./common/jsonPatcher'); +import patch = require('./common/patch'); +var plist = require('plist'); + +export class PlistPatcher extends jsonPatcher.JsonPatcher { + + parse(content: string): any { + return plist.parse(content); + } + + stringify(content: any): string { + return plist.build(content); + } +} \ No newline at end of file diff --git a/Tasks/PlistPatch/task.json b/Tasks/PlistPatch/task.json new file mode 100644 index 0000000..630eb76 --- /dev/null +++ b/Tasks/PlistPatch/task.json @@ -0,0 +1,79 @@ +{ + "id": "599b994d-c4e1-4e74-b58e-0cd3006e5aac", + "name": "PlistPatch", + "friendlyName": "Patch Plist Files", + "description": "Patch Plist files using JSON patch syntax", + "helpMarkDown": "[More Information](https://github.com/geeklearningio/gl-vsts-tasks-file-patch/wiki/Patch-Plist-Files) (Version #{Version}#)", + "category": "Utility", + "visibility": [ + "Build", + "Release" + ], + "author": "Geek Learning", + "version": { + "Major": 0, + "Minor": 0, + "Patch": 0 + }, + "demands": [ + "npm" + ], + "minimumAgentVersion": "1.91.0", + "instanceNameFormat": "Patch files $(PlistTargetFilters)", + "inputs": [ + { + "name": "SyntaxType", + "type": "pickList", + "label": "Syntax type", + "defaultValue": "Standard", + "required": true, + "helpMarkDown": "The syntax used for the patch content. See documentation for more information.", + "options": { + "standard": "Standard Syntax", + "slick": "Slick Syntax" + } + }, + { + "name": "PlistWorkingDir", + "type": "filePath", + "label": "Patch working directory", + "defaultValue": "", + "required": true, + "helpMarkDown": "Working directory. Example: $(Build.SourcesDirectory)" + }, + { + "name": "PlistTargetFilters", + "type": "multiLine", + "label": "Target files", + "defaultValue": "", + "required": true, + "helpMarkDown": "Patch target file. Example: appsettings*.plist" + }, + { + "name": "PlistPatchContent", + "type": "multiLine", + "label": "Patch Content", + "defaultValue": "", + "required": true, + "helpMarkDown": "Patch content.", + "properties": { + "resizable": "true", + "rows": "10", + "maxLength": "5000" + } + }, + { + "name": "OutputPatchFile", + "type": "boolean", + "label": "Output patched file in logs", + "defaultValue": "false", + "helpMarkDown": "Output patched file in logs" + } + ], + "execution": { + "Node": { + "target": "plistPatch.js", + "argumentFormat": "" + } + } +} \ No newline at end of file diff --git a/Tests/PlistPatch/plistPatcher.spec.ts b/Tests/PlistPatch/plistPatcher.spec.ts new file mode 100644 index 0000000..b1c45a7 --- /dev/null +++ b/Tests/PlistPatch/plistPatcher.spec.ts @@ -0,0 +1,36 @@ +import plistPatcher = require("../../Tasks/PlistPatch/plistPatcher"); +var plist = require("../../Tasks/PlistPatch/node_modules/plist"); + + + +describe("PLIST Patcher", () => { + + describe("Operations", () => { + + var source: string; + + beforeEach(function() { + source = plist.build({ + sampleValue : "12" + }); + }); + + describe("Add", () => { + it(": should support basic add.", () => { + var patcher = new plistPatcher.PlistPatcher([ + { + op: "add", path: "/added", value: {} + },{ + op: "add", path: "/added/value", value: "42" + } + ]); + var result = plist.parse(patcher.apply(source)); + + expect(result).toBeDefined(); + expect(result.sampleValue).toBeDefined(); + expect(result.sampleValue).toEqual("12"); + expect(result.added.value).toEqual("42"); + }); + }); + }); +}); \ No newline at end of file diff --git a/Tests/YamlPatch/yamlPatcher.spec.ts b/Tests/YamlPatch/yamlPatcher.spec.ts index fdc8246..42d9667 100644 --- a/Tests/YamlPatch/yamlPatcher.spec.ts +++ b/Tests/YamlPatch/yamlPatcher.spec.ts @@ -2,7 +2,7 @@ import yamlPatcher = require("../../Tasks/YamlPatch/yamlPatcher"); var jsYaml = require("../../Tasks/YamlPatch/node_modules/js-yaml"); -describe("YANL Patcher", () => { +describe("YAML Patcher", () => { describe("Operations", () => { diff --git a/configuration.json b/configuration.json index e2db3cf..629a1b4 100644 --- a/configuration.json +++ b/configuration.json @@ -30,6 +30,7 @@ "VssExtensionGalleryFlags": [], "DisplayNamesSuffix": " (Development)", "TaskIds": { + "PlistPatch" : "36db878b-3060-4465-86d2-d6a73ddbc0ad", "YamlPatch": "14568f85-c21b-490f-b102-568d2a0da4bf", "JsonPatch": "578A87B3-5A3F-461B-828D-E53DA43A8F3A", "XmlPatch": "BD8DCFA1-1592-4DA9-8437-871752577791" diff --git a/tsconfig.json b/tsconfig.json index 34d07d5..8623481 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -20,6 +20,7 @@ "node_modules", "Tasks/JsonPatch/node_modules", "Tasks/YamlPatch/node_modules", + "Tasks/PlistPatch/node_modules", "Common", "BuildScripts/node_modules", ".BuildOutput"