software.amazon.smithy.lsp.ext.model.SmithyBuildExtensions Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of smithy-language-server Show documentation
Show all versions of smithy-language-server Show documentation
Language Server Protocol implementation for Smithy
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 software.amazon.smithy.lsp.ext.model;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import software.amazon.smithy.lsp.ext.LspLog;
import software.amazon.smithy.utils.ListUtils;
import software.amazon.smithy.utils.SmithyBuilder;
import software.amazon.smithy.utils.ToSmithyBuilder;
public final class SmithyBuildExtensions implements ToSmithyBuilder {
private final List imports;
private final List mavenRepositories;
private final List mavenDependencies;
private final MavenConfig maven;
private SmithyBuildExtensions(Builder b) {
this.mavenDependencies = ListUtils.copyOf(b.mavenDependencies);
this.mavenRepositories = ListUtils.copyOf(b.mavenRepositories);
this.imports = ListUtils.copyOf(b.imports);
this.maven = b.maven;
}
public List getImports() {
return imports;
}
public MavenConfig getMavenConfig() {
return maven;
}
public static Builder builder() {
return new Builder();
}
@Override
public SmithyBuilder toBuilder() {
return builder()
.mavenDependencies(mavenDependencies)
.mavenRepositories(mavenRepositories)
.maven(maven);
}
public static final class Builder implements SmithyBuilder {
private final List mavenRepositories = new ArrayList<>();
private final List mavenDependencies = new ArrayList<>();
private final List imports = new ArrayList<>();
private MavenConfig maven = MavenConfig.builder().build();
@Override
public SmithyBuildExtensions build() {
return new SmithyBuildExtensions(this);
}
/**
* Adds configuration from other instance to this builder.
*
* @param other configuration
* @return builder
*/
public Builder merge(SmithyBuildExtensions other) {
MavenConfig.Builder mavenConfigBuilder = maven.toBuilder();
List dependencies = new ArrayList<>(maven.getDependencies());
// Merge dependencies from other extension, preferring those defined on MavenConfig.
if (other.getMavenConfig().getDependencies().isEmpty()) {
dependencies.addAll(other.mavenDependencies);
} else {
dependencies.addAll(other.getMavenConfig().getDependencies());
}
mavenConfigBuilder.dependencies(dependencies);
List repositories = new ArrayList<>(maven.getRepositories());
// Merge repositories from other extension, preferring those defined on MavenConfig.
if (other.getMavenConfig().getRepositories().isEmpty()) {
repositories.addAll(other.mavenRepositories.stream()
.map(repo -> MavenRepository.builder().url(repo).build())
.collect(Collectors.toList()));
} else {
repositories.addAll(other.maven.getRepositories());
}
mavenConfigBuilder.repositories(repositories);
maven = mavenConfigBuilder.build();
imports.addAll(other.imports);
return this;
}
/**
* @deprecated Use {@link MavenConfig.Builder#repositories(Collection)}
*
* Adds resolvers to the builder.
*
* @param mavenRepositories list of maven-compatible repositories
* @return builder
*/
@Deprecated
public Builder mavenRepositories(Collection mavenRepositories) {
MavenConfig config = maven;
// If repositories have not been set on current config, set from mavenRepositories.
if (config.getRepositories().isEmpty()) {
config = config.toBuilder()
.repositories(mavenRepositories.stream()
.map(repo -> MavenRepository.builder().url(repo).build())
.collect(Collectors.toList()))
.build();
LspLog.println("Read deprecated `mavenRepositories` in smithy-build.json. Update smithy-build.json to "
+ "{\"maven\": {\"repositories\": [{\"url\": \"repo url\"}]}}");
}
this.maven = config;
return this;
}
/**
* @deprecated use {@link MavenConfig.Builder#dependencies(Collection)}
*
* Adds dependencies to the builder.
*
* @param mavenDependencies list of artifacts in the org:name:version format
* @return builder
*/
@Deprecated
public Builder mavenDependencies(Collection mavenDependencies) {
MavenConfig config = maven;
// If dependencies have not been set on current config, set from mavenDependencies.
if (config.getDependencies().isEmpty()) {
config = config.toBuilder()
.dependencies(mavenDependencies)
.build();
LspLog.println("Read deprecated `mavenDependencies` in smithy-build.json. Update smithy-build.json to "
+ "{\"maven\": {\"dependencies\": [\"dependencyA\", \"dependencyB\"]}}");
}
this.maven = config;
return this;
}
/**
* Adds a Maven configuration to the builder.
* @param config Maven configuration containing dependencies and repositories.
* @return builder
*/
public Builder maven(MavenConfig config) {
this.maven = MavenConfig.builder()
.dependencies(config.getDependencies())
.repositories(config.getRepositories())
.build();
return this;
}
/**
* Adds imports to the builder.
*
* @param imports list of imports (relative folders of smithy files)
* @return builder
*/
public Builder imports(Collection imports) {
this.imports.clear();
this.imports.addAll(imports);
return this;
}
/**
* Adds import to the builder.
*
* @param imp import to add
* @return builder
*/
public Builder addImport(String imp) {
this.imports.add(imp);
return this;
}
}
@Override
public String toString() {
return "SmithyBuildExtensions(repositories = " + maven.getRepositories().toString() + ", artifacts = "
+ maven.getDependencies().toString() + ", imports = " + imports + ")";
}
}