<commit_before>package com.lightbend.coursegentools

/**
  * Copyright © 2016 Lightbend, Inc
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *
  * NO COMMERCIAL SUPPORT OR ANY OTHER FORM OF SUPPORT IS OFFERED ON
  * THIS SOFTWARE BY LIGHTBEND, Inc.
  *
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */

object Linearize {

  def main(args: Array[String]): Unit = {

    import Helpers._
    import java.io.File
    import sbt.{ IO => sbtio }

    val cmdOptions = LinearizeCmdLineOptParse.parse(args)
    if (cmdOptions.isEmpty) System.exit(-1)
    val LinearizeCmdOptions(masterRepo, linearizedOutputFolder, multiJVM, forceDeleteExistingDestinationFolder) = cmdOptions.get

    val projectName = masterRepo.getName
    val exercises: Seq[String] = getExerciseNames(masterRepo)
    val destinationFolder = new File(linearizedOutputFolder, projectName)

    (destinationFolder.exists(), forceDeleteExistingDestinationFolder) match {
      case (true, false) =>
        println(
          s"""
             |Destination folder ${destinationFolder.getPath} exists: Either remove this folder
             |manually or use the '-f' command-line option to delete it automatically
             |""".stripMargin)
        System.exit(-1)
      case (true, true) =>
        sbtio.delete(destinationFolder)
      case _ =>

    }

    val tmpDir = cleanMasterViaGit(masterRepo, projectName)
    val cleanMasterRepo = new File(tmpDir, projectName)
    println(s"cleanMasterRepo @ ${cleanMasterRepo.getPath}")
    stageFirstExercise(exercises.head, cleanMasterRepo, cleanMasterRepo)
    removeExercisesFromCleanMaster(cleanMasterRepo, exercises)
    val linearizedProject = new File(linearizedOutputFolder, projectName)
    copyMaster(cleanMasterRepo, linearizedProject)
    sbt.IO.delete(tmpDir)
    createBuildFile(linearizedProject, multiJVM)
    cleanUp(List(".git", "navigation.sbt"), linearizedProject)
    initializeGitRepo(linearizedProject)
    commitFirstExercise(exercises.head, linearizedProject)
    commitRemainingExercises(exercises.tail, masterRepo, linearizedProject)
  }
}
<commit_msg>Check that git workspace is clean before linearizing a project (#44)<commit_after>package com.lightbend.coursegentools

/**
  * Copyright © 2016 Lightbend, Inc
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *
  * NO COMMERCIAL SUPPORT OR ANY OTHER FORM OF SUPPORT IS OFFERED ON
  * THIS SOFTWARE BY LIGHTBEND, Inc.
  *
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */

object Linearize {

  def main(args: Array[String]): Unit = {

    import Helpers._
    import java.io.File
    import sbt.{ IO => sbtio }

    val cmdOptions = LinearizeCmdLineOptParse.parse(args)
    if (cmdOptions.isEmpty) System.exit(-1)
    val LinearizeCmdOptions(masterRepo, linearizedOutputFolder, multiJVM, forceDeleteExistingDestinationFolder) = cmdOptions.get

    exitIfGitIndexOrWorkspaceIsntClean(masterRepo)

    val projectName = masterRepo.getName
    val exercises: Seq[String] = getExerciseNames(masterRepo)
    val destinationFolder = new File(linearizedOutputFolder, projectName)

    (destinationFolder.exists(), forceDeleteExistingDestinationFolder) match {
      case (true, false) =>
        println(
          s"""
             |Destination folder ${destinationFolder.getPath} exists: Either remove this folder
             |manually or use the '-f' command-line option to delete it automatically
             |""".stripMargin)
        System.exit(-1)
      case (true, true) =>
        sbtio.delete(destinationFolder)
      case _ =>

    }

    val tmpDir = cleanMasterViaGit(masterRepo, projectName)
    val cleanMasterRepo = new File(tmpDir, projectName)
    println(s"cleanMasterRepo @ ${cleanMasterRepo.getPath}")
    stageFirstExercise(exercises.head, cleanMasterRepo, cleanMasterRepo)
    removeExercisesFromCleanMaster(cleanMasterRepo, exercises)
    val linearizedProject = new File(linearizedOutputFolder, projectName)
    copyMaster(cleanMasterRepo, linearizedProject)
    sbt.IO.delete(tmpDir)
    createBuildFile(linearizedProject, multiJVM)
    cleanUp(List(".git", "navigation.sbt"), linearizedProject)
    initializeGitRepo(linearizedProject)
    commitFirstExercise(exercises.head, linearizedProject)
    commitRemainingExercises(exercises.tail, masterRepo, linearizedProject)
  }
}

{'fraction_non_alphanumeric': 0.08264936056499332, 'fraction_numerical': 0.0041992746707386905, 'mean_word_length': 3.874418604651163, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7318957', 'n_tokens_mistral': 1499, 'n_tokens_neox': 1485, 'n_words': 429}