xfabricator 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4cb829b85a1c809946640551eea258db47b207d2
4
+ data.tar.gz: 95d19734a19e86962e8adf9c01b1f64886247fb2
5
+ SHA512:
6
+ metadata.gz: 6c65ba6fb729820365861262c81f2d8e8121ff9f86a18257d2257cb3934288d26a4b306bc442ca8331313b348ab7ca97f4739de1f476cb5d523425d1d55dedb6
7
+ data.tar.gz: b5f45876f1e1748fb73285c1f11b8438abcc8af8c63cbdbf64b502447b115ae8aed8e9ece1cb338ece1b5030cbf28e7b24514730342faddf8373d159be55cf06
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ workspace.xml
11
+ xfabricator-*.gem
12
+ nicholas.xml
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in xfabricator.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Nicholas Cross
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Xfabricator
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/xfabricator`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'xfabricator'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install xfabricator
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nicholascross/xfabricator.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fabricator"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/bin/xfabricate ADDED
@@ -0,0 +1,107 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'inquirer'
5
+ require 'xfabricator/fabricator'
6
+ require 'xfabricator/fabrication_config'
7
+ require 'xfabricator/template_definition'
8
+
9
+ module Xfabricator
10
+
11
+ class XFabricate
12
+
13
+ def XFabricate.locate_file(file_name, dir = Pathname.pwd)
14
+ config_file = dir + file_name
15
+
16
+ if dir.children.include?(config_file)
17
+ config_file.expand_path
18
+ else
19
+ return nil if dir.expand_path.root?
20
+ locate_file(".xfabricate", dir.parent)
21
+ end
22
+
23
+ end
24
+
25
+ def initialize(xfabricate_file)
26
+ @config = FabricationConfig.load_config xfabricate_file
27
+ end
28
+
29
+ def fabricate(template_name, target_name, output_path, group_name)
30
+ unless template_name
31
+ raise ArgumentError, 'name of template to fabricate not specified'
32
+ end
33
+
34
+ unless target_name
35
+ target_name = @config.default_target.name
36
+ end
37
+
38
+ fabricator = Fabricator.new @config, output_path
39
+
40
+ template_file = Pathname.new("#{@config.template_location}/#{template_name}.xftemplate").expand_path.to_s
41
+ template = TemplateDefinition.load_definition Pathname.new(template_file)
42
+
43
+ prompts = template.variable_prompts
44
+ variables = @config.variables ? @config.variables : {}
45
+
46
+ prompts.each do |variable, prompt|
47
+ value = Ask.input prompt
48
+ variables[variable] = value
49
+ end
50
+
51
+ fabricator.fabricate template, variables, target_name unless group_name
52
+
53
+ if group_name
54
+ group = find_group( group_name )
55
+ fabricator.fabricate_in_group template, variables, target_name, group
56
+ end
57
+
58
+ end
59
+
60
+ def find_group(group_name)
61
+ project = @config.project
62
+ _find_group(project.main_group, group_name)
63
+ end
64
+
65
+ def _find_group(current_group, group_name)
66
+ return current_group if current_group.path == group_name ||current_group.name == group_name
67
+
68
+ current_group.groups.each do |group|
69
+ group_found = _find_group(group,group_name)
70
+ return group_found if group_found
71
+ end
72
+
73
+ return nil
74
+ end
75
+
76
+ end
77
+
78
+ end
79
+
80
+ options = {}
81
+ options[:path] = Pathname.pwd
82
+
83
+ OptionParser.new do |opts|
84
+ opts.banner = "Usage: xfabricate [options]"
85
+
86
+ opts.on("-t template_name", "--template=template_name", "specify template to fabricate") do |template_name|
87
+ options[:template] = template_name
88
+ end
89
+
90
+ opts.on("-a target_name", "--target=target_name", "specify target to add created files") do |target_name|
91
+ options[:target] = target_name
92
+ end
93
+
94
+ opts.on("-g group_name", "--group=group_name", "specify group to add created files") do |group_name|
95
+ options[:group] = group_name
96
+ end
97
+
98
+ opts.on("-p path_name", "--path=path_name", "specify path to generate created files") do |path|
99
+ options[:path] = path
100
+ end
101
+
102
+ end.parse!
103
+
104
+ raise OptionParser::MissingArgument if options[:template].nil?
105
+
106
+ xfabricate = Xfabricator::XFabricate.new Xfabricator::XFabricate.locate_file(".xfabricate")
107
+ xfabricate.fabricate options[:template], options[:target], options[:path], options[:group]
@@ -0,0 +1,51 @@
1
+ require 'xcodeproj'
2
+
3
+ module Xfabricator
4
+
5
+ class FabricationConfig
6
+
7
+ attr_reader :template_location
8
+ attr_reader :project
9
+ attr_reader :variables
10
+ attr_reader :default_target
11
+
12
+ def FabricationConfig.load_config(config_file)
13
+ unless config_file
14
+ raise ArgumentError, 'config file not specified'
15
+ end
16
+
17
+ unless config_file.exist?
18
+ raise ArgumentError, "config file doesn't exist"
19
+ end
20
+
21
+ relative_path = config_file.parent.to_s
22
+ config = eval(File.read(config_file.to_s), binding)
23
+
24
+ unless config
25
+ raise RuntimeError, "config was not loaded"
26
+ end
27
+
28
+ unless config.is_a? FabricationConfig
29
+ raise RuntimeError, "Valid config not loaded"
30
+ end
31
+
32
+ return config
33
+ end
34
+
35
+ def initialize(project_file, template_location, variables, default_target_name)
36
+ project = Xcodeproj::Project.open(project_file)
37
+ @project = project
38
+ @variables = variables
39
+ @template_location = template_location
40
+ @default_target = project.targets.find { |target| target.name == default_target_name }
41
+ end
42
+
43
+ def FabricationConfig.create_config(config)
44
+ options = {}
45
+ config.call(options)
46
+ FabricationConfig.new options[:project_file], options[:template_location], options[:variables], options[:default_target]
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,95 @@
1
+ require "xfabricator/version"
2
+ require 'xcodeproj'
3
+ require 'xfabricator/x_code_file_template'
4
+
5
+ module Xfabricator
6
+
7
+ class Fabricator
8
+
9
+ def initialize(config, fabrication_path)
10
+ @config = config
11
+ @fabrication_path = fabrication_path
12
+ end
13
+
14
+ def fabricate(template, variables, target_name)
15
+ group = find_current_group
16
+
17
+ unless group
18
+ raise ArgumentError, "couldn't find current group"
19
+ end
20
+
21
+ fabricate_in_group(template, variables, target_name, group)
22
+ end
23
+
24
+ def fabricate_in_group(template, variables, target_name, group)
25
+ unless group
26
+ raise ArgumentError, "group not specified"
27
+ end
28
+
29
+ unless target_name
30
+ raise ArgumentError, "target_name not specified"
31
+ end
32
+
33
+ target = @config.project.targets.find { |target| target.name == target_name }
34
+
35
+ unless target
36
+ raise ArgumentError, "Couldn't find target with name: #{target_name}"
37
+ end
38
+
39
+ template.template_files.each do |template_file|
40
+ template_file_path = "#{@config.template_location}/#{template_file.template_file}"
41
+
42
+ unless Pathname.new(template_file_path).exist?
43
+ template_file_path = template_file.template_file
44
+ end
45
+
46
+ if Pathname.new(template_file_path).exist?
47
+ name_template = Mustache.new
48
+ output_name = name_template.render template_file.output_name_template, variables
49
+ output_file = fabricate_file(output_name, template_file_path, variables)
50
+ ref = group.new_reference output_file
51
+ target.add_file_references [ref]
52
+ else
53
+ raise RuntimeError, "template file was not found: #{template_file_path}"
54
+ end
55
+
56
+ @config.project.save
57
+ end
58
+
59
+ end
60
+
61
+ def fabricate_file(output_name, template_file_path, variables)
62
+ output_file = "#{@fabrication_path}/#{output_name}"
63
+
64
+ template = XCodeFileTemplate.new @config.project, variables
65
+ template.template_file = template_file_path
66
+ rendered_content = template.render
67
+
68
+ File.open(output_file, 'w') { |file| file.write(rendered_content) }
69
+ output_file
70
+ end
71
+
72
+ def find_current_group
73
+ project = @config.project
74
+ find_group_for_directory(project.main_group, @fabrication_path, project)
75
+ end
76
+
77
+ def find_group_for_directory(current_group, directory, project)
78
+ group_found = current_group.groups.find { |ref|
79
+ full_path = "#{project.path.parent.to_s}#{ref.hierarchy_path}"
80
+ full_path == directory.to_s
81
+ }
82
+
83
+ unless group_found
84
+ current_group.groups.each do |group|
85
+ full_path = "#{project.path.parent.to_s}#{group.hierarchy_path}"
86
+ return find_group_for_directory(group, directory, project) if directory.to_s.start_with? full_path
87
+ end
88
+ end
89
+
90
+ group_found
91
+ end
92
+
93
+ end
94
+
95
+ end
@@ -0,0 +1,55 @@
1
+ module Xfabricator
2
+
3
+ class TemplateDefinition
4
+
5
+ attr_reader :template_files
6
+ attr_reader :variable_prompts
7
+
8
+ def TemplateDefinition.load_definition(template_def_file)
9
+ unless template_def_file
10
+ raise ArgumentError, 'config file not specified'
11
+ end
12
+
13
+ unless template_def_file.exist?
14
+ raise ArgumentError, "template definition file doesn't exist: #{template_def_file}"
15
+ end
16
+
17
+ template_def = eval(File.read(template_def_file.to_s), binding)
18
+
19
+ unless template_def
20
+ raise RuntimeError, "template definition was not loaded"
21
+ end
22
+
23
+ unless template_def.is_a? TemplateDefinition
24
+ raise RuntimeError, "Valid template definition not loaded"
25
+ end
26
+
27
+ return template_def
28
+ end
29
+
30
+ def initialize(files, variable_prompts)
31
+ @template_files = files
32
+ @variable_prompts = variable_prompts
33
+ end
34
+
35
+ def TemplateDefinition.create_definition(definition)
36
+ options = {}
37
+ definition.call(options)
38
+ TemplateDefinition.new options[:template_files], options[:variable_prompts]
39
+ end
40
+
41
+ end
42
+
43
+ class TemplateFileDefinition
44
+
45
+ attr_reader :output_name_template
46
+ attr_reader :template_file
47
+
48
+ def initialize(template_file, output_name_template)
49
+ @output_name_template = output_name_template
50
+ @template_file = template_file
51
+ end
52
+
53
+ end
54
+
55
+ end
@@ -0,0 +1,3 @@
1
+ module Xfabricator
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,40 @@
1
+ require 'mustache'
2
+
3
+ module Xfabricator
4
+
5
+ class XCodeFileTemplate < Mustache
6
+
7
+ # {{#configurations}}
8
+ # {{.}}
9
+ # {{/configurations}}
10
+
11
+ # {{template_variable}}
12
+
13
+ # {{#build_setting}}build_configuration.variable{{/build_setting}}
14
+
15
+ def initialize(project, variables)
16
+ super({})
17
+ @project = project
18
+
19
+ self[:configurations] = @project.build_configurations.collect do |config|
20
+ config.name
21
+ end
22
+
23
+ variables.each do |key,value|
24
+ self[key] = value
25
+ end
26
+ end
27
+
28
+ def build_setting
29
+ lambda { |key|
30
+ config = key.split('.').first
31
+ setting_name = key.split('.').last
32
+ settings = @project.build_settings config
33
+ return settings[setting_name]
34
+ }
35
+ end
36
+
37
+ end
38
+
39
+
40
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'xfabricator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "xfabricator"
8
+ spec.version = Xfabricator::VERSION
9
+ spec.authors = ["Nicholas Cross"]
10
+
11
+ spec.summary = %q{Generate file templates and add them to XCode project}
12
+ spec.description = %q{Generate file templates and add them to XCode project...}
13
+ spec.homepage = "https://github.com/nicholascross/xfabricator"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features|.idea)/})
18
+ end
19
+
20
+ spec.bindir = "bin"
21
+ spec.executables = ["xfabricate"]
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.13"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "xcodeproj", "~> 1.4"
27
+ spec.add_development_dependency "mustache", "~> 1.0"
28
+ spec.add_development_dependency "inquirer"
29
+
30
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xfabricator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Nicholas Cross
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: xcodeproj
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mustache
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: inquirer
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Generate file templates and add them to XCode project...
84
+ email:
85
+ executables:
86
+ - xfabricate
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE
93
+ - README.md
94
+ - Rakefile
95
+ - bin/console
96
+ - bin/setup
97
+ - bin/xfabricate
98
+ - lib/xfabricator/fabrication_config.rb
99
+ - lib/xfabricator/fabricator.rb
100
+ - lib/xfabricator/template_definition.rb
101
+ - lib/xfabricator/version.rb
102
+ - lib/xfabricator/x_code_file_template.rb
103
+ - xfabricator.gemspec
104
+ homepage: https://github.com/nicholascross/xfabricator
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.5.1
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Generate file templates and add them to XCode project
128
+ test_files: []