Update project to Swift 3#201
Conversation
|
Cool, thanks for this work on the migration! I think you should add an item to your checklist: "refactor the API to be compliant with Swift 3 naming guidelines". It's cool if it compiles, but this migration is the best occasion to also make the internal method names Swift3-compliant too 😉 As per switching to Swift3 being the default template, that would mean a major version bump of SwiftGen IMHO, so that people using the current version will still have their generated code compatible even if they kept the default template. If we manage to merge this PR before the imminent SwiftGen 4.0.0, it might be the right time. But it might be better to do separate PRs (this one to convert these internal code of SwiftGen too Swift 3 without any visible external changes for the users, and a separate one to rename there templates to make the Swift 3 templates the default and update the documentation accordingly) this way we can merge this one quickly then decide about the other, public migration of the templates if we have time to integrate in 4.0 or will do in a 5.0. I'm more in favor of the last solution to allow more flexibility at least it will allow us to decide art the time we decide to release there next version! |
|
Sounds like a plan! I'll do an API audit and aim to keep this branch internal / non-breaking. Thanks! |
cccf0df to
b7a12f9
Compare
|
I've done the API audit and merged up with master. Naming is hard, so let me know if there's anything you take issue with! |
| static let imageSetExtension = "imageset" | ||
|
|
||
| fileprivate func process(_ items: [[String: AnyObject]], prefix: String = "") { | ||
| fileprivate func processCatalogItems(_ items: [[String: AnyObject]], prefixedWith prefix: String = "") { |
There was a problem hiding this comment.
Maybe process(items: , withPrefix: )? Or processCatalog(items: , withPrefix: )
| } | ||
|
|
||
| public func addStoryboardAtPath(_ path: String) { | ||
| public func addStoryboard(atPath path: String) { |
There was a problem hiding this comment.
To be consistent with your naming in fonts & color parsers, I'd change this to addStoryboard(at path: )
|
|
||
| public static func fromFormatString(_ format: String) -> [PlaceholderType] { | ||
| return StringsFileParser.typesFromFormatString(format) | ||
| public static func placeholders(fromFormatString str: String) -> [PlaceholderType] { |
There was a problem hiding this comment.
Leave the parameter type out: placeholders(fromFormat str: String)
|
|
||
| // "I give %d apples to %@" --> [.Int, .String] | ||
| fileprivate static func typesFromFormatString(_ formatString: String) -> [PlaceholderType] { | ||
| fileprivate static func placeholders(fromFormatString formatString: String) -> [PlaceholderType] { |
There was a problem hiding this comment.
Same as above: placeholders(fromFormat formatString: String)
3e81735 to
68775ad
Compare
|
It looks like CI is having an issue with it's pods repo. Based on what I'm seeing it seems like I need to add a |
|
Problem with that is that it takes a really long time. See here: |
|
Indeed. |
|
In the thread I linked, they mention using a new osx image, which version do we use? |
|
Another solution to avoid it taking too long is to use the |
|
yea if we can avoid updating that pods that would be best, does --quick skip repo validation? |
|
try changing the osx image to: |
|
I think maybe we should do both: upgrade to the Travis image |
|
Sounds good to me |
|
success 🎉 |
|
Good job! 👍 Haven't much time to audit the new API, maybe if someone else can audit before merging (as it's the kind of migration where it's easy to miss something)? |
|
Definitely in the more eyes the better camp. I'll see if I can recruit @dostrander to it. |
|
I'll see if I can get this to work, could be useful: |
|
All right, either func swiftIdentifier(fromString string: Stringshould become func swiftIdentifier(from string: StringI tested GenumKit, swiftgen-cli, the unit tests and the expected output. |
|
I'm down for that. I'll push the change in a minute. |
AliSoftware
left a comment
There was a problem hiding this comment.
Various comments to be addressed.
Especially take a closer look at the duplicate build phases that seems to have been added, and at if fileprivate could be reverted to private in most cases.
|
|
||
| public func addImageName(name: String) -> Bool { | ||
| @discardableResult | ||
| public func addImageName(_ name: String) -> Bool { |
There was a problem hiding this comment.
add(imageName: String) -> Bool would be more Swift-3-y maybe?
There was a problem hiding this comment.
addImage(named name: String) to match what you used in the color parser addColor(named name: String)
There was a problem hiding this comment.
I went back and fourth on this one, I think because of the collection is actually imageNames I have to confess I'm still a little unclear on if there is an objective differentiator between addImage() add(image:) 😬 I agree though, addImage(named name: String) is best for api continuity.
|
|
||
| private func parseHexString(hexString: String) throws -> UInt32 { | ||
| let scanner = NSScanner(string: hexString) | ||
| fileprivate func parseHex(_ hexString: String) throws -> UInt32 { |
|
|
||
| public final class ColorsTextFileParser: ColorsFileParser { | ||
| public private(set) var colors = [String:UInt32]() | ||
| public fileprivate(set) var colors = [String:UInt32]() |
There was a problem hiding this comment.
Is this change (private -> fileprivate) really necessary?
That's probably the migrator doing this change for us but Swift 3's private might be good enough in lots of cases I think, worth reviewing them all to check if they can't be reverted to private.
There was a problem hiding this comment.
agree, I'll do a full audit. A lot of these I think are code style between extension grouping.
| let content = try NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding) | ||
| let lines = content.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet()) | ||
| let whitespace = NSCharacterSet.whitespaceCharacterSet() | ||
| let content = try NSString(contentsOfFile: path, encoding: String.Encoding.utf8.rawValue) |
There was a problem hiding this comment.
I wonder why I didn't use methods on String rather than NSString here, or use PathKit's methods to get the content of a file rather than depend on Foundation/NSString…
GenumKit/Stencil/Contexts.swift
Outdated
| case let (l?, r?): | ||
| return l < r | ||
| case (nil, _?): | ||
| return true |
There was a problem hiding this comment.
I'm not a fan of reintroducing comparison on optionals (I quite agree with the rationale behind SE-121 tbh) — especially the fact that nil < nil is true is quite disturbing.
I think we can use a better way to do that, like check where that comparison is used and do exploit checks with guard let et al. instead.
There was a problem hiding this comment.
Agree. I'm kind of impressed the converter produces this 🤔
| let templateStringWithMarkedNewlines = templateString | ||
| .stringByReplacingOccurrencesOfString("\n\n", withString: "\n\u{000b}\n") | ||
| .stringByReplacingOccurrencesOfString("\n\n", withString: "\n\u{000b}\n") | ||
| .replacingOccurrences(of: "\n\n", with: "\n\u{000b}\n") |
There was a problem hiding this comment.
Not sure why I did the replacement twice, but I think that was intentional (like for cases like \n\n\n, the first replacement only replaces the first 2 \n and the second takes care of the remaining ones)… so we should keep that dual replacement in place even after the Swift 3 migration.
There was a problem hiding this comment.
If so, then even a double replacement won't cover all cases. It might be better to use a regex for this:
templateString.replacingOccurrences(of: "\n\n+", with: "\n\u{000b}\n", options: .regularExpression)| let helveticaNeue = FontFamily.HelveticaNeue.Regular.font(size: 20.0) | ||
|
|
||
| let helveticaBoldBig = FontFamily.Helvetica.Bold.font(100.0) | ||
| let helveticaBoldBig = FontFamily.Helvetica.Bold.font(size: 100.0) |
There was a problem hiding this comment.
Don't we generate lowercase cases now, like Helvetica.bold and not Helvetica.Bold? I mean, if the Playground is in Swift 3 (which this font(size: 100) instead of font(100) seems to suggest, should we assume that the code in the Playground might reflect the code from the -t swift3 template, or should we still use the code generated by the default template in the Swift 3 playground?
There was a problem hiding this comment.
I was a little unclear, but I think it should be swift 3 examples. I'll fix this.
SwiftGen.xcodeproj/project.pbxproj
Outdated
| buildConfigurationList = 09A87B451BCC9B8000D9B9F5 /* Build configuration list for PBXNativeTarget "swiftgen" */; | ||
| buildPhases = ( | ||
| 54570B6ACBB962B5ED01A405 /* [CP] Check Pods Manifest.lock */, | ||
| BEC9F3A158B764C68AF03EF9 /* [CP] Check Pods Manifest.lock */, |
There was a problem hiding this comment.
Seems strange that the Build Phase got duplicated here. We gotta double-check that.
SwiftGen.xcodeproj/project.pbxproj
Outdated
| C40C2D80E9C1FEEAA4FD6B14 /* [CP] Embed Pods Frameworks */, | ||
| 6C7EBDA1392A1BFCC9F0A2DA /* [CP] Copy Pods Resources */, | ||
| 1CAEE405320C3DBD251A68B3 /* 📦 Embed Pods Frameworks */, | ||
| B8FA4CC6E874346645F002E3 /* 📦 Copy Pods Resources */, |
There was a problem hiding this comment.
Same, seems like you used an outdated CocoaPods version to regenerate that project, the version of CP when I (it happens that it was me who did that on CP 😄) introduced the emoji in the build phases names, and which got reverted to [CP] in a later version… Gotta clean that up and regenerate the project with a recent version of CP probably…
There was a problem hiding this comment.
Do you think it would it be beneficial to put a Gemfile in the project to make sure people are on the same version of cocoapods?
There was a problem hiding this comment.
Probably a good idea indeed 👍
There was a problem hiding this comment.
Dunno about you, but my cocoapods is installed system wide, and I don't think a gemfile will affect that (never used them).
There was a problem hiding this comment.
@djbe mine are also installed system-wide, that doesn't impact that 😉
When you run bundle install bundler will install all the required gems, in the right versions, system-wide (unless told otherwise). Then you just need to run bundle exec pod install (instead of just pod install) to be sure to run the right version of your system-wide-installed CocoaPods, even if that one (the one defined in the Gemfile) isn't there last version installed in your system.
There was a problem hiding this comment.
Good catch 😬 I'll sort this out. I can add bundler too, maybe once #169 is all sorted out.
SwiftGen.xcodeproj/project.pbxproj
Outdated
| shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-swiftgen/Pods-swiftgen-resources.sh\"\n"; | ||
| showEnvVarsInLog = 0; | ||
| }; | ||
| F815D65995A8B21A935964D2 /* 📦 Copy Pods Resources */ = { |
There was a problem hiding this comment.
Yup, it's clear that this build phase is the duplicate of the one just before…
|
Thanks for the feedback! |
c07261a to
8cabdf1
Compare
- misc style tweaks - changelog tweak
454a4c2 to
c11c336
Compare
|
Feedback is up:
|
| let templateStringWithMarkedNewlines = templateString | ||
| .stringByReplacingOccurrencesOfString("\n\n", withString: "\n\u{000b}\n") | ||
| .stringByReplacingOccurrencesOfString("\n\n", withString: "\n\u{000b}\n") | ||
| .replacingOccurrences(of: "\n\n", with: "\n\u{000b}\n") |
There was a problem hiding this comment.
If so, then even a double replacement won't cover all cases. It might be better to use a regex for this:
templateString.replacingOccurrences(of: "\n\n+", with: "\n\u{000b}\n", options: .regularExpression)|
@djbe I think I agree in principle, but if I understand the code correctly the intent is not to flatten inbound new lines, but to preserve them. |
|
Huh, yeah. I honestly just took a look at the issue (stencilproject/Stencil#22) and there they just mention extra empty lines they want to get rid of. |
|
@AliSoftware could we get this merged? I think al your feedback was covered. |
AliSoftware
left a comment
There was a problem hiding this comment.
Ok just some nitpickings before we go, nothing much.
Sorry for being so pedantic, but this migration is the best time to be sure everything is clean 😉
CHANGELOG.md
Outdated
| ensuring that the parser correctly handles namespaced folders. | ||
| [David Jennes](https://github.com/djbe) | ||
| [#199](https://github.com/AliSoftware/SwiftGen/pull/199) | ||
| * Swift 3 migration |
There was a problem hiding this comment.
Missing the full-stop + 2-spaces at the end of this line so that the rendered markdown is formatted properly.
GenumKit/Stencil/Contexts.swift
Outdated
| let strings = entries.sort { $0.key < $1.key }.map { entryToStringMapper($0, []) } | ||
| let structuredStrings = structure(entries, mapper: entryToStringMapper, currentLevel: 0, maxLevel: 5) | ||
| let strings = entries | ||
| .sorted { $0.key.lowercased() < $1.key.lowercased() } |
There was a problem hiding this comment.
Might be better to use str1.caseInsensitiveCompare(str2) or similar here, to ensure that any string case but also any diacritics and other niceties of Unicode are insensitively-compared properly (even if those are very rare to uncommon in keys)
|
|
||
| extension AssetsCatalogParser { | ||
| private func loadAssetCatalogContents(path: String) -> [[String: AnyObject]]? { | ||
| fileprivate func loadAssetCatalog(at path: String) -> [[String : AnyObject]]? { |
There was a problem hiding this comment.
SwiftLint rules usually prefer no space before : (but one space after) : [[String: AnyObject]]? was fine ^^
|
|
||
| public func addImageName(name: String) -> Bool { | ||
| @discardableResult | ||
| public func addImage(named name: String) -> Bool { |
There was a problem hiding this comment.
Huh, why the indentation change here? Seems like a misindentation was introduced by mistake
|
|
||
| public final class ColorsTextFileParser: ColorsFileParser { | ||
| public private(set) var colors = [String:UInt32]() | ||
| public private(set) var colors = [String : UInt32]() |
There was a problem hiding this comment.
Ditto here, and in some other places as well, just quick find/replace all.
(note that some of those might be from me and my old code-style, not all from changes in this PR? But would be nice to uniform this to match SwiftLint's rules anyway)
|
wooo, that works! |
|
Hell yeah! Let's merge! 🎉 |
|
Wooo! 😄 |
|
Awesome! Thanks a lot guys. I'll rebase and tune up #204 this weekend! |
|
@ahtierney Cool! |
|
@AliSoftware Awesome, thank you! |
As suggested by @dostrander on #201 (comment)
|
@dostrander @djbe I've added a For those of you not familiar with the usage of a Initial setup
Everyday usageEvery time you want to execute a CocoaPods command on the SwiftGen project, like For example on my Mac I currently have both CocoaPods This ensures consistency in the version every contributor uses when working as a team on SwiftGen together. |
Here's a migration to swift 3 as per issue #169 with a goal to bring all the project dependencies up to date.