forked from mulesoft-labs/raml-for-jax-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.gradle
389 lines (375 loc) · 12.7 KB
/
s3.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
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();
}
task deployS3(){
org.jets3t.service.security.AWSCredentials z1=new org.jets3t.service.security.AWSCredentials(awsAccessKey,
awsSecretKey);
RestS3Service z=new RestS3Service(z1);
Settings settings=Settings.INSTANCE;
//settings.projectFolder=project;
settings.bucketName=bucketName;
File td=prepareToDeploy(settings);
File indexHtml=new File(settings.getProjectFolder(),"temp");
File index=new File(indexHtml,"index.html");
indexHtml.mkdirs();
if (index.exists()){
index.delete();
}
String newVersion=determineBuildVersion(settings,true);
println(newVersion);
LinkedHashSet<String> versions=listVersions(z,settings);
versions.add(newVersion);
PrintStream ps=new PrintStream(new FileOutputStream(index));
generateIndexHTML(ps,versions,settings);
ps.close();
processDirectory(z,new S3Bucket(settings.bucketName),indexHtml,indexHtml,settings.commonPart+"/all-versions");
//copy to versions list
processDirectory(z, new S3Bucket(settings.bucketName), td, td, settings.commonPart+"/all-versions/" + settings.commonPrefix + newVersion);
//copy to current
processDirectory(z, new S3Bucket(settings.bucketName), td, td, settings.commonPart+"/current");
}
/**
*
* @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;
}
}
}
}
/**
*
* @param service
* @param settings
* @return set of versions that are currently deployed to s3
*/
LinkedHashSet<String> listVersions(RestS3Service service,Settings settings){
LinkedHashSet<String>result=new LinkedHashSet<>();
for (v in service.listObjects(settings.bucketName,settings.versionStorage,null)){
String subKey=v.key.substring("all-versions/".length());
int slash=subKey.indexOf('/');
if (slash!=-1){
subKey=subKey.substring(0,slash);
if (subKey.startsWith(settings.commonPrefix)) {
subKey = subKey.substring(settings.commonPrefix.length());
result.add(subKey);
}
}
}
return result;
}
/**
* generates version listing
* @param ps
* @param versions
* @param settings
*/
void generateIndexHTML(PrintStream ps,LinkedHashSet<String>versions,Settings settings) {
ps.println("<html>");
ps.println("<head>\n" +
" <title>RAML for JAX-RS - all versions</title>\n" +
" </head>\n" +
" <body>");
for (String version in versions){
ps.println("<ul>");
ps.println("<li>"+settings.commonPrefix+version+"</li>");
//generating links
ps.println("<ul>");
String versionPrefix="http://"+settings.bucketName+"/"+settings.versionStorage+settings.commonPrefix+version;
printLink(ps,versionPrefix+"/eclipse","eclipse");
printLink(ps,versionPrefix+"/CLI/raml-to-jax-rs.jar","raml-to-jax-rs.jar");
printLink(ps,versionPrefix+"/CLI/jax-rs-to-raml.jar","jax-rs-to-raml.jar");
ps.println("</ul>");
ps.println("</ul>");
}
ps.println("</body>\n</html>");
//ps.close();
}
/**
* prints a link to print stream
* @param ps
* @param href
* @param title
*/
void printLink(PrintStream ps,String href,String title){
ps.println("<li>");
ps.println("<a href='"+href+"' target=\"_blank\"/>"+title+"</a>");
ps.println("</li>");
}
/**
*
* @param settings
* @return prepares tempfolder to deploy
*/
File prepareToDeploy(Settings settings){
File root=settings.getProjectFolder();
File annotationsProcessor=null;
File annotationsProcessorFolder=new File(new File(new File(root,"jaxrs-to-raml"),"com.mulesoft.jaxrs.raml.generator.annotations"),"target");
for (File f:annotationsProcessorFolder.listFiles()){
if (f.getName().endsWith("dependencies.jar")){
annotationsProcessor=f;
break;
}
}
File commandLine=null;
File commandLineFolder=new File(new File(new File(root,"raml-to-jaxrs"),"core"),"target");
for (File f:commandLineFolder.listFiles()){
if (f.getName().endsWith("dependencies.jar")){
commandLine=f;
break;
}
}
File sourceEclipse=new File(new File(new File(new File(root,"eclipse"),"updateSite"),"target"),"repository");
File toDeploy=new File(root,"toS3");
File toDeployClI=new File(toDeploy,"CLI");
File toDeployECLIPSE=new File(toDeploy,"eclipse");
toDeploy.mkdir();
toDeployClI.mkdir();
toDeployECLIPSE.mkdir();
new File(toDeployClI,"raml-to-jax-rs.jar").bytes=commandLine.bytes;
new File(toDeployClI,"jax-rs-to-raml.jar").bytes=annotationsProcessor.bytes;
ant.copy(todir: toDeployECLIPSE) {
fileset(dir : sourceEclipse)
}
return toDeploy;
}
/**
* print version item to html
* @param ps
* @param versionNumber
* @param st
*/
void generateVersionContent(PrintStream ps,String versionNumber,Settings st){
ps.println("<ul>");
ps.println("<li>raml-for-jax-rs-"+versionNumber+"</li>");
println(st.rootDeployPath+versionNumber+"/"+st.cliPath+versionNumber+"/raml-to-jax-rs-");
}
/**
* copy files recurrently
* @param s3
* @param b
* @param d
* @param source
* @param prefix
* @throws Exception
*/
void processDirectory(RestS3Service s3, S3Bucket b, File d,File source,prefix) throws Exception {
File [] files = d.listFiles();
for (int i=0; i<files.length; i++) {
if (files[i].isDirectory()) {
processDirectory(s3, b, files[i],source,prefix);
}
else {
copyFile(s3, b, d, files[i].getPath(),source,prefix);
}
}
}
/**
* copy file to s3
* @param s3
* @param b
* @param d
* @param file
* @param source
* @param prefix
* @throws Exception
*/
void copyFile(RestS3Service s3, S3Bucket b, File d, String file,File source,String prefix) throws Exception {
String key = (file).replace('\\', '/');
String rootKey=(source.getAbsolutePath()).replace('\\', '/');
key=prefix+key.substring(rootKey.length());
S3Object obj = new S3Object(b, key);
File f = new File(file);
obj.setContentLength(f.length());
try {
obj.setDataInputFile(f);
obj = s3.putObject(b, obj);
} catch (S3ServiceException e) {
throw e;
}
if (true) {
log("copied : "+key);
}
}
void log (String str){
println(str);
}