From 2ef7cfd714fc3724b82e6dfc4bc93aebdd068886 Mon Sep 17 00:00:00 2001 From: Mathijs Kadijk Date: Mon, 23 Mar 2015 07:49:27 +0100 Subject: [PATCH 1/3] Prevent use of Swift keywords as name Fixes issue #26 by putting keywords between backticks. --- R.swift/func.swift | 3 ++- R.swift/values.swift | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/R.swift/func.swift b/R.swift/func.swift index 4bfc20b5..53511a99 100644 --- a/R.swift/func.swift +++ b/R.swift/func.swift @@ -58,8 +58,9 @@ func sanitizedSwiftName(name: String, lowercaseFirstCharacter: Bool = true) -> S var components = name.componentsSeparatedByString("-") let firstComponent = components.removeAtIndex(0) let swiftName = components.reduce(firstComponent) { $0 + $1.capitalizedString } + let capitalizedSwiftName = lowercaseFirstCharacter ? swiftName.lowercaseFirstCharacter : swiftName - return lowercaseFirstCharacter ? swiftName.lowercaseFirstCharacter : swiftName + return contains(SwiftKeywords, capitalizedSwiftName) ? "`\(capitalizedSwiftName)`" : capitalizedSwiftName } func writeResourceFile(code: String, toFolderURL folderURL: NSURL) { diff --git a/R.swift/values.swift b/R.swift/values.swift index 6a0f3cef..1fd31616 100644 --- a/R.swift/values.swift +++ b/R.swift/values.swift @@ -54,3 +54,5 @@ let ElementNameToTypeMapping = [ "avPlayerViewController": Type(name: "AVPlayerViewController"), "collectionViewController": Type(name: "UICollectionViewController"), ] + +let SwiftKeywords = ["class", "deinit", "enum", "extension", "func", "import", "init", "internal", "let", "operator", "private", "protocol", "public", "static", "struct", "subscript", "typealias", "var", "break", "case", "continue", "default", "do", "else", "fallthrough", "for", "if", "in", "return", "switch", "where", "while", "as", "dynamicType", "false", "is", "nil", "self", "Self", "super", "true", "__COLUMN__", "__FILE__", "__FUNCTION__", "__LINE__"] From 0d8cb4addeba16ac69a96bf6bfd37cf0994481e9 Mon Sep 17 00:00:00 2001 From: Philippe Bernery Date: Sun, 10 May 2015 10:27:19 +0200 Subject: [PATCH 2/3] Handle asset catalog sub-folders --- R.swift/types.swift | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/R.swift/types.swift b/R.swift/types.swift index e1b5c504..58d5b727 100644 --- a/R.swift/types.swift +++ b/R.swift/types.swift @@ -156,8 +156,23 @@ struct AssetFolder { init(url: NSURL, fileManager: NSFileManager) { name = url.filename! - let contents = fileManager.contentsOfDirectoryAtURL(url, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles, error: nil) as! [NSURL] - imageAssets = contents.map { $0.filename! } + // Authorized asset extensions + let assetExtensions = ["appiconset", "launchimage", "imageset"] + + // Browse asset directory recursively and list the authorized assets list. + var assets = [NSURL]() + let enumerator = fileManager.enumeratorAtURL(url, includingPropertiesForKeys: nil, + options: .SkipsHiddenFiles, errorHandler: nil) + if let enumerator = enumerator { + for file in enumerator { + let fileURL = file as! NSURL + if let pathExtension = fileURL.pathExtension where find(assetExtensions, pathExtension) != nil { + assets.append(file as! NSURL) + } + } + } + + imageAssets = assets.map { $0.filename! } } } From c13bb02dddae3f9d1be38802dbc1f2c3ef41181a Mon Sep 17 00:00:00 2001 From: Mathijs Kadijk Date: Tue, 12 May 2015 08:20:55 +0200 Subject: [PATCH 3/3] Small tweaks in code for supporting asset groups --- R.swift/types.swift | 17 ++++++----------- R.swift/values.swift | 2 ++ 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/R.swift/types.swift b/R.swift/types.swift index 58d5b727..c4ea4771 100644 --- a/R.swift/types.swift +++ b/R.swift/types.swift @@ -156,20 +156,15 @@ struct AssetFolder { init(url: NSURL, fileManager: NSFileManager) { name = url.filename! - // Authorized asset extensions - let assetExtensions = ["appiconset", "launchimage", "imageset"] - - // Browse asset directory recursively and list the authorized assets list. + // Browse asset directory recursively and list only the assets folders var assets = [NSURL]() - let enumerator = fileManager.enumeratorAtURL(url, includingPropertiesForKeys: nil, - options: .SkipsHiddenFiles, errorHandler: nil) + let enumerator = fileManager.enumeratorAtURL(url, includingPropertiesForKeys: nil, options: .SkipsHiddenFiles, errorHandler: nil) if let enumerator = enumerator { - for file in enumerator { - let fileURL = file as! NSURL - if let pathExtension = fileURL.pathExtension where find(assetExtensions, pathExtension) != nil { - assets.append(file as! NSURL) - } + for file in enumerator { + if let fileURL = file as? NSURL, pathExtension = fileURL.pathExtension where find(AssetExtensions, pathExtension) != nil { + assets.append(fileURL) } + } } imageAssets = assets.map { $0.filename! } diff --git a/R.swift/values.swift b/R.swift/values.swift index 1fd31616..c037f81b 100644 --- a/R.swift/values.swift +++ b/R.swift/values.swift @@ -43,6 +43,8 @@ let Ordinals = [ (number: 20, word: "twentieth"), ] +let AssetExtensions = ["appiconset", "launchimage", "imageset"] + let ElementNameToTypeMapping = [ "viewController": Type._UIViewController, "glkViewController": Type(name: "GLKViewController"),