Skip to content

Commit

Permalink
Merge pull request #336 from udda1996/delete-dep
Browse files Browse the repository at this point in the history
Add automatic deletion for unused dependencies with distribution removal
  • Loading branch information
udda1996 authored Jan 5, 2024
2 parents ecd8793 + 99403b0 commit d3b5e30
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 14 deletions.
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ swan-lake-version=2201.2.0
swan-lake-spec-version=2022R3
swan-lake-latest-version=2201.8.2
swan-lake-latest-spec-version=2023R1
swan-lake-latest-dependency-version=jdk-17.0.7+7-jre
1-x-channel-version=1.2.0
1-x-channel-dependency-version=jdk8u202-b08-jre
1-x-channel-spec-version=2020R1
1-x-channel-latest-version=1.2.44
18 changes: 18 additions & 0 deletions src/main/java/org/ballerinalang/command/cmd/RemoveCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ private void remove(String version) {
OSUtils.deleteFiles(directory.toPath());
OSUtils.deleteCaches(version, getPrintStream());
getPrintStream().println("Distribution '" + version + "' successfully removed");
ToolUtil.removeUnusedDependencies(version, getPrintStream());
} else {
throw ErrorUtil.createCommandException("distribution '" + version + "' not found");
}
Expand Down Expand Up @@ -146,6 +147,23 @@ private void removeAll() {
}
}
getPrintStream().println("All non-active distributions are successfully removed");
String activeDistribution = ToolUtil.getCurrentBallerinaVersion();
String dependencyForActiveDistribution = ToolUtil.getDependency(getPrintStream(), activeDistribution,
ToolUtil.getType(activeDistribution), activeDistribution);
File[] dependencies = new File(ToolUtil.getDependencyPath()).listFiles();
if (dependencies != null) {
if (dependencies.length > 1) {
getPrintStream().println("Removing unused dependencies");
for (File dependency : dependencies) {
if (dependency.isDirectory() && !dependency.getName().equals(dependencyForActiveDistribution)) {
OSUtils.deleteFiles(dependency.toPath());
}
}
}
} else {
throw ErrorUtil.createCommandException("No dependencies found");
}

} catch (IOException | NullPointerException e) {
throw ErrorUtil.createCommandException("error occurred while removing the distributions" + e);
}
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/org/ballerinalang/command/util/Distribution.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,30 @@ public class Distribution {
private String version;
private String type;
private String channel;
private String dependency;

public Distribution() {
this.name = "";
this.version = "";
this.type = "";
this.channel = "";
this.dependency = "";
}

public Distribution(String version) {
this.name = "";
this.version = version;
this.type = "";
this.channel = "";
this.dependency = "";
}

public Distribution(String name, String version, String type, String channel) {
public Distribution(String name, String version, String type, String channel, String dependency) {
this.name = name;
this.version = version;
this.type = type;
this.channel = channel;
this.dependency = dependency;
}

public String getName() {
Expand Down Expand Up @@ -77,4 +81,12 @@ public String getChannel() {
public void setChannel(String channel) {
this.channel = channel;
}

public String getDependency() {
return dependency;
}

public void setDependency(String dependency) {
this.dependency = dependency;
}
}
71 changes: 68 additions & 3 deletions src/main/java/org/ballerinalang/command/util/ToolUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -297,6 +297,8 @@ public static List<Channel> getDistributions(PrintStream printStream) {
while (matcher.find()) {
if (count++ % 2 == 0) {
distributions.get(i++).setName(matcher.group(1));
} else {
distributions.get(i++).setDependency(matcher.group(1));
}
}

Expand Down Expand Up @@ -529,7 +531,8 @@ public static boolean downloadDistribution(PrintStream printStream, String distr
HttpsURLConnection redirectedConn = getServerUrlWithProxyAuthentication(new URL(newUrl));
redirectedConn.setRequestProperty("content-type", "binary/data");
ToolUtil.downloadDistributionZip(printStream, redirectedConn, distribution);
String dependencyForDistribution = ToolUtil.getDependency(printStream, distribution, distributionType, distributionVersion);
String dependencyForDistribution = ToolUtil.getDependency(printStream, distribution,
distributionType, distributionVersion);
if (!ToolUtil.checkDependencyAvailable(dependencyForDistribution)) {
downloadDependency(printStream, dependencyForDistribution, distributionType,
distributionVersion);
Expand All @@ -539,7 +542,8 @@ public static boolean downloadDistribution(PrintStream printStream, String distr
} else if (conn.getResponseCode() == 200) {
printStream.println("Fetching the '" + distribution + "' distribution from the remote server...");
ToolUtil.downloadDistributionZip(printStream, conn, distribution);
String dependencyForDistribution = ToolUtil.getDependency(printStream, distribution, distributionType, distributionVersion);
String dependencyForDistribution = ToolUtil.getDependency(printStream, distribution,
distributionType, distributionVersion);
if (!ToolUtil.checkDependencyAvailable(dependencyForDistribution)) {
downloadDependency(printStream, dependencyForDistribution, distributionType,
distributionVersion);
Expand Down Expand Up @@ -696,6 +700,67 @@ private static void downloadAndSetupDependency(HttpURLConnection conn, PrintStre
zipFile.delete();
}
}

public static void removeUnusedDependencies(String distributionVersion, PrintStream printStream) {
String dependencyForDistribution = "";
String channelForDistribution = "";
List<String> distributionsWithDependency = new ArrayList<>();
List<Channel> channels = getDistributions(printStream);
for (Channel channel : channels) {
for (Distribution distribution : channel.getDistributions()) {
if (distribution.getVersion().equals(distributionVersion)) {
dependencyForDistribution = distribution.getDependency();
channelForDistribution = channel.getName();
break;
}
}
}
if (dependencyForDistribution.equals("")) {
printStream.println("No dependency found for the given distribution version");
return;
}
for (Channel channel : channels) {
if (channel.getName().equals(channelForDistribution)) {
for (Distribution distribution : channel.getDistributions()) {
if (distribution.getDependency().equals(dependencyForDistribution)) {
distributionsWithDependency.add(distribution.getVersion());
}
}
}
}
List<String> localDistributions = getLocalDistributions();
localDistributions.retainAll(distributionsWithDependency);
if (localDistributions.size() == 0) {
printStream.println("No local distributions found for the dependency '" + dependencyForDistribution + "'\n" +
"Deleting the dependency '" + dependencyForDistribution + "'");
File dependencyToDelete = new File(getDependencyPath() + File.separator + dependencyForDistribution);
if (dependencyToDelete.exists()) {
try {
OSUtils.deleteFiles(dependencyToDelete.toPath());
} catch (IOException e) {
printStream.println("Error occurred while deleting the dependency '" + dependencyForDistribution + "'");
}
}
}
}

private static List<String> getLocalDistributions() {
List<String> localDistributions = new ArrayList<>();
File folder = new File(ToolUtil.getDistributionsPath());
File[] listOfFiles = folder.listFiles();
if (listOfFiles != null) {
Arrays.sort(listOfFiles);
for (File file : listOfFiles) {
if (file.isDirectory()) {
String[] parts = file.getName().split("-");
if (parts.length == 2) {
localDistributions.add(parts[1]);
}
}
}
}
return localDistributions;
}

public static void downloadTool(PrintStream printStream, String toolVersion) {
HttpsURLConnection conn = null;
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/org/ballerinalang/command/RemoveCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.ballerinalang.command.cmd.PullCommand;
import org.ballerinalang.command.cmd.RemoveCommand;
import org.ballerinalang.command.cmd.UpdateCommand;
import org.ballerinalang.command.cmd.UseCommand;
import org.ballerinalang.command.exceptions.CommandException;
import org.testng.Assert;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -88,10 +89,20 @@ public void removeCommandTest() {
new CommandLine(updateCommand).parse();
updateCommand.execute();

PullCommand pullCmd1 = new PullCommand(testStream);
new CommandLine(pullCmd1).parse("2201.8.0");
pullCmd1.execute();

PullCommand pullCmd = new PullCommand(testStream);
new CommandLine(pullCmd).parse("2201.4.2");
pullCmd.execute();

RemoveCommand removeCommand1 = new RemoveCommand(testStream);
new CommandLine(removeCommand1).parse("2201.8.0");
removeCommand1.execute();
Assert.assertTrue(outContent.toString().contains("successfully removed"));
Assert.assertTrue(outContent.toString().contains("Deleting the dependency"));

RemoveCommand removeCommand2 = new RemoveCommand(testStream);
new CommandLine(removeCommand2).parse("2201.5.0");
removeCommand2.execute();
Expand Down Expand Up @@ -125,5 +136,10 @@ public void removeAllCommandwithMultipleArgsTest() {
} catch (CommandException e) {
Assert.assertTrue(e.getMessages().get(0).contains("too many arguments"));
}

RemoveCommand removeAllCommand = new RemoveCommand(testStream);
new CommandLine(removeAllCommand).parse("-a");
removeAllCommand.execute();
Assert.assertTrue(outContent.toString().contains("Removing unused dependencies"));
}
}
11 changes: 11 additions & 0 deletions src/test/java/org/ballerinalang/distribution/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class TestUtils {
public static final String PATH_ARG = TEST_DISTRIBUTION_PATH.resolve(DIST_NAME).resolve("bin").resolve("bal").
toString();
private static final Path DIST_PATH = TEST_DISTRIBUTION_PATH.resolve(DIST_NAME).resolve("distributions");
private static final Path DEPENDENCY_PATH = TEST_DISTRIBUTION_PATH.resolve(DIST_NAME).resolve("dependencies");
public static final Path TEST_DIR = TEST_DISTRIBUTION_PATH.resolve("test");

/**
Expand Down Expand Up @@ -292,6 +293,16 @@ public static Path getDistPath (String version) {
return DIST_PATH.resolve(type + "-" + version);
}

/**
* Get dependency path for the version.
*
* @param version dependency version
* @return returns dependency path for available distributions
*/
public static Path getDependencyPath (String version) {
return DEPENDENCY_PATH.resolve(version);
}

/**
* Get the display text using distribution version
*
Expand Down
27 changes: 17 additions & 10 deletions src/test/java/org/ballerinalang/distribution/UpdateToolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ public class UpdateToolTest {
String swanLakeVersion = System.getProperty("swan-lake-version");
String swanLakeSpecVersion = System.getProperty("swan-lake-spec-version");
String swanLakeLatestVersion = System.getProperty("swan-lake-latest-version");
String swanLakeLatestVersionDependency = System.getProperty("swan-lake-latest-dependency-version");
String swanLakeLatestSpecVersion = System.getProperty("swan-lake-latest-spec-version");
String previouschannelVersion = System.getProperty("1-x-channel-version");
String previousChannelVersion = System.getProperty("1-x-channel-version");
String previousChannelDependencyVersion = System.getProperty("1-x-channel-dependency-version");
String previousChannelSpecVersion = System.getProperty("1-x-channel-spec-version");
String previousChanneLatestVersion = System.getProperty("1-x-channel-latest-version");

Expand All @@ -61,17 +63,17 @@ public void testPullCommand() throws IOException, InterruptedException {
Assert.assertTrue(output.contains("a distribution must be specified to pull"));

args.add("--test");
args.add(previouschannelVersion);
args.add(previousChannelVersion);
output = TestUtils.executeCommand(args);
Assert.assertTrue(output.contains("Fetching the '" + previouschannelVersion +
Assert.assertTrue(output.contains("Fetching the '" + previousChannelVersion +
"' distribution from the remote server"));
Assert.assertTrue(output.contains("Fetching the dependencies for '" + previouschannelVersion +
Assert.assertTrue(output.contains("Fetching the dependencies for '" + previousChannelVersion +
"' from the remote server"));
Assert.assertTrue(output.contains("successfully set as the active distribution"));
Assert.assertTrue(Files.isDirectory(TestUtils.getDistPath(previouschannelVersion)));
Assert.assertTrue(Files.isDirectory(TestUtils.getDistPath(previousChannelVersion)));
output = TestUtils.testInstallation();
Assert.assertEquals(output, TestUtils.getVersionOutput(previouschannelVersion, previousChannelSpecVersion,
TestUtils.MAVEN_VERSION, previouschannelVersion));
Assert.assertEquals(output, TestUtils.getVersionOutput(previousChannelVersion, previousChannelSpecVersion,
TestUtils.MAVEN_VERSION, previousChannelVersion));

args.remove(args.size() - 1);
args.add(swanLakeVersion);
Expand Down Expand Up @@ -163,7 +165,7 @@ public void testUpdateCommand() throws IOException, InterruptedException {
List<String> useArgs = TestUtils.addPathArg();
useArgs.add("dist");
useArgs.add("use");
useArgs.add(previouschannelVersion);
useArgs.add(previousChannelVersion);
output = TestUtils.executeCommand(useArgs);
Assert.assertTrue(output.contains("successfully set as the active distribution"));

Expand Down Expand Up @@ -281,10 +283,11 @@ public void testRemoveCommand() throws IOException, InterruptedException {
Assert.assertTrue(output.contains("The active Ballerina distribution cannot be removed"));

args.remove(args.size() - 1);
args.add(previouschannelVersion);
args.add(previousChannelVersion);
output = TestUtils.executeCommand(args);
Assert.assertTrue(output.contains("successfully removed"));
Assert.assertFalse(Files.exists(TestUtils.getDistPath(previouschannelVersion)));
Assert.assertFalse(Files.exists(TestUtils.getDistPath(previousChannelVersion)));
Assert.assertFalse(Files.exists(TestUtils.getDependencyPath(previousChannelDependencyVersion)));

args.add("arg1");
output = TestUtils.executeCommand(args);
Expand All @@ -295,9 +298,13 @@ public void testRemoveCommand() throws IOException, InterruptedException {
args.add("-a");
output = TestUtils.executeCommand(args);
Assert.assertTrue(output.contains("All non-active distributions are successfully removed"));
Assert.assertTrue(output.contains("Removing unused dependencies"));
Assert.assertTrue(Files.exists(TestUtils.getDistPath(swanLakeLatestVersion)));
Assert.assertFalse(Files.exists(TestUtils.getDistPath(swanLakeVersion)));
Assert.assertFalse(Files.exists(TestUtils.getDistPath(previousChanneLatestVersion)));
Assert.assertTrue(Files.exists(TestUtils.getDependencyPath(swanLakeLatestVersionDependency)));
Assert.assertEquals(Files.list(TestUtils.getDependencyPath(swanLakeLatestVersionDependency).getParent()).count()
, 1);

output = TestUtils.executeCommand(args);
Assert.assertTrue(output.contains("There is nothing to remove. Only active distribution is remaining"));
Expand Down

0 comments on commit d3b5e30

Please sign in to comment.