Skip to content

Commit

Permalink
Refactor the MavenPackageRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
Thevakumar-Luheerathan committed Aug 14, 2023
1 parent a4514eb commit f64f5c6
Show file tree
Hide file tree
Showing 14 changed files with 260 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public void execute() {

if (targetRepository != null) {
MavenResolverClient mavenResolverClient = new MavenResolverClient();
if (!"".equals(targetRepository.username()) && !"".equals(targetRepository.password())) {
if (!targetRepository.username().isEmpty() && !targetRepository.password().isEmpty()) {
mavenResolverClient.addRepository(targetRepository.id(), targetRepository.url(),
targetRepository.username(), targetRepository.password());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public void execute() {
}

MavenResolverClient mvnClient = new MavenResolverClient();
if (!"".equals(targetRepository.username()) && !"".equals(targetRepository.password())) {
if (!targetRepository.username().isEmpty() && !targetRepository.password().isEmpty()) {
mvnClient.addRepository(targetRepository.id(), targetRepository.url(), targetRepository.username(),
targetRepository.password());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
import io.ballerina.projects.internal.ResolutionEngine;
import io.ballerina.projects.internal.ResolutionEngine.DependencyNode;
import io.ballerina.projects.internal.model.BuildJson;
import io.ballerina.projects.internal.repositories.CustomPackageRepository;
import io.ballerina.projects.internal.repositories.CustomPkgRepositoryContainer;
import io.ballerina.projects.internal.repositories.LocalPackageRepository;
import io.ballerina.projects.internal.repositories.MavenPackageRepository;
import io.ballerina.tools.diagnostics.Diagnostic;
import io.ballerina.tools.diagnostics.DiagnosticFactory;
import io.ballerina.tools.diagnostics.DiagnosticInfo;
Expand Down Expand Up @@ -483,7 +483,7 @@ private ModuleResolver createModuleResolver(PackageContext rootPackageContext,

private BlendedManifest createBlendedManifest(PackageContext rootPackageContext,
ProjectEnvironment projectEnvContext, boolean offline) {
Map<String, CustomPackageRepository> customPackageRepositoryMap =
Map<String, MavenPackageRepository> customPackageRepositoryMap =
projectEnvContext.getService(CustomPkgRepositoryContainer.class).getCustomPackageRepositories();
return BlendedManifest.from(rootPackageContext.dependencyManifest(),
rootPackageContext.packageManifest(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import io.ballerina.projects.PackageVersion;
import io.ballerina.projects.SemanticVersion.VersionCompatibilityResult;
import io.ballerina.projects.internal.repositories.AbstractPackageRepository;
import io.ballerina.projects.internal.repositories.CustomPackageRepository;
import io.ballerina.projects.internal.repositories.MavenPackageRepository;
import io.ballerina.projects.util.ProjectConstants;
import io.ballerina.projects.util.ProjectUtils;
import io.ballerina.tools.diagnostics.Diagnostic;
Expand Down Expand Up @@ -63,7 +63,7 @@ private BlendedManifest(PackageContainer<Dependency> pkgContainer, DiagnosticRes
public static BlendedManifest from(DependencyManifest dependencyManifest,
PackageManifest packageManifest,
AbstractPackageRepository localPackageRepository,
Map<String, CustomPackageRepository> customPackageRepositoryMap,
Map<String, MavenPackageRepository> mavenPackageRepositoryMap,
boolean offline) {
List<Diagnostic> diagnostics = new ArrayList<>();
PackageContainer<Dependency> depContainer = new PackageContainer<>();
Expand All @@ -78,7 +78,7 @@ public static BlendedManifest from(DependencyManifest dependencyManifest,
}

for (PackageManifest.Dependency depInPkgManifest : packageManifest.dependencies()) {
AbstractPackageRepository targettedRepository = localPackageRepository;
AbstractPackageRepository targetRepository = localPackageRepository;
Optional<Dependency> existingDepOptional = depContainer.get(
depInPkgManifest.org(), depInPkgManifest.name());
Repository depInPkgManifestRepo = depInPkgManifest.repository() != null &&
Expand All @@ -87,7 +87,7 @@ public static BlendedManifest from(DependencyManifest dependencyManifest,

if (depInPkgManifest.repository() != null) {
if (!depInPkgManifest.repository().equals(ProjectConstants.LOCAL_REPOSITORY_NAME) &&
!customPackageRepositoryMap.containsKey(depInPkgManifest.repository())) {
!mavenPackageRepositoryMap.containsKey(depInPkgManifest.repository())) {
var diagnosticInfo = new DiagnosticInfo(
ProjectDiagnosticErrorCode.CUSTOM_REPOSITORY_NOT_FOUND.diagnosticId(),
"Provided custom repository (" + depInPkgManifest.repository() +
Expand Down Expand Up @@ -116,8 +116,8 @@ public static BlendedManifest from(DependencyManifest dependencyManifest,
}

if (!depInPkgManifest.repository().equals(ProjectConstants.LOCAL_REPOSITORY_NAME)) {
targettedRepository = customPackageRepositoryMap.get(depInPkgManifest.repository());
if (!((CustomPackageRepository) targettedRepository).isPackageExists(depInPkgManifest.org(),
targetRepository = mavenPackageRepositoryMap.get(depInPkgManifest.repository());
if (!((MavenPackageRepository) targetRepository).isPackageExists(depInPkgManifest.org(),
depInPkgManifest.name(), depInPkgManifest.version(), offline)) {
var diagnosticInfo = new DiagnosticInfo(
ProjectDiagnosticErrorCode.PACKAGE_NOT_FOUND.diagnosticId(),
Expand Down Expand Up @@ -145,7 +145,7 @@ public static BlendedManifest from(DependencyManifest dependencyManifest,
depContainer.add(depInPkgManifest.org(), depInPkgManifest.name(),
new Dependency(depInPkgManifest.org(),
depInPkgManifest.name(), depInPkgManifest.version(), DependencyRelation.UNKNOWN,
depInPkgManifestRepo, moduleNames(depInPkgManifest, targettedRepository),
depInPkgManifestRepo, moduleNames(depInPkgManifest, targetRepository),
DependencyOrigin.USER_SPECIFIED));
} else {
Dependency existingDep = existingDepOptional.get();
Expand All @@ -155,7 +155,7 @@ depInPkgManifestRepo, moduleNames(depInPkgManifest, targettedRepository),
compatibilityResult == VersionCompatibilityResult.GREATER_THAN) {
Dependency newDep = new Dependency(depInPkgManifest.org(), depInPkgManifest.name(),
depInPkgManifest.version(), DependencyRelation.UNKNOWN, depInPkgManifestRepo,
moduleNames(depInPkgManifest, targettedRepository), DependencyOrigin.USER_SPECIFIED);
moduleNames(depInPkgManifest, targetRepository), DependencyOrigin.USER_SPECIFIED);
depContainer.add(depInPkgManifest.org(), depInPkgManifest.name(), newDep);
} else if (compatibilityResult == VersionCompatibilityResult.INCOMPATIBLE) {
DiagnosticInfo diagnosticInfo = new DiagnosticInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public enum ProjectDiagnosticErrorCode implements DiagnosticCode {
MODULE_NOT_FOUND("BCE5100", "module.not.found"),
UNSUPPORTED_COMPILER_PLUGIN_TYPE("BCE5200", "unsupported.compiler.plugin.type"),
CONFLICTING_PLATFORM_JAR_FILES("BCE5300", "conflicting.platform.jars.type"),
CUSTOM_REPOSITORY_NOT_FOUND("BCE5400", "custom.repository.not.found"),
CUSTOM_REPOSITORY_NOT_FOUND("BCE5009", "custom.repository.not.found"),
;

private final String diagnosticId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class SettingsBuilder {

public static final String NAME = "name";
public static final String USER_ID = "userId";
public static final String TOKEN = "token";
public static final String ACCESSTOKEN = "accesstoken";
public static final String NEXUS = "nexus";
public static final String ARTIFACTORY = "artifactory";
public static final String FILE_SYSTEM = "file-system";
Expand Down Expand Up @@ -152,7 +152,7 @@ private Settings parseAsSettings() {
url = getStringOrDefaultFromTomlTableNode(repositoryNode, URL, "");
id = getStringOrDefaultFromTomlTableNode(repositoryNode, ID, "");
repositoryUsername = getStringOrDefaultFromTomlTableNode(repositoryNode, USER_ID, "");
repositoryPassword = getStringOrDefaultFromTomlTableNode(repositoryNode, TOKEN, "");
repositoryPassword = getStringOrDefaultFromTomlTableNode(repositoryNode, ACCESSTOKEN, "");
repositories.add(Repository.from(id, url, repositoryUsername, repositoryPassword));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import io.ballerina.projects.environment.Environment;
import io.ballerina.projects.internal.SettingsBuilder;
import io.ballerina.projects.internal.model.Repository;
import io.ballerina.projects.internal.repositories.CustomPackageRepository;
import io.ballerina.projects.internal.repositories.CustomPkgRepositoryContainer;
import io.ballerina.projects.internal.repositories.LocalPackageRepository;
import io.ballerina.projects.internal.repositories.MavenPackageRepository;
Expand Down Expand Up @@ -34,7 +33,7 @@ public final class BallerinaUserHome {
private final Path ballerinaUserHomeDirPath;
private final RemotePackageRepository remotePackageRepository;
private final LocalPackageRepository localPackageRepository;
private final Map<String, CustomPackageRepository> customRepositories;
private final Map<String, MavenPackageRepository> mavenCustomRepositories;

private BallerinaUserHome(Environment environment, Path ballerinaUserHomeDirPath) {
this.ballerinaUserHomeDirPath = ballerinaUserHomeDirPath;
Expand All @@ -53,11 +52,11 @@ private BallerinaUserHome(Environment environment, Path ballerinaUserHomeDirPath
this.remotePackageRepository = RemotePackageRepository
.from(environment, remotePackageRepositoryPath, readSettings());
this.localPackageRepository = createLocalRepository(environment);
this.customRepositories = createCustomRepositories(environment);
this.mavenCustomRepositories = createMavenCustomRepositories(environment);
}

private Map<String, CustomPackageRepository> createCustomRepositories(Environment environment) {
Map<String, CustomPackageRepository> customRepositories = new HashMap<>();
private Map<String, MavenPackageRepository> createMavenCustomRepositories(Environment environment) {
Map<String, MavenPackageRepository> customRepositories = new HashMap<>();
Repository[] repositories = readSettings().getRepositories();
for (Repository repository : repositories) {
Path repositoryPath = ballerinaUserHomeDirPath.resolve(ProjectConstants.REPOSITORIES_DIR)
Expand Down Expand Up @@ -95,12 +94,12 @@ public RemotePackageRepository remotePackageRepository() {
return this.remotePackageRepository;
}

public Map<String, CustomPackageRepository> customRepositories() {
return this.customRepositories;
public Map<String, MavenPackageRepository> customRepositories() {
return this.mavenCustomRepositories;
}

public CustomPkgRepositoryContainer customPkgRepositoryContainer() {
return new CustomPkgRepositoryContainer(customRepositories);
return new CustomPkgRepositoryContainer(mavenCustomRepositories);
}

public LocalPackageRepository localPackageRepository() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://wso2.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.ballerina.projects.internal.model;

/**
* Represents the repository object.
*
* @since 2201.8.0
*/
public class Repository {
private String id;
private String url;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://wso2.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.ballerina.projects.internal.repositories;

import java.util.Map;

/**
* This class acts as a custom package repositories' container.
*
* @since 2201.8.0
*/

public class CustomPkgRepositoryContainer {
Map<String, CustomPackageRepository> customPackageRepositories;
public CustomPkgRepositoryContainer(Map<String, CustomPackageRepository> customPackageRepositories) {
Map<String, MavenPackageRepository> customPackageRepositories;
public CustomPkgRepositoryContainer(Map<String, MavenPackageRepository> customPackageRepositories) {
this.customPackageRepositories = customPackageRepositories;
}

public Map<String, CustomPackageRepository> getCustomPackageRepositories() {
public Map<String, MavenPackageRepository> getCustomPackageRepositories() {
return customPackageRepositories;
}
}

This file was deleted.

Loading

0 comments on commit f64f5c6

Please sign in to comment.