Skip to content

Commit 2c5e8f3

Browse files
committed
Added cleaning outdated output files created by Clojure compile task.
1 parent 42b7423 commit 2c5e8f3

18 files changed

Lines changed: 637 additions & 119 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/.idea/shelf
66
/.idea/libraries
77
/.idea/modules
8+
/.idea/atlassian-ide-plugin.xml
89

910
out
1011
dist

build.gradle

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ buildscript {
3232

3333
group 'com.cursive-ide'
3434
version '1.1.0'
35+
ext.pluginId = 'com.cursive-ide.clojure'
36+
ext.pluginImplementationClass = 'cursive.ClojurePlugin'
3537

3638
apply plugin: 'kotlin'
3739
apply plugin: 'groovy'
3840
apply plugin: 'maven'
3941
apply plugin: 'com.gradle.plugin-publish'
42+
apply plugin: "java-gradle-plugin"
4043

4144
project.afterEvaluate {
4245
// Ugh, see https://discuss.gradle.org/t/kotlin-groovy-and-java-compilation/14903/9
@@ -55,6 +58,18 @@ dependencies {
5558
compile gradleApi()
5659
compile localGroovy()
5760
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
61+
testCompile gradleTestKit()
62+
testCompile "junit:junit:4.12"
63+
testCompile 'net.wuerl.kotlin:assertj-core-kotlin:0.1.3'
64+
}
65+
66+
gradlePlugin {
67+
plugins {
68+
clojurePlugin {
69+
id = pluginId
70+
implementationClass = pluginImplementationClass
71+
}
72+
}
5873
}
5974

6075
pluginBundle {
@@ -65,7 +80,7 @@ pluginBundle {
6580

6681
plugins {
6782
clojurePlugin {
68-
id = 'com.cursive-ide.clojure'
83+
id = pluginId
6984
displayName = 'Gradle Clojure plugin'
7085
}
7186
}

gradle/wrapper/gradle-wrapper.jar

-5 Bytes
Binary file not shown.

gradlew.bat

Lines changed: 90 additions & 90 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/kotlin/cursive/ClojurePlugin.kt

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import org.gradle.api.tasks.SourceSet
3434
import org.gradle.api.tasks.TaskAction
3535
import org.gradle.api.tasks.compile.AbstractCompile
3636
import org.gradle.api.tasks.incremental.IncrementalTaskInputs
37+
import org.gradle.api.tasks.incremental.InputFileDetails
3738
import org.gradle.process.JavaForkOptions
3839
import org.gradle.process.internal.DefaultJavaForkOptions
3940
import org.gradle.process.internal.ExecException
@@ -181,17 +182,19 @@ open class ClojureCompile @Inject constructor(val fileResolver: FileResolver) :
181182
return reflectionWarnings
182183
}
183184

184-
@TaskAction
185-
fun compile(inputs: IncrementalTaskInputs) {
186-
compile()
185+
override fun compile() {
186+
throw UnsupportedOperationException()
187187
}
188188

189-
override fun compile() {
189+
@TaskAction
190+
fun compile(inputs: IncrementalTaskInputs) {
190191
logger.info("Starting ClojureCompile task")
191192

192-
destinationDir.deleteRecursively()
193193
destinationDir.mkdirs()
194194

195+
inputs.outOfDate { removeOutputFilesDerivedFromInputFile(it, destinationDir) }
196+
inputs.removed { removeOutputFilesDerivedFromInputFile(it, destinationDir) }
197+
195198
if (copySourceToOutput ?: !aotCompile) {
196199
project.copy {
197200
it.from(getSource()).into(destinationDir)
@@ -269,6 +272,30 @@ open class ClojureCompile @Inject constructor(val fileResolver: FileResolver) :
269272
}
270273
}
271274

275+
private fun removeOutputFilesDerivedFromInputFile(inputFileDetails: InputFileDetails, destinationDir: File) {
276+
val sourceAbsoluteFile = inputFileDetails.file
277+
if (isClojureSource(sourceAbsoluteFile)) {
278+
logger.debug("Removing class files for {}", inputFileDetails.file)
279+
val sourceCanonicalFileName = sourceAbsoluteFile.canonicalPath
280+
val sourceFileRoot = getSourceRootsFiles()
281+
.find { sourceCanonicalFileName.startsWith(it.canonicalPath) }
282+
?: throw IllegalStateException("No source root found for source file ${sourceAbsoluteFile}")
283+
val sourceRelativeFile = sourceAbsoluteFile.relativeTo(sourceFileRoot)
284+
val sourceRelativeDirectory = sourceRelativeFile.parentFile
285+
val sourceFileName = sourceAbsoluteFile.nameWithoutExtension
286+
destinationDir.resolve(sourceRelativeDirectory)
287+
.listFiles { file -> file.name.startsWith(sourceFileName) }
288+
?.forEach {
289+
logger.debug("Deleting derived file {}", it)
290+
it.delete()
291+
}
292+
}
293+
}
294+
295+
private fun isClojureSource(file: File): Boolean {
296+
return CLJ_EXTENSION_REGEX.matches(file.extension) && getSourceRoots().any { file.canonicalPath.startsWith(it) }
297+
}
298+
272299
private fun executeScript(script: String, stdout: OutputStream, stderr: OutputStream) {
273300
val file = createTempFile("clojure-compiler", ".clj", temporaryDir)
274301
file.bufferedWriter().use { out ->
@@ -281,7 +308,7 @@ open class ClojureCompile @Inject constructor(val fileResolver: FileResolver) :
281308

282309
// clojure.core/compile requires following on its classpath:
283310
// - libs (this.classpath)
284-
// - compiled namespaces sources (getSourceRootsFiles)
311+
// - namespaces sources to be compiled (getSourceRootsFiles)
285312
// - *compile-path* directory (this.destinationDir)
286313
exec.classpath = classpath + SimpleFileCollection(getSourceRootsFiles()) + SimpleFileCollection(destinationDir)
287314

@@ -321,16 +348,15 @@ open class ClojureCompile @Inject constructor(val fileResolver: FileResolver) :
321348
}
322349

323350
private fun getSourceRoots(): HashSet<String> {
324-
val roots = source
325-
.filter { it is SourceDirectorySet }
326-
.flatMap { (it as SourceDirectorySet).srcDirs }
351+
return getSourceRootsFiles()
327352
.map { it.canonicalPath }
328353
.toHashSet()
329-
return roots
330354
}
331355

332356
private fun getSourceRootsFiles(): List<File> {
333-
return getSourceRoots().map(::File)
357+
return source
358+
.filter { it is SourceDirectorySet }
359+
.flatMap { (it as SourceDirectorySet).srcDirs }
334360
}
335361

336362
companion object {
@@ -359,6 +385,7 @@ open class ClojureCompile @Inject constructor(val fileResolver: FileResolver) :
359385
'\\' to "_BSLASH_",
360386
'?' to "_QMARK_")
361387

388+
val CLJ_EXTENSION_REGEX = "cljc?".toRegex()
362389
val DEMUNGE_MAP = CHAR_MAP.map { it.value to it.key }.toMap()
363390
val DEMUNGE_PATTERN = Pattern.compile(DEMUNGE_MAP.keys
364391
.sortedByDescending { it.length }

src/main/resources/META-INF/gradle-plugins/com.cursive-ide.clojure.properties

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2017 Colin Fleming
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+
* http://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+
plugins {
18+
id 'com.cursive-ide.clojure'
19+
}
20+
21+
repositories {
22+
mavenCentral()
23+
}
24+
25+
dependencies {
26+
compile 'org.clojure:clojure:1.8.0'
27+
}

0 commit comments

Comments
 (0)