<commit_before>/*
 * Copyright (c) 2011-2012, <NAME>, <NAME>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package maker.project

import maker.utils.FileUtils._
import java.io.File
import maker.task.compile._
import maker.utils.RichString._
import maker.MakerProps

case class IDEAProjectGenerator(props : MakerProps) {
  val scalaVersion = props.ProjectScalaVersion()

  def generateTopLevelModule(rootDir:File, name:String, excludedFolders:List[String]) {

    val formattedExcludedFolders = if (excludedFolders.nonEmpty) {
      val excludedFoldersString = excludedFolders.sorted.map(folder => {
        """      <excludeFolder url="file://$MODULE_DIR$/%s" />""" % folder
      }).mkString("\n")
      """    <content url="file://$MODULE_DIR$">
%s
    </content>""" % excludedFoldersString
    } else {
        """<content url="file://$MODULE_DIR$" />"""
    }

    val content = """<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" inherit-compiler-output="false">
    <output url="file://$MODULE_DIR$/never-created-classes" />
    <output-test url="file://$MODULE_DIR$/never-created-test-classes" />
    <exclude-output />
%s
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
  </component>
</module>
""".format(formattedExcludedFolders)

    writeToFileIfDifferent(file(rootDir, name + ".iml"), content)
  }

  def generateIDEAProjectDir(rootDir:File, name:String) {
    val ideaProjectRoot = mkdir(file(rootDir, ".idea"))
    writeToFileIfDifferent(file(ideaProjectRoot, ".name"), name)

    // For now we are insisting that there is a "scala directory" at this location: $PROJECT_DIR$/lib/scala/lib_managed/
    // Obviously, this isn't very good and we should allow it to be specified.
    List(props.ProjectScalaLibraryJar(), props.ProjectScalaCompilerJar()).foreach{
      jar => 
      if (!jar.exists) {
        throw new Exception("Maker requires that file " + jar + " + be present. Launch maker with the '-y' flag to download it")
      }
    }

    val librariesDir = mkdir(file(ideaProjectRoot, "libraries"))
    val scalaLibraryContent = """<component name="libraryTable">
  <library name="scala-library-%s">
    <CLASSES>
      <root url="jar://$PROJECT_DIR$/%s!/" />
    </CLASSES>
    <JAVADOC />
    <SOURCES>
      <root url="jar://$PROJECT_DIR$/%s!/" />
    </SOURCES>
  </library>
</component>
""" % (scalaVersion, props.ProjectScalaLibraryJar().getPath, props.ProjectScalaLibrarySourceJar())
    writeToFileIfDifferent(file(librariesDir, "scala_library_%s.xml" % scalaVersion.replace('.', '_')), scalaLibraryContent)

    val scalaCompilerLibraryContent = """<component name="libraryTable">
  <library name="scala-compiler-%s">
    <CLASSES>
      <root url="jar://$PROJECT_DIR$/%s!/" />
      <root url="jar://$PROJECT_DIR$/%s!/" />
    </CLASSES>
    <JAVADOC />
    <SOURCES />
  </library>
</component>
""" % (scalaVersion, props.ProjectScalaCompilerJar(), props.ProjectScalaLibraryJar())
    writeToFileIfDifferent(file(librariesDir, "scala_compiler_%s.xml" % scalaVersion.replace('.', '_')), scalaCompilerLibraryContent)

    val scalaCompilerContent = """<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="ScalacSettings">
    <option name="COMPILER_LIBRARY_NAME" value="scala-compiler-%s" />
    <option name="COMPILER_LIBRARY_LEVEL" value="Project" />
    <option name="MAXIMUM_HEAP_SIZE" value="2048" />
    <option name="FSC_OPTIONS" value="-max-idle 0" />
  </component>
</project>
""" % scalaVersion
    writeToFileIfDifferent(file(ideaProjectRoot, "scala_compiler.xml"), scalaCompilerContent)

    val miscContent = """<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" assert-keyword="true" jdk-15="true" project-jdk-name="JDK6" project-jdk-type="JavaSDK">
    <output url="file://$PROJECT_DIR$/out" />
  </component>
</module>
"""
    writeMiscFileIfRequired(file(ideaProjectRoot, "misc.xml"), miscContent)

    var gitDirFound = false
    var gitDir = rootDir.getAbsoluteFile
    while (!gitDirFound && (gitDir != null)) {
      val fileToCheck = file(gitDir, ".git")
      if (fileToCheck.exists) {
        gitDirFound = true
      } else {
        gitDir = gitDir.getParentFile
      }
    }
    if (gitDir != null) {
      val relDirectory = {
        val rd = gitDir.relativeTo(rootDir).toString
        if (rd.isEmpty) {
          ""
        } else {
          "$PROJECT_DIR$/" + rd
        }<commit_msg>Update the default JDK to JDK7 in master when generating the IDEA project definition.<commit_after>/*
 * Copyright (c) 2011-2012, <NAME>, <NAME>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package maker.project

import maker.utils.FileUtils._
import java.io.File
import maker.task.compile._
import maker.utils.RichString._
import maker.MakerProps

case class IDEAProjectGenerator(props : MakerProps) {
  val scalaVersion = props.ProjectScalaVersion()

  def generateTopLevelModule(rootDir:File, name:String, excludedFolders:List[String]) {

    val formattedExcludedFolders = if (excludedFolders.nonEmpty) {
      val excludedFoldersString = excludedFolders.sorted.map(folder => {
        """      <excludeFolder url="file://$MODULE_DIR$/%s" />""" % folder
      }).mkString("\n")
      """    <content url="file://$MODULE_DIR$">
%s
    </content>""" % excludedFoldersString
    } else {
        """<content url="file://$MODULE_DIR$" />"""
    }

    val content = """<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" inherit-compiler-output="false">
    <output url="file://$MODULE_DIR$/never-created-classes" />
    <output-test url="file://$MODULE_DIR$/never-created-test-classes" />
    <exclude-output />
%s
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
  </component>
</module>
""".format(formattedExcludedFolders)

    writeToFileIfDifferent(file(rootDir, name + ".iml"), content)
  }

  def generateIDEAProjectDir(rootDir:File, name:String) {
    val ideaProjectRoot = mkdir(file(rootDir, ".idea"))
    writeToFileIfDifferent(file(ideaProjectRoot, ".name"), name)

    // For now we are insisting that there is a "scala directory" at this location: $PROJECT_DIR$/lib/scala/lib_managed/
    // Obviously, this isn't very good and we should allow it to be specified.
    List(props.ProjectScalaLibraryJar(), props.ProjectScalaCompilerJar()).foreach{
      jar => 
      if (!jar.exists) {
        throw new Exception("Maker requires that file " + jar + " + be present. Launch maker with the '-y' flag to download it")
      }
    }

    val librariesDir = mkdir(file(ideaProjectRoot, "libraries"))
    val scalaLibraryContent = """<component name="libraryTable">
  <library name="scala-library-%s">
    <CLASSES>
      <root url="jar://$PROJECT_DIR$/%s!/" />
    </CLASSES>
    <JAVADOC />
    <SOURCES>
      <root url="jar://$PROJECT_DIR$/%s!/" />
    </SOURCES>
  </library>
</component>
""" % (scalaVersion, props.ProjectScalaLibraryJar().getPath, props.ProjectScalaLibrarySourceJar())
    writeToFileIfDifferent(file(librariesDir, "scala_library_%s.xml" % scalaVersion.replace('.', '_')), scalaLibraryContent)

    val scalaCompilerLibraryContent = """<component name="libraryTable">
  <library name="scala-compiler-%s">
    <CLASSES>
      <root url="jar://$PROJECT_DIR$/%s!/" />
      <root url="jar://$PROJECT_DIR$/%s!/" />
    </CLASSES>
    <JAVADOC />
    <SOURCES />
  </library>
</component>
""" % (scalaVersion, props.ProjectScalaCompilerJar(), props.ProjectScalaLibraryJar())
    writeToFileIfDifferent(file(librariesDir, "scala_compiler_%s.xml" % scalaVersion.replace('.', '_')), scalaCompilerLibraryContent)

    val scalaCompilerContent = """<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="ScalacSettings">
    <option name="COMPILER_LIBRARY_NAME" value="scala-compiler-%s" />
    <option name="COMPILER_LIBRARY_LEVEL" value="Project" />
    <option name="MAXIMUM_HEAP_SIZE" value="2048" />
    <option name="FSC_OPTIONS" value="-max-idle 0" />
  </component>
</project>
""" % scalaVersion
    writeToFileIfDifferent(file(ideaProjectRoot, "scala_compiler.xml"), scalaCompilerContent)

    val miscContent = """<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" assert-keyword="true" jdk-15="true" project-jdk-name="JDK7" project-jdk-type="JavaSDK">
    <output url="file://$PROJECT_DIR$/out" />
  </component>
</module>
"""
    writeMiscFileIfRequired(file(ideaProjectRoot, "misc.xml"), miscContent)

    var gitDirFound = false
    var gitDir = rootDir.getAbsoluteFile
    while (!gitDirFound && (gitDir != null)) {
      val fileToCheck = file(gitDir, ".git")
      if (fileToCheck.exists) {
        gitDirFound = true
      } else {
        gitDir = gitDir.getParentFile
      }
    }
    if (gitDir != null) {
      val relDirectory = {
        val rd = gitDir.relativeTo(rootDir).toString
        if (rd.isEmpty) {
          ""
        } else {
          "$PROJECT_DIR$/" + rd
        }
{'fraction_non_alphanumeric': 0.13527020159241063, 'fraction_numerical': 0.005675080467558868, 'mean_word_length': 3.9463762044407207, 'pattern_counts': {'":': 0, '<': 107, '<?xml version=': 6, '>': 111, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5930268', 'n_tokens_mistral': 3747, 'n_tokens_neox': 3482, 'n_words': 935}