-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Plugin to create a maven repo list in the .ideas folder (#1233)
- Loading branch information
Showing
10 changed files
with
260 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type: feature | ||
feature: | ||
description: Plugin to create a maven repo list in the .ideas folder | ||
links: | ||
- https://github.com/palantir/gradle-consistent-versions/pull/1233 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/main/java/com/palantir/gradle/versions/GenerateMavenRepositoriesTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* (c) Copyright 2024 Palantir Technologies Inc. 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. | ||
* 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 com.palantir.gradle.versions; | ||
|
||
import com.fasterxml.jackson.annotation.JsonRootName; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
import com.fasterxml.jackson.dataformat.xml.XmlMapper; | ||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; | ||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; | ||
import com.fasterxml.jackson.datatype.guava.GuavaModule; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.gradle.api.DefaultTask; | ||
import org.gradle.api.file.RegularFileProperty; | ||
import org.gradle.api.provider.SetProperty; | ||
import org.gradle.api.tasks.Input; | ||
import org.gradle.api.tasks.OutputFile; | ||
import org.gradle.api.tasks.TaskAction; | ||
import org.immutables.value.Value; | ||
|
||
public abstract class GenerateMavenRepositoriesTask extends DefaultTask { | ||
|
||
private static final ObjectMapper XML_MAPPER = new XmlMapper().registerModule(new GuavaModule()); | ||
|
||
private static final String MAVEN_REPOSITORIES_FILE_NAME = ".idea/gcv-maven-repositories.xml"; | ||
|
||
@Input | ||
public abstract SetProperty<String> getMavenRepositories(); | ||
|
||
@OutputFile | ||
public abstract RegularFileProperty getOutputFile(); | ||
|
||
public GenerateMavenRepositoriesTask() { | ||
getOutputFile().set(getProject().file(MAVEN_REPOSITORIES_FILE_NAME)); | ||
} | ||
|
||
@TaskAction | ||
final void action() { | ||
writeRepositoriesToXml(); | ||
} | ||
|
||
private void writeRepositoriesToXml() { | ||
File file = getOutputFile().get().getAsFile(); | ||
List<RepositoryConfig> repositories = getMavenRepositories().get().stream() | ||
.map(ImmutableRepositoryConfig::of) | ||
.collect(Collectors.toList()); | ||
Repositories wrapped = ImmutableRepositories.of(repositories); | ||
|
||
try { | ||
XML_MAPPER.writerWithDefaultPrettyPrinter().writeValue(file, wrapped); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Value.Immutable | ||
@JsonDeserialize(as = ImmutableRepositoryConfig.class) | ||
@JsonSerialize(as = ImmutableRepositoryConfig.class) | ||
interface RepositoryConfig { | ||
|
||
@Value.Parameter | ||
@JacksonXmlProperty(isAttribute = true) | ||
String url(); | ||
} | ||
|
||
@Value.Immutable | ||
@JsonDeserialize(as = ImmutableRepositories.class) | ||
@JsonSerialize(as = ImmutableRepositories.class) | ||
@JsonRootName("repositories") | ||
interface Repositories { | ||
|
||
@Value.Parameter | ||
@JacksonXmlElementWrapper(useWrapping = false) | ||
@JacksonXmlProperty(localName = "repository") | ||
List<RepositoryConfig> repositories(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/com/palantir/gradle/versions/VersionsPropsIdeaPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* (c) Copyright 2024 Palantir Technologies Inc. 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. | ||
* 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 com.palantir.gradle.versions; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.gradle.StartParameter; | ||
import org.gradle.api.Plugin; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.artifacts.repositories.MavenArtifactRepository; | ||
import org.gradle.api.tasks.TaskProvider; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public final class VersionsPropsIdeaPlugin implements Plugin<Project> { | ||
private static final Logger log = LoggerFactory.getLogger(VersionsPropsIdeaPlugin.class); | ||
|
||
@Override | ||
public void apply(Project project) { | ||
|
||
if (!Boolean.getBoolean("idea.active")) { | ||
return; | ||
} | ||
|
||
TaskProvider<GenerateMavenRepositoriesTask> writeMavenRepositories = project.getTasks() | ||
.register("writeMavenRepositories", GenerateMavenRepositoriesTask.class, task -> { | ||
task.getMavenRepositories().set(project.provider(() -> project.getRepositories().stream() | ||
.filter(repo -> repo instanceof MavenArtifactRepository) | ||
.map(repo -> | ||
((MavenArtifactRepository) repo).getUrl().toString()) | ||
.map(url -> url.endsWith("/") ? url : url + "/") | ||
.collect(Collectors.toSet()))); | ||
}); | ||
|
||
StartParameter startParameter = project.getGradle().getStartParameter(); | ||
List<String> taskNames = startParameter.getTaskNames(); | ||
taskNames.add(String.format(":%s", writeMavenRepositories.getName())); | ||
startParameter.setTaskNames(taskNames); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/test/groovy/com/palantir/gradle/versions/VersionPropsIdeaPluginIntegrationSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* (c) Copyright 2024 Palantir Technologies Inc. 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. | ||
* 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 com.palantir.gradle.versions | ||
|
||
import com.fasterxml.jackson.dataformat.xml.XmlMapper | ||
import com.fasterxml.jackson.datatype.guava.GuavaModule | ||
import nebula.test.IntegrationSpec | ||
import spock.util.environment.RestoreSystemProperties | ||
|
||
import java.util.stream.Collectors | ||
|
||
class VersionPropsIdeaPluginIntegrationSpec extends IntegrationSpec { | ||
|
||
def setup() { | ||
//language=gradle | ||
buildFile << """ | ||
repositories { | ||
maven { | ||
url 'https://test' | ||
} | ||
maven { | ||
url 'https://demo/' | ||
} | ||
mavenCentral() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } } | ||
} | ||
apply plugin: 'com.palantir.version-props-idea' | ||
apply plugin: 'idea' | ||
""".stripIndent(true) | ||
|
||
def ideaDir = new File(projectDir, '.idea') | ||
ideaDir.mkdirs() | ||
} | ||
|
||
def "plugin creates gcv-maven-repositories.xml file in .idea folder"() { | ||
when: 'we run the first time' | ||
runTasksSuccessfully('-Didea.active=true') | ||
|
||
then: 'we generate the correct config' | ||
def repoFile = new File(projectDir, '.idea/gcv-maven-repositories.xml') | ||
repoFile.exists() | ||
|
||
// language=xml | ||
def expectedXml = ''' | ||
<repositories> | ||
<repository url="https://test/"/> | ||
<repository url="https://repo.maven.apache.org/maven2/"/> | ||
<repository url="https://demo/"/> | ||
</repositories> | ||
'''.stripIndent(true).trim() | ||
|
||
def projectNode = new XmlParser().parse(repoFile) | ||
nodeToXmlString(projectNode) == expectedXml | ||
|
||
when: 'we run the second time' | ||
def secondRun = runTasksSuccessfully('-Didea.active=true') | ||
|
||
then: "if nothing has changed, the task is then up-to-date" | ||
secondRun.wasUpToDate(":writeMavenRepositories") | ||
} | ||
|
||
private static String nodeToXmlString(debugRunConf) { | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream() | ||
new XmlNodePrinter(new PrintWriter(baos)).print(debugRunConf) | ||
return baos.toString().trim() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters