From 76a63104fa0d028c56f755ad3202b2abbd7f54df Mon Sep 17 00:00:00 2001 From: Douglas Heriot Date: Thu, 30 Jun 2016 00:26:04 +1000 Subject: [PATCH] Update Travis to use Xcode 8. Also update validate-headers.swift for Swift 3. --- .travis.yml | 2 +- scripts/validate-headers.swift | 44 +++++++++++++++++----------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/.travis.yml b/.travis.yml index da58ee7b..1652c53d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: objective-c -osx_image: xcode7.3 +osx_image: xcode8 notifications: slack: rxswift:3ykt2Z61f8GkdvhCZTYPduOL diff --git a/scripts/validate-headers.swift b/scripts/validate-headers.swift index d337f5e1..67aab7c0 100755 --- a/scripts/validate-headers.swift +++ b/scripts/validate-headers.swift @@ -23,7 +23,7 @@ Validates that all headers are in this standard form Only Project is not checked yet, but it will be soon. */ -let fileManager = NSFileManager.defaultManager() +let fileManager = FileManager.default() let allowedExtensions = [ ".swift", @@ -48,19 +48,19 @@ func isExtensionIncluded(path: String) -> Bool { return (allowedExtensions.map { path.hasSuffix($0) }).reduce(false) { $0 || $1 } } -let whitespace = NSCharacterSet.whitespaceAndNewlineCharacterSet() +let whitespace = NSCharacterSet.whitespacesAndNewlines() let identifier = "(?:\\w|\\+|\\_|\\.|-)+" -let fileLine = try NSRegularExpression(pattern: "// (\(identifier))", options: []) -let projectLine = try NSRegularExpression(pattern: "// (\(identifier))", options: []) +let fileLine = try RegularExpression(pattern: "// (\(identifier))", options: []) +let projectLine = try RegularExpression(pattern: "// (\(identifier))", options: []) -let createdBy = try NSRegularExpression(pattern: "// Created by .* on \\d+/\\d+/\\d+\\.", options: []) -let copyrightLine = try NSRegularExpression(pattern: "// Copyright © (\\d+) Krunoslav Zaher. All rights reserved.", options: []) +let createdBy = try RegularExpression(pattern: "// Created by .* on \\d+/\\d+/\\d+\\.", options: []) +let copyrightLine = try RegularExpression(pattern: "// Copyright © (\\d+) Krunoslav Zaher. All rights reserved.", options: []) -func validateRegexMatches(regularExpression: NSRegularExpression, content: String) -> ([String], Bool) { +func validateRegexMatches(regularExpression: RegularExpression, content: String) -> ([String], Bool) { let range = NSRange(location: 0, length: content.characters.count) - let matches = regularExpression.matchesInString(content, options: [], range: range) + let matches = regularExpression.matches(in: content, options: [], range: range) if matches.count == 0 { print("ERROR: line `\(content)` is invalid: \(regularExpression.pattern)") @@ -76,20 +76,20 @@ func validateRegexMatches(regularExpression: NSRegularExpression, content: Strin return (matches[0 ..< matches.count].flatMap { m -> [String] in return (1 ..< m.numberOfRanges).map { index in - return (content as NSString).substringWithRange(m.rangeAtIndex(index)) + return (content as NSString).substring(with: m.range(at: index)) } }, true) } func validateHeader(path: String) throws -> Bool { - let contents = try String(contentsOfFile: path, encoding: NSUTF8StringEncoding) + let contents = try String(contentsOfFile: path, encoding: String.Encoding.utf8) - let rawLines = contents.componentsSeparatedByString("\n") + let rawLines = contents.components(separatedBy: "\n") - var lines = rawLines.map { $0.stringByTrimmingCharactersInSet(whitespace) } + var lines = rawLines.map { $0.trimmingCharacters(in: whitespace) } if (lines.first ?? "").hasPrefix("#") || (lines.first ?? "").hasPrefix("// This file is autogenerated.") { - lines.removeAtIndex(0) + lines.remove(at: 0) } if lines.count < 8 { @@ -109,7 +109,7 @@ func validateHeader(path: String) throws -> Bool { return false } - let (parsedFileLine, isValidFilename) = validateRegexMatches(fileLine, content: lines[1]) + let (parsedFileLine, isValidFilename) = validateRegexMatches(regularExpression: fileLine, content: lines[1]) if !isValidFilename { print("ERROR: Line[2] Filename line should match `\(fileLine.pattern)`") @@ -122,7 +122,7 @@ func validateHeader(path: String) throws -> Bool { return false } - let (_, isValidProject) = validateRegexMatches(projectLine, content: lines[2]) + let (_, isValidProject) = validateRegexMatches(regularExpression: projectLine, content: lines[2]) if !isValidProject { print("ERROR: Line[3] Line not maching \(projectLine.pattern)") @@ -134,14 +134,14 @@ func validateHeader(path: String) throws -> Bool { return false } - let (_, isValidCreatedBy) = validateRegexMatches(createdBy, content: lines[4]) + let (_, isValidCreatedBy) = validateRegexMatches(regularExpression: createdBy, content: lines[4]) if !isValidCreatedBy { print("ERROR: Line[5] Line not matching \(createdBy.pattern)") return false } - let (year, isValidCopyright) = validateRegexMatches(copyrightLine, content: lines[5]) + let (year, isValidCopyright) = validateRegexMatches(regularExpression: copyrightLine, content: lines[5]) if !isValidCopyright { print("ERROR: Line[6] Line not matching \(copyrightLine.pattern)") @@ -167,16 +167,16 @@ func validateHeader(path: String) throws -> Bool { } func verifyAll(root: String) throws -> Bool { - return try fileManager.subpathsOfDirectoryAtPath(root).map { file -> Bool in + return try fileManager.subpathsOfDirectory(atPath: root).map { file -> Bool in let excluded = excludePaths.map { file.hasPrefix($0) }.reduce(false) { $0 || $1 } if excluded { return true } - if !isExtensionIncluded(file) { + if !isExtensionIncluded(path: file) { return true } - let isValid = try validateHeader("\(root)/\(file)") + let isValid = try validateHeader(path: "\(root)/\(file)") if !isValid { print(" while Validating '\(root)/\(file)'") } @@ -185,12 +185,12 @@ func verifyAll(root: String) throws -> Bool { }.reduce(true) { $0 && $1 } } -let allValid = try fileManager.contentsOfDirectoryAtPath(".").map { rootDir -> Bool in +let allValid = try fileManager.contentsOfDirectory(atPath: ".").map { rootDir -> Bool in if excludedRootPaths.contains(rootDir) { print("Skipping \(rootDir)") return true } - return try verifyAll(rootDir) + return try verifyAll(root: rootDir) }.reduce(true) { $0 && $1 } if !allValid {