forked from mulesoft-labs/raml-for-jax-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateVersions.gradle
206 lines (191 loc) · 6.43 KB
/
updateVersions.gradle
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
buildscript {
repositories {
mavenCentral()
}
dependencies {
//testCompile group: 'junit', name: 'junit', version: '4.11'
//compile 'commons-lang:commons-lang:2.6'
classpath 'commons-lang:commons-lang:2.6','net.java.dev.jets3t:jets3t:0.9.2'
}
}
import org.jets3t.service.impl.rest.httpclient.RestS3Service;
import org.jets3t.service.S3ServiceException;
import org.jets3t.service.model.S3Object;
import org.jets3t.service.model.S3Bucket;
class Settings{
String bucketName="raml-tools-stage.mulesoft.com";
String versionStorage="all-versions/";
String commonPart="raml-for-jax-rs";
String commonPrefix=commonPart+"-";
File projectFolder;
private Settings(){
}
public static Settings INSTANCE=new Settings();
}
Map ENV = System.getenv()
ENV = System.getenv()
String awsAccessKey = ENV['AWSKEY']
String awsSecretKey = ENV['AWSSECRET']
String bucketName=ENV['BUCKET'];
Settings.INSTANCE.projectFolder=projectDir;
task updateVersions(){
String version=determineBuildVersion(Settings.INSTANCE,false);
updateVersionsRecurrently(version,Settings.INSTANCE.projectFolder);
}
public void updateVersionsRecurrently(String version,File folder){
File pom=new File(folder,"pom.xml");
if (pom.exists()){
rewriteAVersion(version,pom);
}
File mf=new File(folder,"MANIFEST.MF");
if (mf.exists()){
rewriteAManifest(version,mf);
}
File feature=new File(folder,"feature.xml");
if (feature.exists()){
rewriteAFeature(version,feature);
}
File category=new File(folder,"category.xml");
if (category.exists()){
rewriteACategory(version,category);
}
for (File f:folder.listFiles()){
if (f.isDirectory()){
updateVersionsRecurrently(version,f);
}
}
}
public void rewriteAVersion(String newVersion,File fl){
ArrayList<String>newLines=new ArrayList<>();
boolean insideDetails=false;
for (String line:fl.readLines()){
int pos=line.indexOf("<version>");
if (line.indexOf("<dependency>")!=-1){
insideDetails=true;
}
if (line.indexOf("</dependency>")!=-1){
insideDetails=false;
}
if (line.indexOf("<plugin>")!=-1){
insideDetails=true;
}
if (line.indexOf("<extensions>")!=-1){
insideDetails=true;
}
if (line.indexOf("</extensions>")!=-1){
insideDetails=false;
}
if (line.indexOf("</plugin>")!=-1){
insideDetails=false;
}
if (!insideDetails) {
if (pos != -1) {
line = line.substring(pos + "<version>".length());
pos = line.indexOf("</version>");
if (pos != -1) {
line = line.substring(0, pos).trim();
if (line.endsWith("-SNAPSHOT")) {
line = line.substring(0, line.length() - "-SNAPSHOT".length());
}
line = "<version>" + newVersion + "</version>";
}
}
}
newLines.add(line);
}
PrintWriter we=fl.newPrintWriter();
for (String q: newLines){
we.println(q);
}
we.close();
}
public void rewriteAFeature(String newVersion,File fl){
ArrayList<String>newLines=new ArrayList<>();
boolean insideDetails=true;
for (String line:fl.readLines()){
if (line.trim().startsWith("<feature")){
insideDetails=false;
}
if (!insideDetails){
int c=line.indexOf("version=\"");
if (c!=-1){
String startLine=line.substring(0,c);
int e=line.indexOf('"',c+"version=\"".length());
String endLine=line.substring(e+1);
println(startLine);
println(endLine);
line=startLine+"version=\""+newVersion.replace("-SNAPSHOT",".qualifier")+"\"";
}
}
if (line.trim().contains(">")){
insideDetails=true;
}
newLines.add(line);
}
PrintWriter we=fl.newPrintWriter();
for (String q: newLines){
we.println(q);
}
we.close();
}
public void rewriteACategory(String newVersion,File fl){
ArrayList<String>newLines=new ArrayList<>();
boolean insideDetails=true;
for (String line:fl.readLines()){
if (line.trim().startsWith("<feature")) {
int c=line.indexOf("id=\"");
if (c!=-1){
int e=line.indexOf('"',c+"id=\"".length());
String featureId=line.substring(c+"id=\"".length(),e);
String featureVer=newVersion.replace("-SNAPSHOT",".qualifier");
String newLine="<feature url=\"features/"+featureId+"_"+featureVer+".jar\"" +
" id=\""+featureId+"\" version=\""+featureVer+"\">";
line=newLine;
}
}
newLines.add(line);
}
PrintWriter we=fl.newPrintWriter();
for (String q: newLines){
we.println(q);
}
we.close();
}
public void rewriteAManifest(String newVersion,File fl){
ArrayList<String>newLines=new ArrayList<>();
boolean insideDetails=false;
for (String line:fl.readLines()){
if (line.trim().startsWith("Bundle-Version: ")){
String manifestVersion=newVersion.replace("-SNAPSHOT",".qualifier");
line="Bundle-Version: "+manifestVersion;
}
newLines.add(line);
}
PrintWriter we=fl.newPrintWriter();
for (String q: newLines){
we.println(q);
}
we.close();
}
/**
*
* @param settings
* @return version number determined from the build
*/
public String determineBuildVersion(Settings settings,boolean stripSnapshot){
File fl=new File(settings.getProjectFolder(),"pom.xml");
for (String line:fl.readLines()){
int pos=line.indexOf("<version>");
if (pos!=-1){
line=line.substring(pos+"<version>".length());
pos=line.indexOf("</version>");
if (pos!=-1){
line=line.substring(0,pos).trim();
if (line.endsWith("-SNAPSHOT")&&stripSnapshot){
line=line.substring(0,line.length()-"-SNAPSHOT".length());
}
return line;
}
}
}
}