Skip to content

The samples using the "SwiftGen" library. (SwiftGen 라이브러리 사용방법을 셈플로 설명했습니다.)

License

Notifications You must be signed in to change notification settings

ClintJang/sample-swiftgen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sample SwiftGen

License Swift 4

  • SwiftGen : How to use it!!!!
  • SwiftGen is a tool to auto-generate Swift code for resources of your projects, to make them type-safe to use.
  • 한국어 설명 링크 : Sample description in Korean

Development Information

| check version information?!!

$ swiftgen --version
  SwiftGen v5.2.1 (Stencil v0.9.0, StencilSwiftKit v2.3.0, SwiftGenKit v2.1.1)
  • Stencil : The magical, reusable web component compiler
  • StencilSwiftKit : StencilSwiftKit is a framework bringing additional Stencil nodes & filters dedicated to Swift code generation.
  • SwiftGenKit : This is the framework behind SwiftGen, responsible for parsing various resources and turning them into Stencil contexts.
    • This repository has been merged into the main SwiftGen repository.

Installation

Before you test, be sure to follow the link and follow the installation process.

ex) 
install

$ brew update
$ brew install swiftgen

confirm
$ swiftgen ...

Samples (Base Template)

Download the sample source and modify the "/Resources/TestCase0?Localizable.strings" file. Then run the build. This will change the file "/Constants/Test.Case0?.Localized.swift".

Script Setting
(Target >> Bulid Phases Tap >> + Button Click >> New Run Script Phase)
Base Template Detail
  • You can create a **** .sh file later and run it.
  • Now let 's test the basics of 5 things!
    • swiftgen strings [OPTIONS] FILE1 …
    • swiftgen storyboards [OPTIONS] DIR1 …
    • swiftgen xcassets [OPTIONS] CATALOG1 …
    • swiftgen colors [OPTIONS] FILE1 …
    • swiftgen fonts [OPTIONS] DIR1 …

Case 01 ~ 07 : Test results

  • ViewController.swift
override func viewDidLoad() {
        super.viewDidLoad()
        
        testSamplesBaseTemplates()
    }

    func testSamplesBaseTemplates() {
        // Case 01 : String
        case01Label.text = L10n.testCase01TitleTop01
        
        // Case 02 : String
        case02Label.text = TestCase02Strings.testCase02TitleTop01
        
        // Case 03 : String
        case03Label.text = TestCase03Strings.testCase03TitleBottom01
        
        // Case 04 : Storyboard
        // Pass
        
        // Case 05-1 : Assets
        //        let swiftGenSample01Image = UIImage(asset: Asset.imageSwiftGenSample01)
        let swiftGenSample01Image = Asset.imageSwiftGenSample01.image
        case0501ImageView.image = swiftGenSample01Image
        debugPrint(Asset.allImages)
        
        //        case0502Label.textColor = UIColor(asset: Asset.test01Color)
        case0502Label.textColor = Asset.test01Color.color
        debugPrint(Asset.allColors)
        
        // Case 06 : Colors
        //        case06Label.textColor = Color(named:ColorName.articleBody)
        case06Label.textColor = ColorName.articleBody.color
        
        // Case 07 : Fonts
        //        case06Label.font = UIFont(font: FontFamily.SFCompactDisplay.ultralight, size: 30.0)
        case07Label.font = UIFont(font: FontFamily.SFProDisplay.blackItalic, size: 30.0)
    }

Let's look at the details.

Case 01 : String

Let's build it!

Script..

# testing script case 01
swiftgen strings "${SRCROOT}/${TARGETNAME}/Resources/TestCase01Localizable.strings" --template structured-swift4 --output "${SRCROOT}/${TARGETNAME}/Constants/BasicTemplate/Test.Case01.Localized.swift"

Read the Link(SwiftGen) "Templates bundled with SwiftGen:".

A swift2 template, compatible with Swift 2
A swift3 template, compatible with Swift 3
A swift4 template, compatible with Swift 4
Other variants, like flat-swift2/3/4 and structured-swift2/3/4 templates for Strings, etc.

result

Case 02 : String

Define the naming of the enum yourself, and abbreviate the command.

  • add : --param enumName=TestCase02Strings
  • modify : --output => -o
  • modify : --template => -t
Script..

# testing script case 02
swiftgen strings --param enumName=TestCase02Strings "${SRCROOT}/${TARGETNAME}/Resources/TestCase02Localizable.strings" -t structured-swift4 -o "${SRCROOT}/${TARGETNAME}/Constants/BasicTemplate/Test.Case02.Localized.swift"

result

Case 03 : String

Add localization

  • modify : ../Resources/.. => ../Resources/Base.lproj/..
difference point
Script.. 

# testing script case 03
swiftgen strings --param enumName=TestCase03Strings "${SRCROOT}/${TARGETNAME}/Resources/Base.lproj/TestCase03Localizable.strings" -t structured-swift4 -o "${SRCROOT}/${TARGETNAME}/Constants/BasicTemplate/Test.Case03.Localized.swift"

result

Case 04 : StoryBoard

Storyboards are a little different but similar.

Script.. 

# testing script case 04
swiftgen storyboards -t swift4 "${SRCROOT}/${TARGETNAME}/Base.lproj/Main.storyboard" -o "${SRCROOT}/${TARGETNAME}/Constants/BasicTemplate/Test.Case04.Storyboards.swift"

result

Case 05 : Xcassets

Let's try Xcassets!

Script.. 

# testing script case 05
swiftgen xcassets "${SRCROOT}/${TARGETNAME}/Assets.xcassets" -t swift4 -o "${SRCROOT}/${TARGETNAME}/Constants/BasicTemplate/Test.Case05.Xcassets.swift"

result

Case 06 : Colors

I added color samples. However, you can add color in xcassets. It will then be added to the output of case 5.

SwiftGen/SwiftGen#315

  • add file (colors.txt)
add colors.txt
Script.. 

# testing script case 06
swiftgen colors -t swift4 "${SRCROOT}/${TARGETNAME}/Resources/colors.txt"  -o "${SRCROOT}/${TARGETNAME}/Constants/BasicTemplate/Test.Case06.Colors.swift"

result

Case 07 : Fonts

I added some of the font files from Apple, and tried to code-generate the fonts in that path into files.

  • add fonts file (in "Font" folder)
add fonts file
Script.. 

# testing script case 07
# https://developer.apple.com/fonts/
swiftgen fonts -t swift4 "${SRCROOT}/${TARGETNAME}/Resources/Font"  -o "${SRCROOT}/${TARGETNAME}/Constants/BasicTemplate/Test.Case07.Fonts.swift"

result

Samples (User Custom, Use of Stencil)

Let's create a file that you want to make.

The Stencil template language

Stencil is a simple and powerful template language for Swift. 
It provides a syntax similar to Django and Mustache. 
If you’re familiar with these, 
you will feel right at home with Stencil.
  • You only need to know how to use it, and you can use Stencil by default.
  • Take a look at the use cases below.

How do I customize it?

  • If you know the grammar, how can you customize it? That way, you can click the link below to get the template source. This can be analyzed and corrected.

https://github.com/SwiftGen/SwiftGen/tree/master/templates

Case 01 : String, Objective-C?

What if you want to make it an Objective-C source? Let's try it.

  • I tried to make it #define simply. :)
Script.. 

# testing script case 01
swiftgen strings "${SRCROOT}/${TARGETNAME}/Resources/TestCase01Localizable.strings" --templatePath ${SRCROOT}/swiftgen/StencilTemplates/Localization/objLocalization.stencil --output "${SRCROOT}/${TARGETNAME}/Constants/CustomTemplate/Test.Custom.Case01.Localized.h"
  • objLocalization.stencil
{% if tables.count > 0 %}

{# You can modify the value below with the actual value. #}
{% macro recursiveBlock table item sp %}
{{sp}}  {% for string in item.strings %}
{{sp}}  {% if not param.noComments %}
{{sp}}  /// {{string.translation}}
{{sp}}  #define {{string.name}} "{{string.key}}"
{{sp}}  {% endif %}
{{sp}}  {% if not param.noComments %}
{{sp}}  /// LOG : {{string}}
{{sp}}  {% endif %}
{{sp}}  {% endfor %}
{% endmacro %}

{# print #}
{% call recursiveBlock tables.first.name tables.first.levels "" %}

{% else %}
// No string found
{% endif %}

result

  /// test(case01) string 1
  #define test_case01_sub_index01 "test_case01_sub_index01"
  /// LOG : ["translation": "test(case01) string 1", "name": "test_case01_sub_index01", "key": "test_case01_sub_index01"]
  /// test(case01) string 2
  #define test_case01_sub_index02 "test_case01_sub_index02"
  /// LOG : ["translation": "test(case01) string 2", "name": "test_case01_sub_index02", "key": "test_case01_sub_index02"]
  /// test(case01) sample 2
  #define test_case01_title_bottom01 "test_case01_title_bottom01"
  /// LOG : ["translation": "test(case01) sample 2", "name": "test_case01_title_bottom01", "key": "test_case01_title_bottom01"]
  /// test(case01) sample 1
  #define test_case01_title_top01 "test_case01_title_top01"
  /// LOG : ["translation": "test(case01) sample 1", "name": "test_case01_title_top01", "key": "test_case01_title_top01"]

Case 02 : String

Want to make your default template look different?

.. doing it.

Conclusion

Why not try it?

You can also manage it by running it as a script command from the shell separately.

About

The samples using the "SwiftGen" library. (SwiftGen 라이브러리 사용방법을 셈플로 설명했습니다.)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published