Skip to content

Commit c0d4e07

Browse files
authored
Merge pull request #44400 from azinneera/prj_api_customRepoPath
Support multiple dependency cache locations
2 parents c9941e5 + 644edb1 commit c0d4e07

File tree

6 files changed

+106
-18
lines changed

6 files changed

+106
-18
lines changed

compiler/ballerina-lang/src/main/java/io/ballerina/projects/environment/EnvironmentBuilder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ public Environment build() {
9595

9696
Map<String, PackageRepository> customRepositories = ballerinaUserHome.customRepositories().entrySet().stream()
9797
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
98+
customRepositories.putAll(ballerinaUserHome.customFSRepositories().entrySet().stream()
99+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
98100

99101
WorkspaceRepository workspaceRepository = null;
100102
if (this.workspaceProject != null) {

compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/SettingsBuilder.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import io.ballerina.tools.diagnostics.Diagnostic;
3939

4040
import java.io.IOException;
41+
import java.nio.file.Path;
4142
import java.util.ArrayList;
4243
import java.util.List;
4344
import java.util.Map;
@@ -51,6 +52,7 @@ public class SettingsBuilder {
5152

5253
public static final String NAME = "name";
5354
public static final String MAVEN = "maven";
55+
public static final String FILESYSTEM = "filesystem";
5456
private static final String CONNECT_TIMEOUT = "connectTimeout";
5557
private static final String READ_TIMEOUT = "readTimeout";
5658
private static final String WRITE_TIMEOUT = "writeTimeout";
@@ -71,6 +73,7 @@ public class SettingsBuilder {
7173
private static final String REPOSITORY = "repository";
7274
private static final String ID = "id";
7375
private static final String URL = "url";
76+
public static final String PATH = "path";
7477
private static final int DEFAULT_CONNECT_TIMEOUT = 60;
7578
private static final int DEFAULT_READ_TIMEOUT = 60;
7679
private static final int DEFAULT_WRITE_TIMEOUT = 60;
@@ -128,7 +131,7 @@ private Settings parseAsSettings() {
128131
int maxRetries = DEFAULT_MAX_RETRY;
129132
String url = "";
130133
String id = "";
131-
String repoName = "";
134+
Path path = null;
132135
String repositoryUsername = "";
133136
String repositoryPassword = "";
134137
ArrayList<Repository> repositories = new ArrayList<>();
@@ -161,7 +164,7 @@ private Settings parseAsSettings() {
161164
for (Map.Entry<String, TopLevelNode> entry : repoEntries.entrySet()) {
162165
String repoKey = entry.getKey();
163166
TopLevelNode repoValue = entry.getValue();
164-
if (!MAVEN.equals(repoKey)) {
167+
if (!MAVEN.equals(repoKey) && !FILESYSTEM.equals(repoKey)) {
165168
continue;
166169
}
167170
List<TomlTableNode> repositoryNodes = ((TomlTableArrayNode) (repoValue)).children();
@@ -170,7 +173,12 @@ private Settings parseAsSettings() {
170173
id = getStringOrDefaultFromTomlTableNode(repositoryNode, ID, "");
171174
repositoryUsername = getStringOrDefaultFromTomlTableNode(repositoryNode, USERNAME, "");
172175
repositoryPassword = getStringOrDefaultFromTomlTableNode(repositoryNode, ACCESS_TOKEN, "");
173-
repositories.add(Repository.from(id, url, repositoryUsername, repositoryPassword));
176+
String pathStr = getStringOrDefaultFromTomlTableNode(repositoryNode, PATH, "");
177+
if (!pathStr.isEmpty()) {
178+
path = Path.of(pathStr);
179+
}
180+
repositories.add(Repository.from(
181+
id, url, repositoryUsername, repositoryPassword, repoKey, path));
174182
}
175183
}
176184
}

compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/environment/BallerinaUserHome.java

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.ballerina.projects.internal.SettingsBuilder;
88
import io.ballerina.projects.internal.model.Repository;
99
import io.ballerina.projects.internal.repositories.CustomPkgRepositoryContainer;
10+
import io.ballerina.projects.internal.repositories.FileSystemRepository;
1011
import io.ballerina.projects.internal.repositories.LocalPackageRepository;
1112
import io.ballerina.projects.internal.repositories.MavenPackageRepository;
1213
import io.ballerina.projects.internal.repositories.RemotePackageRepository;
@@ -22,6 +23,7 @@
2223
import java.util.HashMap;
2324
import java.util.Map;
2425

26+
import static io.ballerina.projects.internal.SettingsBuilder.MAVEN;
2527
import static io.ballerina.runtime.api.constants.RuntimeConstants.USER_HOME;
2628

2729
/**
@@ -34,7 +36,8 @@ public final class BallerinaUserHome {
3436
private final Path ballerinaUserHomeDirPath;
3537
private final RemotePackageRepository remotePackageRepository;
3638
private final LocalPackageRepository localPackageRepository;
37-
private final Map<String, MavenPackageRepository> mavenCustomRepositories;
39+
private Map<String, MavenPackageRepository> mavenCustomRepositories;
40+
private Map<String, FileSystemRepository> customFSRepositories;
3841

3942
private BallerinaUserHome(Environment environment, Path ballerinaUserHomeDirPath) {
4043
this.ballerinaUserHomeDirPath = ballerinaUserHomeDirPath;
@@ -53,27 +56,48 @@ private BallerinaUserHome(Environment environment, Path ballerinaUserHomeDirPath
5356
this.remotePackageRepository = RemotePackageRepository
5457
.from(environment, remotePackageRepositoryPath, readSettings());
5558
this.localPackageRepository = createLocalRepository(environment);
56-
this.mavenCustomRepositories = createMavenCustomRepositories(environment);
59+
createCustomRepositories(environment);
5760
}
5861

59-
private Map<String, MavenPackageRepository> createMavenCustomRepositories(Environment environment) {
60-
Map<String, MavenPackageRepository> customRepositories = new HashMap<>();
62+
private void createCustomRepositories(Environment environment) {
63+
mavenCustomRepositories = new HashMap<>();
64+
customFSRepositories = new HashMap<>();
6165
Repository[] repositories = readSettings().getRepositories();
6266
for (Repository repository : repositories) {
63-
Path repositoryPath = ballerinaUserHomeDirPath.resolve(ProjectConstants.REPOSITORIES_DIR)
64-
.resolve(repository.id());
67+
if (MAVEN.equals(repository.type())) {
68+
Path repositoryPath = ballerinaUserHomeDirPath.resolve(ProjectConstants.REPOSITORIES_DIR)
69+
.resolve(repository.id());
70+
try {
71+
Files.createDirectories(repositoryPath);
72+
} catch (IOException exception) {
73+
throw new ProjectException("unable to create repository: " +
74+
ProjectConstants.LOCAL_REPOSITORY_NAME);
75+
}
76+
77+
if (!mavenCustomRepositories.containsKey(repository.id())) {
78+
mavenCustomRepositories.put(repository.id(), MavenPackageRepository.from(
79+
environment, repositoryPath, repository));
80+
}
81+
continue;
82+
}
83+
Path repositoryPath;
84+
if (repository.path().isPresent()) {
85+
repositoryPath = repository.path().get();
86+
} else {
87+
repositoryPath = ballerinaUserHomeDirPath.resolve(ProjectConstants.REPOSITORIES_DIR)
88+
.resolve(repository.id());
89+
}
6590
try {
6691
Files.createDirectories(repositoryPath);
6792
} catch (IOException exception) {
68-
throw new ProjectException("unable to create repository: " + ProjectConstants.LOCAL_REPOSITORY_NAME);
93+
throw new ProjectException("unable to create repository: " + repositoryPath);
6994
}
7095

71-
if (!customRepositories.containsKey(repository.id())) {
72-
customRepositories.put(repository.id(), MavenPackageRepository.from(environment, repositoryPath,
73-
repository));
96+
if (!customFSRepositories.containsKey(repository.id())) {
97+
customFSRepositories.put(repository.id(), new FileSystemRepository(environment, repositoryPath,
98+
RepoUtils.getBallerinaVersion()));
7499
}
75100
}
76-
return customRepositories;
77101
}
78102

79103
public static BallerinaUserHome from(Environment environment, Path ballerinaUserHomeDirPath) {
@@ -104,6 +128,10 @@ public Map<String, MavenPackageRepository> customRepositories() {
104128
return this.mavenCustomRepositories;
105129
}
106130

131+
public Map<String, FileSystemRepository> customFSRepositories() {
132+
return this.customFSRepositories;
133+
}
134+
107135
public CustomPkgRepositoryContainer customPkgRepositoryContainer() {
108136
return new CustomPkgRepositoryContainer(mavenCustomRepositories);
109137
}

compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/environment/DefaultPackageResolver.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import io.ballerina.projects.environment.ResolutionResponse.ResolutionStatus;
3535
import io.ballerina.projects.internal.ImportModuleRequest;
3636
import io.ballerina.projects.internal.ImportModuleResponse;
37+
import io.ballerina.projects.internal.repositories.FileSystemRepository;
3738
import io.ballerina.projects.util.ProjectConstants;
3839
import io.ballerina.projects.util.ProjectUtils;
3940

@@ -129,6 +130,8 @@ public Collection<PackageMetadataResponse> resolvePackageMetadata(Collection<Res
129130
Collection<ResolutionRequest> localRepoRequests = new ArrayList<>();
130131
Map<PackageRepository, ArrayList<ResolutionRequest>> customRepoRequestMap = new HashMap<>();
131132
Collection<ResolutionRequest> workspaceRequests = new ArrayList<>();
133+
Map<FileSystemRepository, ArrayList<ResolutionRequest>> customFSRepoRequestMap = new HashMap<>();
134+
132135
for (ResolutionRequest request : requests) {
133136
Optional<String> repository = request.packageDescriptor().repository();
134137
if (repository.isPresent() && repository.get().equals(ProjectConstants.LOCAL_REPOSITORY_NAME)) {
@@ -147,6 +150,15 @@ public Collection<PackageMetadataResponse> resolvePackageMetadata(Collection<Res
147150
&& !ProjectUtils.isBuiltInPackage(request.orgName(), request.packageName().toString())
148151
&& !request.skipWorkspace()) {
149152
workspaceRequests.add(request);
153+
} else {
154+
String org = request.packageDescriptor().org().toString();
155+
if (customRepos.containsKey(org)
156+
&& customRepos.get(org) instanceof FileSystemRepository customFSRepository) {
157+
if (!customFSRepoRequestMap.containsKey(customFSRepository)) {
158+
customFSRepoRequestMap.put(customFSRepository, new ArrayList<>());
159+
}
160+
customFSRepoRequestMap.get(customFSRepository).add(request);
161+
}
150162
}
151163
}
152164

@@ -164,6 +176,15 @@ public Collection<PackageMetadataResponse> resolvePackageMetadata(Collection<Res
164176
allCustomRepoPackages.addAll(customRepoPackages);
165177
}
166178

179+
for (Map.Entry<FileSystemRepository, ArrayList<ResolutionRequest>> customFSRepoRequestEntry :
180+
customFSRepoRequestMap.entrySet()) {
181+
PackageRepository customFSRepository = customFSRepoRequestEntry.getKey();
182+
ArrayList<ResolutionRequest> customFSRepoRequests = customFSRepoRequestEntry.getValue();
183+
Collection<PackageMetadataResponse> customFSRepoPackages = customFSRepoRequests.isEmpty() ?
184+
Collections.emptyList() : customFSRepository.getPackageMetadata(customFSRepoRequests, options);
185+
allCustomRepoPackages.addAll(customFSRepoPackages);
186+
}
187+
167188
Collection<PackageMetadataResponse> workspacePackages = workspaceRequests.isEmpty() ?
168189
Collections.emptyList() :
169190
workspaceRepo.getPackageMetadata(workspaceRequests, options);
@@ -266,6 +287,14 @@ private Optional<Package> resolveFromRepository(ResolutionRequest resolutionReq,
266287
return distributionRepo.getPackage(resolutionReq, options);
267288
}
268289

290+
if (customRepos.containsKey(pkgDesc.org().toString()) &&
291+
customRepos.get(pkgDesc.org().toString()) instanceof FileSystemRepository customFSRepository) {
292+
Optional<Package> resolvedPackage = customFSRepository.getPackage(resolutionReq, options);
293+
if (resolvedPackage.isPresent()) {
294+
return resolvedPackage;
295+
}
296+
}
297+
269298
// 2) Try to load from the local repo, if it is requested from the local repo.
270299
if (pkgDesc.repository().isPresent()) {
271300
String repository = pkgDesc.repository().get();

compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/model/Repository.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
package io.ballerina.projects.internal.model;
1818

19+
import java.nio.file.Path;
20+
import java.util.Optional;
21+
22+
import static io.ballerina.projects.internal.SettingsBuilder.MAVEN;
23+
1924
/**
2025
* Represents the repository object.
2126
*
@@ -26,20 +31,28 @@ public class Repository {
2631
private final String url;
2732
private final String username;
2833
private final String password;
34+
private final String type;
35+
private final Path path;
2936

30-
private Repository(String id, String url, String username, String password) {
37+
private Repository(String id, String url, String username, String password, String remoteType, Path path) {
3138
this.id = id;
3239
this.url = url;
3340
this.username = username;
3441
this.password = password;
42+
this.type = remoteType;
43+
this.path = path;
3544
}
3645

3746
public static Repository from(String id, String url, String username, String password) {
38-
return new Repository(id, url, username, password);
47+
return new Repository(id, url, username, password, MAVEN, null);
48+
}
49+
50+
public static Repository from(String id, String url, String username, String password, String type, Path path) {
51+
return new Repository(id, url, username, password, type, path);
3952
}
4053

4154
public static Repository from() {
42-
return new Repository("", "", "", "");
55+
return new Repository("", "", "", "", MAVEN, null);
4356
}
4457

4558
public String id() {
@@ -57,4 +70,12 @@ public String username() {
5770
public String password() {
5871
return password;
5972
}
73+
74+
public String type() {
75+
return type;
76+
}
77+
78+
public Optional<Path> path() {
79+
return Optional.ofNullable(path);
80+
}
6081
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ org.gradle.vfs.watch=false
66
systemProp.scan.capture-build-logging=false
77
systemProp.scan.capture-test-logging=false
88

9-
version=2201.13.0-SNAPSHOT
9+
version=2201.13.1-SNAPSHOT
1010
group=org.ballerinalang
1111
bootstrappedOn=1.1.0-alpha
1212
specVersion=2024R1

0 commit comments

Comments
 (0)