Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding common transforms for Swift in iOS #255

Merged
merged 6 commits into from
Apr 23, 2019

Conversation

tonyheupel
Copy link
Contributor

@tonyheupel tonyheupel commented Mar 11, 2019

Issue #, if available: #161

Description of changes:
Adding ability to use ios-swift and ios-swift-separate transform groups. Examples:

ios-swift: all in one dictionary class file

//
// StyleDictionary.swift
//
// Do not edit directly
// Generated on Tue, 19 Mar 2019 18:31:55 GMT
//

import UIKit

public class StyleDictionary {
    static let colorBaseGrayDark = UIColor(red: 0.07, green: 0.07, blue: 0.07, alpha:1)
    static let colorBaseGrayLight = UIColor(red: 0.80, green: 0.80, blue: 0.80, alpha:1)
    static let colorBaseGrayMedium = UIColor(red: 0.60, green: 0.60, blue: 0.60, alpha:1)
    static let colorBaseGreen = UIColor(red: 0.00, green: 1.00, blue: 0.00, alpha:1)
    static let colorBaseRed = UIColor(red: 1.00, green: 0.00, blue: 0.00, alpha:1)
    static let colorFontBase = UIColor(red: 1.00, green: 0.00, blue: 0.00, alpha:1)
    static let colorFontSecondary = UIColor(red: 0.00, green: 1.00, blue: 0.00, alpha:1)
    static let colorFontTertiary = UIColor(red: 0.80, green: 0.80, blue: 0.80, alpha:1)
    static let sizeFontBase = CGFloat(16.00)
    static let sizeFontLarge = CGFloat(32.00)
    static let sizeFontMedium = CGFloat(16.00)
    static let sizeFontSmall = CGFloat(12.00)
}

ios-swift-separate: allow enum-looking use so each category is separated into different files

//
// StyleDictionaryColor.swift
//
// Do not edit directly
// Generated on Sun, 10 Mar 2019 23:02:19 GMT
//

import UIKit

public enum StyleDictionaryColor {
    static let baseGrayLight = UIColor(red: 0.80, green: 0.80, blue: 0.80, alpha:1)
    static let baseGrayMedium = UIColor(red: 0.60, green: 0.60, blue: 0.60, alpha:1)
    static let baseGrayDark = UIColor(red: 0.07, green: 0.07, blue: 0.07, alpha:1)
    static let baseRed = UIColor(red: 1.00, green: 0.00, blue: 0.00, alpha:1)
    static let baseGreen = UIColor(red: 0.00, green: 1.00, blue: 0.00, alpha:1)
    static let fontBase = UIColor(red: 1.00, green: 0.00, blue: 0.00, alpha:1)
    static let fontSecondary = UIColor(red: 0.00, green: 1.00, blue: 0.00, alpha:1)
    static let fontTertiary = UIColor(red: 0.80, green: 0.80, blue: 0.80, alpha:1)
}
//
// StyleDictionarySize.swift
//
// Do not edit directly
// Generated on Sun, 10 Mar 2019 23:02:19 GMT
//

import UIKit

public enum StyleDictionarySize {
    static let fontSmall: CGFloat = 12.00
    static let fontMedium: CGFloat = 16.00
    static let fontLarge: CGFloat = 32.00
    static let fontBase: CGFloat = 16.00
}

For ios-swift-separate (as shown above), it creates public enums but adds static members to the enum class without any case statements. If we were to type the enum and then set values, every time someone wants to use them they would have to do something like:

let size = StyleDictionarySize.fontBase.rawValue

By doing it the way we do in the proposed changes, we allow for:

let size = StyleDictionary.fontBase

Also added the following transforms:

  • name/ti/camel: supports camel-case without the leading category (Swift encourages succinct names); also added supporting tests.
  • size/swift/remToCGFloat: like the already-in-place color/UIColorSwift, this adds support for CGFloat, which is used in UIKit for decimal values; swift will view a literal number with decimal point values as a Double without a class initializer.
  • content/swift/literal: the content/objC/literal prepends the @; swift doesn't use this for literals.
  • font/swift/literal: the font/objC/literal prepends the @; swift doesn't use this for literals.
  • asset/swift/literal: the asset/objC/literal prepends the @; swift doesn't use this for literals.

Added the ios-swift and ios-swift-separate transform groups using the new transforms as appropriate.

Added support for two specific templates:

  • ios-swift/class.swift: leverages Swift's type inference to allow any literal that is supported (e.g., String, UIColor, CGFloat) to be added as a property to a class.
  • ios-swift/enum.swift: leverages Swift's type inference to allow any literal that is supported (e.g., String, UIColor, CGFloat) but puts it into an enum instead of a class.

Updated the basic example to show size and color to match the Objective-C version by leveraging ios-swift-separate and also added a single ios-swift output to show how they can all be used in one StyleDictionary class instead.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@dbanksdesign
Copy link
Member

This is awesome! I will take a look this week, but thank you so much for doing this!

@davixyz
Copy link
Contributor

davixyz commented Mar 13, 2019

👍

Copy link
Member

@dbanksdesign dbanksdesign left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WTF I thought I submitted a review already.. oh well.. Anyways, great job on this PR! Looks great, I just have a few minor comments

lib/common/templates/ios-swift/static.swift.template Outdated Show resolved Hide resolved
lib/common/transformGroups.js Outdated Show resolved Hide resolved
lib/common/transforms.js Outdated Show resolved Hide resolved
lib/common/transforms.js Outdated Show resolved Hide resolved
@tonyheupel
Copy link
Contributor Author

tonyheupel commented Mar 13, 2019

Review comments addressed, added isAsset along with helper methods for wrapping prop values with characters like single and double quotes. Updated transforms.md with the new transforms and fixed a couple copy/paste errors.
@dbanksdesign

@chazzmoney
Copy link
Collaborator

Hey, this is GREAT! We have wanted to add Swift support for awhile (I'm pretty amateur w/ Swift), so thank you for doing this.

I do have one concern; the setup appears to require the user to output all sizes in one file and all other properties in another. It requires this decision making to happen in the config. Instead, it would be great if there was only a single Swift format and the properties themselves were used to determine when/how to add in type information.

Maybe we create a new transform to add an attribute containing the token type (tokenType? classType?). Then when the formatter is run, it can use that information to output the type info correctly.

Does that make sense? Or am I misinterpreting something about Swift?

@tonyheupel
Copy link
Contributor Author

Honestly, that's a good point. I had considered that, with the way "autocomplete/intellisense" works in XCode, that having to type StyleDictionary.color or StyleDictionary.size is a lot to type to still have a ton of options. That being said, before merging this, let me play with some ideas that can make it more flexible and potentially even allow folks to choose one or the other.

It's the transform groups where we essentially "decide" the conventions for the language, so I'll play with it and get back to you.

@tonyheupel
Copy link
Contributor Author

I've added the default ios-swift transform group that can support just adding everything that meets the filter into one class. I also added ios-swift-separate to support having separate classes and/or enums by filter but that doesn't have the category name in the name of the property. (see the pull request description for the two examples). Ready for you to accept if you are OK with it, @dbanksdesign !

@dbanksdesign
Copy link
Member

My apologies for the lateness, I am currently in Europe right now. I will dig into the code tonight (in about 12 hours), but reading over your comment and description it sounds good! Thank you again for all your work on this.

Copy link
Member

@dbanksdesign dbanksdesign left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 super minor things, after that I'm good to go. Thank you again for your work on this!

"destination": "StyleDictionary.swift",
"format": "ios-swift/class.swift",
"className": "StyleDictionary",
"type": "StyleDictionaryColorName",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It still doesn't look like 'type' here is being used, I can't see it in the format code. Is this meant to do something or could we remove it?

// Filter to only those props wanted based on the filter, then sort
// them by category so we keep like props together, then by name
// so they are easier to find alphabetically.
var filteredProps = _.filter(allProperties, this.filter);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm 99% sure you can remove this, I believe when we properly added the filter functionality we actually run the filters before the format so the format code itself doesn't have to worry about implementing filters. Take a look here

@@ -578,7 +628,7 @@ module.exports = {
return prop.attributes.category === 'content';
},
transformer: function(prop) {
return '\'' + prop.value + '\'';
return wrapValueWith('\'', prop);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this addition!

@tonyheupel
Copy link
Contributor Author

tonyheupel commented Mar 25, 2019 via email

@chazzmoney
Copy link
Collaborator

chazzmoney commented Apr 21, 2019

@tchype Thank again for all your hard work on this. We would love to get this functionality merged in for everyone to use - let me know if we can do anything to help ☺️

@tonyheupel
Copy link
Contributor Author

Sorry -- thought I finished this. Will finish today @chazzmoney !

@tonyheupel
Copy link
Contributor Author

Ready for review/merge @chazzmoney @dbanksdesign . Thanks!

@chazzmoney
Copy link
Collaborator

LGTM! :shipit:

Copy link
Member

@dbanksdesign dbanksdesign left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! :shipit:

@dbanksdesign dbanksdesign merged commit 749db69 into amzn:master Apr 23, 2019
@MDemetrio MDemetrio mentioned this pull request Sep 2, 2019
dbanksdesign added a commit that referenced this pull request Apr 24, 2020
Based on swift implementation ( #255 ), it adds swift transforms, transformGroups, formats, and example: 

* flutter/class.dart format
* flutter and flutter-separate transformGroups
* color/hex8flutter, content/flutter/literal, asset/flutter/literal, font/flutter/literal, and size/flutter/remToDouble transforms
* advanced/flutter example

fixes #288 

Co-authored-by: MDemetrio <[email protected]>
Co-authored-by: Danny Banks <[email protected]>
dbanksdesign added a commit that referenced this pull request Oct 5, 2020
Based on swift implementation ( #255 ), it adds swift transforms, transformGroups, formats, and example: 

* flutter/class.dart format
* flutter and flutter-separate transformGroups
* color/hex8flutter, content/flutter/literal, asset/flutter/literal, font/flutter/literal, and size/flutter/remToDouble transforms
* advanced/flutter example

fixes #288 

Co-authored-by: MDemetrio <[email protected]>
Co-authored-by: Danny Banks <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants