sbconstants 0.0.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 11f60a7cc73a7fd632d10cae568b307ef2c313a5
4
+ data.tar.gz: 555e5f8778c56c11a08673fc58be52e7286c6061
5
+ SHA512:
6
+ metadata.gz: e872b75a0d28fca183a4adf3fea257120e79204bbd4e0cbb88768a99cc13a71ac4b4a11fdb4cf619b52fd4175f879499a8852afb182538bb68a96e1d6b9fadb1
7
+ data.tar.gz: 109e3329c802dc0f93d1a6b2431be3635cda6b47fddce3fd559d790a39cd3cbb20e03cb43b4ed87289960b3f83ba8e50a5258cb9a1ee2915940eaf95737eb652
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # Sbconstants
1
+ # sbconstants
2
2
 
3
- Generate a constants file by grabbing identifiers from storybaords in a project
3
+ Generate a constants file by grabbing identifiers from storyboards in a project. See [my blog post](http://paul-samuels.com/blog/2013/01/31/storyboard-constants/) for more use cases.
4
4
 
5
5
  ## Installation
6
6
 
@@ -10,11 +10,29 @@ Generate a constants file by grabbing identifiers from storybaords in a project
10
10
 
11
11
  For automated use:
12
12
 
13
- 1. Add a file to hold your constants e.g. `PASStoryboardConstants.(h|m)`
13
+ 1. Add a file in Xcode to hold your constants e.g. `PASStoryboardConstants.(h|m)`
14
14
  2. Add a build script to build phases
15
15
  sbconstants path_to_constant_file
16
16
  3. Enjoy your identifiers being added as constants to your constants file
17
17
 
18
+ For manual use (using swift):
19
+
20
+ 1. Add a file in Xcode to hold your constants e.g. `StoryboardIdentifiers.swift`
21
+ 2. Add a command to your [Makefile](https://github.com/artsy/eidolon/blob/15da1330a04615b3553779742f166b707c6ef65f/Makefile#L54) to run something similar to `sbconstants path/to/StoryboardIdentifiers.swift --source-dir path/to/Storyboards --swift`
22
+
23
+ ## Command line API
24
+
25
+ ```sh
26
+ $ sbconstants -h
27
+ Usage: DESTINATION_FILE [options]
28
+ -p, --prefix=<prefix> Only match identifiers with <prefix>
29
+ -s, --source-dir=<source> Directory containing storyboards
30
+ -sw, --swift Use the swift language
31
+ -q, --queries=<queries> YAML file containing queries
32
+ -d, --dry-run Output to STDOUT
33
+ -v, --verbose Verbose output
34
+ ```
35
+
18
36
  ## Contributing
19
37
 
20
38
  1. Fork it
@@ -2,7 +2,8 @@ module SBConstants
2
2
  end
3
3
 
4
4
  require 'sbconstants/cli'
5
- require 'sbconstants/constant_writer'
5
+ require 'sbconstants/objc_constant_writer'
6
+ require 'sbconstants/swift_constant_writer'
6
7
  require 'sbconstants/location'
7
8
  require 'sbconstants/query'
8
9
  require 'sbconstants/options'
@@ -3,21 +3,21 @@ require 'set'
3
3
  module SBConstants
4
4
  class CLI
5
5
  attr_accessor :options, :constants, :sections
6
-
6
+
7
7
  def self.run argv
8
8
  new(Options.parse(argv)).run
9
9
  end
10
-
10
+
11
11
  def initialize options
12
12
  self.options = options
13
13
  self.constants = Hash.new { |h,k| h[k] = Set.new }
14
14
  end
15
-
15
+
16
16
  def run
17
17
  parse_storyboards
18
18
  write
19
19
  end
20
-
20
+
21
21
  def sections
22
22
  @sections ||= begin
23
23
  sections_map = Hash.new { |h,k| h[k] = Set.new }
@@ -31,9 +31,9 @@ module SBConstants
31
31
  @sections = @sections.sort_by { |section| section.locations.map(&:key_path).join(',') }
32
32
  end
33
33
  end
34
-
34
+
35
35
  private
36
-
36
+
37
37
  def parse_storyboards
38
38
  Dir["#{options.source_dir}/**/*.storyboard"].each do |storyboard|
39
39
  File.readlines(storyboard).each_with_index do |line, index|
@@ -41,27 +41,31 @@ module SBConstants
41
41
  next unless value = line[query.regex, 1]
42
42
  next if value.strip.empty?
43
43
  next unless value.start_with?(options.prefix) if options.prefix
44
-
44
+
45
45
  constants[value] << Location.new(query.node, query.attribute, line.strip, File.basename(storyboard, '.storyboard'), index + 1)
46
46
  end
47
47
  end
48
48
  end
49
- end
50
-
49
+ end
50
+
51
51
  def write
52
- int_out, imp_out = $stdout, $stdout
53
-
54
- if !options.dry_run
55
- int_out = File.open("#{options.output_path}.h", 'w')
56
- imp_out = File.open("#{options.output_path}.m", 'w')
57
- end
58
-
59
- ConstantWriter.new(self, int_out, imp_out).write
60
-
61
- if !options.dry_run
62
- int_out.close
63
- imp_out.close
52
+ int_out, imp_out, swift_out = $stdout, $stdout, $stdout
53
+ dry_run = options.dry_run
54
+
55
+ if options.use_swift
56
+ swift_out = File.open("#{options.output_path}.swift", 'w') unless dry_run
57
+ SwiftConstantWriter.new(self, swift_out).write
58
+ swift_out.close
59
+ else
60
+
61
+ int_out = File.open("#{options.output_path}.h", 'w') unless dry_run
62
+ imp_out = File.open("#{options.output_path}.m", 'w') unless dry_run
63
+
64
+ ObjcConstantWriter.new(self, int_out, imp_out).write
65
+
66
+ int_out.close unless dry_run
67
+ imp_out.close unless dry_run
64
68
  end
65
69
  end
66
70
  end
67
- end
71
+ end
@@ -1,10 +1,12 @@
1
1
  ---
2
2
  segue: identifier
3
3
  tableViewCell: reuseIdentifier
4
+ collectionViewCell: reuseIdentifier
4
5
  view: restorationIdentifier
5
6
  ? - navigationController
6
7
  - viewController
7
8
  - tableViewController
8
9
  - collectionViewController
9
10
  : - storyboardIdentifier
10
- - restorationIdentifier
11
+ - restorationIdentifier
12
+
@@ -3,17 +3,17 @@ module SBConstants
3
3
  def key_path
4
4
  @key_path ||= "#{node}.#{attribute}"
5
5
  end
6
-
6
+
7
7
  def debug
8
8
  @debug ||= "#{file}[line:#{line}](#{key_path})"
9
9
  end
10
-
10
+
11
11
  def eql? other
12
12
  self.class == other.class && key_path == other.key_path
13
13
  end
14
-
14
+
15
15
  def hash
16
16
  key_path.hash
17
17
  end
18
18
  end
19
- end
19
+ end
@@ -2,35 +2,37 @@ require 'delegate'
2
2
  require 'erb'
3
3
 
4
4
  module SBConstants
5
- class ConstantWriter < SimpleDelegator
5
+ class ObjcConstantWriter < SimpleDelegator
6
6
  def initialize data_source, int_out, imp_out
7
7
  super data_source
8
8
  @int_out = int_out
9
9
  @imp_out = imp_out
10
10
  end
11
-
11
+
12
12
  def write
13
13
  @int_out.puts header
14
14
  @imp_out.puts implementation
15
15
  end
16
-
16
+
17
17
  def templates_dir
18
18
  @templates_dir ||= File.dirname(__FILE__) + '/templates/'
19
19
  end
20
-
20
+
21
21
  def header
22
22
  template_with_file "\n", %Q{extern NSString * const <%= constant %>;\n}
23
23
  end
24
-
24
+
25
25
  def implementation
26
- head = %Q{\n#import "<%= File.basename(options.output_path) %>.h"\n\n}
26
+ head = %Q{#import "<%= File.basename(options.output_path) %>.h"\n\n}
27
27
  body = %Q{NSString * const <%= constant %> = @"<%= constant %>";\n}
28
28
  template_with_file head, body
29
29
  end
30
-
30
+
31
31
  def template_with_file head, body
32
- pre_processed_template = ERB.new(File.open("#{templates_dir}body.erb").read, nil, '<>').result(binding)
32
+ @head = head
33
+ @body = body
34
+ pre_processed_template = ERB.new(File.open("#{templates_dir}objc_body.erb").read, nil, '<>').result(binding)
33
35
  ERB.new(pre_processed_template, nil, '<>').result(binding)
34
36
  end
35
37
  end
36
- end
38
+ end
@@ -3,55 +3,59 @@ require 'yaml'
3
3
 
4
4
  module SBConstants
5
5
  class Options
6
- attr_accessor :dry_run, :prefix, :destination, :query_config, :output_path, :source_dir, :verbose, :query_file
7
-
6
+ attr_accessor :dry_run, :prefix, :destination, :query_config, :output_path, :source_dir, :verbose, :query_file, :use_swift
7
+
8
8
  def self.parse argv
9
9
  options = self.new
10
10
  OptionParser.new do |opts|
11
11
  opts.banner = "Usage: DESTINATION_FILE [options]"
12
-
12
+
13
13
  opts.on('-p', '--prefix=<prefix>', 'Only match identifiers with <prefix>') do |prefix|
14
14
  options.prefix = prefix
15
15
  end
16
-
16
+
17
17
  opts.on('-s', '--source-dir=<source>', 'Directory containing storyboards') do |source_dir|
18
18
  options.source_dir = source_dir
19
19
  end
20
-
20
+
21
+ opts.on('-w', '--swift', 'Output to a Swift File') do |use_swift|
22
+ options.use_swift = use_swift
23
+ end
24
+
21
25
  opts.on('-q', '--queries=<queries>', 'YAML file containing queries') do |queries|
22
26
  options.query_file = queries
23
27
  end
24
-
28
+
25
29
  opts.on('-d', '--dry-run', 'Output to STDOUT') do |dry_run|
26
30
  options.dry_run = dry_run
27
31
  end
28
-
32
+
29
33
  opts.on('-v', '--verbose', 'Verbose output') do |verbose|
30
34
  options.verbose = verbose
31
35
  end
32
36
  end.parse!(argv)
33
-
37
+
34
38
  if argv.first.nil?
35
39
  options.dry_run = true
36
40
  options.output_path = 'no file given'
37
41
  else
38
- options.output_path = argv.first.chomp(File.extname(ARGV.first))
42
+ options.output_path = argv.first.chomp(File.extname(ARGV.first))
39
43
  end
40
44
  options
41
45
  end
42
-
46
+
43
47
  def initialize
44
48
  self.query_file = File.expand_path('../../sbconstants/identifiers.yml', __FILE__)
45
49
  self.source_dir = Dir.pwd
46
50
  end
47
-
51
+
48
52
  def queries
49
53
  load_queries if @queries.nil?
50
54
  @queries
51
55
  end
52
-
56
+
53
57
  private
54
-
58
+
55
59
  def load_queries
56
60
  @queries = []
57
61
  config = YAML.load(File.open(query_file).read)
@@ -64,4 +68,4 @@ module SBConstants
64
68
  end
65
69
  end
66
70
  end
67
- end
71
+ end
@@ -1,3 +1,16 @@
1
1
  module SBConstants
2
- Section = Struct.new(:locations, :constants)
3
- end
2
+ Section = Struct.new(:locations, :constants) do
3
+
4
+ def pretty_title
5
+ title = locations.map(&:key_path).join('')
6
+ .gsub(".", "")
7
+ .gsub(" ", "")
8
+ .gsub("identifier", "Identifier")
9
+ .gsub("viewcontroller", "ViewController")
10
+ .gsub("storyboard", "Storyboard")
11
+
12
+ title.slice(0,1).capitalize + title.slice(1..-1)
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,28 @@
1
+ require 'delegate'
2
+ require 'erb'
3
+
4
+ module SBConstants
5
+ class SwiftConstantWriter < SimpleDelegator
6
+ def initialize data_source, swift_out
7
+ super data_source
8
+ @swift_out = swift_out
9
+ end
10
+
11
+ def write
12
+ head = %Q{\nimport Foundation"\n}
13
+ body = %Q{ case <%= constant.gsub(" ", "").gsub("-", "") %> = "<%= constant %>"\n}
14
+ @swift_out.puts template_with_file head, body
15
+ end
16
+
17
+ def templates_dir
18
+ @templates_dir ||= File.dirname(__FILE__) + '/templates/'
19
+ end
20
+
21
+ def template_with_file head, body
22
+ @head = head
23
+ @body = body
24
+ pre_processed_template = ERB.new(File.open("#{templates_dir}swift_body.erb").read, nil, '<>').result(binding)
25
+ ERB.new(pre_processed_template, nil, '<>').result(binding)
26
+ end
27
+ end
28
+ end
@@ -1,5 +1,6 @@
1
1
  // Auto generated file - any changes will be lost
2
- <%= head %>
2
+
3
+ <%= @head %>
3
4
  <%% sections.each do |section| %>
4
5
  #pragma mark - <%%= section.locations.map(&:key_path).join(', ') %>
5
6
  <%% section.constants.each do |constant| %>
@@ -11,7 +12,7 @@
11
12
  //
12
13
  <%% end %>
13
14
  <%% end %>
14
- <%= body %>
15
+ <%= @body %>
15
16
  <%% end %>
16
17
 
17
- <%% end %>
18
+ <%% end %>
@@ -0,0 +1,17 @@
1
+ // Auto generated file from SBConstants - any changes may be lost
2
+ <%% sections.each do |section| %>
3
+
4
+ public enum <%%= section.pretty_title %> : String {
5
+ <%% section.constants.each do |constant| %>
6
+ <%% if options.verbose %>
7
+ //
8
+ <%% constants[constant].each do |location| %>
9
+ // info: <%%= location.debug %>
10
+ // context: <%%= location.context %>
11
+ //
12
+ <%% end %>
13
+ <%% end %>
14
+ <%= @body %>
15
+ <%% end %>
16
+ }
17
+ <%% end %>
@@ -1,3 +1,3 @@
1
1
  module SBConstants
2
- VERSION = "0.0.4"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -6,11 +6,12 @@ require 'sbconstants/version'
6
6
  Gem::Specification.new do |gem|
7
7
  gem.name = "sbconstants"
8
8
  gem.version = SBConstants::VERSION
9
- gem.authors = ["Paul Samuels"]
10
- gem.email = ["paulio1987@gmail.com"]
11
- gem.description = %q{Generate constants from storyboards}
12
- gem.summary = %q{Generate constants from storyboards}
9
+ gem.authors = ["Paul Samuels", "Orta Therox"]
10
+ gem.email = ["paulio1987@gmail.com", "orta.therox@gmail.com"]
11
+ gem.description = %q{Generate constants from storyboards in objc and swift.}
12
+ gem.summary = %q{Generate constants from storyboards in Objective-C and Swift.}
13
13
  gem.homepage = "https://github.com/paulsamuels/SBConstants"
14
+ gem.license = "MIT"
14
15
 
15
16
  gem.files = `git ls-files`.split($/)
16
17
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
metadata CHANGED
@@ -1,19 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sbconstants
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
5
- prerelease:
4
+ version: 1.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Paul Samuels
8
+ - Orta Therox
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-05 00:00:00.000000000 Z
12
+ date: 2014-09-24 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: Generate constants from storyboards
14
+ description: Generate constants from storyboards in objc and swift.
15
15
  email:
16
16
  - paulio1987@gmail.com
17
+ - orta.therox@gmail.com
17
18
  executables:
18
19
  - sbconstants
19
20
  extensions: []
@@ -27,38 +28,40 @@ files:
27
28
  - bin/sbconstants
28
29
  - lib/sbconstants.rb
29
30
  - lib/sbconstants/cli.rb
30
- - lib/sbconstants/constant_writer.rb
31
31
  - lib/sbconstants/identifiers.yml
32
32
  - lib/sbconstants/location.rb
33
+ - lib/sbconstants/objc_constant_writer.rb
33
34
  - lib/sbconstants/options.rb
34
35
  - lib/sbconstants/query.rb
35
36
  - lib/sbconstants/section.rb
36
- - lib/sbconstants/templates/body.erb
37
+ - lib/sbconstants/swift_constant_writer.rb
38
+ - lib/sbconstants/templates/objc_body.erb
39
+ - lib/sbconstants/templates/swift_body.erb
37
40
  - lib/sbconstants/version.rb
38
41
  - sbconstants.gemspec
39
42
  homepage: https://github.com/paulsamuels/SBConstants
40
- licenses: []
43
+ licenses:
44
+ - MIT
45
+ metadata: {}
41
46
  post_install_message:
42
47
  rdoc_options: []
43
48
  require_paths:
44
49
  - lib
45
50
  required_ruby_version: !ruby/object:Gem::Requirement
46
- none: false
47
51
  requirements:
48
- - - ! '>='
52
+ - - '>='
49
53
  - !ruby/object:Gem::Version
50
54
  version: '0'
51
55
  required_rubygems_version: !ruby/object:Gem::Requirement
52
- none: false
53
56
  requirements:
54
- - - ! '>='
57
+ - - '>='
55
58
  - !ruby/object:Gem::Version
56
59
  version: '0'
57
60
  requirements: []
58
61
  rubyforge_project:
59
- rubygems_version: 1.8.24
62
+ rubygems_version: 2.0.6
60
63
  signing_key:
61
- specification_version: 3
62
- summary: Generate constants from storyboards
64
+ specification_version: 4
65
+ summary: Generate constants from storyboards in Objective-C and Swift.
63
66
  test_files: []
64
67
  has_rdoc: