sbconstants 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sbconstants.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Paul Samuels
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # Sbconstants
2
+
3
+ Generate a constants file by grabbing identifiers from storybaords in a project
4
+
5
+ ## Installation
6
+
7
+ $ gem install sbconstants
8
+
9
+ ## Usage
10
+
11
+ For automated use:
12
+
13
+ 1. Add a file to hold your constants e.g. `PASStoryboardConstants.(h|m)`
14
+ 2. Add a build script to build phases
15
+ sbconstants path_to_constant_file
16
+ 3. Enjoy your identifiers being added as constants to your constants file
17
+
18
+ ## Contributing
19
+
20
+ 1. Fork it
21
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
22
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
23
+ 4. Push to the branch (`git push origin my-new-feature`)
24
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/sbconstants ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'sbconstants'
4
+
5
+ SBConstants::CLI.run(ARGV)
@@ -0,0 +1,67 @@
1
+ require 'set'
2
+
3
+ module SBConstants
4
+ class CLI
5
+ attr_accessor :options, :constants, :sections
6
+
7
+ def self.run argv
8
+ new(Options.parse(argv)).run
9
+ end
10
+
11
+ def initialize options
12
+ self.options = options
13
+ self.constants = Hash.new { |h,k| h[k] = Set.new }
14
+ end
15
+
16
+ def run
17
+ parse_storyboards
18
+ write
19
+ end
20
+
21
+ def sections
22
+ @sections ||= begin
23
+ sections_map = Hash.new { |h,k| h[k] = Set.new }
24
+ constants.each do |constant, locations|
25
+ sections_map[locations] << constant
26
+ end
27
+ @sections = []
28
+ sections_map.each do |k,v|
29
+ @sections << Section.new(k.to_a, v.to_a.sort)
30
+ end
31
+ @sections = @sections.sort_by { |section| section.locations.map(&:key_path).join(',') }
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def parse_storyboards
38
+ Dir["#{options.source_dir}/**/*.storyboard"].each do |storyboard|
39
+ File.readlines(storyboard).each_with_index do |line, index|
40
+ options.queries.each do |query|
41
+ next unless value = line[query.regex, 1]
42
+ next if value.strip.empty?
43
+ next unless value.start_with?(options.prefix) if options.prefix
44
+
45
+ constants[value] << Location.new(query.node, query.attribute, line.strip, File.basename(storyboard, '.storyboard'), index + 1)
46
+ end
47
+ end
48
+ end
49
+ end
50
+
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
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,36 @@
1
+ require 'delegate'
2
+ require 'erb'
3
+
4
+ module SBConstants
5
+ class ConstantWriter < SimpleDelegator
6
+ def initialize data_source, int_out, imp_out
7
+ super data_source
8
+ @int_out = int_out
9
+ @imp_out = imp_out
10
+ end
11
+
12
+ def write
13
+ @int_out.puts header
14
+ @imp_out.puts implementation
15
+ end
16
+
17
+ def templates_dir
18
+ @templates_dir ||= File.dirname(__FILE__) + '/templates/'
19
+ end
20
+
21
+ def header
22
+ template_with_file "\n", %Q{extern NSString * const <%= constant %>;\n}
23
+ end
24
+
25
+ def implementation
26
+ head = %Q{\n#import "<%= File.basename(options.output_path) %>.h"\n\n}
27
+ body = %Q{NSString * const <%= constant %> = @"<%= constant %>";\n}
28
+ template_with_file head, body
29
+ end
30
+
31
+ def template_with_file head, body
32
+ pre_processed_template = ERB.new(File.open("#{templates_dir}body.erb").read, nil, '<>').result(binding)
33
+ ERB.new(pre_processed_template, nil, '<>').result(binding)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,10 @@
1
+ ---
2
+ segue: identifier
3
+ tableViewCell: reuseIdentifier
4
+ view: restorationIdentifier
5
+ ? - navigationController
6
+ - viewController
7
+ - tableViewController
8
+ - collectionViewController
9
+ : - storyboardIdentifier
10
+ - restorationIdentifier
@@ -0,0 +1,19 @@
1
+ module SBConstants
2
+ Location = Struct.new(:node, :attribute, :context, :file, :line) do
3
+ def key_path
4
+ @key_path ||= "#{node}.#{attribute}"
5
+ end
6
+
7
+ def debug
8
+ @debug ||= "#{file}[line:#{line}](#{key_path})"
9
+ end
10
+
11
+ def eql? other
12
+ self.class == other.class && key_path == other.key_path
13
+ end
14
+
15
+ def hash
16
+ key_path.hash
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,67 @@
1
+ require 'optparse'
2
+ require 'yaml'
3
+
4
+ module SBConstants
5
+ class Options
6
+ attr_accessor :dry_run, :prefix, :destination, :query_config, :output_path, :source_dir, :verbose, :query_file
7
+
8
+ def self.parse argv
9
+ options = self.new
10
+ OptionParser.new do |opts|
11
+ opts.banner = "Usage: DESTINATION_FILE [options]"
12
+
13
+ opts.on('-p', '--prefix=<prefix>', 'Only match identifiers with <prefix>') do |prefix|
14
+ options.prefix = prefix
15
+ end
16
+
17
+ opts.on('-s', '--source-dir=<source>', 'Directory containing storyboards') do |source_dir|
18
+ options.source_dir = source_dir
19
+ end
20
+
21
+ opts.on('-q', '--queries=<queries>', 'YAML file containing queries') do |queries|
22
+ options.query_file = queries
23
+ end
24
+
25
+ opts.on('-d', '--dry-run', 'Output to STDOUT') do |dry_run|
26
+ options.dry_run = dry_run
27
+ end
28
+
29
+ opts.on('-v', '--verbose', 'Verbose output') do |verbose|
30
+ options.verbose = verbose
31
+ end
32
+ end.parse!(argv)
33
+
34
+ if argv.first.nil?
35
+ options.dry_run = true
36
+ options.output_path = 'no file given'
37
+ else
38
+ options.output_path = argv.first.chomp(File.extname(ARGV.first))
39
+ end
40
+ options
41
+ end
42
+
43
+ def initialize
44
+ self.query_file = File.expand_path('../../sbconstants/identifiers.yml', __FILE__)
45
+ self.source_dir = Dir.pwd
46
+ end
47
+
48
+ def queries
49
+ load_queries if @queries.nil?
50
+ @queries
51
+ end
52
+
53
+ private
54
+
55
+ def load_queries
56
+ @queries = []
57
+ config = YAML.load(File.open(query_file).read)
58
+ config.each do |nodes, identifiers|
59
+ Array(nodes).each do |node|
60
+ Array(identifiers).each do |identifier|
61
+ @queries << Query.new(node, identifier)
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,7 @@
1
+ module SBConstants
2
+ Query = Struct.new(:node, :attribute) do
3
+ def regex
4
+ @regex ||= %r!<#{node}\s.*#{attribute}=\"(.*?)\"!
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module SBConstants
2
+ Section = Struct.new(:locations, :constants)
3
+ end
@@ -0,0 +1,17 @@
1
+ // Auto generated file - any changes will be lost
2
+ <%= head %>
3
+ <%% sections.each do |section| %>
4
+ #pragma mark - <%%= section.locations.map(&:key_path).join(', ') %>
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 %>
@@ -0,0 +1,3 @@
1
+ module SBConstants
2
+ VERSION = "0.0.4"
3
+ end
@@ -0,0 +1,10 @@
1
+ module SBConstants
2
+ end
3
+
4
+ require 'sbconstants/cli'
5
+ require 'sbconstants/constant_writer'
6
+ require 'sbconstants/location'
7
+ require 'sbconstants/query'
8
+ require 'sbconstants/options'
9
+ require 'sbconstants/section'
10
+ require "sbconstants/version"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sbconstants/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "sbconstants"
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}
13
+ gem.homepage = "https://github.com/paulsamuels/SBConstants"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sbconstants
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Paul Samuels
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-05 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Generate constants from storyboards
15
+ email:
16
+ - paulio1987@gmail.com
17
+ executables:
18
+ - sbconstants
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - bin/sbconstants
28
+ - lib/sbconstants.rb
29
+ - lib/sbconstants/cli.rb
30
+ - lib/sbconstants/constant_writer.rb
31
+ - lib/sbconstants/identifiers.yml
32
+ - lib/sbconstants/location.rb
33
+ - lib/sbconstants/options.rb
34
+ - lib/sbconstants/query.rb
35
+ - lib/sbconstants/section.rb
36
+ - lib/sbconstants/templates/body.erb
37
+ - lib/sbconstants/version.rb
38
+ - sbconstants.gemspec
39
+ homepage: https://github.com/paulsamuels/SBConstants
40
+ licenses: []
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 1.8.24
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Generate constants from storyboards
63
+ test_files: []
64
+ has_rdoc: