Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #54 from palantir/issue53
Browse files Browse the repository at this point in the history
Fix bugs in support for directory-based idea projects
  • Loading branch information
alicederyn authored Feb 1, 2017
2 parents 2336f24 + c85e3ac commit e5ded24
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 19 deletions.
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ dependencies {
testRuntime files(createClasspathManifest)
}

test {
testLogging {
exceptionFormat = 'full'
}
}

//// Publication //////////////////////////////////////////////////////////////////////

group = 'org.inferred'
Expand Down
31 changes: 20 additions & 11 deletions src/main/groovy/org/inferred/gradle/ProcessorsPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,17 @@ class ProcessorsPlugin implements Plugin<Project> {
})
})

// If the project uses .idea directory structure, update compiler.xml directly
File ideaCompilerXml = project.file('.idea/compiler.xml')
if (ideaCompilerXml.isFile()) {
Node parsedProjectXml = (new XmlParser()).parse(ideaCompilerXml)
updateIdeaCompilerConfiguration(project, parsedProjectXml)
ideaCompilerXml.withWriter { writer ->
XmlNodePrinter nodePrinter = new XmlNodePrinter(new PrintWriter(writer))
nodePrinter.setPreserveWhitespace(true)
nodePrinter.print(parsedProjectXml)
project.afterEvaluate {
// If the project uses .idea directory structure, update compiler.xml directly
File ideaCompilerXml = project.file('.idea/compiler.xml')
if (ideaCompilerXml.isFile()) {
Node parsedProjectXml = (new XmlParser()).parse(ideaCompilerXml)
updateIdeaCompilerConfiguration(project, parsedProjectXml)
ideaCompilerXml.withWriter { writer ->
XmlNodePrinter nodePrinter = new XmlNodePrinter(new PrintWriter(writer))
nodePrinter.setPreserveWhitespace(true)
nodePrinter.print(parsedProjectXml)
}
}
}

Expand Down Expand Up @@ -203,11 +205,18 @@ class ProcessorsPlugin implements Plugin<Project> {
new Node(compilerConfiguration, "annotationProcessing")
}

def outputDir = 'generated_src'
def testOutputDir = 'generated_testSrc'
if (project.hasProperty('idea')) {
outputDir = project.idea.processors.outputDir
testOutputDir = project.idea.processors.testOutputDir
}

compilerConfiguration.annotationProcessing.replaceNode{
annotationProcessing() {
profile(default: 'true', name: 'Default', enabled: 'true') {
sourceOutputDir(name: project.idea.processors.outputDir)
sourceTestOutputDir(name: project.idea.processors.testOutputDir)
sourceOutputDir(name: outputDir)
sourceTestOutputDir(name: testOutputDir)
outputRelativeToContentRoot(value: 'true')
processorPath(useClasspath: 'true')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,10 +595,9 @@ public class ProcessorsPluginFunctionalTest {
.withArguments("idea", "--stacktrace")
.build()

def xml = new XmlSlurper().parse(testProjectDirRoot.toPath().resolve(".idea/compiler.xml").toFile())
def xml = testProjectDirRoot.toPath().resolve(".idea/compiler.xml").toFile().text.trim()

def expected = new XmlSlurper().parseText("""
<?xml version="1.0" encoding="UTF-8"?>
def expected = """
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
Expand All @@ -611,7 +610,7 @@ public class ProcessorsPluginFunctionalTest {
</annotationProcessing>
</component>
</project>
""".trim())
""".stripIndent().trim()

assertEquals(expected, xml)
}
Expand All @@ -638,10 +637,9 @@ public class ProcessorsPluginFunctionalTest {
.withArguments("idea", "--stacktrace")
.build()

def xml = new XmlSlurper().parse(testProjectDirRoot.toPath().resolve(".idea/compiler.xml").toFile())
def xml = testProjectDirRoot.toPath().resolve(".idea/compiler.xml").toFile().text.trim()

def expected = new XmlSlurper().parseText("""
<?xml version="1.0" encoding="UTF-8"?>
def expected = """
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
Expand All @@ -654,7 +652,55 @@ public class ProcessorsPluginFunctionalTest {
</annotationProcessing>
</component>
</project>
""".trim())
""".stripIndent().trim()

assertEquals(expected, xml)
}

@Test
public void testUserSpecifiedDirectoriesUsedInIdeaCompilerXml() throws IOException {
buildFile << """
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.inferred.processors'
idea.processors {
outputDir = 'foo'
testOutputDir = 'bar'
}
"""

new File(testProjectDir.newFolder('.idea'), 'compiler.xml') << """
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing/>
</component>
</project>
""".trim()

File testProjectDirRoot = testProjectDir.getRoot()
GradleRunner.create()
.withProjectDir(testProjectDirRoot)
.withArguments("idea", "--stacktrace")
.build()

def xml = testProjectDirRoot.toPath().resolve(".idea/compiler.xml").toFile().text.trim()

def expected = """
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile default="true" name="Default" enabled="true">
<sourceOutputDir name="foo"/>
<sourceTestOutputDir name="bar"/>
<outputRelativeToContentRoot value="true"/>
<processorPath useClasspath="true"/>
</profile>
</annotationProcessing>
</component>
</project>
""".stripIndent().trim()

assertEquals(expected, xml)
}
Expand Down Expand Up @@ -747,6 +793,98 @@ public class ProcessorsPluginFunctionalTest {
assertAutoValueInFile(new File(runner.projectDir, ".factorypath"))
}

/** @see https://github.com/palantir/gradle-processors/issues/28 */
@Test
public void testIdeaCompilerConfigurationUpdatedWithoutNeedToApplyIdeaPlugin() throws IOException {
buildFile << """
apply plugin: 'java'
apply plugin: 'org.inferred.processors'
"""

new File(testProjectDir.newFolder('.idea'), 'compiler.xml') << """
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing/>
</component>
</project>
""".trim()

File testProjectDirRoot = testProjectDir.getRoot()
GradleRunner.create()
.withProjectDir(testProjectDirRoot)
.withArguments("tasks", "--stacktrace")
.build()

def xml = testProjectDirRoot.toPath().resolve(".idea/compiler.xml").toFile().text.trim()

def expected = """
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile default="true" name="Default" enabled="true">
<sourceOutputDir name="generated_src"/>
<sourceTestOutputDir name="generated_testSrc"/>
<outputRelativeToContentRoot value="true"/>
<processorPath useClasspath="true"/>
</profile>
</annotationProcessing>
</component>
</project>
""".stripIndent().trim()

assertEquals(expected, xml)
}

/** @see https://github.com/palantir/gradle-processors/issues/53 */
@Test
public void testCompilerXmlModificationWhenIdeaPluginImportedLast() throws IOException {
buildFile << """
apply plugin: 'java'
apply plugin: 'org.inferred.processors'
apply plugin: 'idea'
idea.processors {
outputDir = 'foo'
testOutputDir = 'bar'
}
"""

new File(testProjectDir.newFolder('.idea'), 'compiler.xml') << """
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing/>
</component>
</project>
""".trim()

File testProjectDirRoot = testProjectDir.getRoot()
GradleRunner.create()
.withProjectDir(testProjectDirRoot)
.withArguments("idea", "--stacktrace")
.build()

def xml = testProjectDirRoot.toPath().resolve(".idea/compiler.xml").toFile().text.trim()

def expected = """
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile default="true" name="Default" enabled="true">
<sourceOutputDir name="foo"/>
<sourceTestOutputDir name="bar"/>
<outputRelativeToContentRoot value="true"/>
<processorPath useClasspath="true"/>
</profile>
</annotationProcessing>
</component>
</project>
""".stripIndent().trim()

assertEquals(expected, xml)
}

private void assertAutoValueInFile(File file) {
if (!file.any { it.contains("auto-value-1.0.jar") }) {
println "====== $file.name ============================================================"
Expand Down

0 comments on commit e5ded24

Please sign in to comment.