Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Novichkov committed Mar 23, 2018
2 parents b014644 + 83155cf commit 44d6d5c
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 28 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zepcode",
"version": "0.5.1",
"version": "0.5.3",
"description": "Generates Swift snippets from colors, fonts and layers.",
"lint-staged": {
"src/**/*.{js,json}": [
Expand Down
19 changes: 9 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import zepcode from './zepcode';

function styleguideColors(context, colors) {
return zepcode(context).generateColorExtension(colors);
return zepcode(context).generateColorExtension(colors, false);
}

function styleguideTextStyles(context, textStyles) {
Expand All @@ -11,7 +11,6 @@ function styleguideTextStyles(context, textStyles) {
function layer(context, layerParams) {
const zepcodeInstance = zepcode(context);
let string = '';
const newlineBeforeContent = () => (string.length ? '\n\n' : '');

if (layerParams.fills.length) {
const { gradient } = layerParams.fills[0];
Expand All @@ -32,10 +31,13 @@ function layer(context, layerParams) {
string += gradientString;
}

if (string.length) {
string += `\n\n`;
}

if (layerParams.opacity !== 1) {
string += `${newlineBeforeContent()}view.alpha = ${layerParams.opacity.toFixed(
2
)}\n`;
const opacity = Math.round(layerParams.opacity * 100) / 100;
string += `view.alpha = ${opacity}\n`;
}

if (layerParams.borders.length) {
Expand All @@ -52,15 +54,12 @@ function layer(context, layerParams) {
}

if (layerParams.borderRadius > 0) {
string += `${newlineBeforeContent()}view.layer.cornerRadius = ${
layerParams.borderRadius
}`;
string += `view.layer.cornerRadius = ${layerParams.borderRadius}`;
}

if (layerParams.shadows.length) {
const shadow = layerParams.shadows[0];
const { color } = shadow;
string += newlineBeforeContent();

if (color !== undefined) {
const shadowColor = zepcodeInstance.cgColorString(shadow.color);
Expand Down Expand Up @@ -90,7 +89,7 @@ function comment(context, text) {
}

function exportStyleguideColors(context, colors) {
return zepcode(context).generateColorExtension(colors);
return zepcode(context).generateColorExtension(colors, true);
}

function exportStyleguideTextStyles(context, textStyles) {
Expand Down
20 changes: 12 additions & 8 deletions src/templates/color-extension.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import customColorTemplate from './custom-color';
import colorTemplate from './color';

const colorExtensionTemplate = (colors, options) =>`import UIKit
const colorExtensionTemplate = (colors, needHeader, extensionOptions) =>`import UIKit
extension UIColor {
${
options.useCustomColorInitializer
? `\n convenience init(r red: Int, g green: Int, b blue: Int, a: CGFloat = 1) {
self.init(red: CGFloat(red) / 255, green: CGFloat(green) / 255, blue: CGFloat(blue) / 255, alpha: a)
${
extensionOptions.useCustomColorInitializer
? `\n convenience init(r red: Int, g green: Int, b blue: Int, a: CGFloat = 1) { // swiftlint:disable:this identifier_name
self.init(red: CGFloat(red) / 255,
green: CGFloat(green) / 255,
blue: CGFloat(blue) / 255,
alpha: a)
}\n`
: ``}
: ``}
${colors.map(color => `static let ${color.name} = ${
options.useCustomColorInitializer
extensionOptions.useCustomColorInitializer
? customColorTemplate(color)
: colorTemplate(color)
}`).join('\n ')}
}`;
}
`;

export default colorExtensionTemplate;
6 changes: 3 additions & 3 deletions src/templates/font-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const fontExtensionTemplate = uniqueFonts => `import UIKit
extension UIFont {
${uniqueFonts.map(styleName => `
static func ${camelizeFilter(styleName)}(ofSize: CGFloat) -> UIFont {
return UIFont(name: "${styleName}", size: size)!
}`
static func ${camelizeFilter(styleName)}(ofSize: CGFloat) -> UIFont {
return UIFont(name: "${styleName}", size: size)!
}`
).join('\n')}
}`;

Expand Down
11 changes: 11 additions & 0 deletions src/templates/header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const headerTemplate = (fileName, projectName) => `//
// ${fileName}
// ${projectName}
//
// Generated by Zepcode on ${
new Date().toLocaleDateString()
}.
// Copyright © ${new Date().getFullYear()} __MyCompanyName__. All rights reserved.
//`;

export default headerTemplate;
20 changes: 15 additions & 5 deletions src/zepcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import colorTemplate from './templates/color';
import linearGradientTemplate from './templates/linear-gradient';
import radialGradientTemplate from './templates/radial-gradient';
import fontExtensionTemplate from './templates/font-extension';
import headerTemplate from './templates/header';

const zepcode = (() => {
let instance;
Expand Down Expand Up @@ -53,11 +54,20 @@ const zepcode = (() => {
return `${text} ${textOption !== undefined ? textOption : ''}`;
};

me.generateColorExtension = colors => ({
code: colorExtensionTemplate(colors, me.options),
language: 'swift',
filename: 'UIColor+AppColors.swift',
});
me.generateColorExtension = (colors, needHeader) => {
const fileName = 'UIColor+AppColors.swift';
let string = '';
if (needHeader) {
string += headerTemplate(fileName, me.project.name);
string += '\n\n';
}
string += colorExtensionTemplate(colors, needHeader, me.options);
return {
code: string,
language: 'swift',
filename: fileName,
};
};

me.generateFontExtension = textStyles => {
const uniqueFonts = Array.from(
Expand Down

0 comments on commit 44d6d5c

Please sign in to comment.