Giter Site home page Giter Site logo

swiftrsrc's Introduction

swiftrsrc

Resource code generation tool for Swift

swiftrsrc generates Swift code for accessing elements of asset catalogs, storyboards, and color lists in order to avoid the error-prone practice of hardcoding strings into your code. It is heavily inspired by Square's objc-codegenutils, which you should definitely look into if you're working on an Objective-C project.

Installing

The simplest way to install swiftrsrc is to download the latest binary from the Releases page.

Usage

swiftrsrc generate [--platform osx|ios] input_path output_path

[--platform ios|osx]
platform to generate code for. Must be either "ios" or "osx"

input_path
input path to generate code from. Must be an *.xcassets, *.storyboard, or *.clr path

output_path
output path to write the generated code to. If a directory path is specified, the generated code will be placed in a Swift source code file with the same name as the struct

Asset Catalogs

The generated code for asset catalogs only includes image sets, and purposely omits app icons and launch images (as these are not typically referred to programatically). If you put image sets inside folders, a corresponding nested struct will be created for the folder. In the example below, Posts and Main are folders inside Images.xcassets:

struct ImagesCatalog {
	struct Posts {
		static var Star: UIImage { return UIImage(named: "Star")! }
	}
	static var LaunchIcon: UIImage { return UIImage(named: "LaunchIcon")! }
	struct Main {
		static var SearchTabIcon: UIImage { return UIImage(named: "SearchTabIcon")! }
		static var ProfileTabIcon: UIImage { return UIImage(named: "ProfileTabIcon")! }
	}
}

Note that the properties are computed rather than assigned directly in order to avoid the images beind cached for the entire lifecycle of the application.

Storyboards

The generated code for storyboards contains constants for storyboard identifiers, reuse identifiers, and segue identifiers:

struct MainStoryboard {
	struct StoryboardIdentifiers {
		static let MainViewController = "MainViewController"
	}
	struct ReuseIdentifiers {
		static let PostTableViewCell = "PostTableViewCell"
		static let CommentTableViewCell = "CommentTableViewCell"
	}
	struct SegueIdentifiers {
		static let MainToDetail = "MainToDetail"
	}
}

Color Lists

Color lists can be created and edited visually by using the OS X color picker or programmatically using the NSColorList class. swiftrsrc automatically handles the task of converting colors to the appropriate color space depending on the platform that the code is being generated for.

struct AppLightColorList {
	static let Blue = UIColor(red: 0.045, green: 0.549, blue: 0.995, alpha: 1.000)
	static let Red = UIColor(red: 0.998, green: 0.261, blue: 0.321, alpha: 1.000)
	static let Orange = UIColor(red: 0.986, green: 0.525, blue: 0.060, alpha: 1.000)
}

Contact

License

swiftrsrc is licensed under the MIT License. See LICENSE for more information.

swiftrsrc's People

Contributors

ikesyo avatar indragiek avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.