sbconstants 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 11f60a7cc73a7fd632d10cae568b307ef2c313a5
4
- data.tar.gz: 555e5f8778c56c11a08673fc58be52e7286c6061
3
+ metadata.gz: 5f4d5cd1526a1e1c0e4a95aebfe1775614adb585
4
+ data.tar.gz: 3e769892b40a72ca3e5f2818f904cc97346bffbb
5
5
  SHA512:
6
- metadata.gz: e872b75a0d28fca183a4adf3fea257120e79204bbd4e0cbb88768a99cc13a71ac4b4a11fdb4cf619b52fd4175f879499a8852afb182538bb68a96e1d6b9fadb1
7
- data.tar.gz: 109e3329c802dc0f93d1a6b2431be3635cda6b47fddce3fd559d790a39cd3cbb20e03cb43b4ed87289960b3f83ba8e50a5258cb9a1ee2915940eaf95737eb652
6
+ metadata.gz: f5e2179155417c7fdcb0d77d4aa6c4f6a0ce92362a4e1fcf2c8d210d628d8afb66e7687953570805c9d3831cffcabd6d997ed036829aec983c631c16cae9bf0e
7
+ data.tar.gz: 15b565db1bfc24f19eee9bd13140f9bf35a347911c501a2b063fd1cce22ac56a2f9e8daf1da934bd4d6259344d216f9e32cb0646cabee2ae5071e5ef59bf0326
data/Gemfile CHANGED
@@ -2,3 +2,4 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in sbconstants.gemspec
4
4
  gemspec
5
+ gem 'rake'
data/README.md CHANGED
@@ -25,13 +25,27 @@ For manual use (using swift):
25
25
  ```sh
26
26
  $ sbconstants -h
27
27
  Usage: DESTINATION_FILE [options]
28
+ -d, --dry-run Output to STDOUT
28
29
  -p, --prefix=<prefix> Only match identifiers with <prefix>
29
30
  -s, --source-dir=<source> Directory containing storyboards
30
- -sw, --swift Use the swift language
31
+ -t, --templates-dir=<templates> Directory containing the templates to use for code formatting
31
32
  -q, --queries=<queries> YAML file containing queries
32
- -d, --dry-run Output to STDOUT
33
33
  -v, --verbose Verbose output
34
+ -w, --swift Output to a Swift File
35
+ ```
36
+
37
+ ## Custom formatting
38
+
39
+ 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.
40
+
41
+ Inside your custom templates you can interpolate the `constant_name` and `constant_value` like this
42
+
34
43
  ```
44
+ NSString * const <%= constant_name %> = @"<%= constant_value %>";
45
+
46
+ ```
47
+
48
+ 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.
35
49
 
36
50
  ## Contributing
37
51
 
@@ -15,25 +15,55 @@ module SBConstants
15
15
 
16
16
  def run
17
17
  parse_storyboards
18
+ refute_key_collisions
18
19
  write
19
20
  end
20
-
21
+
21
22
  def sections
22
23
  @sections ||= begin
23
- sections_map = Hash.new { |h,k| h[k] = Set.new }
24
+ [].tap { |sections|
25
+ sections_grouped_by_keypath.each do |locations, constants|
26
+ sections << Section.new(locations.to_a, constants.to_a.sort)
27
+ end
28
+ }.sort_by { |section| section.locations.map(&:key_path).join(',') }
29
+ end
30
+ end
31
+
32
+ def sections_grouped_by_keypath
33
+ Hash.new { |h,k| h[k] = Set.new }.tap { |sections_grouped_by_keypath|
24
34
  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)
35
+ sections_grouped_by_keypath[locations] << constant
30
36
  end
31
- @sections = @sections.sort_by { |section| section.locations.map(&:key_path).join(',') }
32
- end
37
+ }
38
+ end
39
+
40
+ def sanitise_key key
41
+ key.gsub(" ", "").gsub("-", "")
33
42
  end
34
43
 
35
44
  private
45
+
46
+ def refute_key_collisions
47
+ keys_requiring_sanitisation, keys = constants.keys.partition { |constant| constant.include?(' ') }
48
+
49
+ keys_requiring_sanitisation.each do |key|
50
+ next unless keys.include?(sanitise_key(key))
51
+
52
+ $stderr.puts <<-EOD
53
+ Error: Key collision
54
+
55
+ The constant "#{key}" will have whitespace and hyphens stripped resulting in a constant named "#{sanitise_key(key)}".
56
+ This creates a problem as the constant "#{sanitise_key(key)}" will want to map to the storyboard constants "#{key}" and "#{sanitise_key(key)}".
36
57
 
58
+ To resolve the issue remove the ambiguity in naming - search your storyboards for the key "#{key}"
59
+ EOD
60
+ exit 1
61
+ end
62
+ end
63
+
64
+ # Parse all found storyboards and build a dictionary of constant => locations
65
+ #
66
+ # A constant key can potentially exist in many files so locations is a collection
37
67
  def parse_storyboards
38
68
  Dir["#{options.source_dir}/**/*.storyboard"].each do |storyboard|
39
69
  File.readlines(storyboard).each_with_index do |line, index|
@@ -61,7 +91,7 @@ module SBConstants
61
91
  int_out = File.open("#{options.output_path}.h", 'w') unless dry_run
62
92
  imp_out = File.open("#{options.output_path}.m", 'w') unless dry_run
63
93
 
64
- ObjcConstantWriter.new(self, int_out, imp_out).write
94
+ ObjcConstantWriter.new(self, int_out, imp_out, options.templates_dir).write
65
95
 
66
96
  int_out.close unless dry_run
67
97
  imp_out.close unless dry_run
@@ -3,10 +3,13 @@ require 'erb'
3
3
 
4
4
  module SBConstants
5
5
  class ObjcConstantWriter < SimpleDelegator
6
- def initialize data_source, int_out, imp_out
7
- super data_source
8
- @int_out = int_out
9
- @imp_out = imp_out
6
+ attr_reader :templates_dir
7
+
8
+ def initialize data_source, int_out, imp_out, templates_dir
9
+ super(data_source)
10
+ @int_out = int_out
11
+ @imp_out = imp_out
12
+ @templates_dir = templates_dir
10
13
  end
11
14
 
12
15
  def write
@@ -14,25 +17,36 @@ module SBConstants
14
17
  @imp_out.puts implementation
15
18
  end
16
19
 
17
- def templates_dir
18
- @templates_dir ||= File.dirname(__FILE__) + '/templates/'
20
+ def default_templates_dir
21
+ @default_templates_dir ||= File.dirname(__FILE__) + '/templates'
19
22
  end
20
23
 
21
24
  def header
22
- template_with_file "\n", %Q{extern NSString * const <%= constant %>;\n}
25
+ template_with_file "\n", File.open(template_file_path("objc_header.erb")).read
23
26
  end
24
27
 
25
28
  def implementation
26
29
  head = %Q{#import "<%= File.basename(options.output_path) %>.h"\n\n}
27
- body = %Q{NSString * const <%= constant %> = @"<%= constant %>";\n}
28
- template_with_file head, body
30
+ template_with_file head, File.open(template_file_path("objc_implementation.erb")).read
29
31
  end
30
32
 
31
33
  def template_with_file head, body
32
34
  @head = head
33
35
  @body = body
34
- pre_processed_template = ERB.new(File.open("#{templates_dir}objc_body.erb").read, nil, '<>').result(binding)
36
+ pre_processed_template = ERB.new(File.open("#{default_templates_dir}/objc_body.erb").read, nil, '<>').result(binding)
35
37
  ERB.new(pre_processed_template, nil, '<>').result(binding)
36
38
  end
39
+
40
+ def present_constants(section)
41
+ section.constants.map { |constant| [ sanitise_key(constant), constant ] }
42
+ end
43
+
44
+ def template_file_path basename
45
+ if templates_dir && File.exist?("#{templates_dir}/#{basename}")
46
+ "#{templates_dir}/#{basename}"
47
+ else
48
+ "#{default_templates_dir}/#{basename}"
49
+ end
50
+ end
37
51
  end
38
52
  end
@@ -3,13 +3,28 @@ 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, :use_swift
6
+ attr_accessor(
7
+ :destination,
8
+ :dry_run,
9
+ :output_path,
10
+ :prefix,
11
+ :query_config,
12
+ :query_file,
13
+ :source_dir,
14
+ :templates_dir,
15
+ :use_swift,
16
+ :verbose,
17
+ )
7
18
 
8
19
  def self.parse argv
9
20
  options = self.new
10
21
  OptionParser.new do |opts|
11
22
  opts.banner = "Usage: DESTINATION_FILE [options]"
12
23
 
24
+ opts.on('-d', '--dry-run', 'Output to STDOUT') do |dry_run|
25
+ options.dry_run = dry_run
26
+ end
27
+
13
28
  opts.on('-p', '--prefix=<prefix>', 'Only match identifiers with <prefix>') do |prefix|
14
29
  options.prefix = prefix
15
30
  end
@@ -17,22 +32,22 @@ module SBConstants
17
32
  opts.on('-s', '--source-dir=<source>', 'Directory containing storyboards') do |source_dir|
18
33
  options.source_dir = source_dir
19
34
  end
20
-
21
- opts.on('-w', '--swift', 'Output to a Swift File') do |use_swift|
22
- options.use_swift = use_swift
35
+
36
+ opts.on('-t', '--templates-dir=<templates>', 'Directory containing the templates to use for code formatting') do |templates_dir|
37
+ options.templates_dir = templates_dir
23
38
  end
24
39
 
25
40
  opts.on('-q', '--queries=<queries>', 'YAML file containing queries') do |queries|
26
41
  options.query_file = queries
27
42
  end
28
43
 
29
- opts.on('-d', '--dry-run', 'Output to STDOUT') do |dry_run|
30
- options.dry_run = dry_run
31
- end
32
-
33
44
  opts.on('-v', '--verbose', 'Verbose output') do |verbose|
34
45
  options.verbose = verbose
35
46
  end
47
+
48
+ opts.on('-w', '--swift', 'Output to a Swift File') do |use_swift|
49
+ options.use_swift = use_swift
50
+ end
36
51
  end.parse!(argv)
37
52
 
38
53
  if argv.first.nil?
@@ -10,7 +10,7 @@ module SBConstants
10
10
 
11
11
  def write
12
12
  head = %Q{\nimport Foundation"\n}
13
- body = %Q{ case <%= constant.gsub(" ", "").gsub("-", "") %> = "<%= constant %>"\n}
13
+ body = %Q{ case <%= sanitise_key(constant) %> = "<%= constant %>"\n}
14
14
  @swift_out.puts template_with_file head, body
15
15
  end
16
16
 
@@ -3,7 +3,7 @@
3
3
  <%= @head %>
4
4
  <%% sections.each do |section| %>
5
5
  #pragma mark - <%%= section.locations.map(&:key_path).join(', ') %>
6
- <%% section.constants.each do |constant| %>
6
+ <%% present_constants(section).each do |constant_name, constant_value| %>
7
7
  <%% if options.verbose %>
8
8
  //
9
9
  <%% constants[constant].each do |location| %>
@@ -0,0 +1 @@
1
+ extern NSString * const <%= constant_name %>;
@@ -0,0 +1 @@
1
+ NSString * const <%= constant_name %> = @"<%= constant_value %>";
@@ -1,3 +1,3 @@
1
1
  module SBConstants
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
data/sbconstants.gemspec CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |gem|
8
8
  gem.version = SBConstants::VERSION
9
9
  gem.authors = ["Paul Samuels", "Orta Therox"]
10
10
  gem.email = ["paulio1987@gmail.com", "orta.therox@gmail.com"]
11
- gem.description = %q{Generate constants from storyboards in objc and swift.}
11
+ gem.description = %q{Generate constants from storyboards in ObjC and Swift.}
12
12
  gem.summary = %q{Generate constants from storyboards in Objective-C and Swift.}
13
13
  gem.homepage = "https://github.com/paulsamuels/SBConstants"
14
14
  gem.license = "MIT"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sbconstants
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Samuels
@@ -9,9 +9,9 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-09-24 00:00:00.000000000 Z
12
+ date: 2014-12-11 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: Generate constants from storyboards in objc and swift.
14
+ description: Generate constants from storyboards in ObjC and Swift.
15
15
  email:
16
16
  - paulio1987@gmail.com
17
17
  - orta.therox@gmail.com
@@ -20,7 +20,7 @@ executables:
20
20
  extensions: []
21
21
  extra_rdoc_files: []
22
22
  files:
23
- - .gitignore
23
+ - ".gitignore"
24
24
  - Gemfile
25
25
  - LICENSE.txt
26
26
  - README.md
@@ -36,6 +36,8 @@ files:
36
36
  - lib/sbconstants/section.rb
37
37
  - lib/sbconstants/swift_constant_writer.rb
38
38
  - lib/sbconstants/templates/objc_body.erb
39
+ - lib/sbconstants/templates/objc_header.erb
40
+ - lib/sbconstants/templates/objc_implementation.erb
39
41
  - lib/sbconstants/templates/swift_body.erb
40
42
  - lib/sbconstants/version.rb
41
43
  - sbconstants.gemspec
@@ -49,19 +51,18 @@ require_paths:
49
51
  - lib
50
52
  required_ruby_version: !ruby/object:Gem::Requirement
51
53
  requirements:
52
- - - '>='
54
+ - - ">="
53
55
  - !ruby/object:Gem::Version
54
56
  version: '0'
55
57
  required_rubygems_version: !ruby/object:Gem::Requirement
56
58
  requirements:
57
- - - '>='
59
+ - - ">="
58
60
  - !ruby/object:Gem::Version
59
61
  version: '0'
60
62
  requirements: []
61
63
  rubyforge_project:
62
- rubygems_version: 2.0.6
64
+ rubygems_version: 2.2.2
63
65
  signing_key:
64
66
  specification_version: 4
65
67
  summary: Generate constants from storyboards in Objective-C and Swift.
66
68
  test_files: []
67
- has_rdoc: