Last active
November 13, 2016 15:43
-
-
Save bkook/f8b8a9d509cfb69c08e1f13f72081408 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env xcrun swift -F Carthage/Build/Mac | |
import Foundation | |
import Markingbird | |
protocol Streamable { | |
var title: String { get } | |
var body: String { get } | |
} | |
extension Streamable { | |
var writableString: String { | |
return "# \(title)\n\n\(body)" | |
} | |
} | |
struct License: Streamable { | |
let libraryName: String | |
let legalText: String | |
var title: String { | |
return libraryName | |
} | |
var body: String { | |
return legalText | |
} | |
} | |
func getLicense(_ URL: URL) throws -> License { | |
let legalText = try String(contentsOf: URL, encoding: .utf8) | |
let pathComponents = URL.pathComponents | |
let libraryName = pathComponents[pathComponents.count - 2] | |
return License(libraryName: libraryName, legalText: legalText) | |
} | |
func run() throws { | |
let cocoaPodsDir = "Pods/" | |
let carthageDir = "Carthage/Checkouts/" | |
let outputFile = "Venmo/Resources/LICENSES.html" | |
let options: FileManager.DirectoryEnumerationOptions = [.skipsPackageDescendants, .skipsHiddenFiles] | |
let fileManager = FileManager.default | |
// Get URL’s for all files in cocoaPodsDir and carthageDir | |
guard | |
let cocoaPodsDirURL = URL(string: cocoaPodsDir), | |
let cocoaPodsEnumerator = fileManager.enumerator(at: cocoaPodsDirURL, includingPropertiesForKeys: nil, options: options, errorHandler: nil) | |
else { | |
print("Error: \(cocoaPodsDir) directory not found. Please run `rake`") | |
return | |
} | |
guard | |
let carthageDirURL = URL(string: carthageDir), | |
let carthageEnumerator = fileManager.enumerator(at: carthageDirURL, includingPropertiesForKeys: nil, options: options, errorHandler: nil) | |
else { | |
print("Error: \(carthageDir) directory not found. Please run `rake`") | |
return | |
} | |
guard | |
let cocoaPodsURLs = cocoaPodsEnumerator.allObjects as? [URL], | |
let carthageURLs = carthageEnumerator.allObjects as? [URL] | |
else { | |
print("Unexpected error: Enumerator contained item that is not URL.") | |
return | |
} | |
let allURLs = cocoaPodsURLs + carthageURLs | |
// Get just the LICENSE files and convert them to License structs | |
let licenseURLs = allURLs.filter { URL in | |
return URL.lastPathComponent.range(of: "LICENSE") != nil || URL.lastPathComponent.range(of: "LICENCE") != nil | |
} | |
let licenses = licenseURLs.flatMap { try? getLicense($0) } | |
// Write each License into outputFile after converting them to HTML using Markingbird | |
var markdown = Markdown() | |
let html = licenses.map { markdown.transform($0.writableString) }.joined(separator: "\n") | |
try html.write(toFile: outputFile, atomically: false, encoding: .utf8) | |
} | |
func main() { | |
do { | |
try run() | |
} catch let error as NSError { | |
print(error.localizedDescription) | |
} | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment