This repository has been archived by the owner on Jul 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
GearSpecManager.java
786 lines (650 loc) · 30.4 KB
/
GearSpecManager.java
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
package Utilities;
import Models.GearSpec.GearSpec;
import Models.GearSpec.GearSpecDependency;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import org.apache.commons.io.FileUtils;
import org.apache.velocity.runtime.directive.Foreach;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URL;
/**
* Created by matthewyork on 4/9/14.
*/
public class GearSpecManager {
///////////////////////
// Install
///////////////////////
public static Boolean installGear(GearSpec spec, Project project, Module module){
return GearSpecManager.installGear(spec, project, module, null);
}
public static Boolean installGear(GearSpec spec, Project project, Module module, GearSpec parentSpec){
if (spec.getType().equals(GearSpec.SPEC_TYPE_MODULE)){
return GearSpecManager.installModule(spec, project, module, parentSpec);
}
else if (spec.getType().equals(GearSpec.SPEC_TYPE_JAR)){
return GearSpecManager.installJar(spec, project, module, parentSpec);
}
else {
return false;
}
}
public static Boolean installModule(GearSpec spec, Project project, Module module, GearSpec parentSpec){
//Create local path separator for speed
String pathSeparator = Utils.pathSeparator();
//Install dependency and sub-dependencies
File specDirectory = Utils.fileInstallPathForSpec(spec, project);
File specTopLevelDirectory = specDirectory.getParentFile();
//Delete the directory. This is for other versions installed
if (specTopLevelDirectory.exists()) {
try {
FileUtils.forceDelete(specTopLevelDirectory);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
//Clone down repo
try {
Git git = Git.cloneRepository()
.setURI(spec.getSource().getUrl())
.setBranch(spec.getSource().getTag())
.setDirectory(specDirectory)
.call();
git.close();
} catch (GitAPIException e) {
e.printStackTrace();
//Clean up a possible bad clone (i.e. no internet)
if (specDirectory.getParentFile().exists()){
try {
FileUtils.forceDelete(specDirectory.getParentFile());
} catch (IOException ioe) {
ioe.printStackTrace();
return false;
}
}
return false;
}
//Check out appropriate branch
/*
File gitDirectory = new File(specDirectory.getAbsolutePath() + pathSeparator + ".git");
try {
Git git = Git.open(gitDirectory);
} catch (IOException e) {
e.printStackTrace();
return false;
}*/
//Move specified folder to root, if paremeter exists
if (spec.getSource().getSource_files() != null){
if (!spec.getSource().getSource_files().equals("")){
//Replaces path seperators with system dependent ones (windows, mac, etc.)
String systemSpecificInnerPath = spec.getSource().getSource_files().replace("/", pathSeparator);
//Get module directory
File moduleDirectory = new File(specDirectory.getAbsolutePath()+ pathSeparator +systemSpecificInnerPath);
//Delete other folders, including source control
for(File file : specDirectory.listFiles()){
//Delete all folders that aren't the lib folder
if (!file.getAbsolutePath().equals(moduleDirectory.getAbsolutePath())){
try {
if (file.isDirectory()){
FileUtils.deleteQuietly(file);
}
else {
FileUtils.forceDelete(file);
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
//Move module to root
for(File file : moduleDirectory.listFiles()){
try {
if (file.isDirectory()){
FileUtils.copyDirectoryToDirectory(file, specDirectory);
}
else {
FileUtils.copyFileToDirectory(file, specDirectory);
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
//Finally, delete old inner module folder
try {
FileUtils.deleteDirectory(moduleDirectory);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
//Download dependencies
if (spec.getDependencies() != null){
if (spec.getDependencies().size() > 0){
for(GearSpecDependency dependency : spec.getDependencies()){
//Get spec from dependency
GearSpec dependencySpec = Utils.specForInfo(dependency.getName(), dependency.getVersion());
//If we get a valid spec from the dependency, go ahead and download the dependency
if (dependencySpec != null){
//See if it is installed already, before we try
if (!dependencySpec.isRegistered(project)){
//Install dependency
if (dependencySpec.getType().equals(GearSpec.SPEC_TYPE_JAR)){
installJar(dependencySpec, project, module, spec);
}
else if (dependencySpec.getType().equals(GearSpec.SPEC_TYPE_MODULE)){
installModule(dependencySpec, project, module, spec);
}
}
}
}
}
}
//Update project settings
if (!updateInstallProjectSettingsForModule(spec, project, module)){
return false;
}
//Update internal dependencies
if (!installInternalDependencies(spec, parentSpec, project)){
return false;
}
//Register spec
if (GearSpecRegistrar.registerGear(spec, project, GearSpec.GearState.GearStateInstalled)){
return true;
}
else {
return false;
}
}
public static Boolean installJar(GearSpec spec, Project project, Module module, GearSpec parentSpec){
//Create local path separator for speed
String pathSeparator = Utils.pathSeparator();
//Create GearsJars directory if not already there
//Install dependency and sub-dependencies
File specDirectory = Utils.fileInstallPathForSpec(spec, project);
File specTopLevelDirectory = specDirectory.getParentFile();
//Delete the directory. This is for other versions installed
if (specTopLevelDirectory.exists()) {
try {
FileUtils.forceDelete(specTopLevelDirectory);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
//Build jar file
File jarFile = new File(specDirectory.getAbsolutePath() + pathSeparator + Utils.jarFileNameForSpecSource(spec.getSource()));
//Build url for gear
String jarUrl;
if(spec.getSource().getUrl().toLowerCase().contains(".jar")){
jarUrl = spec.getSource().getUrl();
}
else {
jarUrl = spec.getSource().getUrl()+"/raw/"+spec.getSource().getTag()+"/"+spec.getSource().getSource_files();
jarUrl = jarUrl.replace(".git", "");
}
//Download file
try {
FileUtils.copyURLToFile(new URL(jarUrl), jarFile, 2000, 30000);
} catch (IOException e) {
e.printStackTrace();
return false;
}
//Download dependencies
if (spec.getDependencies() != null){
if (spec.getDependencies().size() > 0){
for(GearSpecDependency dependency : spec.getDependencies()){
//Get spec from dependency
GearSpec dependencySpec = Utils.specForInfo(dependency.getName(), dependency.getVersion());
//If we get a valid spec from the dependency, go ahead and download the dependency
if (dependencySpec != null){
//See if it is installed already, before we try
if (!dependencySpec.isRegistered(project)){
//Install dependency
if (dependencySpec.getType().equals(GearSpec.SPEC_TYPE_JAR)){
installJar(dependencySpec, project, module, spec);
}
else if (dependencySpec.getType().equals(GearSpec.SPEC_TYPE_MODULE)){
installModule(dependencySpec, project, module, spec);
}
}
}
}
}
}
//Update project settings
if (!updateInstallProjectSettingsForJar(spec, module)){
return false;
}
//Update internal dependencies
if (!installInternalDependencies(spec, parentSpec, project)){
return false;
}
//Register spec
if (GearSpecRegistrar.registerGear(spec, project, GearSpec.GearState.GearStateInstalled)){
return true;
}
else {
return false;
}
}
private static Boolean updateInstallProjectSettingsForModule(GearSpec spec, Project project, Module module){
//Create local path separator for speed
String pathSeparator = Utils.pathSeparator();
//Install dependency and sub-dependencies
File settingsFile = new File(project.getBasePath() + pathSeparator + "settings.gradle");
File buildFile = new File(new File(module.getModuleFilePath()).getParentFile().getAbsolutePath() + pathSeparator + "build.gradle");
File gearBuildFile = new File(Utils.fileInstallPathForSpec(spec, project).getParentFile().getAbsolutePath() + pathSeparator + spec.getVersion() + pathSeparator + "build.gradle");
//Create comment string
String commentString = "\n/////////////////////\n" +
"// Gears Dependencies\n" +
"/////////////////////";
//Read settings file
try {
String settingsFileString = FileUtils.readFileToString(settingsFile);
if (!settingsFileString.contains("include ':Gears:Modules:"+spec.getName()+":"+spec.getVersion()+"'")){
//Make changes to settings.gradle
String newSettingString = "\n"+"include ':Gears:Modules:"+spec.getName()+":"+spec.getVersion()+"'";
int commentIndex = settingsFileString.lastIndexOf(commentString);
//If the comment exists...
if (commentIndex != -1){
settingsFileString = settingsFileString.concat(newSettingString);
}
else {
settingsFileString = settingsFileString.concat(commentString+newSettingString);
}
//Write changes to settings.gradle
FileUtils.forceDelete(settingsFile);
FileUtils.write(settingsFile, settingsFileString);
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
//Read build file
try {
String buildFileString = FileUtils.readFileToString(buildFile);
//Create new addition
String newDependencyString = "\ndependencies{compile project (':Gears:Modules:"+spec.getName()+":"+spec.getVersion()+"')}";
if (!buildFileString.contains(newDependencyString)){
int commentIndex = buildFileString.lastIndexOf(commentString);
//If the comment exists...
if (commentIndex != -1){
buildFileString = buildFileString.concat(newDependencyString);
}
else {
buildFileString = buildFileString.concat(commentString+newDependencyString);
}
//Write changes to settings.gradle
FileUtils.forceDelete(buildFile);
FileUtils.write(buildFile, buildFileString);
}
//Get compileSDK version and buildTools version
String[] appLines = buildFileString.split("\n");
String compileSDKVersion = null;
String buildToolsVersion = null;
for(String line : appLines){
if (line.contains("compileSdkVersion")){
compileSDKVersion = line.replace("compileSdkVersion", "").replace(" ", "");
}
else if (line.contains("buildToolsVersion")){
buildToolsVersion = line.replace("buildToolsVersion", "").replace(" ", "");
}
}
//Apply collected compile version and build tools version
if (compileSDKVersion != null || buildToolsVersion != null) {
String gearBuildFileString = FileUtils.readFileToString(gearBuildFile);
//Apply compiled sdk and build tools sdk to new module
String[] moduleLines = gearBuildFileString.split("\n");
String modifiedModuleBuildFileString = "";
for(String line : moduleLines) {
if (line.contains("'android-library'")){
modifiedModuleBuildFileString = modifiedModuleBuildFileString + "apply plugin: 'com.android.library'" + "\n";
}
else if (line.contains("compileSdkVersion")){
modifiedModuleBuildFileString = modifiedModuleBuildFileString+ "compileSdkVersion "+compileSDKVersion+"\n";
}
else if (line.contains("buildToolsVersion")){
modifiedModuleBuildFileString = modifiedModuleBuildFileString + "buildToolsVersion "+buildToolsVersion+"\n";
}
else {
modifiedModuleBuildFileString = modifiedModuleBuildFileString + line+"\n";
}
}
//Write changes to build.gradle
FileUtils.forceDelete(gearBuildFile);
FileUtils.write(gearBuildFile, modifiedModuleBuildFileString);
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
private static Boolean updateInstallProjectSettingsForJar(GearSpec selectedSpec, Module module){
//Get build file
File buildFile = new File(new File(module.getModuleFilePath()).getParentFile().getAbsolutePath() + Utils.pathSeparator() + "build.gradle");
if (buildFile.exists()){
//Create comment string
String commentString = "\n/////////////////////\n" +
"// Gears Dependencies\n" +
"/////////////////////";
try {
//Read the build file
String buildFileString = FileUtils.readFileToString(buildFile);
//Create new addition
String dependencyString = "\ndependencies{compile fileTree(dir: '../Gears/Jars/"+selectedSpec.getName()+"/"+selectedSpec.getVersion()+"', include: ['*.jar'])}";
//If the build file doesn't contain the jar dependency, go ahead and add it
if (!buildFileString.contains(dependencyString)){
int commentIndex = buildFileString.lastIndexOf(commentString);
//If the comment exists...
if (commentIndex != -1){
buildFileString = buildFileString.concat(dependencyString);
}
else {
buildFileString = buildFileString.concat(commentString+dependencyString);
}
//Write changes to settings.gradle
FileUtils.forceDelete(buildFile);
FileUtils.write(buildFile, buildFileString);
}
} catch (IOException e) {
e.printStackTrace();
}
}
else {
return false;
}
return true;
}
/**
* Adds internal dependency listings to modules that require it. JARs are unaffected.
* @param spec
* @param parentSpec
* @return
*/
private static Boolean installInternalDependencies(GearSpec spec, GearSpec parentSpec, Project project){
if (parentSpec != null){
if (parentSpec.getType().equals(GearSpec.SPEC_TYPE_MODULE)){
//Get parent gradle build file. Remember this is already cloned down, so we should be good!
File parentModuleBuildFile = new File(Utils.fileInstallPathForSpec(parentSpec, project).getAbsolutePath()+Utils.pathSeparator()+"build.gradle");
//Check to see if it exists
if (parentModuleBuildFile.exists()){
//Create comment string
String commentString = "\n/////////////////////\n" +
"// Gears Dependencies\n" +
"/////////////////////";
//Read build file
try {
String buildFileString = FileUtils.readFileToString(parentModuleBuildFile);
//Create new addition
String newDependencyString = "";
//Build new dependency string based on type
if (spec.getType().equals(GearSpec.SPEC_TYPE_MODULE)){
newDependencyString = "\ndependencies{compile project (':Gears:Modules:"+spec.getName()+":"+spec.getVersion()+"')}";
}
else if (spec.getType().equals(GearSpec.SPEC_TYPE_JAR)){
newDependencyString = "\ndependencies{compile fileTree(dir: '../../../../Gears/Jars/"+spec.getName()+"/"+spec.getVersion()+"', include: ['*.jar'])}";
}
//Check for and insert dependency string
if (!buildFileString.contains(newDependencyString)){
int commentIndex = buildFileString.lastIndexOf(commentString);
//If the comment exists...
if (commentIndex != -1){
buildFileString = buildFileString.concat(newDependencyString);
}
else {
buildFileString = buildFileString.concat(commentString+newDependencyString);
}
//Write changes to settings.gradle
FileUtils.forceDelete(parentModuleBuildFile);
FileUtils.write(parentModuleBuildFile, buildFileString);
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
}
return true;
}
///////////////////////
// Uninstall
///////////////////////
public static Boolean uninstallGear(GearSpec spec, Project project, Module module){
if (spec.getType().equals(GearSpec.SPEC_TYPE_MODULE)){
return GearSpecManager.uninstallModule(spec, project, module);
}
else if (spec.getType().equals(GearSpec.SPEC_TYPE_JAR)){
return GearSpecManager.uninstallJar(spec, project, module);
}
else {
return false;
}
}
public static Boolean uninstallModule(GearSpec spec, Project project, Module module){
//Update settings files
//This MUST be run before removing the physical module on disk. Otherwise, the directory structure for the module may be retained after syncing
if (!updateProjectSettingsForModule(spec, project, module)){
return false;
}
//Make local path separator for speed
String pathSeparator = Utils.pathSeparator();
File libsDirectory = new File(project.getBasePath()+ pathSeparator+ "Gears"+ pathSeparator + "Modules"+pathSeparator+spec.getName());
if (!libsDirectory.exists()){
//Unregister just in case
if (GearSpecRegistrar.unregisterGear(spec, project)){
return true;
}
else {
return false;
}
}
//Get the jar file
File moduleDirectory = new File(libsDirectory.getAbsolutePath()+pathSeparator+spec.getVersion());
//Delete the jar
if (moduleDirectory.exists()){
try {
FileUtils.forceDelete(libsDirectory);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
//Finally, unregister gear
if (GearSpecRegistrar.unregisterGear(spec, project)){
return true;
}
else {
return false;
}
}
public static Boolean uninstallJar(GearSpec spec, Project project, Module module){
//Make local path separator for speed
String pathSeparator = Utils.pathSeparator();
//Get the gears jar directory. If it doesn't exist, then we will count that as a win
File libsDirectory = new File(project.getBasePath()+ pathSeparator + "Gears"+ pathSeparator + "Jars"+ pathSeparator + spec.getName());
if (!libsDirectory.exists()){
//Unregister just in case
if (GearSpecRegistrar.unregisterGear(spec, project)){
return true;
}
else {
return false;
}
}
//Get the jar file
File jarFile = new File(libsDirectory.getAbsolutePath()+pathSeparator+spec.getVersion()+pathSeparator+Utils.jarFileNameForSpecSource(spec.getSource()));
//Delete the jar
if (jarFile.exists()){
try {
FileUtils.forceDelete(libsDirectory);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
//Update settings files
if (!updateProjectSettingsForJar(spec, project, module)){
return false;
}
//Finally, unregister gear
if (GearSpecRegistrar.unregisterGear(spec, project)){
return true;
}
else {
return false;
}
}
private static Boolean updateProjectSettingsForModule(GearSpec spec, Project project, Module module){
//Make local path separator for speed
String pathSeparator = Utils.pathSeparator();
//Install dependency and sub-dependencies
File modulesFile = new File(project.getBasePath() + pathSeparator + ".idea"+ pathSeparator +"modules.xml");
File settingsFile = new File(project.getBasePath() + pathSeparator + "settings.gradle");
File buildFile = new File(new File(module.getModuleFilePath()).getParentFile().getAbsolutePath() +pathSeparator + "build.gradle");
//Recreate version entry from the modules.xml
String versionModuleEntry = "<module fileurl=\"file://$PROJECT_DIR$/Gears/Modules/"+spec.getName()+"/"+spec.getVersion()+"/"+spec.getVersion()+".iml\" filepath=\"$PROJECT_DIR$/Gears/Modules/"+spec.getName()+"/"+spec.getVersion()+"/"+spec.getVersion()+".iml\" />";
//Recreate gear entry from modles.xml
String parentModuleEntry = "<module fileurl=\"file://$PROJECT_DIR$/Gears/Modules/"+spec.getName()+"/"+spec.getName()+".iml\" filepath=\"$PROJECT_DIR$/Gears/Modules/"+spec.getName()+"/"+spec.getName()+".iml\" />";
if (modulesFile.exists()){
//Read the build file
try {
String modulesFileString = FileUtils.readFileToString(modulesFile);
if (modulesFileString.contains(versionModuleEntry)){
modulesFileString = modulesFileString.replace(versionModuleEntry, "");
}
if (modulesFileString.contains(parentModuleEntry)){
modulesFileString = modulesFileString.replace(parentModuleEntry, "");
}
//Write changes to settings.gradle
FileUtils.forceDelete(modulesFile);
FileUtils.write(modulesFile, modulesFileString);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
//Modify settings file
if (settingsFile.exists()){
try {
//Read in settings file
String settingsFileString = FileUtils.readFileToString(settingsFile);
//Make comparator strings
String fullLineInclude = "include ':Gears:Modules:"+spec.getName()+":"+spec.getVersion()+"'";
String partialInclude = "':Gears:Modules:"+spec.getName()+":"+spec.getVersion()+"'";
//Look for full line inclusion
if (settingsFileString.contains(fullLineInclude)){
settingsFileString = settingsFileString.replace(fullLineInclude, "");
}
//Look for partial line inclusions
else if (settingsFileString.contains(partialInclude+",")){ //Comma after
settingsFileString = settingsFileString.replace(partialInclude+",", "");
}
else if (settingsFileString.contains(","+partialInclude)){ //Comma before
settingsFileString = settingsFileString.replace(","+partialInclude, "");
}
else if (settingsFileString.contains(", "+partialInclude)){ //Comma before w/ space
settingsFileString = settingsFileString.replace(", "+partialInclude, "");
}
//Write changes to settings.gradle
FileUtils.forceDelete(settingsFile);
FileUtils.write(settingsFile, settingsFileString);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
else {
return false;
}
//Modify build file
if (buildFile.exists()){
try {
//Read the build file
String buildFileString = FileUtils.readFileToString(buildFile);
//Create new addition
String dependencyString = "dependencies{compile project (':Gears:Modules:"+spec.getName()+":"+spec.getVersion()+"')}";
if (buildFileString.contains(dependencyString)){
buildFileString = buildFileString.replace(dependencyString, "");
}
//Write changes to settings.gradle
FileUtils.forceDelete(buildFile);
FileUtils.write(buildFile, buildFileString);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
else{
return false;
}
return true;
}
private static Boolean updateProjectSettingsForJar(GearSpec spec, Project project, Module module){
//Make local path separator for speed
String pathSeparator = Utils.pathSeparator();
File buildFile = new File(new File(module.getModuleFilePath()).getParentFile().getAbsolutePath() + pathSeparator + "build.gradle");
//Modify build file
if (buildFile.exists()){
try {
File libsDirectory = new File(project.getBasePath() + pathSeparator + "Gears"+ pathSeparator + "Jars");
if(libsDirectory.exists()){
//Check to see if all jars are gone. If so, remove the gears jar folder dependency
File[] jars = libsDirectory.listFiles(new FilenameFilter() {
@Override
public boolean accept(File file, String fileName) {
if(fileName.contains(".jar")){
return true;
}
else {
return false;
}
}
});
//No jars, so remove it!
if (jars.length == 0){
//Read the build file
String buildFileString = FileUtils.readFileToString(buildFile);
//Create new addition
String dependencyString = "dependencies{compile fileTree(dir: '../Gears/Jars/"+spec.getName()+"/"+spec.getVersion()+"', include: ['*.jar'])}";
if (buildFileString.contains(dependencyString)){
buildFileString = buildFileString.replace(dependencyString, "");
}
//Write changes to settings.gradle
FileUtils.forceDelete(buildFile);
FileUtils.write(buildFile, buildFileString);
}
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
else{
return false;
}
return true;
}
/**
* Removes internal dependency listings to modules that require them. JARs are unaffected.
* @param spec
* @param parentSpec
* @param project
* @return
*/
private static Boolean uninstallInternalDependencies(GearSpec spec, GearSpec parentSpec, Project project){
if (parentSpec != null){
if (parentSpec.getType().equals(GearSpec.SPEC_TYPE_MODULE)) {
}
}
return true;
}
}