-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.js
233 lines (197 loc) · 7.26 KB
/
module.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
var jsxml = require("node-jsxml");
jsxml.XML.setSettings({ignoreComments : false, ignoreProcessingInstructions : false, createMainDocument: true});
var fs = require('fs');
function PVC() {};
PVC.help = function (){
console.log("");
console.log("pom-version-changer - manage maven pom versions");
console.log(" node pom-version-changer [--help] [--backup] [--config=file]");
console.log("");
console.log(" All optional arguments.");
console.log(" Specify --backup to create a backup file on the same directory as pom.xml.");
console.log(" Specify --config to define which config.json file to process.");
console.log("");
}
PVC.processFromFile = function(configFile, doBackups) {
var data = fs.readFileSync(configFile, 'utf8');
data = JSON.parse(data);
if (typeof data.projects == "undefined") {
console.log("No projects are defined in config.json");
} else {
PVC.process(data, doBackups);
}
}
PVC.process = function(data, doBackups) {
var vc = new VersionChanger(data, doBackups);
if (doBackups) {
vc.backupFiles();
}
vc.updateVersions();
vc.updateProperties();
vc.updateDependencies();
}
PVC.backup = function(file) {
fs.writeFileSync(file+".bak", fs.readFileSync(file));
}
PVC.setVersion = function(file, version) {
var data = fs.readFileSync(file, 'utf8');
var xmlDoc= new jsxml.XML(data);
var node = xmlDoc.child('project').child('version');
if (node != "" && node.getValue()!=version) {
node.setValue(version);
fs.writeFileSync(file, xmlDoc.toXMLString());
}
}
PVC.setParentVersion = function (file, version) {
var data = fs.readFileSync(file, 'utf8');
var xmlDoc= new jsxml.XML(data);
var node = xmlDoc.child('project').child('parent').child('version');
if (node != "" && node.getValue()!=version) {
node.setValue(version);
fs.writeFileSync(file, xmlDoc.toXMLString());
}
}
PVC.setProperties = function(file, properties) {
var data = fs.readFileSync(file, 'utf8');
var xmlDoc= new jsxml.XML(data);
var changed = false;
if (typeof xmlDoc.child('project').child('properties') != "undefined") {
for(var property in properties) {
var node = xmlDoc.child('project').child('properties').child(property);
if (node != "") {
var value = properties[property];
var prevvalue = node.getValue();
if (value!=prevvalue) {
console.log("Updating properties of " + file + ": " + property + "=" + value);
node.setValue(value);
changed = true;
}
}
}
}
if (changed) {
fs.writeFileSync(file, xmlDoc.toXMLString());
}
}
PVC.getDependencyVersion = function(dependencies, groupdId, artifactId) {
return dependencies[groupdId+"/"+artifactId];
}
PVC.setDependencies = function (file, dependencies) {
var data = fs.readFileSync(file, 'utf8');
var xmlDoc= new jsxml.XML(data);
var changed = false;
if (typeof xmlDoc.child('project').child('dependencyManagement') != "undefined") {
var node = xmlDoc.child('project').child('dependencyManagement').child("dependencies");
if (node != "") {
//get all children
var children = node.child("*");
children.each(function(dependency, index) {
var groupId = dependency.child("groupId");
var artifactId = dependency.child("artifactId");
var version = dependency.child("version");
if (version == "") {
//no version
return;
} else if (version.getValue().trim().substring(0,1)!="$"){ //also check if there was a variable there to not replace it
var depVersion = PVC.getDependencyVersion(dependencies,groupId,artifactId);
if (typeof depVersion != "undefined") {
console.log(file + " setting dependency mng. version of " + groupId + "/"+artifactId + " to " + depVersion);
dependency.child("version").setValue(depVersion);
changed = true;
}
}
});
}
}
var node = xmlDoc.child('project').child('dependencies');
if (node != "") {
//get all children
var children = node.child("*");
children.each(function(dependency, index) {
var groupId = dependency.child("groupId");
var artifactId = dependency.child("artifactId");
var version = dependency.child("version");
if (version == "") {
//no version
return;
} else if (version.getValue().trim().substring(0,1)!="$"){ //also check if there was a variable there to not replace it
var depVersion = PVC.getDependencyVersion(dependencies,groupId,artifactId);
if (typeof depVersion != "undefined") {
console.log(file + " setting dependency version of " + groupId + "/"+artifactId + " to " + depVersion);
dependency.child("version").setValue(depVersion);
changed = true;
}
}
});
}
if (changed) {
fs.writeFileSync(file, xmlDoc.toXMLString());
}
}
//////////////////////////////////////////////////////////////
var VersionChanger = function(data, doBackups) {
this.data = data;
this.doBackups = doBackups;
}
VersionChanger.prototype.getProject = function(name) {
return this.data.projects[name];
}
VersionChanger.prototype.getProjectVersion = function(name) {
return this.data.projects[name];
}
VersionChanger.prototype.getPath = function(basepath, relative) {
if (typeof basepath != "undefined" && basepath!="") {
var knownPath = this.data.paths[basepath];
if (typeof knownPath == "undefined") {
//console.err("unknown basepath " + basepath);
throw "unknown basepath " + basepath;
} else {
return knownPath+"/"+relative;
}
} else {
return relative;
}
}
VersionChanger.prototype.backupFiles = function() {
var projects = this.data.projects;
for (var projectname in projects) {
var project = this.getProject(projectname);
PVC.backup(this.getPath(project.basepath, project.pom));
}
}
VersionChanger.prototype.updateVersions = function() {
var projects = this.data.projects;
var projectsversion = this.data["projects-version"];
for (var projectname in projectsversion) {
var project = this.getProject(projectname);
var version = projectsversion[projectname];
console.log("Setting " + projectname + " to version " + version);
// set pom version of the project
PVC.setVersion(this.getPath(project.basepath,project.pom), version);
// now update child projects
for(var proj in projects) {
if (projects[proj].parent == projectname) {
console.log("Setting child project " + proj + " parent version to " + version);
PVC.setParentVersion(this.getPath(projects[proj].basepath,projects[proj].pom), version);
}
}
}}
VersionChanger.prototype.updateProperties = function() {
var projects = this.data.projects;
var properties = this.data["properties"];
for (var projectname in projects) {
PVC.setProperties(this.getPath(projects[projectname].basepath,projects[projectname].pom), properties);
}
}
VersionChanger.prototype.updateDependencies = function() {
var projects = this.data.projects;
var dependenciesversion = this.data["dependencies-version"];
for (var projectname in projects) {
PVC.setDependencies(this.getPath(projects[projectname].basepath,projects[projectname].pom), dependenciesversion);
}
}
module.exports = {
processFromFile: PVC.processFromFile,
process: PVC.process,
help: PVC.help
}