Contents

CaptureContext/package-resources-cli

Code generator for swift-package-resources

Table of contents

Installation

  1. Add required dependencies to your package
.package(
  url: "https://github.com/capturecontext/package-resources-cli.git", 
  .upToNextMajor(from: "2.0.0")
),
.package(
  url: "https://github.com/capturecontext/swift-package-resources.git", 
  .upToNextMajor(from: "4.0.0")
),
  1. Add plugin and target dependency
.target(
  name: "AppUI",
  product: .library(.static),
  dependencies: [
    .product(
      name: "PackageResources",
      package: "swift-package-resources
    )
  ],
  resources: [...],
  plugins: [
    .plugin(
    	name: "package-resources-plugin",
      package: "package-resources-cli"
    ),
  ]
),

[!TIP]

You can skip adding swift-package-resources dependency explicitly if you use exported alias instead:

.product(
     name: "_ExportedPackageResources",
     package: "package-resources-cli
)

it will still work as import PackageResources

Makefile

Make allows you to build and install package-resources-cli globally or locally, if you don't want to use swiftpm plugin

# Download repo
git clone https://github.com/capturecontext/package-resources-cli.git

# Navigate to repo directory
cd package-resources-cli

# Build and install globally using `make install`
# or see Makefile for more options
make install

# You can also delete package-resources-cli using `make uninstall` command

Usage

Supported resource types:

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

[!WARNING]

Fonts require additional setup

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

  • Download fonts and add them to your 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.

✅ Except of some hectic with fonts, installation is enough for using the plugin with swift-package-resources.

Configuration

Package plugin uses .packageresources file at the root of the package with as it's configuration file

output: "<path-to-output-file>"
indentor: "\t"
indent-size: 1
numbers:
  separator: "_"
  allowed-delimeters: []
  next-token-mode: inherit
  single-letter-boundary-options:
  - disable-separators
  - disable-next-token-processing
acronyms:
  processing-policy: default
  values
  - id
  - ID
  - Id
  ...

| Argument | Description | | ---------------------------------------- | ------------------------------------------------------------ | | output | Path to output file. <br />Default is dynamically calculated as input + /Resources.generated.swift | | indentor | Indentation symbol. <br />Default is \t | | indent-size | Amount of indentors per indent level. <br />default is 1<br />default for space/whitespace/" " indentors is 2 | | numbers.separator | Separator for numeric values.<br />default is "" | | numbers.allowed-delimeters | Extends allowed characters for numbers, specifying ["."] might be helpful for enabling FloatingPoint numbers, however it's only allowed delimiter, so with this setting "1.2.3_value" will be tokenized as ["1.2.3", "", "value"]<br />default is ` | | numbers.next-token-mode | Camel case mode for a token after a number.<br />default is inherit (equivalent to automatic) | | numbers.single-letter-boundary-options | Options for numeric boundary with single letter tokens.<br />Both options are enabled by default | | acronyms.processing-policy | See CamelCaseConfig.Acronyms.ProcessingPolicy.<br />default is always-match-case. | | acronyms.values | Overrides all default acronyms.<br />default` can be found here. |

Command plugin

You can run commands manually using swift package resources

Generate command
swift package resources generate \
  --input "<path-to-lookup-root>" \
  --config "<path-to-configuration-file>" \
  --output "<path-to-output-file>" \
  --indentor "\t" \
  --indent-size 1 \
  --numbers-separator "_" \
  --numbers-allowed-delimeters "__package_resources_unspecified" \
  --numbers-next-token-mode inherit \
  --numbers-single-letter-boundary-options default \
  --acronyms-processing-policy default \
  --acronyms-values "acronym1" "acronym2"
Config init command

Dumps default configuration into a config file

swift package resources config init --format yaml # `json` is also supported
swift package resources config init --force # rewrite config file
Config edit command

Utility for editing config files, overrides specific values in config file

swift package resources config edit \
  --indentor "\s" \
  --indent-size 2

Also can remove specific keys from the config, just pass --remove- prefixed arguments as flags

swift package resources config edit --remove-acronyms-values

[!TIP]

Path to config file can be specified for config commands

swift package resources config \
     --path "<path-to-config-file>" \
     edit --remove-output

Todos

  • [ ] Improve docs

[!TIP]

You can find docc reference generated by swift-argument-parser here

  • [ ] Localized strings support
  • [ ] Excludes support
  • [ ] Filesystem expressions support
  • [ ] Resources validation

License

This library is released under the MIT license. See LICENSE for details.

Package Metadata

Repository: CaptureContext/package-resources-cli

Stars: 0

Forks: 0

Open issues: 0

Default branch: main

Primary language: swift

License: MIT

Topics: cli, codegen, plugin, resources, spm-plugin, swift, swiftpackagemanager

README: README.md