<commit_before>/**
 *  Marathon
 *  Copyright (c) <NAME> 2017
 *  Licensed under the MIT license. See LICENSE file.
 */

import Foundation

// MARK: - Error

public enum CreateError {
    case missingName
    case failedToCreateFile(String)
}

extension CreateError: PrintableError {
    public var message: String {
        switch self {
        case .missingName:
            return "No script name given"
        case .failedToCreateFile(let name):
            return "Failed to create script file named '\(name)'"
        }
    }

    public var hint: String? {
        switch self {
        case .missingName:
            return "Pass the name of a script file to create (for example 'marathon create myScript')"
        case .failedToCreateFile:
            return "Make sure you have write permissions to the current folder"
        }
    }
}

// MARK: - Task

internal final class CreateTask: Task, Executable {
    private typealias Error = CreateError

    func execute() throws -> String {
        guard let name = firstArgumentAsScriptPath else {
            throw Error.missingName
        }

        let contents = arguments.element(at: 1)?.data(using: .utf8) ?? Data()
        let file = try perform(folder.createFile(named: name, contents: contents), orThrow: Error.failedToCreateFile(name))
        var output = "🐣  Created script \(name)"<commit_msg>Create: Add default script if no content was given<commit_after>/**
 *  Marathon
 *  Copyright (c) <NAME> 2017
 *  Licensed under the MIT license. See LICENSE file.
 */

import Foundation

// MARK: - Error

public enum CreateError {
    case missingName
    case failedToCreateFile(String)
}

extension CreateError: PrintableError {
    public var message: String {
        switch self {
        case .missingName:
            return "No script name given"
        case .failedToCreateFile(let name):
            return "Failed to create script file named '\(name)'"
        }
    }

    public var hint: String? {
        switch self {
        case .missingName:
            return "Pass the name of a script file to create (for example 'marathon create myScript')"
        case .failedToCreateFile:
            return "Make sure you have write permissions to the current folder"
        }
    }
}

// MARK: - Task

internal final class CreateTask: Task, Executable {
    private typealias Error = CreateError

    func execute() throws -> String {
        guard let name = firstArgumentAsScriptPath else {
            throw Error.missingName
        }

        let script = arguments.element(at: 1) ?? "import Foundation\n\n"
        let file = try perform(folder.createFile(named: name, contents: script.data(using: .utf8)!),
                               orThrow: Error.failedToCreateFile(name))

        var output = "🐣  Created script \(name)"
{'fraction_non_alphanumeric': 0.08552164655784245, 'fraction_numerical': 0.0042583392476933995, 'mean_word_length': 2.355952380952381, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '3809466', 'n_tokens_mistral': 759, 'n_tokens_neox': 736, 'n_words': 272}