Skip to content

Commit

Permalink
feat(ios-swift): adding common transforms for Swift in iOS (#255)
Browse files Browse the repository at this point in the history
fixes: #161
  • Loading branch information
tonyheupel authored and dbanksdesign committed Apr 23, 2019
1 parent c7d3c15 commit 749db69
Show file tree
Hide file tree
Showing 12 changed files with 2,866 additions and 2,423 deletions.
20 changes: 20 additions & 0 deletions __tests__/common/transforms.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ describe('common', () => {
});
});

describe('name/ti/camel', () => {
it('should handle prefix', () => {
expect(transforms["name/ti/camel"].transformer(
{
path: ['one','two','three']
},{
prefix: 'prefix'
}
)).toBe('prefixTwoThree');
});

it('should handle no prefix', () => {
expect(transforms["name/ti/camel"].transformer(
{
path: ['one','two','three']
},{
}
)).toBe('twoThree');
});
});

describe('name/cti/kebab', () => {
it('should handle prefix', () => {
Expand Down
35 changes: 35 additions & 0 deletions __tests__/formats/__snapshots__/all.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,41 @@ NSString * const color_red = #FF0000;
"
`;
exports[`formats all should match ios-swift/class.swift snapshot 1`] = `
"
//
// __output/
//
// Do not edit directly
// Generated on Sat, 01 Jan 2000 00:00:00 GMT
//
import UIKit
public class {
static let color_red = #FF0000
}
"
`;
exports[`formats all should match ios-swift/enum.swift snapshot 1`] = `
"
//
// __output/
//
// Do not edit directly
// Generated on Sat, 01 Jan 2000 00:00:00 GMT
//
import UIKit
public enum {
static let color_red = #FF0000
}
"
`;
exports[`formats all should match javascript/es6 snapshot 1`] = `
"/**
* Do not edit directly
Expand Down
24 changes: 24 additions & 0 deletions docs/formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,30 @@ Creates an Objective-C implementation file of strings
- Add example and usage
* * *
### ios-swift/class.swift
Creates a Swift implementation file of a class with values
**Todo**
- Add example and usage
* * *
### ios-swift/enum.swift
Creates a Swift implementation file of an enum with values
**Todo**
- Add example and usage
* * *
### css/fonts.css
Expand Down
34 changes: 34 additions & 0 deletions docs/transform_groups.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,40 @@ Transforms:
[font/objC/literal](transforms.md#fontobjcliteral)


* * *

### ios-swift


Transforms:

[attribute/cti](transforms.md#attributecti)
[name/cti/camel](transforms.md#namecticamel)
[color/UIColorSwift](transforms.md#coloruicolorswift)
[content/swift/literal](transforms.md#contentswiftliteral)
[asset/swift/literal](transforms.md#assetswiftliteral)
[size/swift/remToCGFloat](transforms.md#sizeswiftremtocgfloat)
[font/swift/literal](transforms.md#fontswiftliteral)


* * *

### ios-swift-separate


Transforms:

[attribute/cti](transforms.md#attributecti)
[name/ti/camel](transforms.md#nameticamel)
[color/UIColorSwift](transforms.md#coloruicolorswift)
[content/swift/literal](transforms.md#contentswiftliteral)
[asset/swift/literal](transforms.md#assetswiftliteral)
[size/swift/remToCGFloat](transforms.md#sizeswiftremtocgfloat)
[font/swift/literal](transforms.md#fontswiftliteral)

This is to be used if you want to have separate files per category and you don't want the category (e.g., color) as the lead value in the name of the property (e.g., StyleDictionaryColor.baseText instead of StyleDictionary.colorBaseText).


* * *

### assets
Expand Down
86 changes: 85 additions & 1 deletion docs/transforms.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ EDIT scripts/handlebars/templates/api.hbs OR JSDOC COMMENT INSTEAD!
-->
# Transforms

Transforms are functions that transform a property so that each platform can consume the property in different ways. A simple example is changing pixel values to point values for iOS and dp or sp for Android. Transforms are applied in a non-destructive way so each platform can transform the properties. Transforms are performed sequentially, so the order you use transforms matters. Transforms are used in your [configuration](config.md), and can be either [pre-defined transforms](transforms.md?id=pre-defined-transforms) supplied by Style Dictionary or [custom transforms](transforms.md?id=defining-custom-transforms).
Transforms are functions that transform a property so that each platform can consume the property in different ways. A simple example is changing pixel values to point values for iOS and dp or sp for Android. Transforms are applied in a non-destructive way so each platform can transform the properties. Transforms are performed sequentially, so the order you use transforms matters. Transforms are used in your [configuration](config.md), and can be either [pre-defined transforms](transforms.md?id=defining-custom-transforms) supplied by Style Dictionary or [custom transforms](transforms.md?id=defining-custom-transforms).

## Using Transforms
You use transforms in your config file under platforms > [platform] > transforms
Expand Down Expand Up @@ -106,6 +106,23 @@ Creates a camel case name. If you define a prefix on the platform in your config
```


* * *

### name/ti/camel


Creates a camel case name without the category at the front. This is most useful when there is a class, struct, enum, etc.
that already has the category in it (e.g., StyleDictionaryColors.baseDarkRed instad of StyleDictionaryColors.colorBaseDarkRed).
If you define a prefix on the platform in your config, it will prepend with your prefix

```js
// Matches: all
// Returns:
"backgroundButtonPrimaryActive"
"prefixBackgroundButtonPrimaryActive"
```


* * *

### name/cti/kebab
Expand Down Expand Up @@ -251,6 +268,20 @@ Transforms the value into an UIColor class for iOS
```
* * *
### color/UIColorSwift
Transforms the value into an UIColor swift class for iOS
```swift
// Matches: prop.attributes.category === 'color'
// Returns:
UIColor(red: 0.67, green: 0.67, blue: 0.67, alpha:0.6)
```


* * *

### color/css
Expand Down Expand Up @@ -364,6 +395,19 @@ Scales the number by 16 (default web font size) and adds 'pt' to the end.
```


* * *

### size/swift/remToCGFloat


Scales the number by 16 to get to points for Swift and initializes a CGFloat

```js
// Matches: prop.attributes.category === 'size'
// Returns: "CGFloat(16.00)""
```


* * *

### size/remToPx
Expand Down Expand Up @@ -419,6 +463,20 @@ Wraps the value in a double-quoted string and prepends an '@' to make a string l

**"string"**: ```

* * *

### content/swift/literal


Wraps the value in a double-quoted string to make a string literal.

```swift
// Matches: prop.attributes.category === 'content'
// Returns:
"string"
```


* * *

### font/objC/literal
Expand All @@ -432,6 +490,19 @@ Wraps the value in a double-quoted string and prepends an '@' to make a string l
```


* * *

### font/swift/literal


Wraps the value in a double-quoted string to make a string literal.

```swift
// Matches: prop.attributes.category === 'font'
// Returns: "string"
```


* * *

### time/seconds
Expand Down Expand Up @@ -489,3 +560,16 @@ Wraps the value in a double-quoted string and prepends an '@' to make a string l

* * *

### asset/swift/literal


Wraps the value in a double-quoted string to make a string literal.

```swift
// Matches: prop.attributes.category === 'asset'
// Returns: "string"
```


* * *

34 changes: 34 additions & 0 deletions examples/basic/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,40 @@
}
}
}]
},
"ios-swift": {
"transformGroup": "ios-swift",
"buildPath": "build/ios-swift/",
"files": [{
"destination": "StyleDictionary.swift",
"format": "ios-swift/class.swift",
"className": "StyleDictionary",
"filter": {}
}]
},
"ios-swift-separate-enums": {
"transformGroup": "ios-swift-separate",
"buildPath": "build/ios-swift/",
"files": [{
"destination": "StyleDictionaryColor.swift",
"format": "ios-swift/enum.swift",
"className": "StyleDictionaryColor",
"filter": {
"attributes": {
"category": "color"
}
}
},{
"destination": "StyleDictionarySize.swift",
"format": "ios-swift/enum.swift",
"className": "StyleDictionarySize",
"type": "float",
"filter": {
"attributes": {
"category": "size"
}
}
}]
}
}
}
21 changes: 21 additions & 0 deletions lib/common/formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,27 @@ module.exports = {
fs.readFileSync(__dirname + '/templates/ios/strings.m.template')
),

/**
* Creates a Swift implementation file of a class with values
*
* @memberof Formats
* @kind member
* @todo Add example and usage
*/
'ios-swift/class.swift': _.template(
fs.readFileSync(__dirname + '/templates/ios-swift/class.swift.template')
),

/**
* Creates a Swift implementation file of an enum with values
*
* @memberof Formats
* @kind member
* @todo Add example and usage
*/
'ios-swift/enum.swift': _.template(
fs.readFileSync(__dirname + '/templates/ios-swift/enum.swift.template')
),
// Css templates

/**
Expand Down
39 changes: 39 additions & 0 deletions lib/common/templates/ios-swift/class.swift.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<%
//
// Copyright 2019 Alaska Air Group or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// A copy of the License is located at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
%>
//
// <%= this.destination %>
//
<%
// for backward compatibility we need to have the user explicitly hide it
var showFileHeader = (this.options && this.options.hasOwnProperty('showFileHeader')) ? this.options.showFileHeader : true;
if(showFileHeader) {
print("// Do not edit directly\n");
print("// Generated on " + new Date().toUTCString());
}
%>
//
<%
// 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 props = _.sortBy(allProperties, item => item.attributes.category + item.name);
%>

import UIKit

public class <%= this.className %> {
<%= _.map(props, function(prop) { return 'static let ' + prop.name + ' = ' + prop.value; }).join('\n ') %>
}
Loading

0 comments on commit 749db69

Please sign in to comment.