<commit_before>/*
* Copyright (c) 2016. <NAME>
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

//This is NOT a gradle script. It includes .gradle so the IDE recognizes it, but it should only use Kotlin stdlib.
println("Welcome in Create new module by Louis CAD")

inline fun <R> runOrRetry(block: () -> R): R {
    while (true) try {
        return block()
    } catch (t: Exception) {
        println(t.message)
    }
}

val currentDir = File(".")
val projectDir = currentDir.parentFile!!
val settingsFile = projectDir.resolve("settings.gradle.kts")
check(settingsFile.exists()) { "No ${settingsFile.name} found!" }
val templatesDirectory = currentDir.resolve("gradle_templates")
check(templatesDirectory.exists()) { "Template directory (${templatesDirectory.name}) not found!" }
val templateDirNames = templatesDirectory.list { file, name ->
    file.isDirectory && name.startsWith('.').not()
}.asList()
check(templateDirNames.isNotEmpty()) { "No templates found!" }
println("Please select a template")
templateDirNames.forEachIndexed { i, fileName ->
    println("${i + 1}. $fileName")
}
println("Enter the number: ")
val templateIndex = runOrRetry {
    val input = readLine() ?: ""
    val number = input.toIntOrNull() ?: throw IllegalStateException("Please, input a number.")
    number.also {
        require(it in 1..templateDirNames.size) { "$it is not among the available options." }
    } - 1
}
val selectedTemplateDirName = templateDirNames[templateIndex]!!
val selectedTemplateDir = templatesDirectory.resolve(selectedTemplateDirName)
println("You selected $selectedTemplateDirName\n")
val moduleName = runOrRetry {
    println("Please type the name of the module you want to create")
    println("Enter the name: ")
    (readLine() ?: "").also {
        require(it.isNotBlank()) { "Name entered is blank" }
        //TODO: Check name is valid
        val file = projectDir.resolve(it)
        check(file.exists().not()) {
            "A ${if (file.isDirectory) "directory" else "file"} already has this name!"
        }
    }
}
val destinationDir = projectDir.resolve(moduleName)
check(selectedTemplateDir.copyRecursively(destinationDir)) { "Copy operation didn't succeed" }

/**
 * Runs the [action] on this file, then, if the result of the lambda (if not null) or this file,
 * is a directory, calls this function on all the files inside.
 * If the directory is moved or renamed in [action], you need to return it in the lambda, or failure
 * will happen because the referenced file no longer exists.
 */
fun File.doRecursively(action: (File) -> File?) {
    val file = action(this) ?: this
    if (file.isDirectory) file.listFiles().forEach { it.doRecursively(action) }
}

class LazyMap<K, V>(
    private val backingMap: MutableMap<K, V> = mutableMapOf(),
    private val createEntry: (K) -> V<commit_msg>Make Create-new-module script use modules sub-dir<commit_after>/*
* Copyright (c) 2016. <NAME>
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

//This is NOT a gradle script. It includes .gradle so the IDE recognizes it, but it should only use Kotlin stdlib.
println("Welcome in Create new module by <NAME>")

inline fun <R> runOrRetry(block: () -> R): R {
    while (true) try {
        return block()
    } catch (t: Exception) {
        println(t.message)
    }
}

val currentDir = File(".")
val projectDir = currentDir.parentFile!!
val modulesDir = projectDir.resolve("modules")
val settingsFile = projectDir.resolve("settings.gradle.kts")
check(settingsFile.exists()) { "No ${settingsFile.name} found!" }
val templatesDirectory = currentDir.resolve("gradle_templates")
check(templatesDirectory.exists()) { "Template directory (${templatesDirectory.name}) not found!" }
val templateDirNames = templatesDirectory.list { file, name ->
    file.isDirectory && name.startsWith('.').not()
}.asList()
check(templateDirNames.isNotEmpty()) { "No templates found!" }
println("Please select a template")
templateDirNames.forEachIndexed { i, fileName ->
    println("${i + 1}. $fileName")
}
println("Enter the number: ")
val templateIndex = runOrRetry {
    val input = readLine() ?: ""
    val number = input.toIntOrNull() ?: throw IllegalStateException("Please, input a number.")
    number.also {
        require(it in 1..templateDirNames.size) { "$it is not among the available options." }
    } - 1
}
val selectedTemplateDirName = templateDirNames[templateIndex]!!
val selectedTemplateDir = templatesDirectory.resolve(selectedTemplateDirName)
println("You selected $selectedTemplateDirName\n")
val moduleName = runOrRetry {
    println("Please type the name of the module you want to create")
    println("Enter the name: ")
    (readLine() ?: "").also {
        require(it.isNotBlank()) { "Name entered is blank" }
        //TODO: Check name is valid
        val file = modulesDir.resolve(it)
        check(file.exists().not()) {
            "A ${if (file.isDirectory) "directory" else "file"} already has this name!"
        }
    }
}
val destinationDir = modulesDir.resolve(moduleName)
check(selectedTemplateDir.copyRecursively(destinationDir)) { "Copy operation didn't succeed" }

/**
 * Runs the [action] on this file, then, if the result of the lambda (if not null) or this file,
 * is a directory, calls this function on all the files inside.
 * If the directory is moved or renamed in [action], you need to return it in the lambda, or failure
 * will happen because the referenced file no longer exists.
 */
fun File.doRecursively(action: (File) -> File?) {
    val file = action(this) ?: this
    if (file.isDirectory) file.listFiles().forEach { it.doRecursively(action) }
}

class LazyMap<K, V>(
    private val backingMap: MutableMap<K, V> = mutableMapOf(),
    private val createEntry: (K) -> V
{'fraction_non_alphanumeric': 0.11826837250818209, 'fraction_numerical': 0.0032728354656352274, 'mean_word_length': 4.066314996232102, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 22, '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': '913525', 'n_tokens_mistral': 1909, 'n_tokens_neox': 1880, 'n_words': 694}