import org.gradle.api.internal.artifacts.dependencies.DefaultSelfResolvingDependency

allprojects {
	apply plugin: "idea"
}

configure(groovyProjects()) {
    idea {
        module {
            scopes.RUNTIME.plus.add(configurations.detachedConfiguration(new DefaultSelfResolvingDependency(files { sourceSets.main.resources.srcDirs })))
            scopes.TEST.plus.add(configurations.detachedConfiguration(new DefaultSelfResolvingDependency(files { sourceSets*.resources*.srcDirs })))
        }
    }
}

idea {
    module {
        excludeDirs += file('intTestHomeDir')
        excludeDirs += file('buildSrc/build')
        excludeDirs += file('buildSrc/.gradle')
        excludeDirs += file('performanceTest/.gradle')
        excludeDirs += file('performanceTest/build')
        excludeDirs += file('website/.gradle')
        excludeDirs += file('website/build')
    }

    project {
        wildcards += ['?*.gradle']
        javaVersion = '1.6'
        languageLevel = '1.5'

        ipr {

            // Exclude resource directories from compilation and add them back in as classpath resources
            withXml { provider ->
                def node = provider.asNode()
                def compilerConfig = node.component.find { it.'@name' == 'CompilerConfiguration' }
                def exclude = compilerConfig.appendNode('excludeFromCompile')
                Collection resourceDirs = groovyProjects().collect { project -> project.sourceSets*.resources*.srcDirs }.flatten()
                resourceDirs.each {
                    exclude.appendNode('directory', [url: "file://\$PROJECT_DIR\$/${rootProject.relativePath(it)}", includeSubdirectories: true])
                }
            }

            withXml { provider ->
                def node = provider.asNode()

                // Use git
                def vcsConfig = node.component.find { it.'@name' == 'VcsDirectoryMappings' }
                vcsConfig.mapping[0].'@vcs' = 'Git'

                // Set gradle home
                def gradleSettings = node.appendNode('component', [name: 'GradleSettings'])
                gradleSettings.appendNode('option', [name: 'SDK_HOME', value: gradle.gradleHomeDir.absolutePath])

                // Code formatting options
                def codeFormatSettings = new XmlParser().parseText('''
          <component name="CodeStyleSettingsManager">
            <option name="PER_PROJECT_SETTINGS">
              <value>
                <option name="USE_SAME_INDENTS" value="true" />
                <option name="RIGHT_MARGIN" value="200" />
                <option name="JD_ALIGN_PARAM_COMMENTS" value="false" />
                <option name="JD_ALIGN_EXCEPTION_COMMENTS" value="false" />
                <option name="JD_P_AT_EMPTY_LINES" value="false" />
                <option name="JD_KEEP_EMPTY_PARAMETER" value="false" />
                <option name="JD_KEEP_EMPTY_EXCEPTION" value="false" />
                <option name="JD_KEEP_EMPTY_RETURN" value="false" />
                <option name="WRAP_COMMENTS" value="true" />
                <option name="IF_BRACE_FORCE" value="3" />
                <option name="DOWHILE_BRACE_FORCE" value="3" />
                <option name="WHILE_BRACE_FORCE" value="3" />
                <option name="FOR_BRACE_FORCE" value="3" />
                <ADDITIONAL_INDENT_OPTIONS fileType="groovy">
                  <option name="INDENT_SIZE" value="2" />
                  <option name="CONTINUATION_INDENT_SIZE" value="8" />
                  <option name="TAB_SIZE" value="4" />
                  <option name="USE_TAB_CHARACTER" value="false" />
                  <option name="SMART_TABS" value="false" />
                  <option name="LABEL_INDENT_SIZE" value="0" />
                  <option name="LABEL_INDENT_ABSOLUTE" value="false" />
                  <option name="USE_RELATIVE_INDENTS" value="false" />
                </ADDITIONAL_INDENT_OPTIONS>
                <ADDITIONAL_INDENT_OPTIONS fileType="java">
                  <option name="INDENT_SIZE" value="4" />
                  <option name="CONTINUATION_INDENT_SIZE" value="8" />
                  <option name="TAB_SIZE" value="4" />
                  <option name="USE_TAB_CHARACTER" value="false" />
                  <option name="SMART_TABS" value="false" />
                  <option name="LABEL_INDENT_SIZE" value="0" />
                  <option name="LABEL_INDENT_ABSOLUTE" value="false" />
                  <option name="USE_RELATIVE_INDENTS" value="false" />
                </ADDITIONAL_INDENT_OPTIONS>
                <ADDITIONAL_INDENT_OPTIONS fileType="js">
                  <option name="INDENT_SIZE" value="4" />
                  <option name="CONTINUATION_INDENT_SIZE" value="8" />
                  <option name="TAB_SIZE" value="4" />
                  <option name="USE_TAB_CHARACTER" value="false" />
                  <option name="SMART_TABS" value="false" />
                  <option name="LABEL_INDENT_SIZE" value="0" />
                  <option name="LABEL_INDENT_ABSOLUTE" value="false" />
                  <option name="USE_RELATIVE_INDENTS" value="false" />
                </ADDITIONAL_INDENT_OPTIONS>
                <ADDITIONAL_INDENT_OPTIONS fileType="jsp">
                  <option name="INDENT_SIZE" value="4" />
                  <option name="CONTINUATION_INDENT_SIZE" value="8" />
                  <option name="TAB_SIZE" value="4" />
                  <option name="USE_TAB_CHARACTER" value="false" />
                  <option name="SMART_TABS" value="false" />
                  <option name="LABEL_INDENT_SIZE" value="0" />
                  <option name="LABEL_INDENT_ABSOLUTE" value="false" />
                  <option name="USE_RELATIVE_INDENTS" value="false" />
                </ADDITIONAL_INDENT_OPTIONS>
                <ADDITIONAL_INDENT_OPTIONS fileType="php">
                  <option name="INDENT_SIZE" value="4" />
                  <option name="CONTINUATION_INDENT_SIZE" value="8" />
                  <option name="TAB_SIZE" value="4" />
                  <option name="USE_TAB_CHARACTER" value="false" />
                  <option name="SMART_TABS" value="false" />
                  <option name="LABEL_INDENT_SIZE" value="0" />
                  <option name="LABEL_INDENT_ABSOLUTE" value="false" />
                  <option name="USE_RELATIVE_INDENTS" value="false" />
                </ADDITIONAL_INDENT_OPTIONS>
                <ADDITIONAL_INDENT_OPTIONS fileType="scala">
                  <option name="INDENT_SIZE" value="2" />
                  <option name="CONTINUATION_INDENT_SIZE" value="2" />
                  <option name="TAB_SIZE" value="2" />
                  <option name="USE_TAB_CHARACTER" value="false" />
                  <option name="SMART_TABS" value="false" />
                  <option name="LABEL_INDENT_SIZE" value="0" />
                  <option name="LABEL_INDENT_ABSOLUTE" value="false" />
                  <option name="USE_RELATIVE_INDENTS" value="false" />
                </ADDITIONAL_INDENT_OPTIONS>
                <ADDITIONAL_INDENT_OPTIONS fileType="sql">
                  <option name="INDENT_SIZE" value="2" />
                  <option name="CONTINUATION_INDENT_SIZE" value="8" />
                  <option name="TAB_SIZE" value="4" />
                  <option name="USE_TAB_CHARACTER" value="false" />
                  <option name="SMART_TABS" value="false" />
                  <option name="LABEL_INDENT_SIZE" value="0" />
                  <option name="LABEL_INDENT_ABSOLUTE" value="false" />
                  <option name="USE_RELATIVE_INDENTS" value="false" />
                </ADDITIONAL_INDENT_OPTIONS>
                <ADDITIONAL_INDENT_OPTIONS fileType="xml">
                  <option name="INDENT_SIZE" value="4" />
                  <option name="CONTINUATION_INDENT_SIZE" value="8" />
                  <option name="TAB_SIZE" value="4" />
                  <option name="USE_TAB_CHARACTER" value="false" />
                  <option name="SMART_TABS" value="false" />
                  <option name="LABEL_INDENT_SIZE" value="0" />
                  <option name="LABEL_INDENT_ABSOLUTE" value="false" />
                  <option name="USE_RELATIVE_INDENTS" value="false" />
                </ADDITIONAL_INDENT_OPTIONS>
              </value>
            </option>
            <option name="USE_PER_PROJECT_SETTINGS" value="true" />
          </component>
        ''')
                node.append(codeFormatSettings)
            }
        }
    }

    workspace.iws.withXml { provider ->
        Node node = provider.asNode()

        Node runManagerConfig = node.component.find { it.'@name' == 'RunManager' }

        // Add int test configuration to JUnit defaults
        Node runConfig = runManagerConfig.configuration.find { it.'@type' == 'JUnit'}

        Node vmParameters = runConfig.option.find { it.'@name' == 'VM_PARAMETERS' }
        vmParameters.'@value' = "\"-DintegTest.gradleHomeDir=${intTestImage.destinationDir}\" -ea -Dorg.gradle.integtest.executer=embedded -XX:MaxPermSize=256m -Xmx512m"

        // Add an application configuration
        runManagerConfig.'@selected' = 'Application.Gradle'
        def appConfig = new XmlParser().parseText('''
    <configuration default="false" name="Gradle" type="Application" factoryName="Application">
      <extension name="coverage" enabled="false" merge="false" />
      <option name="MAIN_CLASS_NAME" value="org.gradle.debug.GradleRunConfiguration" />
      <option name="VM_PARAMETERS" value="" />
      <option name="PROGRAM_PARAMETERS" value="" />
      <option name="WORKING_DIRECTORY" value="file://$PROJECT_DIR$" />
      <option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
      <option name="ALTERNATIVE_JRE_PATH" value="" />
      <option name="ENABLE_SWING_INSPECTOR" value="false" />
      <option name="ENV_VARIABLES" />
      <option name="PASS_PARENT_ENVS" value="true" />
      <module name="integTest" />
      <envs />
      <RunnerSettings RunnerId="Debug">
        <option name="DEBUG_PORT" value="63810" />
        <option name="TRANSPORT" value="0" />
        <option name="LOCAL" value="true" />
      </RunnerSettings>
      <RunnerSettings RunnerId="Run" />
      <ConfigurationWrapper RunnerId="Debug" />
      <ConfigurationWrapper RunnerId="Run" />
      <method />
    </configuration>
''')
        runManagerConfig.append(appConfig)
    }
}
