Generate a constants file by grabbing identifiers from storyboards in a project.
Installation
$ gem install sbconstants
Usage
For automated use:
Add a file in Xcode to hold your constants e.g. PASStoryboardConstants.(h|m)
Add a build script to build phases e.g. sbconstants path_to_constant_file
Enjoy your identifiers being added as constants to your constants file
For manual use (using swift):
Add a file in Xcode to hold your constants e.g. StoryboardIdentifiers.swift
Add a command to your Makefile to run something similar to sbconstants path/to/StoryboardIdentifiers.swift --source-dir path/to/Storyboards --swift
Command line API
$ sbconstants -h
Usage: DESTINATION_FILE [options]
-d, --dry-run Output to STDOUT
-p, --prefix=<prefix> Only match identifiers with <prefix>
-i, --ignore=<files_to_ignore> Comma separated list of files to ignore
-s, --source-dir=<source> Directory containing storyboards
-t, --templates-dir=<templates> Directory containing the templates to use for code formatting
-q, --queries=<queries> YAML file containing queries
-v, --verbose Verbose output
-w, --swift Output to a Swift File
NB The argument is the destination file to dump the constants into - this needs to be added manually
Every time sbconstants is run it will parse the storyboard files and pull out any constants and then dump them into PASStoryboardConstants.(h|m). This of course means that PASStoryboardConstants.(h|m) should not be edited by hand as it will just be clobbered any time we build.
The output of running this tool will look something like this:
PASStoryboardConstants.h
// Auto generated file - any changes will be lost
#import<Foundation/Foundation.h>
#pragma mark - segue.identifier
externNSString * const PSBMasterToDetail;
externNSString * const PSBMasterToSettings;
#pragma mark - storyboardNames
externNSString * const Main;
#pragma mark - tableViewCell.reuseIdentifier
externNSString * const PSBAwesomeCell;
PASStoryboardConstants.m
// Auto generated file - any changes will be lost
#import"PASStoryboardConstants.h"
#pragma mark - segue.identifier
NSString * const PSBMasterToDetail = @"PSBMasterToDetail";
NSString * const PSBMasterToSettings = @"PSBMasterToSettings";
#pragma mark - storyboardNames
NSString * const Main = @"Main";
#pragma mark - tableViewCell.reuseIdentifier
NSString * const PSBAwesomeCell = @"PSBAwesomeCell";
Using the --swift flag this would produce
// Auto generated file from SBConstants - any changes may be lostpublicenumSegueIdentifier : String{
casePSBMasterToDetail="PSBMasterToDetail"casePSBMasterToSettings="PSBMasterToSettings"
}
publicenumStoryboardNames : String{
caseMain="Main"
}
publicenumTableViewCellreuseIdentifier : String{
casePSBAwesomeCell="PSBAwesomeCell"
}
The constants are grouped by where they were found in the storyboard xml e.g. segue.identifier. This can really help give you some context about where/what/when and why a constant exists.
##Options
Options are fun and there are a few to play with - most of these options are really only any good for debugging.
--prefix
-p, --prefix=Onlymatch identifiers with
Using the prefix option you can specify that you only want to grab identifiers that start with a certain prefix, which is always nice.
--ignore=
-i, --ignore=
Comma separated list of files to ignore
Using the ignore option, you can specify files which will not be added to the generated constants file. If you need to ignore several files, you need to separate it with commas (-i MainStoryboard,HelpStoryboard,CellXIB).
If you don't want to run the tool from the root of your app for some reason you can specify the source directory to start searching for storyboard files. The search is recursive using a glob something like
/**/*.storyboard
--dry-run
-d, --dry-run Output to STDOUT
If you just want to run the tool and not write the output to a file then this option will spit the result out to $stdout
--verbose
-v, --verbose Verbose output
Perhaps you want a little more context about where your identifiers are being grabbed from for debugging purposes. Never fear just use the --verbose switch and get output similar to:
Chances are I've missed some identifiers to search for in the storyboard. You don't want to wait for the gem to be updated or have to fork it and fix it. Using this option you can provide a YAML file that contains a description of what identifers to search for. The current one looks something like this (NB this is a great starting point for creating your own yaml):
This looks a little funky but it's essentially groups of keys and values (both the key and the value can be an array). This actually gets expanded to the following table:
-t, --templates-dir=
Directory containing the templates to useforcodeformatting
See below
Custom formatting
If you are running tools that verify your team is sticking to coding conventions you might find that the default output might not fit your requirements. Not to fear you can provide your own templates to decide the formatting you require by passing the --templates-dir option with the path to the directory containing the templates to use.
Inside your custom templates you can interpolate the constant_name and constant_value like this
You can override how the Objective-C constants are formatted by creating objc_header.erb and objc_implementation.erb files and adding the --templates-dir flag pointing to their containing directory.
Contributing
Fork it
Create your feature branch (git checkout -b my-new-feature)
Commit your changes (git commit -am 'Add some feature')
Push to the branch (git push origin my-new-feature)
I want to use this in artsy/eidolon but I want to keep it as swifty as possible.
So I've added another flag to add support for outputting as swift and updated the README. You had a lot of training whitespace, so my editor cleaned those up as I was digging around in the source.
When I tried to just run it from master I couldn't get the templates to run, so I made the head / body instance vars because I couldn't figure out how the data gets passed through. Good old ruby.
Looks like this:
// Auto generated file from SBConstants - any changes may be lostenumSegueIdentifier : String{
case bid ="bid"
}
enumViewControllerStoryboardIdentifier : String{
caseEnterYourBidDetails="Enter Your Bid Details"caseFulfillmentContainerViewController="FulfillmentContainerViewController"caseGetYourBidderDetails="Get Your Bidder Details"casePlaceYourBid="Place Your Bid"caseSwipeCreditCard="Swipe Credit Card"
}
random side-note, I also spent 2 years in the 3 years ago :)
I'm not sure if this will cause any issue. I don't think it should be behind a feature switch as it seems like something that should just be default behaviour.
I don't fully understand the template format yet, so I don't know if this only requires a template update.
Could we include constants for the storyboard name?
As used in + (UIStoryboard *)storyboardWithName:(NSString *)name bundle:(NSBundle *)storyboardBundleOrNil
I have tried everything to make this part of my build phases with no luck.
If I run this outside Xcode it works as expected; but running it like this will fail:
sbconstants destinationFile.h
This will fail with: sbconstants command not found.
If I run it like this:
`which sbconstants` destinationFile.h
The build phase won't fail but no data will be generated to destinationFile.h.
After reading your blog post you say it needs to be installed as "System Ruby" but since I have rvm in theory I don't have access to "System Ruby"; but running it with which should actually generate something (unless I'm missing something).
I tried different options with no luck; even adding -v won't produce any output to the build phase.
I am using some helper methods and swift extensions to work with the enums provided by SBConstants.
However, if e.g. there is no segue identifier setup in the storyboard, the generated SBConstants file omits the enum altogether.
This of course breaks any custom code that directly references the enums, e.g. SegueIdentifier.
I think it would be fine to output the empty enum in case there are no constants defined.
Any opinions?
This has been a pretty valuable tool for us; now not having to depend on manual addition of constants.
However the generated code didn't match our coding standards and with Objective-Clean build phase in place we had to manually fix the code after generating it.
I forked the gem and added an option that removes this space if the option is present; if not it will generate the code like you have it (keeping default behavior to avoid issues with people already using the gem).
New option is -r and will produce constants like this:
externNSString *const HSGuidedFlow; // On the .h
NSString *const HSGuidedFlow = @"HSGuidedFlow"; // On the .m
Everything else is kept the same; the only change is now the ability to specify wether or not to have a space after the *.
Don't know if this is valuable for anyone else but I figure if I was going to fork and add it I might as well make it available for you to judge if its worth merging.
I'm not sure if the existing collectionViewController in identifiers.yml is the version for iOS, but in macOS projects, collection views are stored in collectionViewItem XML nodes in the Storyboard.
I've added support for both collectionViewItems, tableColumn and tableCellView.
Currently sbconstants creates the enums in the format below
public enum SegueIdentifier : String
Most of the patterns and styleguides, and even Apple sample code recommend the format as below (no space between name and column)
public enum SegueIdentifier: String
Considering this is more of a stylistic and opinionated change, would it make sense to update swift_body.erb to match the common conventions? I can create a PR if so.
I know the templates directory parameter solves it for any tweaks users need, but I am guessing there would be a lot who prefer it without the extra space, because the rest of the lines do follow common convention - so this comment
Capacitor File Opener. The plugin is able to open a file given the mimeType and the file uri. This plugin is similar to cordova-plugin-file-opener2 without installation support.