Skip to content

Commit dc9f120

Browse files
committed
Adds a new task to override virtual network names
also fixes a couple bugs
1 parent d52e8d0 commit dc9f120

18 files changed

+694
-8
lines changed

build.ps1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Push-Location .\cscfg-transform
2+
npm install
3+
tsc
4+
Pop-Location
5+
6+
Push-Location .\cscfg-vnetsite
7+
npm install
8+
tsc
9+
Pop-Location
10+
11+
tfx extension create --manifest-globs vss-extension.json
File renamed without changes.

cscfg-transform/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ async function run() {
5151

5252
if (fileMutated) {
5353
const newContents = new XMLSerializer().serializeToString(doc);
54-
fsPromises.writeFile(targetFilePath, newContents);
54+
await fsPromises.writeFile(targetFilePath, newContents);
5555
}
5656
else {
5757
tl.warning("No variable updates were applied to the file.");

cscfg-transform/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
"description": "Transforms Azure Cloud Service cscfg files",
55
"main": "index.js",
66
"scripts": {
7-
"test": "tsc && mocha ./tests/_suite.js",
8-
"package": "tsc && tfx extension create --manifest-globs cscfg-transform-extension.json"
7+
"test": "tsc && mocha ./tests/_suite.js"
98
},
109
"keywords": [
1110
"cscfg"

cscfg-transform/task.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"Minor": 2,
1717
"Patch": 3
1818
},
19-
"instanceNameFormat": "Transform cscfg $(targetFilePath)",
19+
"instanceNameFormat": "Transform CS Config Variables $(targetFilePath)",
2020
"inputs": [
2121
{
2222
"name": "targetFilePath",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
**********************************************************************************************
4+
5+
This file was generated by a tool from the project file: ServiceConfiguration.cscfg
6+
7+
Changes to this file may cause incorrect behavior and will be lost if the file is regenerated.
8+
9+
**********************************************************************************************
10+
-->
11+
<ServiceConfiguration serviceName="ServiceName" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="6" osVersion="*" schemaVersion="2015-04.2.6">
12+
<Role name="RoleName">
13+
<Instances count="1"/>
14+
<ConfigurationSettings>
15+
<Setting name="foo.blank" value=""/>
16+
<Setting name="foo.bar" value="bar"/>
17+
</ConfigurationSettings>
18+
</Role>
19+
<NetworkConfiguration>
20+
<VirtualNetworkSite name=""/>
21+
</NetworkConfiguration>
22+
</ServiceConfiguration>

cscfg-vnetsite/icon.png

11.1 KB
Loading

cscfg-vnetsite/index.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import tl = require('azure-pipelines-task-lib/task');
2+
import { promises as fsPromises } from 'fs';
3+
import { DOMParser, XMLSerializer } from 'xmldom';
4+
5+
async function run() {
6+
try {
7+
const targetFilePath: string | undefined = tl.getInput('targetFilePath', true);
8+
if (targetFilePath === '' || targetFilePath == null) {
9+
tl.setResult(tl.TaskResult.Failed, 'A file path to transform is required');
10+
return;
11+
}
12+
13+
const overrideName: string | undefined = tl.getInput('overrideNetworkSiteName', true);
14+
if (overrideName == null) {
15+
tl.setResult(tl.TaskResult.Failed, 'An override name is required');
16+
return;
17+
}
18+
19+
let fileMutated = false;
20+
21+
const originalContents = await fsPromises.readFile(targetFilePath, 'utf8');
22+
const parser = new DOMParser();
23+
24+
let doc = parser.parseFromString(originalContents, 'text/xml');
25+
if (doc.documentElement.nodeName !== 'ServiceConfiguration') {
26+
tl.setResult(tl.TaskResult.Failed, 'Config file missing root ServiceConfiguration element');
27+
return;
28+
}
29+
30+
let networkNode = doc.documentElement.getElementsByTagName('NetworkConfiguration')[0];
31+
if (!networkNode) {
32+
networkNode = doc.createElement('NetworkConfiguration');
33+
doc.documentElement.appendChild(networkNode);
34+
fileMutated = true;
35+
}
36+
37+
let virtualNetworkSite = networkNode.getElementsByTagName('VirtualNetworkSite')[0];
38+
if (!virtualNetworkSite) {
39+
virtualNetworkSite = doc.createElement('VirtualNetworkSite');
40+
networkNode.appendChild(virtualNetworkSite);
41+
fileMutated = true;
42+
}
43+
44+
const originalName = virtualNetworkSite.getAttribute('name');
45+
if (originalName !== overrideName) {
46+
virtualNetworkSite.setAttribute('name', overrideName);
47+
fileMutated = true;
48+
}
49+
50+
if (fileMutated) {
51+
const newContents = new XMLSerializer().serializeToString(doc);
52+
await fsPromises.writeFile(targetFilePath, newContents);
53+
}
54+
else {
55+
tl.warning("No updates were applied to the file.");
56+
}
57+
58+
}
59+
catch (err) {
60+
tl.setResult(tl.TaskResult.Failed, err.message);
61+
}
62+
}
63+
64+
run();

0 commit comments

Comments
 (0)