paulsamuels / sbconstants Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
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?
Example
#pragma mark - navigationController.storyboardIdentifier
NSString * const Navigation Controller = @"Navigation Controller";
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
Hi there,
so generating the constants from the terminal works fine. However trying to run the same command from a run script added to the build phases gives me this ruby error which seems cryptic to me as I don't know anything about the language. Do you have an idea?
/Library/Ruby/Gems/2.0.0/gems/sbconstants-1.1.2/lib/sbconstants/cli.rb:79:in `[]': invalid byte sequence in US-ASCII (ArgumentError)
from /Library/Ruby/Gems/2.0.0/gems/sbconstants-1.1.2/lib/sbconstants/cli.rb:79:in `block (3 levels) in parse_storyboards'
from /Library/Ruby/Gems/2.0.0/gems/sbconstants-1.1.2/lib/sbconstants/cli.rb:78:in `each'
from /Library/Ruby/Gems/2.0.0/gems/sbconstants-1.1.2/lib/sbconstants/cli.rb:78:in `block (2 levels) in parse_storyboards'
from /Library/Ruby/Gems/2.0.0/gems/sbconstants-1.1.2/lib/sbconstants/cli.rb:77:in `each'
from /Library/Ruby/Gems/2.0.0/gems/sbconstants-1.1.2/lib/sbconstants/cli.rb:77:in `each_with_index'
from /Library/Ruby/Gems/2.0.0/gems/sbconstants-1.1.2/lib/sbconstants/cli.rb:77:in `block in parse_storyboards'
from /Library/Ruby/Gems/2.0.0/gems/sbconstants-1.1.2/lib/sbconstants/cli.rb:69:in `each'
from /Library/Ruby/Gems/2.0.0/gems/sbconstants-1.1.2/lib/sbconstants/cli.rb:69:in `each_with_index'
from /Library/Ruby/Gems/2.0.0/gems/sbconstants-1.1.2/lib/sbconstants/cli.rb:69:in `parse_storyboards'
from /Library/Ruby/Gems/2.0.0/gems/sbconstants-1.1.2/lib/sbconstants/cli.rb:18:in `run'
from /Library/Ruby/Gems/2.0.0/gems/sbconstants-1.1.2/lib/sbconstants/cli.rb:8:in `run'
from /Library/Ruby/Gems/2.0.0/gems/sbconstants-1.1.2/bin/sbconstants:5:in `<top (required)>'
from /usr/local/bin/sbconstants:23:in `load'
from /usr/local/bin/sbconstants:23:in `<main>'
Command /bin/sh failed with exit code 1
Generated constant file doesn't contain raw String Value identifiers.
example:
public enum TableViewCellReuseIdentifier : String {
case MessageCell
}
Hi, I would like to suggest a change from enums to structs
#swift_body.erb
// Auto generated file from SBConstants - any changes may be lost
<%% sections.each do |section| %>
public struct <%%= section.pretty_title %> {
<%% section.constants.each do |constant| %>
<%% if options.verbose %>
//
<%% constants[constant].each do |location| %>
// info: <%%= location.debug %>
// context: <%%= location.context %>
//
<%% end %>
<%% end %>
<%= @body %>
<%% end %>
}
<%% end %>
#swift_constant_writer.rb
def write
head = %Q{\nimport Foundation"\n}
body = %Q{ static let <%= sanitise_key(constant) %> = "<%= constant %>"\n}
@swift_out.puts template_with_file head, body
end
That way the resulting .swift file will be something like this, and will allow us to use them without calling .rawValue on each of them
public struct SegueIdentifiers {
static let AttachmentsScreen = "AttachmentsScreenSegue"
static let TagsScreen = "TagsScreenSegue"
static let TeamsScreen = "TeamsScreenSegue"
}
.../Source/Storyboard/StoryboardIdentifiers.m:3:9: In file included from
error: unknown type name 'NSString' extern NSString * const SomeCell;
There is no Foundation import for generated objc file. If I add id manual it works
File Content
// Auto generated file - any changes will be lost
#pragma mark - tableViewCell.reuseIdentifier
extern NSString * const SomeCell;
The default template is always used for objc_body
.
Hey, is it possible to use SBConstants to extract identifiers from .xib files?
It seems like it skips those, but I have lots of them for custom UITableViewCells and would love to use generated constants in my code.
What is the format of the YAML file containing queries?
Perhaps the default query should be moved to YAML for consistency and reference.
The code below is based on Reusable and uses the StoryboardNames
generated to init VCs
extension UIStoryboard {
/// Convenience Initializers
convenience init(storyboard: StoryboardNames, bundle: Bundle? = nil) {
self.init(name: storyboard.rawValue, bundle: bundle)
}
/// Class Functions
public static func storyboard(storyboard: StoryboardNames = .Main, bundle: Bundle? = nil) -> UIStoryboard {
return UIStoryboard(name: storyboard.rawValue, bundle: bundle)
}
/// View Controller Instantiation from Generics
func instantiateViewController<T: ViewControllerInitializer>(_: T.Type) -> T {
guard let viewController = self.instantiateViewController(withIdentifier: T.storyboardIdentifier) as? T else {
fatalError("Couldn't instantiate view controller with identifier \(T.storyboardIdentifier) ")
}
return viewController
}
}
Usage: let vc = UIStoryboard(storyboard: .Main)
extension UIViewController {
public static func instantiateFromStoryboard(name storyboard: StoryboardNames = .Main) -> Self {
func instantiateFromStoryboard<T: UIViewController>(_: T.Type) -> T {
let sb = UIStoryboard(storyboard: storyboard)
return sb.instantiateViewController(withIdentifier: String(describing: T.self)) as! T
}
return instantiateFromStoryboard(self)
}
}
Usage: let vc = SearchViewController.instantiateFromStoryboard(name: .Main)
I'm new to ruby or I'd make the PR myself, leaving the idea here if anyone does want to implement it.
I've noticed that when sbconstants
encounters a file with spaces in the name, it tokenises it to strip whitespace, like so:
New Document.storyboard
โ StoryboardNames.NewDocument
That's great, but it results in an unusable enum
entry. It would be great if this were handled automatically, i.e.:
public enum StoryboardNames: String {
case NewDocument = "New Document"
}
Hi there,
I'm using a .yml
as parameter to exclude everything else but the segue identifier.
The .yml
looks simply like this:
segue: identifier
Unfortunately I always end up having the storyboard names as constants in the header. Is there any way to exclude storyboard names from parsing?
Thanks for your effort! I love this tool so far.
The blog post linked to from the README is currently inaccessible. Maybe, once the site is back, we could copy common use cases from the post into the README?
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.
Hi, is it possible to have custom formatting for the different nodes as well?
Something like this for eg:
NSString * const PSBMasterToDetail = @"PSBMasterToDetail-Segue";
NSString * const PSBMasterToSettings = @"PSBMasterToSettings-Segue";
NSString * const Main = @"Main-Name";
NSString * const PSBAwesomeCell = @"PSBAwesomeCell-ReuseIdentifier";
also, thanks for the awesome project!
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
Already there.
Hi,
it will be really useful to have a small changelog by each version in https://github.com/paulsamuels/SBConstants/releases, what do you think?
The Objective-C templates will crash when -v
or --verbose
option is used:
(erb):9:in `block (2 levels) in method_missing': undefined local variable or method `constant' for #<SBConstants::ObjcConstantWriter:0x007fbdb3a6c200> (NameError)
from (erb):6:in `each'
from (erb):6:in `block in method_missing'
from (erb):4:in `each'
from (erb):4:in `method_missing'
from /usr/local/var/rbenv/versions/2.1.3/lib/ruby/2.1.0/erb.rb:850:in `eval'
from /usr/local/var/rbenv/versions/2.1.3/lib/ruby/2.1.0/erb.rb:850:in `result'
from /Users/X/.gem/gems/sbconstants-1.1.0/lib/sbconstants/objc_constant_writer.rb:37:in `template_with_file'
from /Users/X/.gem/gems/sbconstants-1.1.0/lib/sbconstants/objc_constant_writer.rb:25:in `header'
from /Users/X/.gem/gems/sbconstants-1.1.0/lib/sbconstants/objc_constant_writer.rb:16:in `write'
from /Users/X/.gem/gems/sbconstants-1.1.0/lib/sbconstants/cli.rb:94:in `write'
from /Users/X/.gem/gems/sbconstants-1.1.0/lib/sbconstants/cli.rb:19:in `run'
from /Users/X/.gem/gems/sbconstants-1.1.0/lib/sbconstants/cli.rb:8:in `run'
from /Users/X/.gem/gems/sbconstants-1.1.0/bin/sbconstants:5:in `<top (required)>'
from /Users/X/.gem/bin/sbconstants:23:in `load'
from /Users/X/.gem/bin/sbconstants:23:in `<main>'
SBConstants take a query cli parameter, but I can't seem to find any docs describing what it does.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.