|
| 1 | +import Foundation |
| 2 | +import ContentBlockerConverter |
| 3 | + |
| 4 | +/** |
| 5 | + * Command line wrapper |
| 6 | + * Usage: |
| 7 | + * ./CommandLineWrapper -limit=0 -optimize=true -advancedBlocking=false |
| 8 | + */ |
| 9 | + |
| 10 | +func writeToStdError(str: String) { |
| 11 | + let handle = FileHandle.standardError; |
| 12 | + |
| 13 | + if let data = str.data(using: String.Encoding.utf8, allowLossyConversion: false) { |
| 14 | + handle.write(data) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +func writeToStdOut(str: String) { |
| 19 | + let handle = FileHandle.standardOutput; |
| 20 | + |
| 21 | + if let data = str.data(using: String.Encoding.utf8, allowLossyConversion: false) { |
| 22 | + handle.write(data) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func encodeJson(_ result: ConversionResult) throws -> String { |
| 27 | + let encoder = JSONEncoder(); |
| 28 | + encoder.outputFormatting = .prettyPrinted |
| 29 | + |
| 30 | + let json = try encoder.encode(result); |
| 31 | + return String(data: json, encoding: .utf8)!.replacingOccurrences(of: "\\/", with: "/"); |
| 32 | +} |
| 33 | + |
| 34 | +do { |
| 35 | + Logger.log("AG: Conversion started"); |
| 36 | + |
| 37 | + let arguments: [String] = CommandLine.arguments; |
| 38 | + if (arguments.count < 4) { |
| 39 | + writeToStdError(str: "AG: Invalid arguments: Usage: ./CommandLineWrapper -limit=0 -optimize=false -advancedBlocking)"); |
| 40 | + exit(EXIT_FAILURE); |
| 41 | + } |
| 42 | + |
| 43 | + let limit = Int(arguments[1][String.Index(encodedOffset: 7)...]) ?? 0; |
| 44 | + Logger.log("AG: Limit: \(limit)"); |
| 45 | + |
| 46 | + let optimize = arguments[2] == "-optimize=true"; |
| 47 | + Logger.log("AG: Optimize: \(optimize)"); |
| 48 | + |
| 49 | + let advancedBlocking = arguments[3] == "-advancedBlocking=true"; |
| 50 | + Logger.log("AG: AdvancedBlocking: \(advancedBlocking)"); |
| 51 | + |
| 52 | + var rules = [String](); |
| 53 | + var line: String? = nil; |
| 54 | + while (true) { |
| 55 | + line = readLine(strippingNewline: true); |
| 56 | + if (line == nil || line == "") { |
| 57 | + break; |
| 58 | + } |
| 59 | + |
| 60 | + rules.append(line!); |
| 61 | + } |
| 62 | + |
| 63 | + Logger.log("AG: Rules to convert: \(rules.count)"); |
| 64 | + |
| 65 | + let result: ConversionResult? = ContentBlockerConverter().convertArray( |
| 66 | + rules: rules, limit: limit, optimize: optimize, advancedBlocking: advancedBlocking |
| 67 | + ); |
| 68 | + |
| 69 | + Logger.log("AG: Conversion done"); |
| 70 | + |
| 71 | + if (result == nil) { |
| 72 | + writeToStdError(str: "AG: ContentBlockerConverter: Empty result."); |
| 73 | + exit(EXIT_FAILURE); |
| 74 | + } |
| 75 | + |
| 76 | + let encoded = try encodeJson(result!); |
| 77 | + |
| 78 | + writeToStdOut(str: "\(encoded)"); |
| 79 | + exit(EXIT_SUCCESS); |
| 80 | +} catch { |
| 81 | + writeToStdError(str: "AG: ContentBlockerConverter: Unexpected error: \(error)"); |
| 82 | + exit(EXIT_FAILURE); |
| 83 | +} |
| 84 | + |
0 commit comments