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

Commit

Permalink
Fix bugs in support for directory-based idea projects
Browse files Browse the repository at this point in the history
Two issues have arisen in our support for directory based idea projects:

 * We regressed on issue #28, once again requiring the user import the 'idea' plugin
 * Issue #53: correctness depends on plugin ordering

This PR fixes both by only updating compiler.xml after the project is evaluated, and using default values for sourceOutputDir/sourceTestOutputDir if the IDEA plugin has not been applied.
  • Loading branch information
alicederyn committed Feb 1, 2017
1 parent f0afed4 commit c85e3ac
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 11 deletions.
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 @@ -657,6 +657,54 @@ public class ProcessorsPluginFunctionalTest {
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)
}

@Test
public void testOnlyApplyToSubProject() {
testProjectDir.newFolder("projectA")
Expand Down Expand Up @@ -745,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 c85e3ac

Please sign in to comment.