Skip to content

Commit

Permalink
Merge pull request #42057 from Thevakumar-Luheerathan/fix-ballerina-l…
Browse files Browse the repository at this point in the history
…ang-iss-40264-8.x

[2201.8.x] Add local repository support for tools
  • Loading branch information
Thevakumar-Luheerathan authored Jan 30, 2024
2 parents 61e02f6 + e7fd4a5 commit 0c24ddd
Show file tree
Hide file tree
Showing 77 changed files with 1,331 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package io.ballerina.cli.cmd;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.ballerina.cli.BLauncherCmd;
import io.ballerina.cli.utils.FileUtils;
import io.ballerina.projects.DependencyManifest;
Expand Down Expand Up @@ -44,14 +46,18 @@
import org.wso2.ballerinalang.util.RepoUtils;
import picocli.CommandLine;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
Expand All @@ -60,6 +66,7 @@
import static io.ballerina.cli.utils.CentralUtils.authenticate;
import static io.ballerina.cli.utils.CentralUtils.getBallerinaCentralCliTokenUrl;
import static io.ballerina.cli.utils.CentralUtils.getCentralPackageURL;
import static io.ballerina.projects.util.ProjectConstants.LOCAL_TOOLS_JSON;
import static io.ballerina.projects.util.ProjectConstants.SETTINGS_FILE_NAME;
import static io.ballerina.projects.util.ProjectUtils.getAccessTokenOfCLI;
import static io.ballerina.projects.util.ProjectUtils.initializeProxy;
Expand All @@ -73,6 +80,11 @@
@CommandLine.Command(name = PUSH_COMMAND, description = "Publish a package to Ballerina Central")
public class PushCommand implements BLauncherCmd {

private static final String TOOL_DIR = "tool";
private static final String BAL_TOOL_JSON = "bal-tool.json";
private static final String TOOL_ID = "tool_id";
private static final String ORG = "org";
private static final String PACKAGE_NAME = "name";
@CommandLine.Parameters (arity = "0..1")
private Path balaPath;

Expand Down Expand Up @@ -387,9 +399,11 @@ private void pushBalaToCustomRepo(Path balaFilePath) {
ProjectUtils.deleteDirectory(balaCachesPath);
}
ProjectUtils.extractBala(balaFilePath, balaDestPath);
createLocalToolsJsonIfLocalTool(balaDestPath, org, packageName, repoPath.resolve(
ProjectConstants.BALA_DIR_NAME));
} catch (IOException e) {
throw new ProjectException("error while pushing bala file '" + balaFilePath + "' to '"
+ ProjectConstants.LOCAL_REPOSITORY_NAME + "' repository. " + e.getMessage());
+ ProjectConstants.LOCAL_REPOSITORY_NAME + "' repository: " + e.getMessage());
}

Path relativePathToBalaFile;
Expand All @@ -402,6 +416,51 @@ private void pushBalaToCustomRepo(Path balaFilePath) {
+ " to '" + repositoryName + "' repository.");
}

private void createLocalToolsJsonIfLocalTool(Path balaDestPath, String org, String packageName,
Path localRepoBalaPath) {
Path balToolJsonPath = balaDestPath.resolve(TOOL_DIR).resolve(BAL_TOOL_JSON);
JsonObject balToolJson;
JsonObject localToolJson;
Gson gson = new Gson();
if (!balToolJsonPath.toFile().exists()) {
return;
}
try (BufferedReader bufferedReader = Files.newBufferedReader(balToolJsonPath, StandardCharsets.UTF_8)) {
balToolJson = gson.fromJson(bufferedReader, JsonObject.class);
} catch (IOException e) {
throw new ProjectException("Failed to read bal-tools.json file: " + e.getMessage());
}
Optional<String> optionalToolId = Optional.ofNullable(balToolJson.get(TOOL_ID).getAsString());
if (optionalToolId.isEmpty()) {
return;
}
String toolId = optionalToolId.get();
JsonObject packageDesc = new JsonObject();
packageDesc.addProperty(ORG, org);
packageDesc.addProperty(PACKAGE_NAME, packageName);
Path localToolJsonPath = localRepoBalaPath.resolve(LOCAL_TOOLS_JSON);
if (localToolJsonPath.toFile().exists()) {
try (BufferedReader bufferedReader = Files.newBufferedReader(localToolJsonPath, StandardCharsets.UTF_8)) {
localToolJson = gson.fromJson(bufferedReader, JsonObject.class);
if (localToolJson.has(toolId)) {
localToolJson.remove(toolId);
}
localToolJson.add(toolId, packageDesc);
} catch (IOException e) {
throw new ProjectException("Failed to read local-tools.json file: " + e.getMessage());
}
} else {
localToolJson = new JsonObject();
localToolJson.add(toolId, packageDesc);
}

try (FileWriter writer = new FileWriter(localToolJsonPath.toFile(), StandardCharsets.UTF_8)) {
writer.write(gson.toJson(localToolJson));
} catch (IOException e) {
throw new ProjectException("Failed to write local-tools.json file: " + e.getMessage());
}
}

/**
* Push a bala file to remote repository.
*
Expand Down
Loading

0 comments on commit 0c24ddd

Please sign in to comment.