25
25
import com .google .gson .reflect .TypeToken ;
26
26
import me .tongfei .progressbar .ProgressBar ;
27
27
import me .tongfei .progressbar .ProgressBarStyle ;
28
+ import org .apache .commons .io .FileUtils ;
28
29
import org .ballerinalang .central .client .exceptions .CentralClientException ;
29
30
import org .ballerinalang .central .client .exceptions .ConnectionErrorException ;
30
31
import org .ballerinalang .central .client .exceptions .PackageAlreadyExistsException ;
36
37
import java .net .HttpURLConnection ;
37
38
import java .net .MalformedURLException ;
38
39
import java .net .ProtocolException ;
40
+ import java .net .URI ;
39
41
import java .net .URL ;
42
+ import java .nio .file .FileSystem ;
43
+ import java .nio .file .FileSystems ;
40
44
import java .nio .file .Files ;
41
45
import java .nio .file .Path ;
42
46
import java .nio .file .Paths ;
47
+ import java .nio .file .StandardCopyOption ;
43
48
import java .security .KeyManagementException ;
44
49
import java .security .NoSuchAlgorithmException ;
50
+ import java .util .HashMap ;
45
51
import java .util .List ;
52
+ import java .util .Optional ;
53
+ import java .util .stream .Collectors ;
46
54
47
55
import javax .net .ssl .HttpsURLConnection ;
48
56
import javax .net .ssl .SSLContext ;
@@ -86,14 +94,15 @@ public void checkServerTrusted(java.security.cert.X509Certificate[] certs, Strin
86
94
*
87
95
* @param conn http connection
88
96
* @param pkgPathInBalaCache package path in bala cache, <user.home>.ballerina/bala_cache/<org-name>/<pkg-name>
89
- * @param pkgNameWithOrg package name with org, <org-name>/<pkg-name>
97
+ * @param pkgOrg package org
98
+ * @param pkgName package name
90
99
* @param isNightlyBuild is nightly build
91
100
* @param newUrl new redirect url
92
101
* @param contentDisposition content disposition header
93
102
* @param outStream Output print stream
94
103
* @param logFormatter log formatter
95
104
*/
96
- public static void createBalaInHomeRepo (HttpURLConnection conn , Path pkgPathInBalaCache , String pkgNameWithOrg ,
105
+ public static void createBalaInHomeRepo (HttpURLConnection conn , Path pkgPathInBalaCache , String pkgOrg , String pkgName ,
97
106
boolean isNightlyBuild , String newUrl , String contentDisposition , PrintStream outStream ,
98
107
LogFormatter logFormatter ) throws CentralClientException {
99
108
long responseContentLength = conn .getContentLengthLong ();
@@ -110,17 +119,22 @@ public static void createBalaInHomeRepo(HttpURLConnection conn, Path pkgPathInBa
110
119
111
120
String validPkgVersion = validatePackageVersion (pkgVersion , logFormatter );
112
121
String balaFile = getBalaFileName (contentDisposition , uriParts [uriParts .length - 1 ]);
113
- Path balaCacheWithPkgPath = pkgPathInBalaCache .resolve (validPkgVersion );
122
+ String platform = getPlatformFromBala (balaFile , pkgName , validPkgVersion );
123
+ Path balaCacheWithPkgPath = pkgPathInBalaCache .resolve (validPkgVersion ).resolve (platform );
114
124
//<user.home>.ballerina/bala_cache/<org-name>/<pkg-name>/<pkg-version>
115
125
116
- Path balaPath = Paths .get (balaCacheWithPkgPath .toString (), balaFile );
117
- if (balaPath .toFile ().exists ()) {
126
+ try {
127
+ if (Files .isDirectory (balaCacheWithPkgPath ) && Files .list (balaCacheWithPkgPath ).findAny ().isPresent ()) {
128
+ throw new PackageAlreadyExistsException (
129
+ logFormatter .formatLog ("package already exists in the home repository: " + balaCacheWithPkgPath .toString ()));
130
+ }
131
+ } catch (IOException e ) {
118
132
throw new PackageAlreadyExistsException (
119
- logFormatter .formatLog ("package already exists in the home repository : " + balaPath .toString ()));
133
+ logFormatter .formatLog ("error accessing bala : " + balaCacheWithPkgPath .toString ()));
120
134
}
121
135
122
136
createBalaFileDirectory (balaCacheWithPkgPath , logFormatter );
123
- writeBalaFile (conn , balaPath , pkgNameWithOrg + ":" + validPkgVersion , responseContentLength , outStream ,
137
+ writeBalaFile (conn , balaCacheWithPkgPath . resolve ( balaFile ), pkgOrg + "/" + pkgName + ":" + validPkgVersion , responseContentLength , outStream ,
124
138
logFormatter );
125
139
handleNightlyBuild (isNightlyBuild , balaCacheWithPkgPath , logFormatter );
126
140
}
@@ -194,6 +208,8 @@ static void writeBalaFile(HttpURLConnection conn, Path balaPath, String fullPkgN
194
208
FileOutputStream outputStream = new FileOutputStream (balaPath .toString ())) {
195
209
writeAndHandleProgress (inputStream , outputStream , resContentLength / 1024 , fullPkgName , outStream ,
196
210
logFormatter );
211
+ extractBala (balaPath , Optional .of (balaPath .getParent ()).get ());
212
+ Files .delete (balaPath );
197
213
} catch (IOException e ) {
198
214
throw new CentralClientException (
199
215
logFormatter .formatLog ("error occurred copying the bala file: " + e .getMessage ()));
@@ -343,4 +359,25 @@ static long getTotalFileSizeInKB(Path filePath) throws CentralClientException {
343
359
static List <String > getAsList (String arrayString ) {
344
360
return new Gson ().fromJson (arrayString , new TypeToken <List <String >>() { }.getType ());
345
361
}
362
+
363
+ private static String getPlatformFromBala (String balaName , String packageName , String version ) {
364
+ return balaName .split (packageName + "-" )[1 ].split ("-" + version )[0 ];
365
+ }
366
+
367
+ private static void extractBala (Path balaFilePath , Path balaFileDestPath ) throws IOException {
368
+ Files .createDirectories (balaFileDestPath );
369
+ URI zipURI = URI .create ("jar:" + balaFilePath .toUri ().toString ());
370
+ try (FileSystem zipFileSystem = FileSystems .newFileSystem (zipURI , new HashMap <>())) {
371
+ Path packageRoot = zipFileSystem .getPath ("/" );
372
+ List <Path > paths = Files .walk (packageRoot ).filter (path -> path != packageRoot ).collect (Collectors .toList ());
373
+ for (Path path : paths ) {
374
+ Path destPath = balaFileDestPath .resolve (packageRoot .relativize (path ).toString ());
375
+ // Handle overwriting existing bala
376
+ if (destPath .toFile ().isDirectory ()) {
377
+ FileUtils .deleteDirectory (destPath .toFile ());
378
+ }
379
+ Files .copy (path , destPath , StandardCopyOption .REPLACE_EXISTING );
380
+ }
381
+ }
382
+ }
346
383
}
0 commit comments