knife-skeleton 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- ODg0MmRjMzk1ZWMyNGM3ODQwZjAxMDg4OWEzOGU0MmM1NDJmNGFiMQ==
5
- data.tar.gz: !binary |-
6
- NjZjODkyM2NhYWNiMzQ2ZGIzN2IxMThjNTgxNDI1NTA3NzAzMGJiYg==
2
+ SHA1:
3
+ metadata.gz: 9e21bf5141891cd7df596fa29fb4e3eaace5db13
4
+ data.tar.gz: 2606421903e6d939ec21d5cab9d2b117b4ee037c
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- MTU1Nzk3MjUxYWMxZWU1YjNiODU1ZDM1MTEzYjJiN2EwMDkwYjg4ZGJjMWU3
10
- MzZjYTcxYmYxNjdjZjkzOWM1ZTVjYmE2YTBhZTkxMDM4ZjQxN2QwNjNiNjMx
11
- MmEyNGI2MzY0NjBkMTg3NjRiZDA1ZDJkMzg4NjllODFhMTA0ZGU=
12
- data.tar.gz: !binary |-
13
- NDAyYjJlOTgxNTVkNzI4N2UyNjE4MGE0MjdkOThjMWY4MzI2ZmZlODJkNjcz
14
- OTEzNzA2OGY5MjRhMzJmZDQxZGZlMzRhMzIwZGIyMmY0YWRlODJkYzc5OThi
15
- MDY0NjRlY2VhYmMxOTk1MDJiOTYwYjQwYzk0MDdjYTZjOTM5YWE=
6
+ metadata.gz: 86aef585240e3d1cba71f0eea5888a6f6304b5847eb6711cd08e945ebc970c209b8812b66e977d37f187c875f5f523a531763e74746ae130e51282786fefa785
7
+ data.tar.gz: ce1f1d4ca6ea9b7af009e99d95bb82e2fb7025356b6bab26882468079d8c7122177fcb374c09e080bd1783b9efa980f5c7a7289362ec05fbbc97f47586bcdc7e
data/README.md CHANGED
@@ -23,38 +23,9 @@ rake install
23
23
 
24
24
  ##Usage
25
25
 
26
- ```bash
27
- $ knife skeleton create -h
28
- knife skeleton create COOKOOK (options)
29
- -s, --server-url URL Chef Server URL
30
- --chef-zero-port PORT Port to start chef-zero on
31
- -k, --key KEY API Client Key
32
- --[no-]color Use colored output, defaults to false on Windows, true otherwise
33
- -c, --config CONFIG The configuration file to use
34
- -C, --copyright COPYRIGHT Name of Copyright holder
35
- -m, --email EMAIL Email address of cookbook maintainer
36
- -I, --license LICENSE License apachev2, gplv2, gplv3, mit or none
37
- -o, --cookbook-path PATH The directory where the cookbook will be created
38
- --defaults Accept default values for all questions
39
- -d, --disable-editing Do not open EDITOR, just accept the data as is
40
- -e, --editor EDITOR Set the editor to use for interactive commands
41
- -E, --environment ENVIRONMENT Set the Chef environment
42
- -F, --format FORMAT Which format to use for output
43
- -z, --local-mode Point knife commands at local repository instead of server
44
- -u, --user USER API Client Username
45
- --print-after Show the data after a destructive operation
46
- -r, --readme-format FORMAT Format of the README file, supported formats are 'md', 'rdoc' and 'txt'
47
- -V, --verbose More verbose output. Use twice for max verbosity
48
- -v, --version Show chef version
49
- -y, --yes Say yes to all prompts for confirmation
50
- -h, --help Show this message
51
- ```
52
-
53
- Create your cookbook
26
+ There is no usage instructions! Simply having knife-skeleton installed will automatically override Chef's default `cookbook create` command.
54
27
 
55
- ```
56
- knife skeleton create my-cookbook -o /tmp/cookbooks
57
- ```
28
+ `knife cookbook create COOKBOOK`
58
29
 
59
30
  ## License and Authors
60
31
 
data/files/Gemfile CHANGED
@@ -5,7 +5,6 @@ gem 'chef', '~> 11.12.2'
5
5
  gem 'berkshelf', '~> 2.0.16'
6
6
 
7
7
  group :integration do
8
- gem 'rspec_junit_formatter'
9
8
  gem 'rubocop'
10
9
  gem 'strainer', '~> 3.4.0'
11
10
  gem 'test-kitchen', '~> 1.2.1'
@@ -0,0 +1,330 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'chef/knife/cookbook_create'
3
+ require 'pathname'
4
+ require 'knife_skeleton/template'
5
+
6
+ class Chef
7
+ class Knife
8
+ # Cookbook class
9
+ class CookbookCreate < Chef::Knife
10
+ banner 'knife skeleton create COOKOOK (options)'
11
+
12
+ option(:cookbook_path,
13
+ short: '-o PATH',
14
+ long: '--cookbook-path PATH',
15
+ description: <<-eos
16
+ The directory where the cookbook will be created
17
+ eos
18
+ )
19
+
20
+ option(:readme_format,
21
+ short: '-r FORMAT',
22
+ long: '--readme-format FORMAT',
23
+ description: <<-eos
24
+ Format of the README file, supported formats are 'md', 'rdoc' and 'txt'
25
+ eos
26
+ )
27
+ option(:cookbook_license,
28
+ short: '-I LICENSE',
29
+ long: '--license LICENSE',
30
+ description: <<-eos
31
+ License apachev2, gplv2, gplv3, mit or none
32
+ eos
33
+ )
34
+ option(:cookbook_copyright,
35
+ short: '-C COPYRIGHT',
36
+ long: '--copyright COPYRIGHT',
37
+ description: <<-eos
38
+ Name of Copyright holder
39
+ eos
40
+ )
41
+ option(:cookbook_email,
42
+ short: '-m EMAIL',
43
+ long: '--email EMAIL',
44
+ description: <<-eos
45
+ Email address of cookbook maintainer
46
+ eos
47
+ )
48
+
49
+ # Public: Knife skeleton create runner
50
+ #
51
+ # @return [Void]
52
+ #
53
+ def run
54
+ self.config = Chef::Config.merge!(config)
55
+ if @name_args.length < 1
56
+ show_usage
57
+ ui.fatal('You must specify a cookbook name')
58
+ exit 1
59
+ end
60
+
61
+ if parameter_empty?(config[:cookbook_path])
62
+ fail ArgumentError, <<-eos
63
+ Default cookbook_path is not specified in the
64
+ knife.rb config file, and a value to -o is
65
+ not provided. Nowhere to write the new cookbook to.
66
+ eos
67
+ end
68
+
69
+ params = {
70
+ cookbook_path: File.expand_path(Array(config[:cookbook_path]).first),
71
+ cookbook_name: @name_args.first,
72
+ copyright: cookbook_copyright,
73
+ email: cookbook_email,
74
+ license: cookbook_license,
75
+ license_name: cookbook_license_name,
76
+ readme_format: cookbook_readme_format
77
+ }
78
+
79
+ create_cookbook_directories(
80
+ params[:cookbook_path],
81
+ params[:cookbook_name]
82
+ )
83
+ create_cookbook_files(params[:cookbook_path], params[:cookbook_name])
84
+ create_cookbook_templates(params)
85
+ end
86
+
87
+ # Public: Retrieve license name
88
+ #
89
+ # Examples:
90
+ #
91
+ # # With mit license
92
+ # cookbook_license_name
93
+ # # => 'MIT'
94
+ # # With apachev2 license
95
+ # cookbook_license_name
96
+ # # => 'Apache 2.0'
97
+ # # With gplv3 license
98
+ # cookbook_license_name
99
+ # # => 'GNU Public LIcense 3.0'
100
+ #
101
+ # @return [String]
102
+ #
103
+ def cookbook_license_name
104
+ case cookbook_license
105
+ when 'apachev2'
106
+ 'Apache 2.0'
107
+ when 'gplv2'
108
+ 'GNU Public License 2.0'
109
+ when 'gplv3'
110
+ 'GNU Public License 3.0'
111
+ when 'mit'
112
+ 'MIT'
113
+ when 'none'
114
+ 'All rights reserved'
115
+ end
116
+ end
117
+
118
+ # Create cookbook directories
119
+ #
120
+ # Examples:
121
+ #
122
+ # create_cookbook_directories('/tmp', 'my-cookbook')
123
+ #
124
+ # @param [String] cookbook_path Cookbook path
125
+ # @param [String] cookbook_name Cookbook name
126
+ # @return [Void]
127
+ #
128
+ def create_cookbook_directories(cookbook_path, cookbook_name)
129
+ ui.msg("** Create cookbook #{cookbook_name} into #{cookbook_path}")
130
+
131
+ %w(
132
+ attributes
133
+ definitions
134
+ libraries
135
+ providers
136
+ recipes
137
+ resources
138
+ spec
139
+ files/default
140
+ templates/default
141
+ test/integration/default/serverspec).each do |dir|
142
+ FileUtils.mkdir_p(File.join(
143
+ cookbook_path,
144
+ cookbook_name,
145
+ dir))
146
+ end
147
+ end
148
+
149
+ # Create cookbook files from templates
150
+ #
151
+ # Examples:
152
+ #
153
+ # create_cookbook_templates({ cookbook_path: '/tmp', title: 'GoT' })
154
+ #
155
+ # @params [Hash] params An Hash of parameters to use for binding template
156
+ # @return [Void]
157
+ #
158
+ def create_cookbook_templates(params)
159
+ params[:license_content] = File.read(File.join(files_directory,
160
+ 'licenses',
161
+ params[:license])
162
+ ) if params[:license] != 'none'
163
+
164
+ params[:license_content] = '' unless params[:license] != 'none'
165
+
166
+ %W(
167
+ metadata.rb
168
+ CHANGELOG.#{params[:readme_format]}
169
+ README.#{params[:readme_format]}
170
+ .kitchen.yml
171
+ attributes/default.rb
172
+ recipes/default.rb
173
+ spec/default_spec.rb
174
+ spec/spec_helper.rb
175
+ test/integration/default/serverspec/spec_helper.rb
176
+ test/integration/default/serverspec/default_spec.rb
177
+ ).each do |file_name|
178
+ render_template(file_name, params)
179
+ end
180
+ end
181
+
182
+ # Copy all files into the cookbook
183
+ #
184
+ # Examples:
185
+ #
186
+ # create_cookbook_files('/tmp', 'my-cookbook')
187
+ #
188
+ # @param [String] cookbook_path Cookbook path
189
+ # @param [String] cookbook_name Cookbook name
190
+ # @return [Void]
191
+ #
192
+ def create_cookbook_files(cookbook_path, cookbook_name)
193
+ %w(
194
+ Berksfile
195
+ Gemfile
196
+ .gitignore
197
+ .rubocop.yml
198
+ .travis.yml
199
+ Strainerfile
200
+ ).each do |file_name|
201
+ copy_file(cookbook_path, cookbook_name, file_name)
202
+ end
203
+ end
204
+
205
+ # Copy files
206
+ #
207
+ # Examples:
208
+ #
209
+ # copy_file('/tmp', '/cookbooks', 'my-cookbook', 'README.md')
210
+ #
211
+ # @param [String] cookbook_path Cookbook path
212
+ # @param [String] cookbook_name Cookbook name
213
+ # @param [String] file_name File name to used without erb extension
214
+ # @return [Void]
215
+ #
216
+ def copy_file(cookbook_path, cookbook_name, file_name)
217
+ dst = File.join(cookbook_path,
218
+ cookbook_name,
219
+ file_name)
220
+
221
+ if File.exist?(dst)
222
+ ui.warn("'#{file_name}' already exists")
223
+ else
224
+ ui.msg("** Create '#{file_name}'")
225
+ FileUtils.cp(File
226
+ .join(
227
+ files_directory,
228
+ file_name.gsub(/^\./, '')),
229
+ dst)
230
+ end
231
+ end
232
+
233
+ # Render template
234
+ #
235
+ # Examples:
236
+ #
237
+ # render_template('/tmp', 'my-file.rb', { title: 'GoT' })
238
+ #
239
+ # @param [String] file_name File name to used without erb extension
240
+ # @param [Hash] params Binding parameters
241
+ # @return [void]
242
+ #
243
+ def render_template(file_name, params)
244
+ dst = File.join(params[:cookbook_path],
245
+ params[:cookbook_name],
246
+ file_name)
247
+
248
+ if File.exist?(dst)
249
+ ui.warn("'#{file_name}' already exists")
250
+ else
251
+ ui.msg("** Create '#{file_name}'")
252
+ File.open(dst, 'w+') do |file|
253
+ file.write(KnifeSkeleton::Template
254
+ .render(File.read(File.join(
255
+ templates_directory,
256
+ file_name + '.erb')),
257
+ params))
258
+ end
259
+ end
260
+ end
261
+
262
+ # Test if parameter is empty
263
+ #
264
+ # Examples:
265
+ #
266
+ # parameter_empty?('my string')
267
+ # # => false
268
+ # parameter_empty?('')
269
+ # # => true
270
+ #
271
+ # @param [Mixed] parameter The tested parameter
272
+ # @return [String]
273
+ #
274
+ def parameter_empty?(parameter)
275
+ parameter.nil? || parameter.empty?
276
+ end
277
+
278
+ # Get cookbook copyright
279
+ #
280
+ # @return [String]
281
+ #
282
+ def cookbook_copyright
283
+ config[:cookbook_copyright] || 'YOUR_COMPANY_NAME'
284
+ end
285
+
286
+ # Get maintener email
287
+ #
288
+ # @return [String]
289
+ #
290
+ def cookbook_email
291
+ config[:cookbook_email] || 'YOUR_EMAIL'
292
+ end
293
+
294
+ # Get license name
295
+ #
296
+ # @return [String]
297
+ #
298
+ def cookbook_license
299
+ ((config[:cookbook_license] != 'false') &&
300
+ config[:cookbook_license]) || 'none'
301
+ end
302
+
303
+ # Get readme format
304
+ #
305
+ # @return [String]
306
+ #
307
+ def cookbook_readme_format
308
+ ((config[:readme_format] != 'false') && config[:readme_format]) || 'md'
309
+ end
310
+
311
+ # Get files directory
312
+ #
313
+ # @return [String]
314
+ #
315
+ def files_directory
316
+ File.expand_path('../../../../files',
317
+ Pathname.new(__FILE__).realpath)
318
+ end
319
+
320
+ # Get templates directory
321
+ #
322
+ # @return [String]
323
+ #
324
+ def templates_directory
325
+ File.expand_path('../../../../templates',
326
+ Pathname.new(__FILE__).realpath)
327
+ end
328
+ end
329
+ end
330
+ end
@@ -1,6 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  # Knife skeleton version
3
3
  module KnifeSkeleton
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  MAJOR, MINOR, TINY = VERSION.split('.')
6
6
  end
@@ -1,13 +1,13 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  require 'spec_helper'
3
- require 'chef/knife/skeleton_create'
3
+ require 'chef/knife/cookbook_create_extension'
4
4
  require 'fakefs/spec_helpers'
5
5
 
6
- describe Knife::SkeletonCreate do
6
+ describe Chef::Knife::CookbookCreate do
7
7
  include FakeFS::SpecHelpers
8
8
 
9
9
  before(:each) do
10
- @knife = Knife::SkeletonCreate.new
10
+ @knife = Chef::Knife::CookbookCreate.new
11
11
  @knife.config = {}
12
12
  @knife.name_args = ['foobar']
13
13
  @stdout = StringIO.new
@@ -5,4 +5,4 @@
5
5
  #
6
6
  # Copyright <%= Time.now.year %>, <%= copyright %>
7
7
  #
8
- <%= license_content %>
8
+ <%= license_content -%>
@@ -2,21 +2,5 @@
2
2
 
3
3
  require 'serverspec'
4
4
 
5
- include SpecInfra::Helper::Exec
6
- include SpecInfra::Helper::DetectOS
7
-
8
- begin
9
- require 'rspec_junit_formatter'
10
- rescue LoadError
11
- require 'rubygems/dependency_installer'
12
- Gem::DependencyInstaller.new.install('rspec_junit_formatter')
13
- require 'rspec_junit_formatter'
14
- end
15
-
16
- RSpec.configure do |c|
17
- c.before :all do
18
- c.os = backend(Serverspec::Commands::Base).check_os
19
- end
20
- c.path = '/sbin:/usr/sbin:/usr/bin:/bin'
21
- c.add_formatter 'RspecJunitFormatter', '/tmp/kitchen.xml'
22
- end
5
+ set :backend, :exec
6
+ set :path, '$PATH:/sbin:/usr/sbin:/usr/bin:/bin'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-skeleton
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pierre Rambaud
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-09-04 00:00:00.000000000 Z
12
+ date: 2014-10-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: chef
@@ -99,21 +99,19 @@ dependencies:
99
99
  name: fakefs
100
100
  requirement: !ruby/object:Gem::Requirement
101
101
  requirements:
102
- - - ! '>='
102
+ - - '>='
103
103
  - !ruby/object:Gem::Version
104
104
  version: 0.5.3
105
105
  type: :development
106
106
  prerelease: false
107
107
  version_requirements: !ruby/object:Gem::Requirement
108
108
  requirements:
109
- - - ! '>='
109
+ - - '>='
110
110
  - !ruby/object:Gem::Version
111
111
  version: 0.5.3
112
- description: ! 'Knife plugin to create skeleton with rubocop, chefspec, kitchen,
113
-
112
+ description: |
113
+ Knife plugin to create skeleton with rubocop, chefspec, kitchen,
114
114
  strainer, etc...
115
-
116
- '
117
115
  email:
118
116
  - pierre.rambaud@numergy.com
119
117
  - antoine.rouyer@numergy.com
@@ -139,10 +137,10 @@ files:
139
137
  - files/rubocop.yml
140
138
  - files/travis.yml
141
139
  - knife_skeleton.gemspec
142
- - lib/chef/knife/skeleton_create.rb
140
+ - lib/chef/knife/cookbook_create_extension.rb
143
141
  - lib/knife_skeleton/template.rb
144
142
  - lib/knife_skeleton/version.rb
145
- - spec/chef/knife/knife_skeleton_create_spec.rb
143
+ - spec/chef/knife/knife_cookbook_create_extension_spec.rb
146
144
  - spec/knife_skeleton/template_spec.rb
147
145
  - spec/spec_helper.rb
148
146
  - templates/.kitchen.yml.erb
@@ -169,22 +167,23 @@ require_paths:
169
167
  - lib
170
168
  required_ruby_version: !ruby/object:Gem::Requirement
171
169
  requirements:
172
- - - ! '>='
170
+ - - '>='
173
171
  - !ruby/object:Gem::Version
174
172
  version: '0'
175
173
  required_rubygems_version: !ruby/object:Gem::Requirement
176
174
  requirements:
177
- - - ! '>='
175
+ - - '>='
178
176
  - !ruby/object:Gem::Version
179
177
  version: '0'
180
178
  requirements: []
181
179
  rubyforge_project:
182
- rubygems_version: 2.4.1
180
+ rubygems_version: 2.0.14
183
181
  signing_key:
184
182
  specification_version: 4
185
183
  summary: Knife plugin to create skeleton with rubocop, chefspec, kitchen, strainer,
186
184
  etc...
187
185
  test_files:
188
- - spec/chef/knife/knife_skeleton_create_spec.rb
186
+ - spec/chef/knife/knife_cookbook_create_extension_spec.rb
189
187
  - spec/knife_skeleton/template_spec.rb
190
188
  - spec/spec_helper.rb
189
+ has_rdoc:
@@ -1,354 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- require 'chef/knife'
3
- require 'pathname'
4
- require 'knife_skeleton/template'
5
-
6
- module Knife
7
- # Cookbook class
8
- class SkeletonCreate < Chef::Knife
9
- banner 'knife skeleton create COOKOOK (options)'
10
-
11
- option :cookbook_path,
12
- short: '-o PATH',
13
- long: '--cookbook-path PATH',
14
- description: <<-eos
15
- The directory where the cookbook will be created
16
- eos
17
-
18
- option :readme_format,
19
- short: '-r FORMAT',
20
- long: '--readme-format FORMAT',
21
- description: <<-eos
22
- Format of the README file, supported formats are 'md', 'rdoc' and 'txt'
23
- eos
24
-
25
- option :cookbook_license,
26
- short: '-I LICENSE',
27
- long: '--license LICENSE',
28
- description: <<-eos
29
- License apachev2, gplv2, gplv3, mit or none
30
- eos
31
-
32
- option :cookbook_copyright,
33
- short: '-C COPYRIGHT',
34
- long: '--copyright COPYRIGHT',
35
- description: <<-eos
36
- Name of Copyright holder
37
- eos
38
-
39
- option :cookbook_email,
40
- short: '-m EMAIL',
41
- long: '--email EMAIL',
42
- description: <<-eos
43
- Email address of cookbook maintainer
44
- eos
45
-
46
- # Public: Knife skeleton create runner
47
- #
48
- # @return [Void]
49
- #
50
- def run
51
- self.config = Chef::Config.merge!(config)
52
- if @name_args.length < 1
53
- show_usage
54
- ui.fatal('You must specify a cookbook name')
55
- exit 1
56
- end
57
-
58
- if parameter_empty?(config[:cookbook_path])
59
- fail ArgumentError, <<-eos
60
- Default cookbook_path is not specified in the
61
- knife.rb config file, and a value to -o is
62
- not provided. Nowhere to write the new cookbook to.
63
- eos
64
- end
65
-
66
- params = {
67
- cookbook_path: File.expand_path(Array(config[:cookbook_path]).first),
68
- cookbook_name: @name_args.first,
69
- copyright: cookbook_copyright,
70
- email: cookbook_email,
71
- license: cookbook_license,
72
- license_name: cookbook_license_name,
73
- readme_format: cookbook_readme_format
74
- }
75
-
76
- create_cookbook_directories(
77
- params[:cookbook_path],
78
- params[:cookbook_name]
79
- )
80
- create_cookbook_files(params[:cookbook_path], params[:cookbook_name])
81
- create_cookbook_templates(params)
82
- end
83
-
84
- # Public: Retrieve license name
85
- #
86
- # Examples:
87
- #
88
- # # With mit license
89
- # cookbook_license_name
90
- # # => 'MIT'
91
- # # With apachev2 license
92
- # cookbook_license_name
93
- # # => 'Apache 2.0'
94
- # # With gplv3 license
95
- # cookbook_license_name
96
- # # => 'GNU Public LIcense 3.0'
97
- #
98
- # @return [String]
99
- #
100
- def cookbook_license_name
101
- case cookbook_license
102
- when 'apachev2'
103
- 'Apache 2.0'
104
- when 'gplv2'
105
- 'GNU Public License 2.0'
106
- when 'gplv3'
107
- 'GNU Public License 3.0'
108
- when 'mit'
109
- 'MIT'
110
- when 'none'
111
- 'All rights reserved'
112
- end
113
- end
114
-
115
- # Create cookbook directories
116
- #
117
- # Examples:
118
- #
119
- # create_cookbook_directories('/tmp', 'my-cookbook')
120
- #
121
- # @param [String] cookbook_path Cookbook path
122
- # @param [String] cookbook_name Cookbook name
123
- # @return [Void]
124
- #
125
- def create_cookbook_directories(cookbook_path, cookbook_name)
126
- ui.msg("** Create cookbook #{cookbook_name} into #{cookbook_path}")
127
-
128
- %w(
129
- attributes
130
- definitions
131
- libraries
132
- providers
133
- recipes
134
- resources
135
- spec
136
- files/default
137
- templates/default
138
- test/integration/default/serverspec).each do |dir|
139
- FileUtils.mkdir_p(
140
- File.join(
141
- cookbook_path,
142
- cookbook_name,
143
- dir
144
- )
145
- )
146
- end
147
- end
148
-
149
- # Create cookbook files from templates
150
- #
151
- # Examples:
152
- #
153
- # create_cookbook_templates({ cookbook_path: '/tmp', title: 'GoT' })
154
- #
155
- # @params [Hash] params An Hash of parameters to use for binding template
156
- # @return [Void]
157
- #
158
- def create_cookbook_templates(params)
159
- params[:license_content] = File.read(
160
- File.join(
161
- files_directory,
162
- 'licenses',
163
- params[:license]
164
- )
165
- ) if params[:license] != 'none'
166
-
167
- params[:license_content] = '' unless params[:license] != 'none'
168
-
169
- %W(
170
- metadata.rb
171
- CHANGELOG.#{params[:readme_format]}
172
- README.#{params[:readme_format]}
173
- .kitchen.yml
174
- attributes/default.rb
175
- recipes/default.rb
176
- spec/default_spec.rb
177
- spec/spec_helper.rb
178
- test/integration/default/serverspec/spec_helper.rb
179
- test/integration/default/serverspec/default_spec.rb
180
- ).each do |file_name|
181
- render_template(file_name, params)
182
- end
183
- end
184
-
185
- # Copy all files into the cookbook
186
- #
187
- # Examples:
188
- #
189
- # create_cookbook_files('/tmp', 'my-cookbook')
190
- #
191
- # @param [String] cookbook_path Cookbook path
192
- # @param [String] cookbook_name Cookbook name
193
- # @return [Void]
194
- #
195
- def create_cookbook_files(
196
- cookbook_path,
197
- cookbook_name
198
- )
199
- %w(
200
- Berksfile
201
- Gemfile
202
- .gitignore
203
- .rubocop.yml
204
- .travis.yml
205
- Strainerfile
206
- ).each do |file_name|
207
- copy_file(cookbook_path, cookbook_name, file_name)
208
- end
209
- end
210
-
211
- # Copy files
212
- #
213
- # Examples:
214
- #
215
- # copy_file('/tmp', '/cookbooks', 'my-cookbook', 'README.md')
216
- #
217
- # @param [String] cookbook_path Cookbook path
218
- # @param [String] cookbook_name Cookbook name
219
- # @param [String] file_name File name to used without erb extension
220
- # @return [Void]
221
- #
222
- def copy_file(cookbook_path, cookbook_name, file_name)
223
- dst = File.join(
224
- cookbook_path,
225
- cookbook_name,
226
- file_name
227
- )
228
-
229
- if File.exist?(dst)
230
- ui.warn("'#{file_name}' already exists")
231
- else
232
- ui.msg("** Create '#{file_name}'")
233
- FileUtils.cp(
234
- File.join(
235
- files_directory,
236
- file_name.gsub(/^\./, '')
237
- ),
238
- dst
239
- )
240
- end
241
- end
242
-
243
- # Render template
244
- #
245
- # Examples:
246
- #
247
- # render_template('/tmp', 'my-file.rb', { title: 'GoT' })
248
- #
249
- # @param [String] file_name File name to used without erb extension
250
- # @param [Hash] params Binding parameters
251
- # @return [void]
252
- #
253
- def render_template(file_name, params)
254
- dst = File.join(
255
- params[:cookbook_path],
256
- params[:cookbook_name],
257
- file_name
258
- )
259
-
260
- if File.exist?(dst)
261
- ui.warn("'#{file_name}' already exists")
262
- else
263
- ui.msg("** Create '#{file_name}'")
264
- File.open(
265
- dst,
266
- 'w+'
267
- ) do |file|
268
- file.write(
269
- KnifeSkeleton::Template.render(
270
- File.read(
271
- File.join(
272
- templates_directory,
273
- file_name + '.erb'
274
- )
275
- ),
276
- params
277
- )
278
- )
279
- end
280
- end
281
- end
282
-
283
- # Test if parameter is empty
284
- #
285
- # Examples:
286
- #
287
- # parameter_empty?('my string')
288
- # # => false
289
- # parameter_empty?('')
290
- # # => true
291
- #
292
- # @param [Mixed] parameter The tested parameter
293
- # @return [String]
294
- #
295
- def parameter_empty?(parameter)
296
- parameter.nil? || parameter.empty?
297
- end
298
-
299
- # Get cookbook copyright
300
- #
301
- # @return [String]
302
- #
303
- def cookbook_copyright
304
- config[:cookbook_copyright] || 'YOUR_COMPANY_NAME'
305
- end
306
-
307
- # Get maintener email
308
- #
309
- # @return [String]
310
- #
311
- def cookbook_email
312
- config[:cookbook_email] || 'YOUR_EMAIL'
313
- end
314
-
315
- # Get license name
316
- #
317
- # @return [String]
318
- #
319
- def cookbook_license
320
- ((config[:cookbook_license] != 'false') &&
321
- config[:cookbook_license]) || 'none'
322
- end
323
-
324
- # Get readme format
325
- #
326
- # @return [String]
327
- #
328
- def cookbook_readme_format
329
- ((config[:readme_format] != 'false') && config[:readme_format]) || 'md'
330
- end
331
-
332
- # Get files directory
333
- #
334
- # @return [String]
335
- #
336
- def files_directory
337
- File.expand_path(
338
- '../../../../files',
339
- Pathname.new(__FILE__).realpath
340
- )
341
- end
342
-
343
- # Get templates directory
344
- #
345
- # @return [String]
346
- #
347
- def templates_directory
348
- File.expand_path(
349
- '../../../../templates',
350
- Pathname.new(__FILE__).realpath
351
- )
352
- end
353
- end
354
- end