GenStore is a lightweight swift code generator for your resources.

Overview

GenStore

GenStore is a lightweight swift code generator for your resources. GenStore can create classes for your images, colors and localized strings.

Using String-based APIs to access your resources has many drawbacks. There's always the risk of typos, as well as using resources that are no longer there. Auto generating code from resources and using it ensures that all the drawbacks from using String-based APIs will simply cease to exist as they will be automatically caught while compiling. You will also have the benefit of auto-completion which is always cool.

Installation

Using Mint

$ mint install hassaanelgarem/GenStore-swift

From Source

$ git clone https://github.com/hassaanelgarem/GenStore-swift
$ cd GenStore-swift
$ make

Usage

$ gen-store --type <type> --source <source> --output <output>

Options:

  • --type: Type of the store being generated. Accepted values: [colors, images, strings]
  • --source: Path of the source used to generate the store
  • --output: Path of the output file

All three options are required for the command to run. For more details use: gen-store --help

Strings Store Example
To generate a swift class from a .strings file use the following command:
$ gen-store --type strings --source Localizable.strings --output StringsStore.swift

Given the following Localizable.strings file:

"jim" = "Bears. Beets. Battlestar Galactica.";
"dwight" = "Identity theft is not a joke, Jim!";

The generated code will look like this:

class StringsStore {

    static let jim = "jim".localized
    static let dwight = "dwight".localized

}

fileprivate extension String {
    var localized: String {
        return NSLocalizedString(self, comment: "")
    }
}

And now you can use it this way in your code:

let message = StringsStore.jim
let reply = StringsStore.dwight

Colors Store Example
To generate a swift class represting your colors from an xcassets file use the following command:
$ gen-store --type colors --source Colors.xcasseets --output ColorsStore.swift

Given the following Colors.xcasseets file:

Colors xcasseets

The generated code will look like this:

UIColor { guard let color = UIColor(named: name) else { print("[ERROR] - Color with name (\"\(name)\") not found in assets cataloug") return UIColor.white } return color } } ">
class ColorsStore {

    static let dijonMustard: UIColor = Self.unwrappedColor(named: "dijonMustard")
    static let spicyBrown: UIColor = Self.unwrappedColor(named: "spicyBrown")
    static let yellowMustard: UIColor = Self.unwrappedColor(named: "yellowMustard")

    static func unwrappedColor(named name: String) -> UIColor {
        guard let color = UIColor(named: name) else {
            print("[ERROR] - Color with name (\"\(name)\") not found in assets cataloug")
            return UIColor.white
        }
        return color
    }
}

And now you can use it this way in your code:

let shirtColor = ColorsStore.spicyBrown

Images Store Example
To generate a swift class represting your images from an xcassets file use the following command:
$ gen-store --type images --source Images.xcasseets --output ImagesStore.swift

Given the following Images.xcasseets file:

Images xcasseets

The generated code will look like this:

class ImagesStore {

    static let babiesPoster: UIImage? = UIImage(named: "babiesPoster")
    static let chairModel: UIImage? = UIImage(named: "chairModel")

}

And now you can use it this way in your code:

let theOne = ImagesStore.chairModel

Xcode Integration

To make the best out of GenStore, you should integrate it in your Xcode project by adding it as a "Run Script Build Phase". By doing so, this will generate your classes every time you build your project, which will make sure that your generated classes are always up to date.

Adding a new script phase:

  1. Open project file form inside Xcode by selecting it from the project navigator on the left

  2. Select your app target

  3. Select "Build Phases" tab

  4. Click on the "+" button to add a new phase then select "New Run Script Phase"

  5. Name the new phase whatever you want

  6. Fill in the script the same way you would use GenStore in the terminal. You can make use of environment variables to make the script work on any machine. Your script can look something like this:

    gen-store -t strings -s $SRCROOT/Example/Storyboards/en.lproj/Localizable.strings -o $SRCROOT/Example/Stores/StringsStore.swift
    gen-store -t images -s $SRCROOT/Example/Resources/Assets.xcassets -o $SRCROOT/Example/Stores/ImagesStore.swift
    gen-store -t colors -s $SRCROOT/Example/Resources/Assets.xcassets -o $SRCROOT/Example/Stores/ColorsStore.swift

Alternatives

If GenStore doesn't meet your needs try SwiftGen which is a great alternative.

License

GenStore is licensed under the MIT license. See LICENSE for more info.

You might also like...
an Apple Watch® BINGO number generator app with histogram and history.

B4-BINGO-Number-Generator an Apple Watch® BINGO number generator app with histogram and history. This is a basic app using the Apple Watch which displ

SMAP: Swiss Topo Map URL Generator

smap - Swiss Topo Map URL Generator Usage: smap [-b] image-file-path Reads fil

LibAuthentication will simplify your code when if you want to use FaceID/TouchID in your tweaks.

LibAuthentication will simplify your code when if you want to use FaceID/TouchID in your tweaks.

⏲ A tiny package to measure code execution time. Only 20 lines of code.

Measure ⏲ A tiny package to measure code execution time. Measure.start("create-user") let user = User() Measure.finish("create-user") Console // ⏲ Mea

Useful Swift code samples, extensions, functionalities and scripts to cherry-pick and use in your projects

SwiftyPick 🦅 🍒 Useful Swift code samples, extensions, functionalities and scripts to cherry-pick and use in your projects. Purpose The idea behind t

Async+ for Swift provides a simple chainable interface for your async and throwing code, similar to promises and futures
Async+ for Swift provides a simple chainable interface for your async and throwing code, similar to promises and futures

Async+ for Swift provides a simple chainable interface for your async and throwing code, similar to promises and futures. Have the best of both worlds

Simple and Lightweight App Version Tracking for iOS written in Swift

AEAppVersion Simple and lightweight iOS App Version Tracking written in Swift I made this for personal use, but feel free to use it or contribute. For

A lightweight extension to Swift's CollectionDifference, supporting moves in addition to removals and insertions, critical when updating interfaces and managing reference types.

DifferenceTracker is a lightweight extension to Swift's CollectionDifference. It defines moves in addition to removals and insertions, critical when updating interfaces and managing reference types.

Butterfly is a lightweight library for integrating bug-report and feedback features with shake-motion event.
Butterfly is a lightweight library for integrating bug-report and feedback features with shake-motion event.

Butterfly is a lightweight library for integrating bug-report and feedback features with shake-motion event. Goals of this project One of th

Owner
null
Strong typed, autocompleted resources like images, fonts and segues in Swift projects

R.swift Get strong typed, autocompleted resources like images, fonts and segues in Swift projects Why use this? It makes your code that uses resources

Mathijs Kadijk 8.9k Jan 4, 2023
Enable autocomplete use resources in swift project.

ResourceKit Enable autocomplete use resources in swift project. まだハードコードで消耗してるの? ResourceKitで安全コーディング! How does ResourceKit work? ResouceKit makes you

bannzai 90 Nov 25, 2022
qr code generator tool

qr code generator tool Small command line tool for generate and reconition qr codes written in Swift Using Usage: ./qrgen [options] -m, --mode:

Igor 3 Jul 15, 2022
A tiny generator of random data for swift

SwiftRandom SwiftRandom is a tiny help suite for generating random data such as Random human stuff like: names, gender, titles, tags, conversations Ra

Kan Yilmaz 559 Dec 29, 2022
A software bill of materials (SBoM) generator for Swift packages

Swift Package SBoM A software bill of materials (SBoM) generator for Swift packages. Run this command to print a JSON representation of a CycloneDX SB

Mattt 17 Dec 12, 2022
Swift Quote Generator App

Swift_QuoteGeneratorApp UIKit을 이용하 SWIFT APP 예제 <> </>

null 0 Dec 27, 2021
A documentation generator for Swift projects

swift-doc A package for generating documentation for Swift projects. Given a directory of Swift files, swift-doc generates HTML or CommonMark (Markdow

SwiftDoc 1.7k Dec 6, 2022
Forblaze - A Python Mac Steganography Payload Generator

Forblaze - A Python Mac Steganography Payload Generator Author: AsaurusRex Disclaimer DO NOT use this project for purposes other than legitimate red t

null 54 Sep 5, 2022
Simple and blunt static site generator

StaticSite This contains a bunch of helper functions to generate a static site in Swift.

objc.io 33 Dec 15, 2022
An offline random passcode generator.

Passcode-Generator An offline random passcode generator. Usage Generates random passcode. Install Files and source code could be found in releases. Pr

Vaida 0 Dec 10, 2021