2525import com .google .gson .reflect .TypeToken ;
2626import me .tongfei .progressbar .ProgressBar ;
2727import me .tongfei .progressbar .ProgressBarStyle ;
28+ import org .apache .commons .io .FileUtils ;
2829import org .ballerinalang .central .client .exceptions .CentralClientException ;
2930import org .ballerinalang .central .client .exceptions .ConnectionErrorException ;
3031import org .ballerinalang .central .client .exceptions .PackageAlreadyExistsException ;
3637import java .net .HttpURLConnection ;
3738import java .net .MalformedURLException ;
3839import java .net .ProtocolException ;
40+ import java .net .URI ;
3941import java .net .URL ;
42+ import java .nio .file .FileSystem ;
43+ import java .nio .file .FileSystems ;
4044import java .nio .file .Files ;
4145import java .nio .file .Path ;
4246import java .nio .file .Paths ;
47+ import java .nio .file .StandardCopyOption ;
4348import java .security .KeyManagementException ;
4449import java .security .NoSuchAlgorithmException ;
50+ import java .util .HashMap ;
4551import java .util .List ;
52+ import java .util .Optional ;
53+ import java .util .stream .Collectors ;
4654
4755import javax .net .ssl .HttpsURLConnection ;
4856import javax .net .ssl .SSLContext ;
@@ -86,14 +94,15 @@ public void checkServerTrusted(java.security.cert.X509Certificate[] certs, Strin
8694 *
8795 * @param conn http connection
8896 * @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
9099 * @param isNightlyBuild is nightly build
91100 * @param newUrl new redirect url
92101 * @param contentDisposition content disposition header
93102 * @param outStream Output print stream
94103 * @param logFormatter log formatter
95104 */
96- public static void createBalaInHomeRepo (HttpURLConnection conn , Path pkgPathInBalaCache , String pkgNameWithOrg ,
105+ public static void createBalaInHomeRepo (HttpURLConnection conn , Path pkgPathInBalaCache , String pkgOrg , String pkgName ,
97106 boolean isNightlyBuild , String newUrl , String contentDisposition , PrintStream outStream ,
98107 LogFormatter logFormatter ) throws CentralClientException {
99108 long responseContentLength = conn .getContentLengthLong ();
@@ -110,17 +119,22 @@ public static void createBalaInHomeRepo(HttpURLConnection conn, Path pkgPathInBa
110119
111120 String validPkgVersion = validatePackageVersion (pkgVersion , logFormatter );
112121 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 );
114124 //<user.home>.ballerina/bala_cache/<org-name>/<pkg-name>/<pkg-version>
115125
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 ) {
118132 throw new PackageAlreadyExistsException (
119- logFormatter .formatLog ("package already exists in the home repository : " + balaPath .toString ()));
133+ logFormatter .formatLog ("error accessing bala : " + balaCacheWithPkgPath .toString ()));
120134 }
121135
122136 createBalaFileDirectory (balaCacheWithPkgPath , logFormatter );
123- writeBalaFile (conn , balaPath , pkgNameWithOrg + ":" + validPkgVersion , responseContentLength , outStream ,
137+ writeBalaFile (conn , balaCacheWithPkgPath . resolve ( balaFile ), pkgOrg + "/" + pkgName + ":" + validPkgVersion , responseContentLength , outStream ,
124138 logFormatter );
125139 handleNightlyBuild (isNightlyBuild , balaCacheWithPkgPath , logFormatter );
126140 }
@@ -194,6 +208,8 @@ static void writeBalaFile(HttpURLConnection conn, Path balaPath, String fullPkgN
194208 FileOutputStream outputStream = new FileOutputStream (balaPath .toString ())) {
195209 writeAndHandleProgress (inputStream , outputStream , resContentLength / 1024 , fullPkgName , outStream ,
196210 logFormatter );
211+ extractBala (balaPath , Optional .of (balaPath .getParent ()).get ());
212+ Files .delete (balaPath );
197213 } catch (IOException e ) {
198214 throw new CentralClientException (
199215 logFormatter .formatLog ("error occurred copying the bala file: " + e .getMessage ()));
@@ -343,4 +359,25 @@ static long getTotalFileSizeInKB(Path filePath) throws CentralClientException {
343359 static List <String > getAsList (String arrayString ) {
344360 return new Gson ().fromJson (arrayString , new TypeToken <List <String >>() { }.getType ());
345361 }
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+ }
346383}
0 commit comments