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

Commit

Permalink
Stop manipulating compiler.xml when importing from IDEA (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
dansanduleac authored Oct 12, 2018
1 parent 660503f commit fe91df5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 235 deletions.
42 changes: 11 additions & 31 deletions src/main/groovy/org/inferred/gradle/ProcessorsPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import groovy.text.SimpleTemplateEngine
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.XmlProvider
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ResolvedConfiguration
import org.gradle.api.file.FileCollection
Expand All @@ -16,8 +17,10 @@ import org.gradle.api.tasks.Delete
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.api.tasks.javadoc.Javadoc
import org.gradle.plugins.ide.api.XmlFileContentMerger
import org.gradle.plugins.ide.eclipse.EclipsePlugin
import org.gradle.plugins.ide.idea.IdeaPlugin
import org.gradle.plugins.ide.idea.model.IdeaModel
import org.gradle.util.GUtil

class ProcessorsPlugin implements Plugin<Project> {
Expand Down Expand Up @@ -176,36 +179,18 @@ class ProcessorsPlugin implements Plugin<Project> {
addGeneratedSourceFolder(project, { getIdeaSourceTestOutputDir(project) }, true)

// Root project configuration
if (project.rootProject.hasProperty('idea') && project.rootProject.idea.project != null) {
project.rootProject.idea.project.ipr {
withXml {
def ideaModel = project.rootProject.extensions.findByType(IdeaModel)
if (ideaModel != null && ideaModel.project != null) {
project.rootProject.extensions.getByType(IdeaModel).project.ipr { XmlFileContentMerger merger ->
merger.withXml { XmlProvider it ->
// This file is only generated in the root project, but the user may not have applied
// the gradle-processors plugin to the root project. Instead, we update it from
// every project idempotently.
updateIdeaCompilerConfiguration(project.rootProject, node, false)
updateIdeaCompilerConfiguration(project.rootProject, it.asNode())
}
}
}
})

project.afterEvaluate {
// If the project uses .idea directory structure, and we are running within IntelliJ, update
// compiler.xml directly
// This file is only generated in the root project, but the user may not have applied
// the gradle-processors plugin to the root project. Instead, we update it from every
// project idempotently.
def inIntelliJ = System.properties.'idea.active' as boolean
File ideaCompilerXml = project.rootProject.file('.idea/compiler.xml')
if (inIntelliJ && ideaCompilerXml.isFile()) {
Node parsedProjectXml = (new XmlParser()).parse(ideaCompilerXml)
updateIdeaCompilerConfiguration(project.rootProject, parsedProjectXml, true)
ideaCompilerXml.withWriter { writer ->
XmlNodePrinter nodePrinter = new XmlNodePrinter(new PrintWriter(writer))
nodePrinter.setPreserveWhitespace(true)
nodePrinter.print(parsedProjectXml)
}
}
}
}

private static void configureFindBugs(Project project) {
Expand Down Expand Up @@ -307,10 +292,7 @@ class ProcessorsPlugin implements Plugin<Project> {
}
}

static void updateIdeaCompilerConfiguration(
Project project,
Node projectConfiguration,
boolean directoryBasedProject) {
static void updateIdeaCompilerConfiguration(Project project, Node projectConfiguration) {
Object compilerConfiguration = projectConfiguration.component
.find { it.@name == 'CompilerConfiguration' }

Expand All @@ -322,13 +304,11 @@ class ProcessorsPlugin implements Plugin<Project> {
new Node(compilerConfiguration, "annotationProcessing")
}

def dirPrefix = (directoryBasedProject ? '../' : '')

compilerConfiguration.annotationProcessing.replaceNode{
annotationProcessing() {
profile(default: 'true', name: 'Default', enabled: 'true') {
sourceOutputDir(name: dirPrefix + getIdeaSourceOutputDir(project))
sourceTestOutputDir(name: dirPrefix + getIdeaSourceTestOutputDir(project))
sourceOutputDir(name: getIdeaSourceOutputDir(project))
sourceTestOutputDir(name: getIdeaSourceTestOutputDir(project))
outputRelativeToContentRoot(value: 'true')
processorPath(useClasspath: 'true')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,36 +626,22 @@ class ProcessorsPluginFunctionalTest extends AbstractPluginTest {
apply plugin: 'org.inferred.processors'
"""

file('.idea/compiler.xml') << """
def compilerXml = """
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing/>
</component>
</project>
""".trim()
""".stripIndent().trim()
file('.idea/compiler.xml') << compilerXml

runTasksSuccessfully("-Didea.active=true", "--stacktrace")

def xml = file(".idea/compiler.xml").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()

expect:
expected == xml
compilerXml == xml
}

void testCompilerXmlNotTouchedIfIdeaNotActive() throws IOException {
Expand Down Expand Up @@ -691,35 +677,21 @@ class ProcessorsPluginFunctionalTest extends AbstractPluginTest {
apply plugin: 'org.inferred.processors'
"""

file('.idea/compiler.xml') << """
def compilerXml = """
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
</component>
</project>
""".trim()
""".stripIndent().trim()
file('.idea/compiler.xml') << compilerXml

runTasksSuccessfully("-Didea.active=true", "--stacktrace")

def xml = file(".idea/compiler.xml").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()

expect:
expected == xml
compilerXml == xml
}

void testUserSpecifiedDirectoriesUsedInIdeaIprFile() throws IOException {
Expand All @@ -744,50 +716,6 @@ class ProcessorsPluginFunctionalTest extends AbstractPluginTest {
profile.sourceTestOutputDir.first().@name == "bar"
}

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

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

runTasksSuccessfully("-Didea.active=true", "--stacktrace")

def xml = file(".idea/compiler.xml").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()

expect:
expected == xml
}

void testOnlyApplyToSubProject() {
buildFile << """
apply plugin: 'idea'
Expand Down Expand Up @@ -853,130 +781,6 @@ class ProcessorsPluginFunctionalTest extends AbstractPluginTest {
assertAutoValueInFile(file(".factorypath"))
}

/** See <a href="https://github.com/palantir/gradle-processors/issues/28">issue #28</a> */
void testIdeaCompilerConfigurationUpdatedWithoutNeedToApplyIdeaPlugin() throws IOException {
buildFile << """
apply plugin: 'java'
apply plugin: 'org.inferred.processors'
"""

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

runTasksSuccessfully("-Didea.active=true", "--stacktrace")

def xml = file(".idea/compiler.xml").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()

expect:
expected == xml
}

/** See <a href="https://github.com/palantir/gradle-processors/issues/53">issue #53</a> */
void testCompilerXmlModificationWhenIdeaPluginImportedLast() throws IOException {
buildFile << """
apply plugin: 'java'
apply plugin: 'org.inferred.processors'
apply plugin: 'idea'
idea.processors {
outputDir = 'foo'
testOutputDir = 'bar'
}
"""

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

runTasksSuccessfully("-Didea.active=true", "--stacktrace")

def xml = file(".idea/compiler.xml").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()

expect:
expected == xml
}

/** See <a href="https://github.com/palantir/gradle-processors/issues/53">issue #53</a> */
void testCompilerXmlModificationWhenIdeaPluginNotAppliedToRootProject() throws IOException {
multiProject.addSubproject("A", """
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.inferred.processors'
""".stripIndent())

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

runTasksSuccessfully("-Didea.active=true", "--stacktrace")

def xml = file(".idea/compiler.xml").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()

expect:
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 fe91df5

Please sign in to comment.