Skip to content

Commit 45e98d8

Browse files
JudeRVjdaugherty
authored andcommitted
feedback: cleanup create release drop down task to cache correctly regardless of tag existence
1 parent 0634ed4 commit 45e98d8

File tree

3 files changed

+63
-53
lines changed

3 files changed

+63
-53
lines changed

grails-gradle/docs-core/src/main/groovy/grails/doc/dropdown/CreateReleaseDropDownTask.groovy

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919

2020
package grails.doc.dropdown
2121

22-
import groovy.transform.CompileDynamic
2322
import groovy.transform.CompileStatic
2423
import org.gradle.api.DefaultTask
2524
import org.gradle.api.GradleException
2625
import org.gradle.api.Project
2726
import org.gradle.api.file.ConfigurableFileCollection
2827
import org.gradle.api.file.DirectoryProperty
28+
import org.gradle.api.file.RegularFileProperty
2929
import org.gradle.api.model.ObjectFactory
3030
import org.gradle.api.provider.Property
3131
import org.gradle.api.tasks.*
@@ -45,50 +45,65 @@ abstract class CreateReleaseDropDownTask extends DefaultTask {
4545
private static final String GRAILS_DOC_BASE_URL = "https://docs.grails.org"
4646

4747
@Input
48-
final Property<String> slug
48+
final Property<String> githubSlug
4949

5050
@InputDirectory
5151
@PathSensitive(PathSensitivity.RELATIVE)
52-
final DirectoryProperty docsDirectory
52+
final DirectoryProperty sourceDocsDirectory
53+
54+
@InputFile
55+
@PathSensitive(PathSensitivity.RELATIVE)
56+
final RegularFileProperty gitTags
57+
58+
@Input
59+
final Property<String> projectVersion
5360

5461
@Input
55-
final Property<String> version
62+
final Property<SoftwareVersion> minimumVersion
5663

5764
@InputFiles
5865
@PathSensitive(PathSensitivity.RELATIVE)
59-
final ConfigurableFileCollection inputFiles
66+
final ConfigurableFileCollection filesToAddDropdowns
6067

6168
@OutputDirectory
62-
final DirectoryProperty outputDir
69+
final DirectoryProperty modifiedPagesDirectory
6370

6471
@Inject
6572
CreateReleaseDropDownTask(ObjectFactory objects, Project project) {
66-
slug = objects.property(String).convention('apache/grails-doc')
67-
docsDirectory = objects.directoryProperty().convention(project.layout.buildDirectory.dir('manual'))
68-
version = objects.property(String).convention(project.provider { project.version.toString() })
69-
inputFiles = objects.fileCollection()
70-
outputDir = objects.directoryProperty().convention(project.layout.buildDirectory.dir("modified-pages"))
7173
group = 'documentation'
74+
githubSlug = objects.property(String).convention(project.provider {
75+
project.findProperty('githubSlug') as String ?: 'apache/grails-doc'
76+
})
77+
sourceDocsDirectory = objects.directoryProperty().convention(project.layout.buildDirectory.dir('manual'))
78+
projectVersion = objects.property(String).convention(project.provider { project.version as String })
79+
filesToAddDropdowns = objects.fileCollection()
80+
modifiedPagesDirectory = objects.directoryProperty().convention(project.layout.buildDirectory.dir("modified-pages"))
81+
gitTags = objects.fileProperty().convention(project.layout.buildDirectory.file('git-tags.txt'))
82+
minimumVersion = objects.property(SoftwareVersion).convention(new SoftwareVersion(major: 5))
7283
}
7384

7485
/**
75-
* Add the release dropdown to the documentation
76-
*/
86+
* Add the release dropdown to the documentation*/
7787
@TaskAction
7888
void createReleaseDropDown() {
79-
String projectVersion = version.get()
80-
SoftwareVersion minimumVersion = new SoftwareVersion(major: 5)
89+
Path targetOutputDirectory = modifiedPagesDirectory.get().asFile.toPath()
90+
if (Files.exists(targetOutputDirectory)) {
91+
targetOutputDirectory.deleteDir()
92+
}
93+
Files.createDirectories(targetOutputDirectory)
94+
95+
String projectVersion = projectVersion.get()
8196

82-
final List<String> result = listRepoTags()
83-
List<SoftwareVersion> softwareVersions = parseSoftwareVersions(result, minimumVersion)
97+
final List<String> result = gitTags.get().asFile.readLines()*.trim()
98+
List<SoftwareVersion> softwareVersions = parseSoftwareVersions(projectVersion, result)
8499
logger.lifecycle("Detected Project Version: ${projectVersion} and Software Versions: ${softwareVersions*.versionText.join(',')}")
85100

86101
final String versionHtml = "<p><strong>Version:</strong> ${projectVersion}</p>"
87-
Path guideDirectory = docsDirectory.get().asFile.toPath()
88-
Path targetOutputDirectory = outputDir.get().asFile.toPath()
102+
Path guideDirectory = sourceDocsDirectory.get().asFile.toPath()
89103

90-
Map<String, Path> filesToChange = inputFiles.collectEntries { [it.absolutePath, it.toPath()] }
104+
Map<String, Path> filesToAddDropdown = filesToAddDropdowns.collectEntries { [it.absolutePath, it.toPath()] }
91105
Files.walkFileTree(guideDirectory, new SimpleFileVisitor<Path>() {
106+
92107
@Override
93108
FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
94109
Path targetDir = targetOutputDirectory.resolve(guideDirectory.relativize(dir))
@@ -101,23 +116,16 @@ abstract class CreateReleaseDropDownTask extends DefaultTask {
101116
@Override
102117
FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
103118
Path targetFile = targetOutputDirectory.resolve(guideDirectory.relativize(file))
104-
if (Files.exists(targetFile)) {
105-
Files.deleteIfExists(targetFile)
106-
}
107-
108-
109119
String absolutePath = targetFile.toAbsolutePath().toString()
110-
if (filesToChange.containsKey(absolutePath)) {
111-
//Need to add the version dropdown
112-
String page = guideDirectory.toFile().relativePath(file.toFile())
113-
String selectHtml = select(options(projectVersion, page, softwareVersions))
120+
if (filesToAddDropdown.containsKey(absolutePath)) {
121+
String pageRelativePath = guideDirectory.toFile().relativePath(file.toFile())
122+
String selectHtml = select(options(projectVersion, pageRelativePath, softwareVersions))
114123

115124
final String versionWithSelectHtml = "<p><strong>Version:</strong>&nbsp;<span style='width:100px;display:inline-block;'>${selectHtml}</span></p>"
116125
targetFile.toFile().text = file.text.replace(versionHtml, versionWithSelectHtml)
117126

118-
filesToChange.remove(absolutePath)
119-
}
120-
else {
127+
filesToAddDropdown.remove(absolutePath)
128+
} else {
121129
Files.copy(file, targetFile, StandardCopyOption.REPLACE_EXISTING)
122130
}
123131
FileVisitResult.CONTINUE
@@ -134,25 +142,25 @@ abstract class CreateReleaseDropDownTask extends DefaultTask {
134142
* Generate the options for the select tag.
135143
*
136144
* @param version The current version of the documentation
137-
* @param page The page to add the dropdown to
145+
* @param pageRelativePath The relative path for the page to add the dropdown to
138146
* @param softwareVersions The list of software versions to include in the dropdown
139147
* @return The list of options for the select tag
140148
*/
141-
private List<String> options(String version, String page, List<SoftwareVersion> softwareVersions) {
149+
private List<String> options(String version, String pageRelativePath, List<SoftwareVersion> softwareVersions) {
142150
List<String> options = []
143-
final String snapshotHref = GRAILS_DOC_BASE_URL + "/snapshot" + page
151+
final String snapshotHref = GRAILS_DOC_BASE_URL + "/snapshot" + pageRelativePath
144152
options << option(snapshotHref, "SNAPSHOT", version.endsWith("-SNAPSHOT"))
145153

146154
softwareVersions
147155
.forEach { softwareVersion ->
148156
final String versionName = softwareVersion?.versionText
149-
final String href = GRAILS_DOC_BASE_URL + "/" + versionName + "/" + page
157+
final String href = GRAILS_DOC_BASE_URL + "/" + versionName + "/" + pageRelativePath
150158
options << option(href, versionName, version == versionName)
151159
}
160+
152161
options
153162
}
154163

155-
156164
/**
157165
* Generate the select tag
158166
*
@@ -161,8 +169,7 @@ abstract class CreateReleaseDropDownTask extends DefaultTask {
161169
*/
162170
private String select(List<String> options) {
163171
String selectHtml = "<select onChange='window.document.location.href=this.options[this.selectedIndex].value;'>"
164-
options.each { option ->
165-
selectHtml += option
172+
options.each { option -> selectHtml += option
166173
}
167174
selectHtml += '</select>'
168175
selectHtml
@@ -192,13 +199,16 @@ abstract class CreateReleaseDropDownTask extends DefaultTask {
192199
* @param minimumVersion Minimum SoftwareVersion to include in the list. Default version is 0.0.0
193200
* @return The list of software versions
194201
*/
195-
@CompileDynamic
196-
private List<SoftwareVersion> parseSoftwareVersions(List<String> tags, SoftwareVersion minimumVersion = SoftwareVersion.build('0.0.0')) {
202+
private List<SoftwareVersion> parseSoftwareVersions(String projectVersion, List<String> tags) {
203+
def minimum = minimumVersion.get()
204+
205+
LinkedHashSet<String> combined = ["v${projectVersion}" as String]
206+
combined.addAll(tags)
197207

198-
tags.findAll { it?.startsWith('v') }
208+
combined.findAll { it?.startsWith('v') }
199209
.collect { it.replace('v', '') }
200210
.collect { SoftwareVersion.build(it) }
201-
.findAll {it >= minimumVersion}
211+
.findAll { it >= minimum }
202212
.toSorted()
203213
.unique()
204214
.reverse()

grails-gradle/docs-core/src/main/groovy/grails/doc/dropdown/SoftwareVersion.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
package grails.doc.dropdown
2121

22-
class SoftwareVersion implements Comparable<SoftwareVersion> {
22+
class SoftwareVersion implements Comparable<SoftwareVersion>, Serializable {
23+
private static final long serialVersionUID = 1L;
2324

2425
int major
2526
int minor

grails-gradle/docs-core/src/main/groovy/grails/doc/gradle/PublishGuide.groovy renamed to grails-gradle/docs-core/src/main/groovy/grails/doc/gradle/PublishGuideTask.groovy

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ import org.gradle.api.AntBuilder
3232
import org.gradle.api.tasks.*
3333

3434
import javax.inject.Inject
35+
import java.nio.file.Files
3536

3637
/**
3738
* Gradle task for generating a gdoc-based HTML user guide.
3839
*/
3940
@CacheableTask
40-
class PublishGuide extends DefaultTask {
41+
class PublishGuideTask extends DefaultTask {
4142

4243
@Optional
4344
@Input
@@ -64,11 +65,6 @@ class PublishGuide extends DefaultTask {
6465
@PathSensitive(PathSensitivity.RELATIVE)
6566
final DirectoryProperty sourceDir
6667

67-
@Optional
68-
@InputDirectory
69-
@PathSensitive(PathSensitivity.RELATIVE)
70-
final DirectoryProperty workDir
71-
7268
@Optional
7369
@InputDirectory
7470
@PathSensitive(PathSensitivity.RELATIVE)
@@ -84,15 +80,14 @@ class PublishGuide extends DefaultTask {
8480
private final AntBuilder ant
8581

8682
@Inject
87-
PublishGuide(ObjectFactory objects, Project project) {
83+
PublishGuideTask(ObjectFactory objects, Project project) {
8884
this.ant = project.ant
8985
language = objects.property(String).convention(null as String)
9086
sourceRepo = objects.property(String)
9187
properties = objects.mapProperty(String, Object).convention([:])
9288
asciidoc = objects.property(Boolean).convention(true)
9389
propertiesFiles = objects.fileCollection()
9490
sourceDir = objects.directoryProperty().convention(project.layout.projectDirectory.dir("src"))
95-
workDir = objects.directoryProperty().convention(project.layout.buildDirectory)
9691
resourcesDir = objects.directoryProperty().convention(project.layout.projectDirectory.dir("resources"))
9792
macros = objects.listProperty(Object).convention([])
9893
targetDir = objects.directoryProperty().convention(project.layout.buildDirectory.dir("docs"))
@@ -103,6 +98,8 @@ class PublishGuide extends DefaultTask {
10398
def publishGuide() {
10499
Properties combinedProperties = new Properties()
105100

101+
File workingDir = Files.createTempDirectory('grails-doc-publish-guide').toFile()
102+
106103
File resources = resourcesDir.get().asFile
107104
File docProperties = new File(resources, 'doc.properties')
108105
if(docProperties.exists()) {
@@ -126,7 +123,7 @@ class PublishGuide extends DefaultTask {
126123
def publisher = new DocPublisher(sourceDir.get().asFile, apiDir)
127124
publisher.ant = ant
128125
publisher.asciidoc = asciidoc
129-
publisher.workDir = workDir.get().asFile
126+
publisher.workDir = workingDir
130127
publisher.apiDir = apiDir
131128
publisher.language = language.getOrElse('')
132129
publisher.sourceRepo = sourceRepo.getOrElse('')
@@ -170,6 +167,8 @@ class PublishGuide extends DefaultTask {
170167

171168
// Restore the old context class loader.
172169
Thread.currentThread().contextClassLoader = oldClassLoader
170+
171+
workingDir.deleteDir()
173172
}
174173
}
175174

0 commit comments

Comments
 (0)