forked from davidsommer/IconViewer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.gradle.kts
291 lines (262 loc) · 12.9 KB
/
build.gradle.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import com.adarshr.gradle.testlogger.theme.ThemeType
import com.github.benmanes.gradle.versions.reporter.PlainTextReporter
import com.github.benmanes.gradle.versions.reporter.result.Result
import com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask
import com.palantir.gradle.gitversion.VersionDetails
import groovy.lang.Closure
import org.apache.commons.io.FileUtils
import org.jetbrains.changelog.Changelog
import org.w3c.dom.Document
import java.io.BufferedReader
import java.io.ByteArrayInputStream
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.URL
import java.nio.file.Files
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.xpath.XPathConstants
import javax.xml.xpath.XPathFactory
plugins {
id("java")
id("org.jetbrains.intellij") version "1.17.3" // https://github.com/JetBrains/gradle-intellij-plugin
id("org.jetbrains.changelog") version "2.2.1" // https://github.com/JetBrains/gradle-changelog-plugin
id("com.github.ben-manes.versions") version "0.51.0" // https://github.com/ben-manes/gradle-versions-plugin
id("com.adarshr.test-logger") version "4.0.0" // https://github.com/radarsh/gradle-test-logger-plugin
id("com.palantir.git-version") version "3.1.0" // https://github.com/palantir/gradle-git-version
id("com.github.andygoossens.modernizer") version "1.9.3" // https://github.com/andygoossens/gradle-modernizer-plugin
id("biz.lermitage.oga") version "1.1.1" // https://github.com/jonathanlermitage/oga-gradle-plugin
}
// Import variables from gradle.properties file
val pluginDownloadIdeaSources: String by project
val pluginVersion: String by project
val pluginJavaVersion: String by project
val testLoggerStyle: String by project
val pluginEnableDebugLogs: String by project
val pluginClearSandboxedIDESystemLogsBeforeRun: String by project
val pluginIdeaVersion = detectBestIdeVersion()
version = if (pluginVersion == "auto") {
val versionDetails: Closure<VersionDetails> by extra
val lastTag = versionDetails().lastTag
if (lastTag.startsWith("v", ignoreCase = true)) {
lastTag.substring(1)
} else {
lastTag
}
} else {
pluginVersion
}
logger.quiet("Will use IDEA $pluginIdeaVersion and Java $pluginJavaVersion. Plugin version set to $version")
group = "lermitage.intellij.iconviewer"
repositories {
mavenCentral()
}
val twelvemonkeysVersion = "3.12.0"
val jsvgVersion = "1.7.0"
dependencies {
implementation("com.twelvemonkeys.imageio:imageio-core:$twelvemonkeysVersion") // https://github.com/haraldk/TwelveMonkeys/releases
implementation("com.twelvemonkeys.imageio:imageio-bmp:$twelvemonkeysVersion") // https://github.com/haraldk/TwelveMonkeys/wiki/BMP-Plugin
implementation("com.twelvemonkeys.imageio:imageio-hdr:$twelvemonkeysVersion") // https://github.com/haraldk/TwelveMonkeys/wiki/HDR-Plugin
implementation("com.twelvemonkeys.imageio:imageio-icns:$twelvemonkeysVersion") // https://github.com/haraldk/TwelveMonkeys/wiki/ICNS-Plugin
implementation("com.twelvemonkeys.imageio:imageio-iff:$twelvemonkeysVersion") // https://github.com/haraldk/TwelveMonkeys/wiki/IFF-Plugin
implementation("com.twelvemonkeys.imageio:imageio-jpeg:$twelvemonkeysVersion") // https://github.com/haraldk/TwelveMonkeys/wiki/JPEG-Plugin
implementation("com.twelvemonkeys.imageio:imageio-pcx:$twelvemonkeysVersion") // https://github.com/haraldk/TwelveMonkeys/wiki/PCX-Plugin
implementation("com.twelvemonkeys.imageio:imageio-pict:$twelvemonkeysVersion") // https://github.com/haraldk/TwelveMonkeys/wiki/PICT-Plugin
implementation("com.twelvemonkeys.imageio:imageio-pnm:$twelvemonkeysVersion") // https://github.com/haraldk/TwelveMonkeys/wiki/PNM-Plugin
implementation("com.twelvemonkeys.imageio:imageio-psd:$twelvemonkeysVersion") // https://github.com/haraldk/TwelveMonkeys/wiki/PSD-Plugin
implementation("com.twelvemonkeys.imageio:imageio-sgi:$twelvemonkeysVersion") // https://github.com/haraldk/TwelveMonkeys/wiki/SGI-Plugin
implementation("com.twelvemonkeys.imageio:imageio-tga:$twelvemonkeysVersion") // https://github.com/haraldk/TwelveMonkeys/wiki/TGA-Plugin
implementation("com.twelvemonkeys.imageio:imageio-tiff:$twelvemonkeysVersion") // https://github.com/haraldk/TwelveMonkeys/wiki/TIFF-Plugin
implementation("com.github.weisj:jsvg:$jsvgVersion") // https://github.com/weisJ/jsvg
}
intellij {
downloadSources.set(pluginDownloadIdeaSources.toBoolean() && !System.getenv().containsKey("CI"))
instrumentCode.set(true)
pluginName.set("Icon Viewer 2")
sandboxDir.set("${rootProject.projectDir}/.idea-sandbox/${shortenIdeVersion(pluginIdeaVersion)}")
updateSinceUntilBuild.set(false)
version.set(pluginIdeaVersion)
}
changelog {
headerParserRegex.set("(.*)".toRegex())
itemPrefix.set("*")
}
modernizer {
includeTestClasses = true
// Find exclusion names at https://github.com/gaul/modernizer-maven-plugin/blob/master/modernizer-maven-plugin/src/main/resources/modernizer.xml
exclusions = setOf("java/util/Optional.get:()Ljava/lang/Object;")
}
testlogger {
try {
theme = ThemeType.valueOf(testLoggerStyle)
} catch (e: Exception) {
theme = ThemeType.PLAIN
logger.warn("Invalid testLoggerRichStyle value '$testLoggerStyle', " +
"will use PLAIN style instead. Accepted values are PLAIN, STANDARD and MOCHA.")
}
showSimpleNames = true
}
tasks {
register("clearSandboxedIDESystemLogs") {
doFirst {
if (pluginClearSandboxedIDESystemLogsBeforeRun.toBoolean()) {
val sandboxLogDir = File("${rootProject.projectDir}/.idea-sandbox/${shortenIdeVersion(pluginIdeaVersion)}/system/log/")
if (sandboxLogDir.exists() && sandboxLogDir.isDirectory) {
FileUtils.deleteDirectory(sandboxLogDir)
logger.quiet("Deleted sandboxed IDE's log folder $sandboxLogDir")
}
}
}
}
withType<JavaCompile> {
sourceCompatibility = pluginJavaVersion
targetCompatibility = pluginJavaVersion
options.compilerArgs = listOf("-Xlint:deprecation")
options.encoding = "UTF-8"
}
withType<Test> {
useJUnitPlatform()
// avoid JBUIScale "Must be precomputed" error, because IDE is not started (LoadingState.APP_STARTED.isOccurred is false)
jvmArgs("-Djava.awt.headless=true")
}
withType<DependencyUpdatesTask> {
checkForGradleUpdate = true
gradleReleaseChannel = "current"
revision = "release"
rejectVersionIf {
isNonStable(candidate.version)
}
outputFormatter = closureOf<Result> {
unresolved.dependencies.removeIf {
val coordinates = "${it.group}:${it.name}"
coordinates.startsWith("unzipped.com") || coordinates.startsWith("com.jetbrains:ideaI")
}
PlainTextReporter(project, revision, gradleReleaseChannel)
.write(System.out, this)
}
}
runIde {
dependsOn("clearSandboxedIDESystemLogs")
maxHeapSize = "1g" // https://docs.gradle.org/current/dsl/org.gradle.api.tasks.JavaExec.html
// force detection of slow operations in EDT when playing with sandboxed IDE (SlowOperations.assertSlowOperationsAreAllowed)
jvmArgs("-Dide.slow.operations.assertion=true")
if (pluginEnableDebugLogs.toBoolean()) {
systemProperties(
"idea.log.debug.categories" to "#lermitage.intellij.iconviewer"
)
}
autoReloadPlugins.set(false)
// If any warning or error with missing --add-opens, wait for the next gradle-intellij-plugin's update that should sync
// with https://raw.githubusercontent.com/JetBrains/intellij-community/master/plugins/devkit/devkit-core/src/run/OpenedPackages.txt
// or do it manually
}
buildSearchableOptions {
enabled = false
}
patchPluginXml {
changeNotes.set(provider {
with(changelog) {
renderItem(getLatest(), Changelog.OutputType.HTML)
}
})
}
publishPlugin {
token.set(System.getenv("JLE_IJ_PLUGINS_PUBLISH_TOKEN"))
}
}
fun isNonStable(version: String): Boolean {
if (listOf("RELEASE", "FINAL", "GA").any { version.uppercase().endsWith(it) }) {
return false
}
return listOf("alpha", "Alpha", "ALPHA", "b", "beta", "Beta", "BETA", "rc", "RC", "M", "EA", "pr", "atlassian").any {
"(?i).*[.-]${it}[.\\d-]*$".toRegex().matches(version)
}
}
/** Return an IDE version string without the optional PATCH number.
* In other words, replace IDE-MAJOR-MINOR(-PATCH) by IDE-MAJOR-MINOR. */
fun shortenIdeVersion(version: String): String {
if (version.contains("SNAPSHOT", ignoreCase = true)) {
return version
}
val matcher = Regex("[A-Za-z]+[\\-]?[0-9]+[\\.]{1}[0-9]+")
return try {
matcher.findAll(version).map { it.value }.toList()[0]
} catch (e: Exception) {
logger.warn("Failed to shorten IDE version $version: ${e.message}")
version
}
}
/** Find latest IntelliJ stable version from JetBrains website. Result is cached locally for 24h. */
fun findLatestStableIdeVersion(): String {
val t1 = System.currentTimeMillis()
val definitionsUrl = URL("https://www.jetbrains.com/updates/updates.xml")
val cachedLatestVersionFile = File(System.getProperty("java.io.tmpdir") + "/jle-ij-latest-version.txt")
var latestVersion: String
try {
if (cachedLatestVersionFile.exists()) {
val cacheDurationMs = Integer.parseInt(project.findProperty("pluginIdeaVersionCacheDurationInHours") as String) * 60 * 60_000
if (cachedLatestVersionFile.exists() && cachedLatestVersionFile.lastModified() < (System.currentTimeMillis() - cacheDurationMs)) {
logger.quiet("Cache expired, find latest stable IDE version from $definitionsUrl then update cached file $cachedLatestVersionFile")
latestVersion = getOnlineLatestStableIdeVersion(definitionsUrl)
cachedLatestVersionFile.delete()
Files.writeString(cachedLatestVersionFile.toPath(), latestVersion, Charsets.UTF_8)
} else {
logger.quiet("Find latest stable IDE version from cached file $cachedLatestVersionFile")
latestVersion = Files.readString(cachedLatestVersionFile.toPath())!!
}
} else {
logger.quiet("Find latest stable IDE version from $definitionsUrl")
latestVersion = getOnlineLatestStableIdeVersion(definitionsUrl)
Files.writeString(cachedLatestVersionFile.toPath(), latestVersion, Charsets.UTF_8)
}
} catch (e: Exception) {
if (cachedLatestVersionFile.exists()) {
logger.warn("Error: ${e.message}. Will find latest stable IDE version from cached file $cachedLatestVersionFile")
latestVersion = Files.readString(cachedLatestVersionFile.toPath())!!
} else {
throw RuntimeException(e)
}
}
if (logger.isDebugEnabled) {
val t2 = System.currentTimeMillis()
logger.debug("Operation took ${t2 - t1} ms")
}
return latestVersion
}
/** Find latest IntelliJ stable version from given url. */
fun getOnlineLatestStableIdeVersion(definitionsUrl: URL): String {
val definitionsStr = readRemoteContent(definitionsUrl)
val builderFactory = DocumentBuilderFactory.newInstance()
val builder = builderFactory.newDocumentBuilder()
val xmlDocument: Document = builder.parse(ByteArrayInputStream(definitionsStr.toByteArray()))
val xPath = XPathFactory.newInstance().newXPath()
val expression = "/products/product[@name='IntelliJ IDEA']/channel[@id='IC-IU-RELEASE-licensing-RELEASE']/build[1]/@version"
return xPath.compile(expression).evaluate(xmlDocument, XPathConstants.STRING) as String
}
/** Read a remote file as String. */
fun readRemoteContent(url: URL): String {
val t1 = System.currentTimeMillis()
val content = StringBuilder()
val conn = url.openConnection() as HttpURLConnection
conn.requestMethod = "GET"
BufferedReader(InputStreamReader(conn.inputStream)).use { rd ->
var line: String? = rd.readLine()
while (line != null) {
content.append(line)
line = rd.readLine()
}
}
val t2 = System.currentTimeMillis()
logger.quiet("Download $url, took ${t2 - t1} ms (${content.length} B)")
return content.toString()
}
/** Get IDE version from gradle.properties or, of wanted, find latest stable IDE version from JetBrains website. */
fun detectBestIdeVersion(): String {
val pluginIdeaVersionFromProps = project.findProperty("pluginIdeaVersion")
if (pluginIdeaVersionFromProps.toString() == "IC-LATEST-STABLE") {
return "IC-${findLatestStableIdeVersion()}"
}
if (pluginIdeaVersionFromProps.toString() == "IU-LATEST-STABLE") {
return "IU-${findLatestStableIdeVersion()}"
}
return pluginIdeaVersionFromProps.toString()
}