knife-skeleton 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/files/rubocop.yml ADDED
@@ -0,0 +1,29 @@
1
+ AllCops:
2
+ Include:
3
+ - metadata.rb
4
+ - '**/Gemfile'
5
+ - attributes/**/*
6
+ - definitions/**/*
7
+ - libraries/**/*
8
+ - providers/**/*
9
+ - recipes/**/*
10
+ - resources/**/*
11
+ - spec/**/*
12
+ - test/**/*
13
+ Exclude:
14
+ - cookbooks/**/*
15
+ - vendor/**/*
16
+
17
+ Encoding:
18
+ Exclude:
19
+ - metadata.rb
20
+ - Gemfile
21
+
22
+ NumericLiterals:
23
+ Enabled: false
24
+
25
+ LineLength:
26
+ Enabled: false
27
+
28
+ WordArray:
29
+ MinSize: 3
data/files/travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ before_script:
6
+ - bundle exec berks install
7
+ script: bundle exec strainer test --except kitchen
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/knife_skeleton/version', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.authors = ['Pierre Rambaud']
6
+ s.email = ['pierre.rambaud@numergy.com']
7
+ s.description = <<-eos
8
+ Knife plugin to create skeleton with rubocop, chefspec, kitchen,
9
+ strainer, etc...
10
+ eos
11
+ s.summary = s.description
12
+ s.homepage = 'https://github.com/Numergy/knife-skeleton'
13
+ s.license = 'Apache 2.0'
14
+
15
+ s.files = `git ls-files`.split($OUTPUT_RECORD_SEPARATOR)
16
+ s.executables = s.files.grep(/^bin\//).map { |f| File.basename(f) }
17
+ s.test_files = s.files.grep(/^(test|spec|features)\//)
18
+ s.name = 'knife-skeleton'
19
+ s.require_paths = ['lib']
20
+ s.version = KnifeSkeleton::VERSION
21
+
22
+ s.add_dependency 'chef', '~> 0.10'
23
+ s.add_dependency 'erubis', '~> 0.2'
24
+
25
+ s.add_development_dependency 'rake', '~> 10'
26
+ s.add_development_dependency 'rspec', '~> 2.14'
27
+ s.add_development_dependency 'rubocop', '~> 0.25'
28
+ s.add_development_dependency 'simplecov', '~> 0.9'
29
+ s.add_development_dependency 'fakefs', '~> 0.5'
30
+ end
@@ -0,0 +1,346 @@
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
+ # Returns void
49
+ def run
50
+ self.config = Chef::Config.merge!(config)
51
+ if @name_args.length < 1
52
+ show_usage
53
+ ui.fatal('You must specify a cookbook name')
54
+ exit 1
55
+ end
56
+
57
+ if parameter_empty?(config[:cookbook_path])
58
+ fail ArgumentError, <<-eos
59
+ Default cookbook_path is not specified in the
60
+ knife.rb config file, and a value to -o is
61
+ not provided. Nowhere to write the new cookbook to.
62
+ eos
63
+ end
64
+
65
+ params = {
66
+ cookbook_path: File.expand_path(Array(config[:cookbook_path]).first),
67
+ cookbook_name: @name_args.first,
68
+ copyright: cookbook_copyright,
69
+ email: cookbook_email,
70
+ license: cookbook_license,
71
+ license_name: cookbook_license_name,
72
+ readme_format: cookbook_readme_format
73
+ }
74
+
75
+ create_cookbook_directories(
76
+ params[:cookbook_path],
77
+ params[:cookbook_name]
78
+ )
79
+ create_cookbook_files(params[:cookbook_path], params[:cookbook_name])
80
+ create_cookbook_templates(params)
81
+ end
82
+
83
+ # Public: Retrieve license name
84
+ #
85
+ # Examples:
86
+ #
87
+ # # With mit license
88
+ # cookbook_license_name
89
+ # # => 'MIT'
90
+ # # With apachev2 license
91
+ # cookbook_license_name
92
+ # # => 'Apache 2.0'
93
+ # # With gplv3 license
94
+ # cookbook_license_name
95
+ # # => 'GNU Public LIcense 3.0'
96
+ #
97
+ # Returns string
98
+ def cookbook_license_name
99
+ case cookbook_license
100
+ when 'apachev2'
101
+ 'Apache 2.0'
102
+ when 'gplv2'
103
+ 'GNU Public License 2.0'
104
+ when 'gplv3'
105
+ 'GNU Public License 3.0'
106
+ when 'mit'
107
+ 'MIT'
108
+ when 'none'
109
+ 'All rights reserved'
110
+ end
111
+ end
112
+
113
+ protected
114
+
115
+ # Protected: Create cookbook directories
116
+ #
117
+ # cookbook_path - Cookbook path
118
+ # cookbook_name - Cookbook name
119
+ #
120
+ # Examples:
121
+ #
122
+ # create_cookbook_directories('/tmp', 'my-cookbook')
123
+ #
124
+ # Returns void
125
+ def create_cookbook_directories(cookbook_path, cookbook_name)
126
+ ui.msg("** Create cookbook #{cookbook_name} into #{cookbook_path}")
127
+
128
+ %w(
129
+ definitions
130
+ libraries
131
+ providers
132
+ recipes
133
+ resources
134
+ spec
135
+ files/default
136
+ templates/default
137
+ test/integration/default/serverspec).each do |dir|
138
+ FileUtils.mkdir_p(
139
+ File.join(
140
+ cookbook_path,
141
+ cookbook_name,
142
+ dir
143
+ )
144
+ )
145
+ end
146
+ end
147
+
148
+ # Protected: Create cookbook files from templates
149
+ #
150
+ # params - An Hash of parameters to use for binding template
151
+ #
152
+ # Examples:
153
+ #
154
+ # create_cookbook_templates({ cookbook_path: '/tmp', title: 'GoT' })
155
+ #
156
+ # Returns void
157
+ def create_cookbook_templates(params)
158
+ params[:license_content] = File.read(
159
+ File.join(
160
+ files_directory,
161
+ 'licenses',
162
+ params[:license]
163
+ )
164
+ ) if params[:license] != 'none'
165
+
166
+ params[:license_content] = '' unless params[:license] != 'none'
167
+
168
+ %W(
169
+ metadata.rb
170
+ CHANGELOG.#{params[:readme_format]}
171
+ README.#{params[:readme_format]}
172
+ .kitchen.yml
173
+ recipes/default.rb
174
+ spec/default_spec.rb
175
+ spec/spec_helper.rb
176
+ test/integration/default/serverspec/spec_helper.rb
177
+ test/integration/default/serverspec/default_spec.rb
178
+ ).each do |file_name|
179
+ render_template(file_name, params)
180
+ end
181
+ end
182
+
183
+ # Protected: Copy all files into the cookbook
184
+ #
185
+ # cookbook_path - Cookbook path
186
+ # cookbook_name - Cookbook name
187
+ #
188
+ # Examples:
189
+ #
190
+ # create_cookbook_files('/tmp', 'my-cookbook')
191
+ #
192
+ # Returns void
193
+ def create_cookbook_files(
194
+ cookbook_path,
195
+ cookbook_name
196
+ )
197
+ %w(
198
+ Berksfile
199
+ Gemfile
200
+ .gitignore
201
+ .rubocop.yml
202
+ .travis.yml
203
+ Strainerfile
204
+ ).each do |file_name|
205
+ copy_file(cookbook_path, cookbook_name, file_name)
206
+ end
207
+ end
208
+
209
+ # Protected: Copy files
210
+ #
211
+ # cookbook_path - Cookbook path
212
+ # cookbook_name - Cookbook name
213
+ # file_name - File name to used without erb extension
214
+ #
215
+ # Examples:
216
+ #
217
+ # copy_file('/tmp', '/cookbooks', 'my-cookbook', 'README.md')
218
+ #
219
+ # Returns void
220
+ def copy_file(cookbook_path, cookbook_name, file_name)
221
+ dst = File.join(
222
+ cookbook_path,
223
+ cookbook_name,
224
+ file_name
225
+ )
226
+
227
+ if File.exist?(dst)
228
+ ui.warn("'#{file_name}' already exists")
229
+ else
230
+ ui.msg("** Create '#{file_name}'")
231
+ FileUtils.cp(
232
+ File.join(
233
+ files_directory,
234
+ file_name.gsub(/^\./, '')
235
+ ),
236
+ dst
237
+ )
238
+ end
239
+ end
240
+
241
+ # Protected: Render template
242
+ #
243
+ # file_name - File name to used without erb extension
244
+ # params - Binding parameters
245
+ #
246
+ # Examples:
247
+ #
248
+ # render_template('/tmp', 'my-file.rb', { title: 'GoT' })
249
+ #
250
+ # Returns void
251
+ def render_template(file_name, params)
252
+ dst = File.join(
253
+ params[:cookbook_path],
254
+ params[:cookbook_name],
255
+ file_name
256
+ )
257
+
258
+ if File.exist?(dst)
259
+ ui.warn("'#{file_name}' already exists")
260
+ else
261
+ ui.msg("** Create '#{file_name}'")
262
+ File.open(
263
+ dst,
264
+ 'w+'
265
+ ) do |file|
266
+ file.write(
267
+ KnifeSkeleton::Template.render(
268
+ File.read(
269
+ File.join(
270
+ templates_directory,
271
+ file_name + '.erb'
272
+ )
273
+ ),
274
+ params
275
+ )
276
+ )
277
+ end
278
+ end
279
+ end
280
+
281
+ # Protected: Test if parameter is empty
282
+ #
283
+ # parameter - The tested parameter
284
+ #
285
+ # Examples:
286
+ #
287
+ # parameter_empty?('my string')
288
+ # # => false
289
+ # parameter_empty?('')
290
+ # # => true
291
+ #
292
+ # Returns string
293
+ def parameter_empty?(parameter)
294
+ parameter.nil? || parameter.empty?
295
+ end
296
+
297
+ # Protected: Get cookbook copyright
298
+ #
299
+ # Returns string
300
+ def cookbook_copyright
301
+ config[:cookbook_copyright] || 'YOUR_COMPANY_NAME'
302
+ end
303
+
304
+ # Protected: Get maintener email
305
+ #
306
+ # Returns string
307
+ def cookbook_email
308
+ config[:cookbook_email] || 'YOUR_EMAIL'
309
+ end
310
+
311
+ # Protected: Get license name
312
+ #
313
+ # Returns string
314
+ def cookbook_license
315
+ ((config[:cookbook_license] != 'false') &&
316
+ config[:cookbook_license]) || 'none'
317
+ end
318
+
319
+ # Protected: Get readme format
320
+ #
321
+ # Returns string
322
+ def cookbook_readme_format
323
+ ((config[:readme_format] != 'false') && config[:readme_format]) || 'md'
324
+ end
325
+
326
+ # Protected: Get files directory
327
+ #
328
+ # Returns string
329
+ def files_directory
330
+ File.expand_path(
331
+ '../../../../files',
332
+ Pathname.new(__FILE__).realpath
333
+ )
334
+ end
335
+
336
+ # Protected: Get templates directory
337
+ #
338
+ # Returns string
339
+ def templates_directory
340
+ File.expand_path(
341
+ '../../../../templates',
342
+ Pathname.new(__FILE__).realpath
343
+ )
344
+ end
345
+ end
346
+ end
@@ -0,0 +1,24 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'erubis'
3
+
4
+ module KnifeSkeleton
5
+ # Render template
6
+ class Template
7
+ # Static: Render template with Erubis
8
+ #
9
+ # template - Template string to used for rendering
10
+ # data - Data binding
11
+ #
12
+ # Examples:
13
+ #
14
+ # create_cookbook_directories('Hello <%= title %>', {title: 'GoT'})
15
+ # # => "Hello GoT"
16
+ #
17
+ # Returns string
18
+ def self.render(template, data)
19
+ eruby = Erubis::Eruby.new(template)
20
+ output = eruby.result(data)
21
+ output
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,6 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Knife skeleton version
3
+ module KnifeSkeleton
4
+ VERSION = '0.0.1'
5
+ MAJOR, MINOR, TINY = VERSION.split('.')
6
+ end