Skip to content

Resources boilerplate code generator for Swift

Notifications You must be signed in to change notification settings

CaptureContext/spmgen

 
 

Repository files navigation

spmgen

Code generator for Swift.

Installation

Makefile

# Download repo
git clone https://github.com/capturecontext/spmgen.git

# Navigate to repo directory
cd spmgen

# Build and install globally using Make
# See Makefile for more options
make install

# You can also delete spmgen using `make uninstall` command

Resources command

spmgen provides static resource factories for various resource types.

Supported resources:

Resource Extensions Is reliable
ColorResource .xcassets true
FontResource .ttf .otf true
ImageResource .xcassets true
NibResource .xib not used
StoryboardResource .storyboard not used
SCNSceneResource .scnassets/.scn true

Todos:

  • Localized strings support
  • Configuration file support
  • Excludes support
  • Filesystem expressions support
  • Resources validation

Integration

You can try SwiftPM Plugin but it's still experimental, so here is the recommended way

Add PackageResources dependency to your package

.package(
  name: "swift-package-resources",
  url: "https://github.com/capturecontext/swift-package-resources.git", 
  .upToNextMajor(from: "3.0.0")
)

Create Resources target with a following structure

Sources
  Resources
    Resources
      <#Assets#>

Specify resource processing and add PackageResources dependency to your target

.target(
  name: "MyTarget",
  dependencies: [
    .product(
      name: "PackageResources",
      package: "swift-package-resources"
    )
  ],
  resources: [
    <#Declare resources#>
  ]
)

Add a script to your Run Script target build phases

spmgen resources "$SRCROOT/Sources/MyTarget/Resources" \
  --output "$SRCROOT/Sources/Resources/Resources.generated.swift" \
  --indentor " " \
  --tab-size 2
  
# You can replace "$SRCROOT/Sources/MyTarget/Resources"
# with your own path to resources
# You can add other targets and just remember to add a script

Another integration example can be found here

Usage

Import your <#Project#>Resources package and initialize objects using .resource() static factory

import Resources
import UIKit

let label = UILabel()
label.backgroundColor = .resource(.accentColor)
label.textColor = .resource(.primaryText)
label.font = .primary(ofSize: 12, weight: .semibold, style: .italic)

let imageView = UIImageView(image: .resource(.logo))

Note: Fonts require additional setup

For example you want to add Monsterrat and Arimo fonts with different styles

  • Download fonts and add them to Sources/<#Project#>Resources/Resources folder

  • Add a static factories for your custom fonts (Example)

  • Register custom fonts on app launch (in AppDelegate, for example)

    • UIFont.bootstrap() if you are using code from the example above.

About

Resources boilerplate code generator for Swift

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 98.0%
  • Makefile 2.0%