<commit_before>package com.prezi.haskell.gradle.tasks

import java.io.File

import com.prezi.haskell.gradle.external.SnapshotVersions
import com.prezi.haskell.gradle.model.Sandbox
import org.apache.commons.io.FileUtils
import org.gradle.api.{GradleException, DefaultTask}
import org.gradle.api.tasks.TaskAction

class GenerateStackYaml
  extends DefaultTask
  with HaskellProjectSupport
  with HaskellDependencies
  with UsingHaskellTools
  with UsingGit {

  private var targetFile_ : Option[File] = None

  def targetFile = targetFile_
  def targetFile_=(value: Option[File]): Unit = {
    targetFile_ = value

    if (value.isDefined) {
      getOutputs.file(value.get)
    }
  }

  @TaskAction
  def run(): Unit = {
    needsConfigurationSet
    needsGitSet
    needsToolsSet

    if (!targetFile.isDefined) {
      throw new IllegalStateException("targetFile is not specified")
    }

    val yamlFile = generateContent(dependentSandboxes)
    FileUtils.writeStringToFile(targetFile.get, yamlFile)
  }

  private def generateContent(sandboxes: List[Sandbox]): String = {
    val content = new StringBuilder()
    val resolver = s"ghc-${haskellExtension.ghcVersion}"

    content.append(
      s"""flags: {}
        | packages:
        |   - .
        | resolver: $resolver""".stripMargin)

    content.append("\n\nextra-package-dbs:")

    if (sandboxes.isEmpty) {
      content.append(" []\n")
    } else {
      content.append('\n')
      for (sandbox <- sandboxes) {
        content.append("  - ")
        content.append(sandbox.packageDb.getAbsolutePath)
        content.append('\n')
      }
    }

    val snapshotVersions = new SnapshotVersions(haskellExtension.getEnvConfigurer, getProject.exec, tools.get, git.get)
    val deps = snapshotVersions.run(haskellExtension.snapshotId, findCabalFile())

    if (deps.length > 0) {
      content.append("extra-deps:\n")
      for (dep <- deps) {
        content.append("  - ")
        content.append(dep)
        content.append('\n')
      }
    } else {
      content.append("extra-deps: []\n")
    }

    val snapshotId = haskellExtension.snapshotId

    content.mkString
  }

  private def findCabalFile(): File =
    getProject.getRootDir.listFiles().filter(_.getName.endsWith(".cabal")).headOption match {
      case Some(file) => file
      case None => throw new GradleException(s"Could not find any .cabal files in ${getProject.getRootDir.getAbsolutePath}")
    }
}
<commit_msg>Fixed project dir for stack yaml generator<commit_after>package com.prezi.haskell.gradle.tasks

import java.io.File

import com.prezi.haskell.gradle.external.SnapshotVersions
import com.prezi.haskell.gradle.model.Sandbox
import org.apache.commons.io.FileUtils
import org.gradle.api.{GradleException, DefaultTask}
import org.gradle.api.tasks.TaskAction

class GenerateStackYaml
  extends DefaultTask
  with HaskellProjectSupport
  with HaskellDependencies
  with UsingHaskellTools
  with UsingGit {

  private var targetFile_ : Option[File] = None

  def targetFile = targetFile_
  def targetFile_=(value: Option[File]): Unit = {
    targetFile_ = value

    if (value.isDefined) {
      getOutputs.file(value.get)
    }
  }

  @TaskAction
  def run(): Unit = {
    needsConfigurationSet
    needsGitSet
    needsToolsSet

    if (!targetFile.isDefined) {
      throw new IllegalStateException("targetFile is not specified")
    }

    val yamlFile = generateContent(dependentSandboxes)
    FileUtils.writeStringToFile(targetFile.get, yamlFile)
  }

  private def generateContent(sandboxes: List[Sandbox]): String = {
    val content = new StringBuilder()
    val resolver = s"ghc-${haskellExtension.ghcVersion}"

    content.append(
      s"""flags: {}
        | packages:
        |   - .
        | resolver: $resolver""".stripMargin)

    content.append("\n\nextra-package-dbs:")

    if (sandboxes.isEmpty) {
      content.append(" []\n")
    } else {
      content.append('\n')
      for (sandbox <- sandboxes) {
        content.append("  - ")
        content.append(sandbox.packageDb.getAbsolutePath)
        content.append('\n')
      }
    }

    val snapshotVersions = new SnapshotVersions(haskellExtension.getEnvConfigurer, getProject.exec, tools.get, git.get)
    val deps = snapshotVersions.run(haskellExtension.snapshotId, findCabalFile())

    if (deps.length > 0) {
      content.append("extra-deps:\n")
      for (dep <- deps) {
        content.append("  - ")
        content.append(dep)
        content.append('\n')
      }
    } else {
      content.append("extra-deps: []\n")
    }

    content.mkString
  }

  private def findCabalFile(): File =
    getProject.getProjectDir.listFiles().find(_.getName.endsWith(".cabal")) match {
      case Some(file) => file
      case None => throw new GradleException(s"Could not find any .cabal files in ${getProject.getRootDir.getAbsolutePath}")
    }
}

{'fraction_non_alphanumeric': 0.11683494745518236, 'fraction_numerical': 0.00041211621677313, 'mean_word_length': 3.113559322033898, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5832736', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1497, 'n_words': 289}