<commit_before>#!/usr/bin/env xcrun swift

import Foundation

// MARK: NSUserDefaults extension
extension NSUserDefaults {
    var arguments: (String, String) {
        var assetPath  = self.stringForKey("path")
        var outputPath = self.stringForKey("exportPath")
        if assetPath == nil {
            fatalError("An asset catalog path must be specified by \"-path\".")
        }
        if outputPath == nil {
            fatalError("An output path must be specified by \"-exportPath\".")
        }
        return (assetPath!, outputPath!)
    }
}
// MARK: NSDate extension
extension NSDate {
    var formatString: String {
        var df = NSDateFormatter()
        df.dateFormat = "yyyy-MM-dd"
        return df.stringFromDate(self)
    }
}
// MARK: NSFileManager
extension NSFileManager {
    func imagesets(inAssetsPath path: String) -> [String]? {
        var error: NSError?
        var imagesets = self.subpathsOfDirectoryAtPath(path, error: &error)?
            .filter { $0.hasSuffix("imageset") }
            .map { $0.lastPathComponent.componentsSeparatedByString(".")[0] }
        if let err = error {
            println("[Warning] An error occurred in \(__FUNCTION__).\n\t error: \(err)")
        }
        return imagesets
    }
    
    func writeimagesets(imagesets: [String], atPath path: String) -> Bool {
        let indent = "    " // indent is 4 spaces
        var file: String = ""
        // file header
        file += "//\n"
        file += "// \(path.lastPathComponent)\n"
        file += "//\n"
        file += "// Created by Misen(https://github.com/tasanobu/Misen).\n"
        file += "//\n"
        file += "//\n"
        // file body
        file += "\n"
        file += "import UIKit\n"
        file += "\n"
        file += "extension UIImage {\n"
        // initializer
        file += "\n"
        file += "\(indent)convenience init!(assetName: AssetName) {\n"
        file += "\(indent)\(indent)self.init(named: assetName.rawValue)\n"
        file += "\(indent)}\n"
        // enum
        file += "\n"
        file += "\(indent)enum AssetName: String {\n"
        for str in imagesets {
            file += "\(indent)\(indent)case \(str) = \"\(str)\"\n"
        }
        file += "\(indent)}\n"
        // end of file
        file += "}\n"
        
        let data = file.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
        
        return self.createFileAtPath(path, contents: data, attributes: nil)
    }
}

// MARK: - Main
let (path, exportPath) = NSUserDefaults.standardUserDefaults().arguments
if let imagesets = NSFileManager.defaultManager().imagesets(inAssetsPath: path) where !imagesets.isEmpty {
    let result = NSFileManager.defaultManager().writeimagesets(imagesets, atPath: exportPath)
    println("Succeeded to create an UIImage extension file at \(exportPath).")
} else {
    println("[Error] No imageset is found and failed to export a file...")
}
<commit_msg>Change the output file format<commit_after>#!/usr/bin/env xcrun swift

import Foundation

// MARK: NSUserDefaults extension
extension NSUserDefaults {
    var arguments: (String, String, String) {
        var assetPath  = stringForKey("path")
        var outputPath = stringForKey("exportPath")
        // enumName is optional and "ImageAsset" is used for enum as default value.
        var enumName   = stringForKey("enumName") ?? "ImageAsset"
        if assetPath == nil {
            fatalError("An asset catalog path must be specified by \"-path\".")
        }
        if outputPath == nil {
            fatalError("An output path must be specified by \"-exportPath\".")
        }
        return (assetPath!, outputPath!, enumName)
    }
}
// MARK: NSFileManager
extension NSFileManager {
    func imagesets(inAssetsPath path: String) -> [String]? {
        var error: NSError?
        var imagesets = self.subpathsOfDirectoryAtPath(path, error: &error)?
            .filter { $0.hasSuffix("imageset") }
            .map { $0.lastPathComponent.componentsSeparatedByString(".")[0] }
        if let err = error {
            println("[Warning] An error occurred in \(__FUNCTION__).\n\t error: \(err)")
        }
        return imagesets
    }
    
    func build(assets: [String], _ exportPath: String, _ enumName: String) -> Bool {
        let indent = "    " // indent is 4 spaces
        var file: String = ""
        /// file header
        file += "// Generated with Misen by tasanobu - https://github.com/tasanobu/Misen" + "\n"
        file += "\n"
        file += "import UIKit" + "\n"
        file += "\n"
        
        /// UIImage extension
        file += "// MARK: - UIImage extension" + "\n"
        file += "extension UIImage {" + "\n"
        // initializer
        file += indent + "convenience init!(assetName: \(enumName)) {" + "\n"
        file += indent + indent + "self.init(named: assetName.rawValue)" + "\n"
        file += indent + "}" + "\n"
        ///end of UIImage extension
        file += "}" + "\n"
        
        file += "\n"
        
        /// enum
        file += "// MARK: - " + enumName + "\n"
        file += "enum \(enumName): String {" + "\n"
        for str in assets {
            file += indent + "case \(str) = \"\(str)\"" + "\n"
        }
        file += "\n"
        file += indent + "var image: UIImage {" + "\n"
        file += indent + indent + "return UIImage(named: self.rawValue)!" + "\n"
        file += indent + "}" + "\n"
        /// end of enum
        file += "}" + "\n"
        
        let data = file.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
        return createFileAtPath(exportPath, contents: data, attributes: nil)
    }
}

// MARK: - Main
let (path, exportPath, enumName) = NSUserDefaults.standardUserDefaults().arguments
let fm = NSFileManager.defaultManager()

if let imagesets = fm.imagesets(inAssetsPath: path) where !imagesets.isEmpty {
    let result = fm.build(imagesets, exportPath, enumName)
    let resultStr = result ? "Succeeded" : "Failed"
    println("\(resultStr) to generate enum and UIImage extension file at \(exportPath).")
} else {
    println("[Error] No imageset is found and failed to export a file...")
}

{'fraction_non_alphanumeric': 0.14306522090953228, 'fraction_numerical': 0.0016183848519177862, 'mean_word_length': 2.1708568496664955, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '3159473', 'n_tokens_mistral': 1849, 'n_tokens_neox': 1800, 'n_words': 503}