<!--
  ~ Copyright 2010 the original author or authors.
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~      http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<chapter id="idea_plugin">
    <title>The IDEA Plugin</title>

    <para>The IDEA plugin generates files that are used by <ulink url="http://www.jetbrains.com/idea/">IntelliJ IDEA</ulink>, thus
        making it possible to open the project from IDEA (<guimenuitem>File</guimenuitem> - <guimenuitem>Open Project</guimenuitem>).
        Both external dependencies (including associated source and Javadoc files) and project dependencies are considered.</para>

    <para>What exactly the IDEA plugin generates depends on which other plugins are used:</para>
    <table>
        <title>IDEA plugin behavior</title>
        <thead>
            <tr>
                <td>Plugin</td><td>Description</td>
            </tr>
        </thead>
        <tr>
            <td>None</td><td>Generates an IDEA module file. Also generates an IDEA project and workspace file if the project
            is the root project.</td>
        </tr>
        <tr>
            <td>
                <link linkend="java_plugin">Java</link>
            </td>
            <td>Adds Java configuration to the module and project files.</td>
        </tr>
    </table>

    <para>One focus of the IDEA plugin is to be open to customization. The plugin provides a standardized set of hooks
        for adding and removing content from the generated files.
    </para>

    <section>
        <title>Usage</title>
        <para>To use the IDEA plugin, include this in your build script:</para>
        <sample id="useIdeaPlugin" dir="idea" title="Using the IDEA plugin">
            <sourcefile file="build.gradle" snippet="use-plugin"/>
        </sample>
        <para>The IDEA plugin adds a number of tasks to your project. The main tasks that you will use
            are the <literal>idea</literal> and <literal>cleanIdea</literal> tasks.
        </para>
    </section>
    <section>
        <title>Tasks</title>

        <para>The IDEA plugin adds the tasks shown below to a project.
            Notice that the <literal>clean</literal> task does not depend on the <literal>cleanIdeaWorkspace</literal> task.
            This is because the workspace typically contains a lot of user specific temporary data and it is not desirable to manipulate it outside IDEA.
        </para>

        <table id='ideatasks'>
            <title>IDEA plugin - Tasks</title>
            <thead>
                <tr>
                    <td>Task name</td>
                    <td>Depends on</td>
                    <td>Type</td>
                    <td>Description</td>
                </tr>
            </thead>
            <tr>
                <td>
                    <literal>idea</literal>
                </td>
                <td><literal>ideaProject</literal>, <literal>ideaModule</literal>, <literal>ideaWorkspace</literal></td>
                <td><literal>-</literal></td>
                <td>Generates all IDEA configuration files</td>
            </tr>
            <tr>
                <td>
                    <literal>cleanIdea</literal>
                </td>
                <td>
                    <literal>cleanIdeaProject</literal>, <literal>cleanIdeaModule</literal>
                </td>
                <td><apilink class="org.gradle.api.tasks.Delete"/></td>
                <td>Removes all IDEA configuration files</td>
            </tr>
            <tr>
                <td>
                    <literal>cleanIdeaProject</literal>
                </td>
                <td>
                    <literal>-</literal>
                </td>
                <td><apilink class="org.gradle.api.tasks.Delete"/></td>
                <td>Removes the IDEA project file</td>
            </tr>
            <tr>
                <td>
                    <literal>cleanIdeaModule</literal>
                </td>
                <td>
                    <literal>-</literal>
                </td>
                <td><apilink class="org.gradle.api.tasks.Delete"/></td>
                <td>Removes the IDEA module file</td>
            </tr>
            <tr>
                <td>
                    <literal>cleanIdeaWorkspace</literal>
                </td>
                <td>
                    <literal>-</literal>
                </td>
                <td><apilink class="org.gradle.api.tasks.Delete"/></td>
                <td>Removes the IDEA workspace file</td>
            </tr>
            <tr>
                <td>
                    <literal>ideaProject</literal>
                </td>
                <td>
                    <literal>-</literal>
                </td>
                <td><apilink class="org.gradle.plugins.ide.idea.GenerateIdeaProject"/></td>
                <td>Generates the <literal>.ipr</literal> file. This task is only added to the root project.</td>
            </tr>
            <tr>
                <td>
                    <literal>ideaModule</literal>
                </td>
                <td>
                    <literal>-</literal>
                </td>
                <td><apilink class="org.gradle.plugins.ide.idea.GenerateIdeaModule"/></td>
                <td>Generates the <literal>.iml</literal> file</td>
            </tr>
            <tr>
                <td>
                    <literal>ideaWorkspace</literal>
                </td>
                <td>
                    <literal>-</literal>
                </td>
                <td><apilink class="org.gradle.plugins.ide.idea.GenerateIdeaWorkspace"/></td>
                <td>Generates the <literal>.iws</literal> file. This task is only added to the root project.</td>
            </tr>
        </table>
    </section>

    <section>
        <title>Configuration</title>

        <table id='idea-configuration'>
            <title>Configuration of the idea plugin</title>
            <thead>
                <tr>
                    <td>Model</td>
                    <td>Reference name</td>
                    <td>Description</td>
                </tr>
            </thead>
            <tr>
                <td>
                    <apilink class="org.gradle.plugins.ide.idea.model.IdeaModel"/>
                </td>
                <td><literal>idea</literal></td>
                <td>Top level element that enables configuration of the idea plugin in a DSL-friendly fashion</td>
            </tr>
            <tr>
                <td>
                    <apilink class="org.gradle.plugins.ide.idea.model.IdeaProject"/>
                </td>
                <td><literal>idea.project</literal></td>
                <td>Allows configuring project information</td>
            </tr>
            <tr>
                <td>
                    <apilink class="org.gradle.plugins.ide.idea.model.IdeaModule"/>
                </td>
                <td><literal>idea.module</literal></td>
                <td>Allows configuring module information</td>
            </tr>
            <tr>
                <td>
                    <apilink class="org.gradle.plugins.ide.idea.model.IdeaWorkspace"/>
                </td>
                <td><literal>idea.workspace</literal></td>
                <td>Allows configuring the workspace XML</td>
            </tr>
        </table>
    </section>

    <section>
        <title>Customizing the generated files</title>
        <para>The IDEA plugin provides hooks and behavior for customizing the generated content.
            The workspace file can effectively only be manipulated via the <code>withXml</code> hook
            because its corresponding domain object is essentially empty.</para>
        <para>The tasks recognize existing IDEA files, and merge them with the generated content.</para>
        <section>
            <title>Merging</title>
            <para>Sections of existing IDEA files that are also the target of generated content will be amended or overwritten,
                depending on the particular section. The remaining sections will be left as-is.</para>
            <section id="sec:complete-overwrite">
                <title>Disabling merging with a complete overwrite</title>
                <para>To completely rewrite existing IDEA files, execute a clean task together with its corresponding generation task,
                    like “<userinput>gradle cleanIdea idea</userinput>” (in that order). If you want to make this
                    the default behavior, add “<code>tasks.idea.dependsOn(cleanIdea)</code>” to your build script. This makes it
                    unnecessary to execute the clean task explicitly.
                </para>
                <para>This strategy can also be used for individual files that the plugin would generate.  For instance,
                this can be done for the “<literal>.iml</literal>” file with “<userinput>gradle cleanIdeaModule ideaModule</userinput>”.
                </para>
            </section>
        </section>
        <section>
            <title>Hooking into the generation lifecycle</title>
            <para>The plugin provides objects modeling the sections of the metadata files
                that are generated by Gradle. The generation lifecycle is as follows:
                <orderedlist>
                    <listitem>The file is read; or a default version provided by Gradle is used if it does not exist</listitem>
                    <listitem>The <code>beforeMerged</code> hook is executed with a domain object representing the existing file</listitem>
                    <listitem>The existing content is merged with the configuration inferred from the Gradle build or defined explicitly in the eclipse DSL</listitem>
                    <listitem>The <code>whenMerged</code> hook is executed with a domain object representing contents of the file to be persisted</listitem>
                    <listitem>The <code>withXml</code> hook is executed with a raw representation of the XML that will be persisted</listitem>
                    <listitem>The final XML is persisted</listitem>
                </orderedlist>
                The following table lists the domain object used for each of the model types:
            </para>
            <table id='idea-hooks'>
                <title>Idea plugin hooks</title>
                <thead>
                    <tr>
                        <td>Model</td>
                        <td><literal>beforeMerged { arg -> }</literal> argument type</td>
                        <td><literal>whenMerged { arg -> }</literal> argument type</td>
                        <td><literal>withXml { arg -> }</literal> argument type</td>
                    </tr>
                </thead>
                <tr>
                    <td><apilink class="org.gradle.plugins.ide.idea.model.IdeaProject"/></td>
                    <td><apilink class="org.gradle.plugins.ide.idea.model.Project"/></td>
                    <td><apilink class="org.gradle.plugins.ide.idea.model.Project"/></td>
                    <td><apilink class="org.gradle.api.XmlProvider"/></td>
                </tr>
                <tr>
                    <td><apilink class="org.gradle.plugins.ide.idea.model.IdeaModule"/></td>
                    <td><apilink class="org.gradle.plugins.ide.idea.model.Module"/></td>
                    <td><apilink class="org.gradle.plugins.ide.idea.model.Module"/></td>
                    <td><apilink class="org.gradle.api.XmlProvider"/></td>
                </tr>
                <tr>
                    <td><apilink class="org.gradle.plugins.ide.idea.model.IdeaWorkspace"/></td>
                    <td><apilink class="org.gradle.plugins.ide.idea.model.Workspace"/></td>
                    <td><apilink class="org.gradle.plugins.ide.idea.model.Workspace"/></td>
                    <td><apilink class="org.gradle.api.XmlProvider"/></td>
                </tr>
            </table>
            <section id="sec:partial-rewrite">
                <title>Partial rewrite of existing content</title>
                <para>A <link linkend="sec:complete-rewrite">complete rewrite</link> causes all existing content to be discarded,
                    thereby losing any changes made directly in the IDE. The <code>beforeMerged</code> hook makes it possible
                    to overwrite just certain parts of the existing content. The following example removes all existing dependencies
                    from the <literal>Module</literal> domain object:
                    <sample id="partialRewrites" dir="idea"
                            title="Partial Rewrite for Module">
                        <sourcefile file="build.gradle" snippet="module-before-merged"/>
                    </sample>
                    The resulting module file will only contain Gradle-generated dependency entries, but
                    not any other dependency entries that may have been present in the original file. (In the case of dependency entries,
                    this is also the default behavior.) Other sections of the module file will be either left as-is or merged.
                    The same could be done for the module paths in the project file:
                    <sample id="partialRewritesProject" dir="idea"
                            title="Partial Rewrite for Project">
                        <sourcefile file="build.gradle" snippet="project-before-merged"/>
                    </sample>
                </para>
            </section>
            <section>
                <title>Modifying the fully populated domain objects</title>
                <para>The <code>whenMerged</code> hook allows you to manipulate the fully populated domain objects. Often this is the
                    preferred way to customize IDEA files. Here is how you would export all the dependencies of an IDEA module:
                    <sample id="exportDependencies" dir="idea"
                            title="Export Dependencies">
                        <sourcefile file="build.gradle" snippet="module-when-merged"/>
                    </sample>
                    
                </para>
            </section>
            <section>
                <title>Modifying the XML representation</title>
                <para>The <code>withXml</code>hook allows you to manipulate the in-memory XML representation just before the file gets written to disk.
                    Although Groovy's XML support makes up for a lot, this approach is less convenient than manipulating the domain objects.
                    In return, you get total control over the generated file, including sections not modeled by the domain objects.
                    <sample id="projectWithXml" dir="idea"
                            title="Customizing the XML">
                        <sourcefile file="build.gradle" snippet="project-with-xml"/>
                        <test args="idea"/>
                    </sample>
                </para>
            </section>
        </section>
    </section>
    <section>
        <title>Further things to consider</title>
        <para>The paths of dependencies in the generated IDEA files are absolute. If you manually define a path variable
            pointing to the Gradle dependency cache, IDEA will automatically replace the absolute dependency paths with
            this path variable. you can configure this path variable via the “<literal>idea.pathVariables</literal>” property,
            so that it can do a proper merge without creating duplicates.</para>
    </section>
</chapter>
