Skip to content

Commit 2c5e8f3

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

File tree

18 files changed

+637
-119
lines changed

18 files changed

+637
-119
lines changed

.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
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,90 @@
1-
@if "%DEBUG%" == "" @echo off
2-
@rem ##########################################################################
3-
@rem
4-
@rem Gradle startup script for Windows
5-
@rem
6-
@rem ##########################################################################
7-
8-
@rem Set local scope for the variables with windows NT shell
9-
if "%OS%"=="Windows_NT" setlocal
10-
11-
set DIRNAME=%~dp0
12-
if "%DIRNAME%" == "" set DIRNAME=.
13-
set APP_BASE_NAME=%~n0
14-
set APP_HOME=%DIRNAME%
15-
16-
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17-
set DEFAULT_JVM_OPTS=
18-
19-
@rem Find java.exe
20-
if defined JAVA_HOME goto findJavaFromJavaHome
21-
22-
set JAVA_EXE=java.exe
23-
%JAVA_EXE% -version >NUL 2>&1
24-
if "%ERRORLEVEL%" == "0" goto init
25-
26-
echo.
27-
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28-
echo.
29-
echo Please set the JAVA_HOME variable in your environment to match the
30-
echo location of your Java installation.
31-
32-
goto fail
33-
34-
:findJavaFromJavaHome
35-
set JAVA_HOME=%JAVA_HOME:"=%
36-
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37-
38-
if exist "%JAVA_EXE%" goto init
39-
40-
echo.
41-
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42-
echo.
43-
echo Please set the JAVA_HOME variable in your environment to match the
44-
echo location of your Java installation.
45-
46-
goto fail
47-
48-
:init
49-
@rem Get command-line arguments, handling Windows variants
50-
51-
if not "%OS%" == "Windows_NT" goto win9xME_args
52-
if "%@eval[2+2]" == "4" goto 4NT_args
53-
54-
:win9xME_args
55-
@rem Slurp the command line arguments.
56-
set CMD_LINE_ARGS=
57-
set _SKIP=2
58-
59-
:win9xME_args_slurp
60-
if "x%~1" == "x" goto execute
61-
62-
set CMD_LINE_ARGS=%*
63-
goto execute
64-
65-
:4NT_args
66-
@rem Get arguments from the 4NT Shell from JP Software
67-
set CMD_LINE_ARGS=%$
68-
69-
:execute
70-
@rem Setup the command line
71-
72-
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73-
74-
@rem Execute Gradle
75-
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76-
77-
:end
78-
@rem End local scope for the variables with windows NT shell
79-
if "%ERRORLEVEL%"=="0" goto mainEnd
80-
81-
:fail
82-
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83-
rem the _cmd.exe /c_ return code!
84-
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85-
exit /b 1
86-
87-
:mainEnd
88-
if "%OS%"=="Windows_NT" endlocal
89-
90-
:omega
1+
@if "%DEBUG%" == "" @echo off
2+
@rem ##########################################################################
3+
@rem
4+
@rem Gradle startup script for Windows
5+
@rem
6+
@rem ##########################################################################
7+
8+
@rem Set local scope for the variables with windows NT shell
9+
if "%OS%"=="Windows_NT" setlocal
10+
11+
set DIRNAME=%~dp0
12+
if "%DIRNAME%" == "" set DIRNAME=.
13+
set APP_BASE_NAME=%~n0
14+
set APP_HOME=%DIRNAME%
15+
16+
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17+
set DEFAULT_JVM_OPTS=
18+
19+
@rem Find java.exe
20+
if defined JAVA_HOME goto findJavaFromJavaHome
21+
22+
set JAVA_EXE=java.exe
23+
%JAVA_EXE% -version >NUL 2>&1
24+
if "%ERRORLEVEL%" == "0" goto init
25+
26+
echo.
27+
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28+
echo.
29+
echo Please set the JAVA_HOME variable in your environment to match the
30+
echo location of your Java installation.
31+
32+
goto fail
33+
34+
:findJavaFromJavaHome
35+
set JAVA_HOME=%JAVA_HOME:"=%
36+
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37+
38+
if exist "%JAVA_EXE%" goto init
39+
40+
echo.
41+
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42+
echo.
43+
echo Please set the JAVA_HOME variable in your environment to match the
44+
echo location of your Java installation.
45+
46+
goto fail
47+
48+
:init
49+
@rem Get command-line arguments, handling Windows variants
50+
51+
if not "%OS%" == "Windows_NT" goto win9xME_args
52+
if "%@eval[2+2]" == "4" goto 4NT_args
53+
54+
:win9xME_args
55+
@rem Slurp the command line arguments.
56+
set CMD_LINE_ARGS=
57+
set _SKIP=2
58+
59+
:win9xME_args_slurp
60+
if "x%~1" == "x" goto execute
61+
62+
set CMD_LINE_ARGS=%*
63+
goto execute
64+
65+
:4NT_args
66+
@rem Get arguments from the 4NT Shell from JP Software
67+
set CMD_LINE_ARGS=%$
68+
69+
:execute
70+
@rem Setup the command line
71+
72+
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73+
74+
@rem Execute Gradle
75+
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76+
77+
:end
78+
@rem End local scope for the variables with windows NT shell
79+
if "%ERRORLEVEL%"=="0" goto mainEnd
80+
81+
:fail
82+
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83+
rem the _cmd.exe /c_ return code!
84+
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85+
exit /b 1
86+
87+
:mainEnd
88+
if "%OS%"=="Windows_NT" endlocal
89+
90+
:omega

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)