resgen.swift/README.md
Thibaut Schmitt 41733d2680
Some checks failed
gitea-openium/resgen.swift/pipeline/head There was a failure building this commit
Squashed commit of the following:
commit aa59ef28ea56315eb421ba044ddaf0c4066bfff8
Author: Thibaut Schmitt <t.schmitt@openium.fr>
Date:   Mon Nov 7 10:26:28 2022 +0100

    Add trailing carrier at the end of generated extension files

commit e985950aa1e39d81d4938e15f8724c0f7723b003
Author: Thibaut Schmitt <t.schmitt@openium.fr>
Date:   Fri Nov 4 16:37:34 2022 +0100

    Replace installation script by script that install completion file
    Setup a Makefile to install resgen

commit d574384c151259610a4c2f837b14068bb7716e44
Author: Thibaut Schmitt <t.schmitt@openium.fr>
Date:   Fri Nov 4 14:33:39 2022 +0100

    Refactor
    Improve testability
    Add tests on SwiftUI generated code
    Add tests on  command

commit d9e76632c3037da0ed9e1dd37056685416579da9
Author: Thibaut Schmitt <t.schmitt@openium.fr>
Date:   Thu Nov 3 15:43:47 2022 +0100

    Fixing bad merge on FontOptions

commit 76b5ebfcd1cde7a7d4c83f516a4fc937841e7715
Author: Thibaut Schmitt <t.schmitt@openium.fr>
Date:   Thu Nov 3 15:37:28 2022 +0100

    Squashed commit of the following:

    commit 085f1ffc3347d3c48af91ffb00a1a9b381ce47d1
    Author: Thibaut Schmitt <t.schmitt@openium.fr>
    Date:   Thu Nov 3 15:00:27 2022 +0100

        Refactor SwiftUI extension generation and generation SwiftUI extension for images

    commit 4f7d7e18b138343a07cbb0bb47213818678ced6b
    Author: Thibaut Schmitt <t.schmitt@openium.fr>
    Date:   Mon Oct 31 16:21:32 2022 +0100

        Génération de composant SwiftUI: Color et Image

    commit 0797667b2510f6fd45b9845f2d29c0c1e31da877
    Author: Thibaut Schmitt <t.schmitt@openium.fr>
    Date:   Mon Oct 31 16:21:12 2022 +0100

        Génération de composant SwiftUI: Color et Image

commit 417a2630925841dd486ae1d684d28ab7dca240e0
Author: Thibaut Schmitt <t.schmitt@openium.fr>
Date:   Wed Oct 19 17:13:03 2022 +0200

    Update Info.plist UIAppFonts key when generating fonts (if plist path if defined)
2022-11-07 10:32:17 +01:00

9.1 KiB

ResgenSwift

ResgenSwift is a package, fully written in Swift, to help you automatize ressource update and generation.

🧐 For all commands, see samples files in SampleFiles

Fonts

Font generator generates an extension of UIFont and Font (or custom classes). It also prints content of UIAppFonts from your project .plist. If project .plist is specified, it will update UIAppFonts content of all .plist.

iOS required to use the real name of the font, this name can be different from its filename. To get the real name, it uses fc-scan. So, be sure that the $PATH contains path of fc-scan.

Example

swift run -c release ResgenSwift fonts $FORCE_FLAG "./Fonts/fonts.txt" \
 --extension-output-path "./Fonts/Generated" \
 --extension-name "AppFont" \
 --extension-name-swift-ui "SUIAppFont" \
 --extension-suffix "GreatApp" \
 --static-members true \
 --info-plist-paths "./path/one/to/Info.plist ./path/two/to/Info.plist"

Parameters

  1. -f: force generation
  2. Font input folder, it will search for every .ttf and .otf files specified in fonts.txt
  3. --extension-output-path: path where to generate generated extension
  4. --extension-name (optional) : name of the class to add UIKit getters
  5. --extension-name-swift-ui (optional) : name of the class to add SwiftUI getters
  6. --extension-suffix (optional) : additional text which is added to the filename (ex: AppFont+GreatApp.swift)
  7. --static-members (optional): generate static properties or not
  8. --info-plist-paths (optional): array of .plist, you can specify multiple Info.plist for multiple targets

Colors

Colors generator generates an extension of UIColor (or a custom class) along with colorsets in specified xcassets.

swift run -c release ResgenSwift colors $FORCE_FLAG "./Colors/colors.txt" \
 --style all \
 --xcassets-path "./Colors/colors.xcassets" \
 --extension-output-path "./Colors/Generated/" \
 --extension-name "AppColor" \
 --extension-name-swift-ui "SUIAppColor" \ 
 --extension-suffix "GreatApp" \
 --static-members true

Parameters

  1. -f: force generation
  2. Input colors file
  3. --style can be all or light
  4. --extension-output-path: path where to generate generated extension
  5. --extension-name (optional) : name of the class to add UIKit getters
  6. --extension-name-swift-ui (optional) : name of the class to add SwiftUI getters
  7. --extension-suffix (optional) : additional text which is added to filename (ex: AppColor+GreatApp.swift)
  8. --static-members (optional): generate static properties or not

Strings

Strings command allows to generate strings files along with extensions to access those strings easily. It can do it 2 ways: Twine and Stringium. It is not recommended to use Twine except on legacy projects or while migrating to ResgenSwift, because it use https://github.com/openium/twine. Using Stringium is recommended because it does not required external dependency and allow more customisation.

swift run -c release ResgenSwift strings twine $FORCE_FLAG "./Twine/strings.txt" \
 --output-path "./Twine/Generated" \
 --langs "fr en en-us" \
 --default-lang "en" \
 --extension-output-path "./Twine/Generated"

Parameters

  1. -f: force generation
  2. Input translations file (must be Twine formatted)
  3. --langs: langs to generate (string with space between each lang)
  4. --default-lang: default lang that will be in Base.lproj. It must be in langs as well
  5. --extension-output-path: path where to generate generated extension
swift run -c release ResgenSwift strings stringium $FORCE_FLAG "./Strings/strings.txt" \
 --output-path "./Strings/Generated" \
 --langs "fr en en-us" \
 --default-lang "en" \
 --extension-output-path "./Strings/Generated" \
 --extension-name "AppString" \
 --extension-suffix "GreatApp" \
 --static-members true

Parameters

  1. -f: force generation
  2. Input translations file (must be Twine formatted)
  3. --langs: langs to generate (string with space between each lang)
  4. --default-lang: default lang that will be in Base.lproj. It must be in langs as well
  5. --extension-output-path: path where to generate generated extension
  6. --extension-name (optional) : name of class to add the extension
  7. --extension-suffix (optional) : additional text which is added to filename (ex: AppString+GreatApp.swift)
  8. --static-members (optional): generate static properties or not

Tags

Tags is also a subcommand of Strings. Input files are formatted the same way. Tags will generate properties which return exactly what is specified in the input file. It was designed to be used for analytics purpose and to be shared with any other platform to have the same analytics keys.

swift run -c release ResgenSwift strings tags $FORCE_FLAG "./Tags/tags.txt" \
 --lang "ium" \
 --extension-output-path "./Tags/Generated" \
 --extension-name "AppTags" \
 --extension-suffix "GreatApp" \
 --static-members true

Parameters

  1. -f: force generation
  2. Input tags file (must be Twine formatted)
  3. --lang: langs to look at in input file
  4. --extension-output-path: path where to generate generated extension
  5. --extension-name (optional) : name of class to add the extension
  6. --extension-suffix (optional) : additional text which is added to filename (ex: AppTags+GreatApp.swift)
  7. --static-members (optional): generate static properties or not

⚠️ If extension name is not set or is Tags, it will generate the following typaloas typealias Tags = String.

Images

Images generator will generate images assets along with extensions to access those images easily.

swift run -c release ResgenSwift images $FORCE_FLAG "./Images/images.txt" \
 --xcassets-path "./Images/app.xcassets" \
 --extension-output-path "./Images/Generated" \
 --extension-name "AppImage" \
 --extension-name-swift-ui "SUIAppImage" \ 
 --extension-suffix "GreatApp" \
 --static-members true

Parameters

  1. -f: force generation
  2. Input images definitions file
  3. --xcassets-path: xcasset path where to generate imagesets
  4. --extension-output-path: path where to generate generated extension
  5. --extension-name (optional) : name of the class to add UIKit getters
  6. --extension-name-swift-ui (optional) : name of the class to add SwiftUI getters
  7. --extension-suffix (optional) : additional text which is added to filename (ex: AppImage+GreatApp.swift)
  8. --static-members (optional): generate static properties or not

All at once

Another command exists to generate all ressources at the same time: generate. It use the following commands: Fonts, Colors, Strings/Stringium, Strings/Tags, Images.

All parameters can be specified in a configuration file in Yaml:

Order of configuration types does not matter. Order them to fit your needs.

---
colors:
-
  inputFile: String
  style: [light/all]
  xcassetsPath: String
  extensionOutputPath: String
  extensionName: String?
  extensionNameSwiftUI: String?
  extensionSuffix: String?
  staticMembers: Bool?

fonts:
-
  inputFile: String
  extensionOutputPath: String
  extensionName: String?
  extensionNameSwiftUI: String?
  extensionSuffix: String?
  staticMembers: Bool?
  infoPlistPaths: [String]
  
images:
- 
  inputFile: String
  xcassetsPath: String
  extensionOutputPath: String
  extensionName: String?
  extensionNameSwiftUI: String?
  extensionSuffix: String?
  staticMembers: Bool?

strings:
- 
  inputFile: String
  outputPath: String
  langs: String
  defaultLang: String
  extensionOutputPath: String
  extensionName: String?
  extensionSuffix: String?
  staticMembers: Bool?

tags:
- 
  inputFile: String
  lang: String
  extensionOutputPath: String
  extensionName: String?
  extensionSuffix: String?
  staticMembers: Bool?

Multiple configurations

In some case, you may need to have 2 colors files in your projects. You will need 2 colors configurations. Every configuration type is an array and can contains as many configurations as you need.

Sample for 2 colors configurations:

...
colors:
-
  inputFile: String
  style: [light/all]
  xcassetsPath: String
  extensionOutputPath: String
  extensionName: String?
  extensionNameSwiftUI: String?
  extensionSuffix: String?
  staticMembers: Bool?
-
  inputFile: String
  style: [light/all]
  xcassetsPath: String
  extensionOutputPath: String
  extensionName: String?
  extensionNameSwiftUI: String?
  extensionSuffix: String?
  staticMembers: Bool?  
...

No configuration

In some case, you may not need to generate tags for example. You must specified tags as an empty array :

...
tags: []
...

Usage

swift run -c release ResgenSwift generate path/to/configuration.yml --project-directory ${PROJECT_DIR}

⚠️ Every path in configuration.yml will be prepended by content of --project-directory if they are relative path (not starting with /)

Installation

Simple run ./install.sh. Binary will be install in /usr/local/bin. Then, use ResgenSwift like any other command:

Example:

ResgenSwift generate path/to/configuration.yml --project-directory ${PROJECT_DIR}