<commit_before>import sbt.Keys._

val ScalaVersion                  = "2.11.8"
val ScalaJsDomVersion             = "0.9.0"
val ScalaJsJQueryVersion          = "0.9.0"
val JUnitInterfaceVersion         = "0.11"
val UPickleVersion                = "0.4.1"

val DefaultOrbeonFormsVersion     = "2016.2-SNAPSHOT"
val DefaultOrbeonEdition          = "CE"

val ExplodedWarWebInf             = "build/orbeon-war/WEB-INF"
val ExplodedWarLibPath            = ExplodedWarWebInf + "/lib"

val ExplodedWarResourcesPath      = ExplodedWarWebInf + "/resources"
val FormBuilderResourcesPathInWar = "forms/orbeon/builder/resources"

val MatchScalaJSFileNameFormatRE  = """((.+)-(fastopt|opt)).js""".r
val MatchRawJarNameRE             = """([^_]+)(?:_.*)?\.jar""".r

val copyJarToExplodedWar          = taskKey[Option[File]]("Copy JAR file to local WEB-INF/bin for development.")
val fastOptJSToExplodedWar        = taskKey[Unit]("Copy fast-optimized JavaScript files to the exploded WAR.")
val fullOptJSToExplodedWar        = taskKey[Unit]("Copy full-optimized JavaScript files to the exploded WAR.")

val orbeonVersionFromProperties   = settingKey[String]("Orbeon Forms version from system properties.")
val orbeonEditionFromProperties   = settingKey[String]("Orbeon Forms edition from system properties.")

// TBH I don't know whether `in ThisBuild` is needed
orbeonVersionFromProperties in ThisBuild := sys.props.get("orbeon.version") getOrElse DefaultOrbeonFormsVersion
orbeonEditionFromProperties in ThisBuild := sys.props.get("orbeon.edition") getOrElse DefaultOrbeonEdition

val PathsToExcludeFromCoreJAR = Nil

val JarFilesToExcludeFromWar = Set(
  "orbeon-form-builder-client",
  "orbeon-form-builder-shared",
  "orbeon-proxy-portlet"
)

val TestResourceManagerPaths = List(
  "src/test/resources",    // so that Java processor tests work
  "src/resources",         // ultimately should be moved
  "src/resources-packaged" // ultimately should be moved
)

val ResourceManagerProperties: List[String] = {

  val pkg = "org.orbeon.oxf.resources"

  val props =
    for ((dir, i) ← TestResourceManagerPaths.zipWithIndex)
      yield
        s"-Doxf.resources.priority.${i + 1}=$pkg.FilesystemResourceManagerFactory"           ::
        s"-Doxf.resources.priority.${i + 1}.oxf.resources.filesystem.sandbox-directory=$dir" ::
        Nil

  s"-Doxf.resources.factory=$pkg.PriorityResourceManagerFactory"                             ::
    props.flatten                                                                            :::
    s"-Doxf.resources.priority.${props.size + 1}=$pkg.ClassLoaderResourceManagerFactory"     ::
    Nil
}

val TestJavaOptions = List(
  "-ea",
  "-server",
  "-Djava.awt.headless=true",
  "-Xms256m",
  "-Xmx2G",
  // Unneeded with Java 8
  "-XX:MaxPermSize=512m",
  // Some code uses the default time zone, which might different on different system, so we need to set it explicitly
  "-Duser.timezone=America/Los_Angeles",
  "-Doxf.resources.common.min-reload-interval=50",
//  "-Djava.io.tmpdir=build/temp/test",
  // Getting a JDK error, per http://stackoverflow.com/a/13575810/5295
  "-Djava.util.Arrays.useLegacyMergeSort=true"
) ++ ResourceManagerProperties

val JUnitTestArguments = List(
  //"-q",
  "-v",
  "-s",
  "-a"
  //"--run-listener=org.orbeon.junit.OrbeonJUnitRunListener",
) ++ ResourceManagerProperties

val JunitTestOptions = List(
  libraryDependencies                += "com.novocode" % "junit-interface" % JUnitInterfaceVersion % "test",
  //    libraryDependencies          += "com.lihaoyi" %% "pprint" % "0.4.1" % "test",

  testOptions       in Test          += Tests.Argument(TestFrameworks.JUnit, JUnitTestArguments: _*),
  testOptions       in Test          += Tests.Filter(s ⇒ s.endsWith("Test")),
  testOptions       in Test          += Tests.Filter(s ⇒ s.endsWith("Test") && ! s.contains("CombinedClientTest")),
  parallelExecution in Test          := false,
  fork              in Test          := true, // "By default, tests executed in a forked JVM are executed sequentially"
  javaOptions       in Test          ++= TestJavaOptions,
  baseDirectory     in Test          := baseDirectory.value / ".."
)

def deleteAndCreateTempTestDirectory(base: File) = {

  val dir = base / "build" / "temp" / "test"

  IO.delete(dir)
  IO.createDirectory(dir)
}

def copyScalaJSToExplodedWar(sourceFile: File, rootDirectory: File): Unit = {

  val (prefix, optType) =
    sourceFile.name match { case MatchScalaJSFileNameFormatRE(_, prefix, optType) ⇒ prefix → optType }

  val launcherName  = s"$prefix-launcher.js"
  val jsdepsName    = s"$prefix-jsdeps${if (optType == "opt") ".min" else ""}.js"
  val sourceMapName = s"${sourceFile.name}.map"

  val targetDir =
    rootDirectory / ExplodedWarResourcesPath / FormBuilderResourcesPathInWar / "scalajs"

  IO.createDirectory(targetDir)

  val names = List(
    sourceFile                                 → s"$prefix.js",
    (sourceFile.getParentFile / launcherName)  → launcherName,
    (sourceFile.getParentFile / jsdepsName)    → jsdepsName,
    (sourceFile.getParentFile / sourceMapName) → sourceMapName
  )

  for ((sourceFile, newName) ← names) {
    val targetFile = targetDir / newName
    println(s"Copying Scala.js file ${sourceFile.name} to ${targetFile.absolutePath}.")
    IO.copyFile(
      sourceFile           = sourceFile,
      targetFile           = targetFile,
      preserveLastModified = true
    )
  }
}

lazy val DatabaseTest = config("db")         extend Test
lazy val DebugTest    = config("debug-test") extend Test

lazy val commonSettings = Seq(
  organization                  := "org.orbeon",
  version                       := orbeonVersionFromProperties.value,
  scalaVersion                  := ScalaVersion,

  jsEnv                         := JSDOMNodeJSEnv().value,

  javacOptions  ++= Seq(
    "-encoding", "utf8",
    "-source", "1.6",
    "-target", "1.6"
  ),
  scalacOptions ++= Seq(
    "-encoding", "utf8",
    "-feature",
    "-language:postfixOps",
    "-language:reflectiveCalls",
    "-language:implicitConversions",
    "-language:higherKinds",
    "-language:existentials"
    // Consider the following flags
//    "-deprecation",
//    "-unchecked",
//    "-Xfatal-warnings",
//    "-Xlint",
//    "-Yno-adapted-args",
//    "-Ywarn-dead-code",        // N.B. doesn't work well with the ??? hole
//    "-Ywarn-numeric-widen",
//    "-Ywarn-value-discard",
//    "-Xfuture",
//    "-Ywarn-unused-import"     // 2.11 only
  ),

  copyJarToExplodedWar := {

    val sourceJarFile = (packageBin in Compile).value
    val MatchRawJarNameRE(sourceJarRawName) = sourceJarFile.name
    val targetJarFile = new File(ExplodedWarLibPath + '/' + sourceJarRawName + ".jar")

    if (! sourceJarFile.name.contains("_sjs")        &&
        ! JarFilesToExcludeFromWar(sourceJarRawName) &&
        (! targetJarFile.exists || sourceJarFile.lastModified > targetJarFile.lastModified)) {
      println(s"Copying JAR ${sourceJarFile.name} to ${targetJarFile.absolutePath}.")
      IO.copy(List(sourceJarFile → targetJarFile), overwrite = false, preserveLastModified = false)
      Some(targetJarFile)
    } else {
      None
    }
  }
)

lazy val common = (crossProject.crossType(CrossType.Full) in file("common"))
  .settings(commonSettings: _*)
  .settings(
    name := "orbeon-common"
  )
  .jvmSettings(
    unmanagedBase := baseDirectory.value / ".." / ".." / "lib"
  )
  .jsSettings(
    libraryDependencies += "org.scalactic" %%% "scalactic" % "3.0.0" % "test",
    libraryDependencies += "org.scalatest" %%% "scalatest" % "3.0.0" % "test"
  )<commit_msg>sbt: copy task for Liferay dev<commit_after>import sbt.Keys._

val ScalaVersion                  = "2.11.8"
val ScalaJsDomVersion             = "0.9.0"
val ScalaJsJQueryVersion          = "0.9.0"
val JUnitInterfaceVersion         = "0.11"
val UPickleVersion                = "0.4.1"

val DefaultOrbeonFormsVersion     = "2016.2-SNAPSHOT"
val DefaultOrbeonEdition          = "CE"

val ExplodedWarWebInf             = "build/orbeon-war/WEB-INF"
val ExplodedWarLibPath            = ExplodedWarWebInf + "/lib"
val LiferayWarLibPath             = "/Users/ebruchez/Google Drive/Work/liferay-portal-6.2-ce-ga6/tomcat-7.0.62/webapps/proxy-portlet/WEB-INF/lib"

val ExplodedWarResourcesPath      = ExplodedWarWebInf + "/resources"
val FormBuilderResourcesPathInWar = "forms/orbeon/builder/resources"

val MatchScalaJSFileNameFormatRE  = """((.+)-(fastopt|opt)).js""".r
val MatchRawJarNameRE             = """([^_]+)(?:_.*)?\.jar""".r

val copyJarToExplodedWar          = taskKey[Option[File]]("Copy JAR file to local WEB-INF/lib for development.")
val copyJarToLiferayWar           = taskKey[Option[File]]("Copy JAR file to Liferay WEB-INF/lib for development.")
val fastOptJSToExplodedWar        = taskKey[Unit]("Copy fast-optimized JavaScript files to the exploded WAR.")
val fullOptJSToExplodedWar        = taskKey[Unit]("Copy full-optimized JavaScript files to the exploded WAR.")

val orbeonVersionFromProperties   = settingKey[String]("Orbeon Forms version from system properties.")
val orbeonEditionFromProperties   = settingKey[String]("Orbeon Forms edition from system properties.")

// TBH I don't know whether `in ThisBuild` is needed
orbeonVersionFromProperties in ThisBuild := sys.props.get("orbeon.version") getOrElse DefaultOrbeonFormsVersion
orbeonEditionFromProperties in ThisBuild := sys.props.get("orbeon.edition") getOrElse DefaultOrbeonEdition

val PathsToExcludeFromCoreJAR = Nil

val JarFilesToExcludeFromWar = Set(
  "orbeon-form-builder-client",
  "orbeon-form-builder-shared",
  "orbeon-proxy-portlet"
)

val JarFilesToExcludeFromLiferayWar = Set(
  "orbeon-form-builder-client",
  "orbeon-form-builder-shared"
)

val TestResourceManagerPaths = List(
  "src/test/resources",    // so that Java processor tests work
  "src/resources",         // ultimately should be moved
  "src/resources-packaged" // ultimately should be moved
)

val ResourceManagerProperties: List[String] = {

  val pkg = "org.orbeon.oxf.resources"

  val props =
    for ((dir, i) ← TestResourceManagerPaths.zipWithIndex)
      yield
        s"-Doxf.resources.priority.${i + 1}=$pkg.FilesystemResourceManagerFactory"           ::
        s"-Doxf.resources.priority.${i + 1}.oxf.resources.filesystem.sandbox-directory=$dir" ::
        Nil

  s"-Doxf.resources.factory=$pkg.PriorityResourceManagerFactory"                             ::
    props.flatten                                                                            :::
    s"-Doxf.resources.priority.${props.size + 1}=$pkg.ClassLoaderResourceManagerFactory"     ::
    Nil
}

val TestJavaOptions = List(
  "-ea",
  "-server",
  "-Djava.awt.headless=true",
  "-Xms256m",
  "-Xmx2G",
  // Unneeded with Java 8
  "-XX:MaxPermSize=512m",
  // Some code uses the default time zone, which might different on different system, so we need to set it explicitly
  "-Duser.timezone=America/Los_Angeles",
  "-Doxf.resources.common.min-reload-interval=50",
//  "-Djava.io.tmpdir=build/temp/test",
  // Getting a JDK error, per http://stackoverflow.com/a/13575810/5295
  "-Djava.util.Arrays.useLegacyMergeSort=true"
) ++ ResourceManagerProperties

val JUnitTestArguments = List(
  //"-q",
  "-v",
  "-s",
  "-a"
  //"--run-listener=org.orbeon.junit.OrbeonJUnitRunListener",
) ++ ResourceManagerProperties

val JunitTestOptions = List(
  libraryDependencies                += "com.novocode" % "junit-interface" % JUnitInterfaceVersion % "test",
  //    libraryDependencies          += "com.lihaoyi" %% "pprint" % "0.4.1" % "test",

  testOptions       in Test          += Tests.Argument(TestFrameworks.JUnit, JUnitTestArguments: _*),
  testOptions       in Test          += Tests.Filter(s ⇒ s.endsWith("Test")),
  testOptions       in Test          += Tests.Filter(s ⇒ s.endsWith("Test") && ! s.contains("CombinedClientTest")),
  parallelExecution in Test          := false,
  fork              in Test          := true, // "By default, tests executed in a forked JVM are executed sequentially"
  javaOptions       in Test          ++= TestJavaOptions,
  baseDirectory     in Test          := baseDirectory.value / ".."
)

def copyJarFile(sourceJarFile: File, destination: String, excludes: Set[String]) = {
  val MatchRawJarNameRE(sourceJarRawName) = sourceJarFile.name
  val targetJarFile = new File(destination + '/' + sourceJarRawName + ".jar")

  if (! sourceJarFile.name.contains("_sjs")        &&
      ! excludes(sourceJarRawName) &&
      (! targetJarFile.exists || sourceJarFile.lastModified > targetJarFile.lastModified)) {
    println(s"Copying JAR ${sourceJarFile.name} to ${targetJarFile.absolutePath}.")
    IO.copy(List(sourceJarFile → targetJarFile), overwrite = false, preserveLastModified = false)
    Some(targetJarFile)
  } else {
    None
  }
}

def deleteAndCreateTempTestDirectory(base: File) = {

  val dir = base / "build" / "temp" / "test"

  IO.delete(dir)
  IO.createDirectory(dir)
}

def copyScalaJSToExplodedWar(sourceFile: File, rootDirectory: File): Unit = {

  val (prefix, optType) =
    sourceFile.name match { case MatchScalaJSFileNameFormatRE(_, prefix, optType) ⇒ prefix → optType }

  val launcherName  = s"$prefix-launcher.js"
  val jsdepsName    = s"$prefix-jsdeps${if (optType == "opt") ".min" else ""}.js"
  val sourceMapName = s"${sourceFile.name}.map"

  val targetDir =
    rootDirectory / ExplodedWarResourcesPath / FormBuilderResourcesPathInWar / "scalajs"

  IO.createDirectory(targetDir)

  val names = List(
    sourceFile                                 → s"$prefix.js",
    (sourceFile.getParentFile / launcherName)  → launcherName,
    (sourceFile.getParentFile / jsdepsName)    → jsdepsName,
    (sourceFile.getParentFile / sourceMapName) → sourceMapName
  )

  for ((sourceFile, newName) ← names) {
    val targetFile = targetDir / newName
    println(s"Copying Scala.js file ${sourceFile.name} to ${targetFile.absolutePath}.")
    IO.copyFile(
      sourceFile           = sourceFile,
      targetFile           = targetFile,
      preserveLastModified = true
    )
  }
}

lazy val DatabaseTest = config("db")         extend Test
lazy val DebugTest    = config("debug-test") extend Test

lazy val commonSettings = Seq(
  organization                  := "org.orbeon",
  version                       := orbeonVersionFromProperties.value,
  scalaVersion                  := ScalaVersion,

  jsEnv                         := JSDOMNodeJSEnv().value,

  javacOptions  ++= Seq(
    "-encoding", "utf8",
    "-source", "1.6",
    "-target", "1.6"
  ),
  scalacOptions ++= Seq(
    "-encoding", "utf8",
    "-feature",
    "-language:postfixOps",
    "-language:reflectiveCalls",
    "-language:implicitConversions",
    "-language:higherKinds",
    "-language:existentials"
    // Consider the following flags
//    "-deprecation",
//    "-unchecked",
//    "-Xfatal-warnings",
//    "-Xlint",
//    "-Yno-adapted-args",
//    "-Ywarn-dead-code",        // N.B. doesn't work well with the ??? hole
//    "-Ywarn-numeric-widen",
//    "-Ywarn-value-discard",
//    "-Xfuture",
//    "-Ywarn-unused-import"     // 2.11 only
  ),

  copyJarToExplodedWar := copyJarFile((packageBin in Compile).value, ExplodedWarLibPath, JarFilesToExcludeFromWar),
  copyJarToLiferayWar  := copyJarFile((packageBin in Compile).value, LiferayWarLibPath,  JarFilesToExcludeFromLiferayWar)
)

lazy val common = (crossProject.crossType(CrossType.Full) in file("common"))
  .settings(commonSettings: _*)
  .settings(
    name := "orbeon-common"
  )
  .jvmSettings(
    unmanagedBase := baseDirectory.value / ".." / ".." / "lib"
  )
  .jsSettings(
    libraryDependencies += "org.scalactic" %%% "scalactic" % "3.0.0" % "test",
    libraryDependencies += "org.scalatest" %%% "scalatest" % "3.0.0" % "test"
  )
{'fraction_non_alphanumeric': 0.13268608414239483, 'fraction_numerical': 0.008566533409480296, 'mean_word_length': 2.9301745635910224, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1480625', 'n_tokens_mistral': 5210, 'n_tokens_neox': 5031, 'n_words': 1019}