19
19
20
20
package grails.doc.dropdown
21
21
22
- import groovy.transform.CompileDynamic
23
22
import groovy.transform.CompileStatic
24
23
import org.gradle.api.DefaultTask
25
24
import org.gradle.api.GradleException
26
25
import org.gradle.api.Project
27
26
import org.gradle.api.file.ConfigurableFileCollection
28
27
import org.gradle.api.file.DirectoryProperty
28
+ import org.gradle.api.file.RegularFileProperty
29
29
import org.gradle.api.model.ObjectFactory
30
30
import org.gradle.api.provider.Property
31
31
import org.gradle.api.tasks.*
@@ -45,50 +45,65 @@ abstract class CreateReleaseDropDownTask extends DefaultTask {
45
45
private static final String GRAILS_DOC_BASE_URL = " https://docs.grails.org"
46
46
47
47
@Input
48
- final Property<String > slug
48
+ final Property<String > githubSlug
49
49
50
50
@InputDirectory
51
51
@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
53
60
54
61
@Input
55
- final Property<String > version
62
+ final Property<SoftwareVersion > minimumVersion
56
63
57
64
@InputFiles
58
65
@PathSensitive (PathSensitivity .RELATIVE )
59
- final ConfigurableFileCollection inputFiles
66
+ final ConfigurableFileCollection filesToAddDropdowns
60
67
61
68
@OutputDirectory
62
- final DirectoryProperty outputDir
69
+ final DirectoryProperty modifiedPagesDirectory
63
70
64
71
@Inject
65
72
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" ))
71
73
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 ))
72
83
}
73
84
74
85
/**
75
- * Add the release dropdown to the documentation
76
- */
86
+ * Add the release dropdown to the documentation*/
77
87
@TaskAction
78
88
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()
81
96
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 )
84
99
logger. lifecycle(" Detected Project Version: ${ projectVersion} and Software Versions: ${ softwareVersions*.versionText.join(',')} " )
85
100
86
101
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()
89
103
90
- Map<String , Path > filesToChange = inputFiles . collectEntries { [it. absolutePath, it. toPath()] }
104
+ Map<String , Path > filesToAddDropdown = filesToAddDropdowns . collectEntries { [it. absolutePath, it. toPath()] }
91
105
Files . walkFileTree(guideDirectory, new SimpleFileVisitor<Path > () {
106
+
92
107
@Override
93
108
FileVisitResult preVisitDirectory (Path dir , BasicFileAttributes attrs ) throws IOException {
94
109
Path targetDir = targetOutputDirectory. resolve(guideDirectory. relativize(dir))
@@ -101,23 +116,16 @@ abstract class CreateReleaseDropDownTask extends DefaultTask {
101
116
@Override
102
117
FileVisitResult visitFile (Path file , BasicFileAttributes attrs ) throws IOException {
103
118
Path targetFile = targetOutputDirectory. resolve(guideDirectory. relativize(file))
104
- if (Files . exists(targetFile)) {
105
- Files . deleteIfExists(targetFile)
106
- }
107
-
108
-
109
119
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))
114
123
115
124
final String versionWithSelectHtml = " <p><strong>Version:</strong> <span style='width:100px;display:inline-block;'>${ selectHtml} </span></p>"
116
125
targetFile. toFile(). text = file. text. replace(versionHtml, versionWithSelectHtml)
117
126
118
- filesToChange. remove(absolutePath)
119
- }
120
- else {
127
+ filesToAddDropdown. remove(absolutePath)
128
+ } else {
121
129
Files . copy(file, targetFile, StandardCopyOption . REPLACE_EXISTING )
122
130
}
123
131
FileVisitResult . CONTINUE
@@ -134,25 +142,25 @@ abstract class CreateReleaseDropDownTask extends DefaultTask {
134
142
* Generate the options for the select tag.
135
143
*
136
144
* @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
138
146
* @param softwareVersions The list of software versions to include in the dropdown
139
147
* @return The list of options for the select tag
140
148
*/
141
- private List<String > options (String version , String page , List<SoftwareVersion > softwareVersions ) {
149
+ private List<String > options (String version , String pageRelativePath , List<SoftwareVersion > softwareVersions ) {
142
150
List<String > options = []
143
- final String snapshotHref = GRAILS_DOC_BASE_URL + " /snapshot" + page
151
+ final String snapshotHref = GRAILS_DOC_BASE_URL + " /snapshot" + pageRelativePath
144
152
options << option(snapshotHref, " SNAPSHOT" , version. endsWith(" -SNAPSHOT" ))
145
153
146
154
softwareVersions
147
155
.forEach { softwareVersion ->
148
156
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
150
158
options << option(href, versionName, version == versionName)
151
159
}
160
+
152
161
options
153
162
}
154
163
155
-
156
164
/**
157
165
* Generate the select tag
158
166
*
@@ -161,8 +169,7 @@ abstract class CreateReleaseDropDownTask extends DefaultTask {
161
169
*/
162
170
private String select (List<String > options ) {
163
171
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
166
173
}
167
174
selectHtml + = ' </select>'
168
175
selectHtml
@@ -192,13 +199,16 @@ abstract class CreateReleaseDropDownTask extends DefaultTask {
192
199
* @param minimumVersion Minimum SoftwareVersion to include in the list. Default version is 0.0.0
193
200
* @return The list of software versions
194
201
*/
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)
197
207
198
- tags . findAll { it?. startsWith(' v' ) }
208
+ combined . findAll { it?. startsWith(' v' ) }
199
209
.collect { it. replace(' v' , ' ' ) }
200
210
.collect { SoftwareVersion . build(it) }
201
- .findAll {it >= minimumVersion }
211
+ .findAll { it >= minimum }
202
212
.toSorted()
203
213
.unique()
204
214
.reverse()
0 commit comments