<commit_before>package org.jetbrains.jps.incremental.scala

import java.io._

import org.jetbrains.jps.builders.java.JavaBuilderUtil
import org.jetbrains.jps.incremental.messages.{BuildMessage, CompilerMessage}
import org.jetbrains.jps.incremental.{CompileContext, FSCache, ProjectBuildException}
import org.jetbrains.plugin.scala.compiler.IncrementalType

import scala.collection.JavaConverters._

/**
 * Nikolay.Tropin
 * 12/5/13
 */
class IncrementalTypeChecker(context: CompileContext) {

  def checkAndUpdate() {
    val settings = SettingsManager.getProjectSettings(context.getProjectDescriptor)
    val previousIncrementalType = getPreviousIncrementalType
    val incrType = settings.incrementalType
    previousIncrementalType match {
      case _ if JavaBuilderUtil.isForcedRecompilationAllJavaModules(context) => //isRebiuld
        setPreviousIncrementalType(incrType)
      case None =>
        ScalaBuilderDelegate.Log.info("scala: cannot find type of the previous incremental compiler, full rebuild may be required")
      case Some(`incrType`) => //same incremental type, nothing to be done
      case Some(_) if isMakeProject =>
        cleanCaches()
        setPreviousIncrementalType(incrType)
        context.processMessage(new CompilerMessage("scala", BuildMessage.Kind.WARNING,
          "type of incremental compiler has been changed, full rebuild..."))
      case Some(_) =>
        throw new ProjectBuildException("scala: type of incremental compiler has been changed, full rebuild is required")
    }
  }

  def cleanCaches() {
    context.getProjectDescriptor.setFSCache(FSCache.NO_CACHE)
    deleteProjectSystemDirectory()
  }

  def setPreviousIncrementalType(incrType: IncrementalType) {
    storageFile.foreach { file =>
      val parentDir = file.getParentFile
      if (!parentDir.exists()) parentDir.mkdirs()
      using(new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)))) {
        _.writeUTF(incrType.name)
      }
    }
  }

  private def isMakeProject: Boolean = JavaBuilderUtil.isCompileJavaIncrementally(context) && {
    for {
      chunk <- context.getProjectDescriptor.getBuildTargetIndex.getSortedTargetChunks(context).asScala
      target <- chunk.getTargets.asScala
    } {
      if (!context.getScope.isAffected(target)) return false
    }
    true
  }

  private def deleteProjectSystemDirectory() {
    def delete(f: File) {
      if (f.isDirectory) {
        f.listFiles.foreach(delete)
      }
      f.delete()
    }

    try {
      val directory = context.getProjectDescriptor.dataManager.getDataPaths.getDataStorageRoot
      delete(directory)
    }
    catch {
      case e: Exception => throw new IOException("Can not delete project system directory: \n" + e.getMessage)
    }
  }

  private def getPreviousIncrementalType: Option[IncrementalType] = {
    storageFile.filter(_.exists).flatMap { file =>
      using(new DataInputStream(new BufferedInputStream(new FileInputStream(file)))) { in =>
        try {
          Some(IncrementalType.valueOf(in.readUTF()))
        } catch {
          case _: IOException => None
        }
      }
    }<commit_msg>exceptions of enum's valueOf are handled in IncrementalTypeChecker<commit_after>package org.jetbrains.jps.incremental.scala

import java.io._

import org.jetbrains.jps.builders.java.JavaBuilderUtil
import org.jetbrains.jps.incremental.messages.{BuildMessage, CompilerMessage}
import org.jetbrains.jps.incremental.{CompileContext, FSCache, ProjectBuildException}
import org.jetbrains.plugin.scala.compiler.IncrementalType

import scala.collection.JavaConverters._

/**
 * Nikolay.Tropin
 * 12/5/13
 */
class IncrementalTypeChecker(context: CompileContext) {

  def checkAndUpdate() {
    val settings = SettingsManager.getProjectSettings(context.getProjectDescriptor)
    val previousIncrementalType = getPreviousIncrementalType
    val incrType = settings.incrementalType
    previousIncrementalType match {
      case _ if JavaBuilderUtil.isForcedRecompilationAllJavaModules(context) => //isRebiuld
        setPreviousIncrementalType(incrType)
      case None =>
        ScalaBuilderDelegate.Log.info("scala: cannot find type of the previous incremental compiler, full rebuild may be required")
      case Some(`incrType`) => //same incremental type, nothing to be done
      case Some(_) if isMakeProject =>
        cleanCaches()
        setPreviousIncrementalType(incrType)
        context.processMessage(new CompilerMessage("scala", BuildMessage.Kind.WARNING,
          "type of incremental compiler has been changed, full rebuild..."))
      case Some(_) =>
        throw new ProjectBuildException("scala: type of incremental compiler has been changed, full rebuild is required")
    }
  }

  def cleanCaches() {
    context.getProjectDescriptor.setFSCache(FSCache.NO_CACHE)
    deleteProjectSystemDirectory()
  }

  def setPreviousIncrementalType(incrType: IncrementalType) {
    storageFile.foreach { file =>
      val parentDir = file.getParentFile
      if (!parentDir.exists()) parentDir.mkdirs()
      using(new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)))) {
        _.writeUTF(incrType.name)
      }
    }
  }

  private def isMakeProject: Boolean = JavaBuilderUtil.isCompileJavaIncrementally(context) && {
    for {
      chunk <- context.getProjectDescriptor.getBuildTargetIndex.getSortedTargetChunks(context).asScala
      target <- chunk.getTargets.asScala
    } {
      if (!context.getScope.isAffected(target)) return false
    }
    true
  }

  private def deleteProjectSystemDirectory() {
    def delete(f: File) {
      if (f.isDirectory) {
        f.listFiles.foreach(delete)
      }
      f.delete()
    }

    try {
      val directory = context.getProjectDescriptor.dataManager.getDataPaths.getDataStorageRoot
      delete(directory)
    }
    catch {
      case e: Exception => throw new IOException("Can not delete project system directory: \n" + e.getMessage)
    }
  }

  private def getPreviousIncrementalType: Option[IncrementalType] = {
    storageFile.filter(_.exists).flatMap { file =>
      val result = using(new DataInputStream(new BufferedInputStream(new FileInputStream(file)))) { in =>
        try {
          Some(IncrementalType.valueOf(in.readUTF()))
        } catch {
          case _: IOException | _: IllegalArgumentException | _: NullPointerException => None
        }
      }
      if (result.isEmpty) file.delete()
      result
    }
{'fraction_non_alphanumeric': 0.09219307775880801, 'fraction_numerical': 0.0015520720161415489, 'mean_word_length': 3.579957356076759, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, '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': '536740', 'n_tokens_mistral': 1863, 'n_tokens_neox': 1772, 'n_words': 377}