<commit_before>import Foundation
import RunnerLib

import Files
import Logger
import MarathonCore

func editDanger(logger: Logger) throws {
    let createDangerfile: (String) -> String = { dangerfilePath in
        do {
            let template = "import Danger \nlet danger = Danger()"
            let data = template.data(using: .utf8)!
            return try FileSystem().createFile(at: dangerfilePath, contents: data).path
        } catch {
            logger.logError("Could not find or generate a Dangerfile")
            exit(1)
        }
    }

    let dangerfilePath: String

    if let dangerfileArgumentPath = DangerfilePathFinder.dangerfilePath() {
        dangerfilePath = dangerfileArgumentPath

        if !FileManager.default.fileExists(atPath: dangerfileArgumentPath) {
            _ = createDangerfile(dangerfileArgumentPath)
        }

    } else {
        dangerfilePath = Runtime.getDangerfile() ?? createDangerfile("Dangerfile.swift")
    }

    let absoluteLibPath: String
    let libName: String

    if let spmDanger = SPMDanger() {
        spmDanger.buildDependencies()
        absoluteLibPath = FileManager.default.currentDirectoryPath + "/" + SPMDanger.buildFolder
        libName = spmDanger.depsLibName
    } else {
        guard let libPath = Runtime.getLibDangerPath() else {
            let potentialFolders = Runtime.potentialLibraryFolders
            logger.logError("Could not find a libDanger to link against at any of: \(potentialFolders)",
                            "Or via Homebrew, or Marathon",
                            separator: "\n")
            exit(1)
        }

        absoluteLibPath = try Folder(path: libPath).path
        libName = "Danger"
    }

    guard let dangerfileContent = try? File(path: dangerfilePath).readAsString() else {
        logger.logError("Could not read the dangerPath")
        exit(1)
    }

    let importsFinder = ImportsFinder()
    let importedFiles = importsFinder.findImports(inString: dangerfileContent)

    let arguments = CommandLine.arguments
    let scriptManager = try getScriptManager(logger)
    let script = try scriptManager.script(atPath: dangerfilePath, allowRemote: true)

    let configPath = NSTemporaryDirectory() + "config.xcconfig"

    try createConfig(atPath: configPath, libPath: absoluteLibPath, libName: libName)

    try script.setupForEdit(arguments: arguments, importedFiles: importedFiles, configPath: configPath)

    try script.watch(arguments: arguments, importedFiles: importedFiles)
}<commit_msg>Move createDangerfile on a separated func<commit_after>import Foundation
import RunnerLib

import Files
import Logger
import MarathonCore

func editDanger(logger: Logger) throws {
    let dangerfilePath: String

    if let dangerfileArgumentPath = DangerfilePathFinder.dangerfilePath() {
        dangerfilePath = dangerfileArgumentPath

        if !FileManager.default.fileExists(atPath: dangerfileArgumentPath) {
            createDangerfile(dangerfileArgumentPath)
        }

    } else {
        dangerfilePath = Runtime.getDangerfile() ?? createDangerfile("Dangerfile.swift")
    }

    let absoluteLibPath: String
    let libName: String

    if let spmDanger = SPMDanger() {
        spmDanger.buildDependencies()
        absoluteLibPath = FileManager.default.currentDirectoryPath + "/" + SPMDanger.buildFolder
        libName = spmDanger.depsLibName
    } else {
        guard let libPath = Runtime.getLibDangerPath() else {
            let potentialFolders = Runtime.potentialLibraryFolders
            logger.logError("Could not find a libDanger to link against at any of: \(potentialFolders)",
                            "Or via Homebrew, or Marathon",
                            separator: "\n")
            exit(1)
        }

        absoluteLibPath = try Folder(path: libPath).path
        libName = "Danger"
    }

    guard let dangerfileContent = try? File(path: dangerfilePath).readAsString() else {
        logger.logError("Could not read the dangerPath")
        exit(1)
    }

    let importsFinder = ImportsFinder()
    let importedFiles = importsFinder.findImports(inString: dangerfileContent)

    let arguments = CommandLine.arguments
    let scriptManager = try getScriptManager(logger)
    let script = try scriptManager.script(atPath: dangerfilePath, allowRemote: true)

    let configPath = NSTemporaryDirectory() + "config.xcconfig"

    try createConfig(atPath: configPath, libPath: absoluteLibPath, libName: libName)

    try script.setupForEdit(arguments: arguments, importedFiles: importedFiles, configPath: configPath)

    try script.watch(arguments: arguments, importedFiles: importedFiles)
}

@discardableResult
private func createDangerfile(_ dangerfilePath: String) -> String {
    do {
        let template = "import Danger \nlet danger = Danger()"
        let data = template.data(using: .utf8)!
        return try FileSystem().createFile(at: dangerfilePath, contents: data).path
    } catch {
        logger.logError("Could not find or generate a Dangerfile")
        exit(1)
    }
}
{'fraction_non_alphanumeric': 0.08148295003965107, 'fraction_numerical': 0.0015860428231562252, 'mean_word_length': 2.9912974683544302, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3810939', 'n_tokens_mistral': 1390, 'n_tokens_neox': 1343, 'n_words': 350}