|
| 1 | +/* |
| 2 | + * Copyright 2019-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.gradle.github.changelog; |
| 18 | + |
| 19 | +import org.gradle.api.Action; |
| 20 | +import org.gradle.api.Plugin; |
| 21 | +import org.gradle.api.Project; |
| 22 | +import org.gradle.api.Task; |
| 23 | +import org.gradle.api.artifacts.Configuration; |
| 24 | +import org.gradle.api.artifacts.DependencySet; |
| 25 | +import org.gradle.api.artifacts.repositories.ExclusiveContentRepository; |
| 26 | +import org.gradle.api.artifacts.repositories.InclusiveRepositoryContentDescriptor; |
| 27 | +import org.gradle.api.artifacts.repositories.IvyArtifactRepository; |
| 28 | +import org.gradle.api.artifacts.repositories.IvyPatternRepositoryLayout; |
| 29 | +import org.gradle.api.tasks.JavaExec; |
| 30 | + |
| 31 | +import java.io.File; |
| 32 | +import java.nio.file.Paths; |
| 33 | + |
| 34 | +public class GitHubChangelogPlugin implements Plugin<Project> { |
| 35 | + |
| 36 | + public static final String CHANGELOG_GENERATOR_CONFIGURATION_NAME = "changelogGenerator"; |
| 37 | + |
| 38 | + @Override |
| 39 | + public void apply(Project project) { |
| 40 | + createRepository(project); |
| 41 | + createChangelogGeneratorConfiguration(project); |
| 42 | + project.getTasks().register("generateChangelog", JavaExec.class, new Action<JavaExec>() { |
| 43 | + @Override |
| 44 | + public void execute(JavaExec generateChangelog) { |
| 45 | + File outputFile = project.file(Paths.get(project.getBuildDir().getPath(), "changelog/release-notes.md")); |
| 46 | + outputFile.getParentFile().mkdirs(); |
| 47 | + generateChangelog.setGroup("Release"); |
| 48 | + generateChangelog.setDescription("Generates the changelog"); |
| 49 | + generateChangelog.setWorkingDir(project.getRootDir()); |
| 50 | + generateChangelog.classpath(project.getConfigurations().getAt(CHANGELOG_GENERATOR_CONFIGURATION_NAME)); |
| 51 | + generateChangelog.doFirst(new Action<Task>() { |
| 52 | + @Override |
| 53 | + public void execute(Task task) { |
| 54 | + generateChangelog.args("--spring.config.location=scripts/release/release-notes-sections.yml", project.property("nextVersion"), outputFile.toString()); |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + private void createChangelogGeneratorConfiguration(Project project) { |
| 62 | + project.getConfigurations().create(CHANGELOG_GENERATOR_CONFIGURATION_NAME, new Action<Configuration>() { |
| 63 | + @Override |
| 64 | + public void execute(Configuration configuration) { |
| 65 | + configuration.defaultDependencies(new Action<DependencySet>() { |
| 66 | + @Override |
| 67 | + public void execute(DependencySet dependencies) { |
| 68 | + dependencies.add(project.getDependencies().create("spring-io:github-changelog-generator:0.0.6")); |
| 69 | + } |
| 70 | + }); |
| 71 | + } |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + private void createRepository(Project project) { |
| 76 | + IvyArtifactRepository repository = project.getRepositories().ivy(new Action<IvyArtifactRepository>() { |
| 77 | + @Override |
| 78 | + public void execute(IvyArtifactRepository repository) { |
| 79 | + repository.setUrl("https://github.com/"); |
| 80 | + repository.patternLayout(new Action<IvyPatternRepositoryLayout>() { |
| 81 | + @Override |
| 82 | + public void execute(IvyPatternRepositoryLayout layout) { |
| 83 | + layout.artifact("[organization]/[artifact]/releases/download/v[revision]/[artifact].[ext]"); |
| 84 | + } |
| 85 | + }); |
| 86 | + repository.getMetadataSources().artifact(); |
| 87 | + } |
| 88 | + }); |
| 89 | + project.getRepositories().exclusiveContent(new Action<ExclusiveContentRepository>() { |
| 90 | + @Override |
| 91 | + public void execute(ExclusiveContentRepository exclusiveContentRepository) { |
| 92 | + exclusiveContentRepository.forRepositories(repository); |
| 93 | + exclusiveContentRepository.filter(new Action<InclusiveRepositoryContentDescriptor>() { |
| 94 | + @Override |
| 95 | + public void execute(InclusiveRepositoryContentDescriptor descriptor) { |
| 96 | + descriptor.includeGroup("spring-io"); |
| 97 | + } |
| 98 | + }); |
| 99 | + } |
| 100 | + }); |
| 101 | + } |
| 102 | +} |
0 commit comments