cookbook_creator 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +4 -4
  2. data/bin/cookbook_create +11 -0
  3. data/cookbook_creator.gemspec +33 -0
  4. data/lib/cookbook_creator/cli.rb +100 -0
  5. data/lib/cookbook_creator/config.rb +24 -0
  6. data/lib/cookbook_creator/config_loader.rb +157 -0
  7. data/lib/cookbook_creator/generator.rb +202 -0
  8. data/lib/cookbook_creator/helpers.rb +25 -0
  9. data/lib/cookbook_creator/main.rb +42 -0
  10. data/lib/cookbook_creator/null_logger.rb +70 -0
  11. data/lib/cookbook_creator/pathhelper.rb +33 -0
  12. data/lib/cookbook_creator/runner.rb +74 -0
  13. data/lib/cookbook_creator/skeletons/code_generator/files/default/Berksfile +3 -0
  14. data/lib/cookbook_creator/skeletons/code_generator/files/default/chefignore +100 -0
  15. data/lib/cookbook_creator/skeletons/code_generator/files/default/cookbook_readmes/README-policy.md +9 -0
  16. data/lib/cookbook_creator/skeletons/code_generator/files/default/cookbook_readmes/README.md +54 -0
  17. data/lib/cookbook_creator/skeletons/code_generator/files/default/gitignore +16 -0
  18. data/lib/cookbook_creator/skeletons/code_generator/files/default/repo/README.md +28 -0
  19. data/lib/cookbook_creator/skeletons/code_generator/files/default/repo/cookbooks/example/README.md +27 -0
  20. data/lib/cookbook_creator/skeletons/code_generator/files/default/repo/cookbooks/example/attributes/default.rb +7 -0
  21. data/lib/cookbook_creator/skeletons/code_generator/files/default/repo/cookbooks/example/metadata.rb +3 -0
  22. data/lib/cookbook_creator/skeletons/code_generator/files/default/repo/cookbooks/example/recipes/default.rb +8 -0
  23. data/lib/cookbook_creator/skeletons/code_generator/files/default/repo/data_bags/README.md +58 -0
  24. data/lib/cookbook_creator/skeletons/code_generator/files/default/repo/data_bags/example/example_item.json +4 -0
  25. data/lib/cookbook_creator/skeletons/code_generator/files/default/repo/dot-chef-repo.txt +6 -0
  26. data/lib/cookbook_creator/skeletons/code_generator/files/default/repo/environments/README.md +9 -0
  27. data/lib/cookbook_creator/skeletons/code_generator/files/default/repo/environments/example.json +13 -0
  28. data/lib/cookbook_creator/skeletons/code_generator/files/default/repo/policies/README.md +24 -0
  29. data/lib/cookbook_creator/skeletons/code_generator/files/default/repo/roles/README.md +9 -0
  30. data/lib/cookbook_creator/skeletons/code_generator/files/default/repo/roles/example.json +13 -0
  31. data/lib/cookbook_creator/skeletons/code_generator/files/default/serverspec_spec_helper.rb +8 -0
  32. data/lib/cookbook_creator/skeletons/code_generator/files/default/spec_helper.rb +2 -0
  33. data/lib/cookbook_creator/skeletons/code_generator/files/default/spec_helper_policyfile.rb +2 -0
  34. data/lib/cookbook_creator/skeletons/code_generator/metadata.rb +8 -0
  35. data/lib/cookbook_creator/skeletons/code_generator/recipes/app.rb +97 -0
  36. data/lib/cookbook_creator/skeletons/code_generator/recipes/attribute.rb +12 -0
  37. data/lib/cookbook_creator/skeletons/code_generator/recipes/cookbook.rb +112 -0
  38. data/lib/cookbook_creator/skeletons/code_generator/recipes/cookbook_file.rb +24 -0
  39. data/lib/cookbook_creator/skeletons/code_generator/recipes/lwrp.rb +23 -0
  40. data/lib/cookbook_creator/skeletons/code_generator/recipes/policyfile.rb +8 -0
  41. data/lib/cookbook_creator/skeletons/code_generator/recipes/recipe.rb +27 -0
  42. data/lib/cookbook_creator/skeletons/code_generator/recipes/repo.rb +67 -0
  43. data/lib/cookbook_creator/skeletons/code_generator/recipes/template.rb +32 -0
  44. data/lib/cookbook_creator/skeletons/code_generator/templates/default/Berksfile.erb +3 -0
  45. data/lib/cookbook_creator/skeletons/code_generator/templates/default/LICENSE.all_rights.erb +3 -0
  46. data/lib/cookbook_creator/skeletons/code_generator/templates/default/LICENSE.apache2.erb +201 -0
  47. data/lib/cookbook_creator/skeletons/code_generator/templates/default/LICENSE.gplv2.erb +339 -0
  48. data/lib/cookbook_creator/skeletons/code_generator/templates/default/LICENSE.gplv3.erb +674 -0
  49. data/lib/cookbook_creator/skeletons/code_generator/templates/default/LICENSE.mit.erb +21 -0
  50. data/lib/cookbook_creator/skeletons/code_generator/templates/default/README.md.erb +4 -0
  51. data/lib/cookbook_creator/skeletons/code_generator/templates/default/attribute.rb.erb +1 -0
  52. data/lib/cookbook_creator/skeletons/code_generator/templates/default/cookbook_file.erb +1 -0
  53. data/lib/cookbook_creator/skeletons/code_generator/templates/default/kitchen.yml.erb +25 -0
  54. data/lib/cookbook_creator/skeletons/code_generator/templates/default/library.rb.erb +7 -0
  55. data/lib/cookbook_creator/skeletons/code_generator/templates/default/metadata.rb.erb +7 -0
  56. data/lib/cookbook_creator/skeletons/code_generator/templates/default/provider.rb.erb +10 -0
  57. data/lib/cookbook_creator/skeletons/code_generator/templates/default/recipe.rb.erb +5 -0
  58. data/lib/cookbook_creator/skeletons/code_generator/templates/default/recipe_spec.rb.erb +20 -0
  59. data/lib/cookbook_creator/skeletons/code_generator/templates/default/repo/gitignore.erb +11 -0
  60. data/lib/cookbook_creator/skeletons/code_generator/templates/default/resource.rb.erb +7 -0
  61. data/lib/cookbook_creator/skeletons/code_generator/templates/default/serverspec_default_spec.rb.erb +9 -0
  62. data/lib/cookbook_creator/skeletons/code_generator/templates/default/template.erb +0 -0
  63. data/lib/skeletons/code_generator/files/default/Berksfile +3 -0
  64. data/lib/skeletons/code_generator/files/default/chefignore +100 -0
  65. data/lib/skeletons/code_generator/files/default/cookbook_readmes/README-policy.md +9 -0
  66. data/lib/skeletons/code_generator/files/default/cookbook_readmes/README.md +54 -0
  67. data/lib/skeletons/code_generator/files/default/gitignore +16 -0
  68. data/lib/skeletons/code_generator/files/default/repo/README.md +28 -0
  69. data/lib/skeletons/code_generator/files/default/repo/cookbooks/example/README.md +27 -0
  70. data/lib/skeletons/code_generator/files/default/repo/cookbooks/example/attributes/default.rb +7 -0
  71. data/lib/skeletons/code_generator/files/default/repo/cookbooks/example/metadata.rb +3 -0
  72. data/lib/skeletons/code_generator/files/default/repo/cookbooks/example/recipes/default.rb +8 -0
  73. data/lib/skeletons/code_generator/files/default/repo/data_bags/README.md +58 -0
  74. data/lib/skeletons/code_generator/files/default/repo/data_bags/example/example_item.json +4 -0
  75. data/lib/skeletons/code_generator/files/default/repo/dot-chef-repo.txt +6 -0
  76. data/lib/skeletons/code_generator/files/default/repo/environments/README.md +9 -0
  77. data/lib/skeletons/code_generator/files/default/repo/environments/example.json +13 -0
  78. data/lib/skeletons/code_generator/files/default/repo/policies/README.md +24 -0
  79. data/lib/skeletons/code_generator/files/default/repo/roles/README.md +9 -0
  80. data/lib/skeletons/code_generator/files/default/repo/roles/example.json +13 -0
  81. data/lib/skeletons/code_generator/files/default/serverspec_spec_helper.rb +8 -0
  82. data/lib/skeletons/code_generator/files/default/spec_helper.rb +2 -0
  83. data/lib/skeletons/code_generator/files/default/spec_helper_policyfile.rb +2 -0
  84. data/lib/skeletons/code_generator/metadata.rb +8 -0
  85. data/lib/skeletons/code_generator/recipes/app.rb +97 -0
  86. data/lib/skeletons/code_generator/recipes/attribute.rb +12 -0
  87. data/lib/skeletons/code_generator/recipes/cookbook.rb +94 -0
  88. data/lib/skeletons/code_generator/recipes/cookbook_file.rb +24 -0
  89. data/lib/skeletons/code_generator/recipes/lwrp.rb +23 -0
  90. data/lib/skeletons/code_generator/recipes/policyfile.rb +8 -0
  91. data/lib/skeletons/code_generator/recipes/recipe.rb +27 -0
  92. data/lib/skeletons/code_generator/recipes/repo.rb +67 -0
  93. data/lib/skeletons/code_generator/recipes/template.rb +32 -0
  94. data/lib/skeletons/code_generator/templates/default/LICENSE.all_rights.erb +3 -0
  95. data/lib/skeletons/code_generator/templates/default/LICENSE.apache2.erb +201 -0
  96. data/lib/skeletons/code_generator/templates/default/LICENSE.gplv2.erb +339 -0
  97. data/lib/skeletons/code_generator/templates/default/LICENSE.gplv3.erb +674 -0
  98. data/lib/skeletons/code_generator/templates/default/LICENSE.mit.erb +21 -0
  99. data/lib/skeletons/code_generator/templates/default/Policyfile.rb.erb +25 -0
  100. data/lib/skeletons/code_generator/templates/default/README.md.erb +4 -0
  101. data/lib/skeletons/code_generator/templates/default/attribute.rb.erb +0 -0
  102. data/lib/skeletons/code_generator/templates/default/cookbook_file.erb +0 -0
  103. data/lib/skeletons/code_generator/templates/default/kitchen.yml.erb +21 -0
  104. data/lib/skeletons/code_generator/templates/default/kitchen_policyfile.yml.erb +32 -0
  105. data/lib/skeletons/code_generator/templates/default/metadata.rb.erb +7 -0
  106. data/lib/skeletons/code_generator/templates/default/provider.rb.erb +0 -0
  107. data/lib/skeletons/code_generator/templates/default/recipe.rb.erb +5 -0
  108. data/lib/skeletons/code_generator/templates/default/recipe_spec.rb.erb +20 -0
  109. data/lib/skeletons/code_generator/templates/default/repo/gitignore.erb +11 -0
  110. data/lib/skeletons/code_generator/templates/default/resource.rb.erb +0 -0
  111. data/lib/skeletons/code_generator/templates/default/serverspec_default_spec.rb.erb +9 -0
  112. data/lib/skeletons/code_generator/templates/default/template.erb +0 -0
  113. data/test.txt +0 -0
  114. metadata +119 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 62da9dff7f403b80e5a45fbca0be0db4e8b20787
4
- data.tar.gz: 3d885c992b6f24119a281826aff4badab3a62664
3
+ metadata.gz: f70ff162f70eff1a36152803cc46ca952c5d966f
4
+ data.tar.gz: 86bc88ba6ac05b613b49841cbc8af0ed4c5df02d
5
5
  SHA512:
6
- metadata.gz: d6b642cc52cc9db33593ff1fac05dc8106259e70b6dc83e4dbb7868541e4eb218bd0964d3193eeb3676a215925211f1d67abe95366f7e95137eb68aa44aeee64
7
- data.tar.gz: 1b0aadb499adc8060da45decdfa1c6aae9fb3184ed1f6ea8b2287433f5058515ccf4056ad8f86a3093542a7a6eb8b8decea59d5abdac2a869e767a0cc893d877
6
+ metadata.gz: 8373416c2a9ac6e9d05d4ab1fd6403ac9dbd2814a7768cd00a08ffcedbabaab438eb6000542df1799ca695e4671eb124ec511b6e9db130ddf9de71183f83469e
7
+ data.tar.gz: 8337b4a7c0e71c039e04d24de2f83d39e6842a8f5c5b8a1759046f0c27608c01fc999a275980bbae70cdd6b00f7ec14cc8bbef66ed9bf3712becd04c52542f9f
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Copyright (c) 2016 Tacit Knowledge, All Rights Reserved.
4
+ #
5
+
6
+ Kernel.trap(:INT) { print("\n"); exit 1 }
7
+
8
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))
9
+ require 'cookbook_creator/main'
10
+
11
+ CookbookCreator::Main.new(ARGV.clone).run
@@ -0,0 +1,33 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'cookbook_creator'
3
+ s.version = '0.0.2'
4
+ s.date = '2016-01-11'
5
+ s.summary = "Everything required for cookbook generation"
6
+ s.description = "This gem is used to generate cookbook skeleton"
7
+ s.authors = ['Serghei Anicheev']
8
+ s.email = 'sanicheev@tacitknowledge.com'
9
+ s.require_paths = ['lib']
10
+ s.required_ruby_version = '~> 2.2.1'
11
+ s.bindir = 'bin'
12
+ s.executables = [ 'cookbook_create' ]
13
+ s.files = %w(test.txt) + Dir.glob("*.gemspec") +
14
+ Dir.glob("{lib,spec}/**/*", File::FNM_DOTMATCH).reject { |f| File.directory?(f) }
15
+ s.add_dependency "chef", "~> 12.6.0"
16
+ s.add_dependency "bundler"
17
+ s.add_dependency "berkshelf", "~> 4.0.1"
18
+ s.add_dependency "berkshelf-api-client", "~> 2.0.0"
19
+ s.add_dependency "chef-zero", "~> 4.4.0"
20
+ s.add_dependency "kitchen", "~> 0.0.3"
21
+ s.add_dependency "json", "~> 1.8.3"
22
+ s.add_dependency "ohai", "~> 8.8.1"
23
+ s.add_dependency "rake", "~> 10.4.2"
24
+ s.add_dependency "rspec", "~> 3.4.0"
25
+ s.add_dependency "chefspec", "~> 4.5.0"
26
+ s.add_dependency "serverspec", "~> 2.25.0"
27
+ s.add_dependency "mixlib-cli", "~> 1.5.0"
28
+ s.add_dependency "mixlib-config", "~> 2.2.1"
29
+
30
+ s.homepage =
31
+ 'http://rubygems.org/gems/cookbook_creator'
32
+ s.license = 'Free License'
33
+ end
@@ -0,0 +1,100 @@
1
+ #
2
+ # Author:: Anicheev Serghei (<sanicheev@tacitknowledge.com>)
3
+ # Copyright:: Copyright (c) 2016 Tacit Knowledge.
4
+ #
5
+
6
+ require 'mixlib/cli'
7
+
8
+ module CookbookCreator
9
+ class MCLI
10
+ include Mixlib::CLI
11
+
12
+ option :config_file,
13
+ :short => "-c config_file",
14
+ :long => "--config",
15
+ :description => "The configuration file to use",
16
+ :default => ConfigLoader.new(nil, nil).config_location
17
+
18
+ option :generator_cookbook,
19
+ :short => "-g generator_cookbook",
20
+ :long => "--generator_cookbook",
21
+ :description => "Path to generator cookbook templates",
22
+ :default => File.expand_path("../skeletons", __FILE__)
23
+
24
+ option :maintainer,
25
+ :short => "-m maintainer",
26
+ :long => "--maintainer",
27
+ :description => "Maintaining organisation",
28
+ :default => 'Example Org.'
29
+
30
+ option :maintainer_email,
31
+ :short => "-e maintainer_email",
32
+ :long => "--maintainer_email",
33
+ :description => "Maintainers Email",
34
+ :default => 'operator@example.org'
35
+
36
+ option :provisioner,
37
+ :short => "-p provisioner",
38
+ :long => "--provisioner",
39
+ :description => "Provisioner to use",
40
+ :default => 'chef_zero'
41
+
42
+ option :license,
43
+ :short => "-l license",
44
+ :long => "--license",
45
+ :description => "License to use",
46
+ :default => 'all_rights'
47
+
48
+ option :platform,
49
+ :short => "-P platform",
50
+ :long => "--platform",
51
+ :description => "Platform to use",
52
+ :default => 'centos-7.1'
53
+
54
+ option :driver,
55
+ :short => "-d driver",
56
+ :long => "--driver",
57
+ :description => "Driver to use",
58
+ :default => 'docker'
59
+
60
+ option :image,
61
+ :short => "-i driver",
62
+ :long => "--image",
63
+ :description => "Docker image to use",
64
+ :default => 'centos-7.1'
65
+
66
+ option :supermarket_url,
67
+ :short => "-s supermarket_url",
68
+ :long => "--supermarket_url",
69
+ :description => "Supermarket URL",
70
+ :default => 'https://supermarket.chef.io'
71
+
72
+ option :lwrp,
73
+ :short => "-L",
74
+ :long => "--lwrp",
75
+ :description => "Should cookbook contain LWRP?",
76
+ :boolean => true,
77
+ :default => false
78
+
79
+ option :lib,
80
+ :short => "-i",
81
+ :long => "--lib",
82
+ :description => "Should cookbook contain lib?",
83
+ :boolean => true,
84
+ :default => false
85
+
86
+ option :help,
87
+ :short => "-h",
88
+ :long => "--help",
89
+ :description => "Show this message",
90
+ :on => :tail,
91
+ :boolean => true,
92
+ :show_options => true,
93
+ :exit => 0
94
+
95
+ def run(argv=[])
96
+ parse_options(argv)
97
+ end
98
+
99
+ end
100
+ end
@@ -0,0 +1,24 @@
1
+ #
2
+ # Author:: Anicheev Serghei (<sanicheev@tacitknowledge.com>)
3
+ # Copyright:: Copyright (c) 2016 Tacit Knowledge.
4
+ #
5
+
6
+ require 'mixlib/config'
7
+
8
+ module CookbookCreator
9
+ class Config
10
+ extend Mixlib::Config
11
+ config_strict_mode true
12
+ configurable :maintainer
13
+ configurable :maintainer_email
14
+ configurable :provisioner
15
+ configurable :license
16
+ configurable :platform
17
+ configurable :driver
18
+ configurable :supermarket_url
19
+ configurable :lwrp
20
+ configurable :lib
21
+ configurable :generator_cookbook
22
+ configurable :image
23
+ end
24
+ end
@@ -0,0 +1,157 @@
1
+ #
2
+ # Author:: Anicheev Serghei (<sanicheev@tacitknowledge.com>)
3
+ # Copyright:: Copyright (c) 2016 Tacit Knowledge.
4
+ #
5
+ # Initially this code block was developed by Chef community.
6
+ # But in order to fit for our needs it was modified
7
+ #
8
+
9
+
10
+ require 'cookbook_creator/null_logger'
11
+ require 'cookbook_creator/pathhelper'
12
+ require 'pathname'
13
+
14
+ module CookbookCreator
15
+ class ConfigLoader
16
+
17
+ # Path to a config file requested by user, (e.g., via command line option). Can be nil
18
+ attr_accessor :explicit_config_file
19
+
20
+ def initialize(explicit_config_file, logger=nil)
21
+ @explicit_config_file = explicit_config_file
22
+ @generator_config_dir = nil
23
+ @config_location = nil
24
+ @logger = logger || NullLogger.new
25
+ end
26
+
27
+ def no_config_found?
28
+ config_location.nil?
29
+ end
30
+
31
+ def config_location
32
+ @config_location ||= (explicit_config_file || locate_local_config)
33
+ end
34
+
35
+ def generator_config_dir
36
+ if @generator_config_dir.nil?
37
+ @generator_config_dir = false
38
+ full_path = working_directory.split(File::SEPARATOR)
39
+ (full_path.length - 1).downto(0) do |i|
40
+ candidate_directory = File.join(full_path[0..i] + [".generator"])
41
+ if File.exist?(candidate_directory) && File.directory?(candidate_directory)
42
+ @generator_config_dir = candidate_directory
43
+ break
44
+ end
45
+ end
46
+ end
47
+ @generator_config_dir
48
+ end
49
+
50
+ def load
51
+ # Ignore it if there's no explicit_config_file and can't find one at a
52
+ # default path.
53
+ return false if config_location.nil?
54
+
55
+ if explicit_config_file && !path_exists?(config_location)
56
+ raise "Specified config file #{config_location} does not exist"
57
+ end
58
+
59
+ # Have to set Config.config_file b/c other config is derived from it.
60
+ Config.config_file = config_location
61
+ read_config(IO.read(config_location), config_location)
62
+ end
63
+
64
+ # (Private API, public for test purposes)
65
+ def env
66
+ ENV
67
+ end
68
+
69
+ # (Private API, public for test purposes)
70
+ def path_exists?(path)
71
+ Pathname.new(path).expand_path.exist?
72
+ end
73
+
74
+ private
75
+
76
+ def have_config?(path)
77
+ if path_exists?(path)
78
+ logger.info("Using config at #{path}")
79
+ true
80
+ else
81
+ logger.debug("Config not found at #{path}, trying next option")
82
+ false
83
+ end
84
+ end
85
+
86
+ def locate_local_config
87
+ candidate_configs = []
88
+
89
+ # Look for $ARROW_HOME/generator.rb (allow multiple generator config on same machine)
90
+ if env['ARROW_HOME']
91
+ candidate_configs << File.join(env['ARROW_HOME'], 'generator.rb')
92
+ end
93
+ # Look for $PWD/generator.rb
94
+ if Dir.pwd
95
+ candidate_configs << File.join(Dir.pwd, 'generator.rb')
96
+ end
97
+ # Look for $UPWARD/.generator/generator.rb
98
+ if generator_config_dir
99
+ candidate_configs << File.join(generator_config_dir, 'generator.rb')
100
+ end
101
+ # Look for $HOME/.generator/generator.rb
102
+ PathHelper.home('.generator') do |dot_generator_dir|
103
+ candidate_configs << File.join(dot_generator_dir, 'generator.rb')
104
+ end
105
+
106
+ candidate_configs.find do | candidate_config |
107
+ have_config?(candidate_config)
108
+ end
109
+ end
110
+
111
+ def working_directory
112
+ Dir.pwd
113
+ end
114
+
115
+ def read_config(config_content, config_file_path)
116
+ Config.from_string(config_content, config_file_path)
117
+ rescue SignalException
118
+ raise
119
+ rescue SyntaxError => e
120
+ message = ""
121
+ message << "You have invalid ruby syntax in your config file #{config_file_path}\n\n"
122
+ message << "#{e.class.name}: #{e.message}\n"
123
+ if file_line = e.message[/#{Regexp.escape(config_file_path)}:[\d]+/]
124
+ line = file_line[/:([\d]+)$/, 1].to_i
125
+ message << highlight_config_error(config_file_path, line)
126
+ end
127
+ raise ChefConfig::ConfigurationError, message
128
+ rescue Exception => e
129
+ message = "You have an error in your config file #{config_file_path}\n\n"
130
+ message << "#{e.class.name}: #{e.message}\n"
131
+ filtered_trace = e.backtrace.grep(/#{Regexp.escape(config_file_path)}/)
132
+ filtered_trace.each {|bt_line| message << " " << bt_line << "\n" }
133
+ if !filtered_trace.empty?
134
+ line_nr = filtered_trace.first[/#{Regexp.escape(config_file_path)}:([\d]+)/, 1]
135
+ message << highlight_config_error(config_file_path, line_nr.to_i)
136
+ end
137
+ raise ChefConfig::ConfigurationError, message
138
+ end
139
+
140
+
141
+ def highlight_config_error(file, line)
142
+ config_file_lines = []
143
+ IO.readlines(file).each_with_index {|l, i| config_file_lines << "#{(i + 1).to_s.rjust(3)}: #{l.chomp}"}
144
+ if line == 1
145
+ lines = config_file_lines[0..3]
146
+ else
147
+ lines = config_file_lines[Range.new(line - 2, line)]
148
+ end
149
+ "Relevant file content:\n" + lines.join("\n") + "\n"
150
+ end
151
+
152
+ def logger
153
+ @logger
154
+ end
155
+
156
+ end
157
+ end
@@ -0,0 +1,202 @@
1
+ #
2
+ # Author:: Anicheev Serghei (<sanicheev@tacitknowledge.com>)
3
+ # Copyright:: Copyright (c) 2016 Tacit Knowledge.
4
+ #
5
+ # Initially this code block was developed by Chef community.
6
+ # But in order to fit for our needs it was modified
7
+ #
8
+
9
+ module CookbookCreator
10
+
11
+ module Generator
12
+ # This is here to hold attr_accessor data for Generator context variables
13
+ class Context
14
+
15
+
16
+ def self.add_attr(name)
17
+ @attributes ||= [ ]
18
+
19
+ if !@attributes.include?(name)
20
+ @attributes << name
21
+ attr_accessor(name)
22
+ end
23
+ end
24
+
25
+ def self.cookbook_root
26
+ File.dirname(cookbook_full_path)
27
+ end
28
+
29
+ def self.cookbook_full_path
30
+ File.expand_path(cookbook_name_or_path, Dir.pwd)
31
+ end
32
+
33
+ def self.cookbook_name_or_path
34
+ name = ARGV[0]
35
+ File.expand_path(name, Dir.pwd)
36
+ end
37
+
38
+ def self.recipe
39
+ "cookbook"
40
+ end
41
+
42
+ def self.recipe_name
43
+ "default"
44
+ end
45
+
46
+ def self.cookbook_name
47
+ File.basename(cookbook_full_path)
48
+ end
49
+
50
+ def self.have_git?
51
+ path = ENV["PATH"] || ""
52
+ paths = path.split(File::PATH_SEPARATOR)
53
+ paths.any? {|bin_path| File.exist?(File.join(bin_path, "git#{RbConfig::CONFIG['EXEEXT']}"))}
54
+ end
55
+
56
+ def self.reset
57
+ return if @attributes.nil?
58
+
59
+ @attributes.each do |attr|
60
+ remove_method(attr)
61
+ end
62
+
63
+ @attributes = nil
64
+ end
65
+ end
66
+
67
+ def self.reset
68
+ @context = nil
69
+ end
70
+
71
+ def self.context
72
+ @context ||= Context.new
73
+ end
74
+
75
+ def self.add_attr_to_context(name, value=nil)
76
+ sym_name = name.to_sym
77
+ CookbookCreator::Generator::Context.add_attr(sym_name)
78
+ CookbookCreator::Generator::TemplateHelper.delegate_to_app_context(sym_name)
79
+ context.public_send("#{sym_name}=", value)
80
+ end
81
+
82
+ def self.setup_context(config_options)
83
+ CookbookCreator::Generator.add_attr_to_context(:cookbook_root, CookbookCreator::Generator::Context.cookbook_root)
84
+ CookbookCreator::Generator.add_attr_to_context(:cookbook_name, CookbookCreator::Generator::Context.cookbook_name)
85
+ CookbookCreator::Generator.add_attr_to_context(:recipe_name, CookbookCreator::Generator::Context.recipe_name)
86
+ CookbookCreator::Generator.add_attr_to_context(:copyright_holder, config_options[:maintainer])
87
+ CookbookCreator::Generator.add_attr_to_context(:email, config_options[:maintainer_email])
88
+ CookbookCreator::Generator.add_attr_to_context(:license, config_options[:license])
89
+ CookbookCreator::Generator.add_attr_to_context(:provisioner, config_options[:provisioner])
90
+ CookbookCreator::Generator.add_attr_to_context(:driver, config_options[:driver])
91
+ CookbookCreator::Generator.add_attr_to_context(:platform, config_options[:platform])
92
+ CookbookCreator::Generator.add_attr_to_context(:supermarket_url, config_options[:supermarket_url])
93
+ CookbookCreator::Generator.add_attr_to_context(:lwrp, config_options[:lwrp])
94
+ CookbookCreator::Generator.add_attr_to_context(:lib, config_options[:lib])
95
+ CookbookCreator::Generator.add_attr_to_context(:image, config_options[:image])
96
+ end
97
+
98
+ module TemplateHelper
99
+
100
+ def self.delegate_to_app_context(name)
101
+ define_method(name) do
102
+ CookbookCreator::Generator.context.public_send(name)
103
+ end
104
+ end
105
+
106
+ def year
107
+ Time.now.year
108
+ end
109
+
110
+ # Prints the short description of the license, suitable for use in a
111
+ # preamble to a file. Optionally specify a comment to prepend to each line.
112
+ def license_description(comment=nil)
113
+ case license
114
+ when 'all_rights'
115
+ result = "Copyright (c) #{year} #{copyright_holder}, All Rights Reserved."
116
+ when 'apache2'
117
+ result = <<-EOH
118
+ Copyright #{year} #{copyright_holder}
119
+
120
+ Licensed under the Apache License, Version 2.0 (the "License");
121
+ you may not use this file except in compliance with the License.
122
+ You may obtain a copy of the License at
123
+
124
+ http://www.apache.org/licenses/LICENSE-2.0
125
+
126
+ Unless required by applicable law or agreed to in writing, software
127
+ distributed under the License is distributed on an "AS IS" BASIS,
128
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
129
+ See the License for the specific language governing permissions and
130
+ limitations under the License.
131
+ EOH
132
+ when 'mit'
133
+ result = <<-EOH
134
+ The MIT License (MIT)
135
+
136
+ Copyright (c) #{year} #{copyright_holder}
137
+
138
+ Permission is hereby granted, free of charge, to any person obtaining a copy
139
+ of this software and associated documentation files (the "Software"), to deal
140
+ in the Software without restriction, including without limitation the rights
141
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
142
+ copies of the Software, and to permit persons to whom the Software is
143
+ furnished to do so, subject to the following conditions:
144
+
145
+ The above copyright notice and this permission notice shall be included in
146
+ all copies or substantial portions of the Software.
147
+
148
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
149
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
150
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
151
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
152
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
153
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
154
+ THE SOFTWARE.
155
+ EOH
156
+ when 'gplv2'
157
+ result = <<-EOH
158
+ Copyright (C) #{year} #{copyright_holder}
159
+
160
+ This program is free software; you can redistribute it and/or modify
161
+ it under the terms of the GNU General Public License as published by
162
+ the Free Software Foundation; either version 2 of the License, or
163
+ (at your option) any later version.
164
+
165
+ This program is distributed in the hope that it will be useful,
166
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
167
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
168
+ GNU General Public License for more details.
169
+
170
+ You should have received a copy of the GNU General Public License along
171
+ with this program; if not, write to the Free Software Foundation, Inc.,
172
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
173
+ EOH
174
+ when 'gplv3'
175
+ result = <<-EOH
176
+ Copyright (C) #{year} #{copyright_holder}
177
+
178
+ This program is free software: you can redistribute it and/or modify
179
+ it under the terms of the GNU General Public License as published by
180
+ the Free Software Foundation, either version 3 of the License, or
181
+ (at your option) any later version.
182
+
183
+ This program is distributed in the hope that it will be useful,
184
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
185
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
186
+ GNU General Public License for more details.
187
+
188
+ You should have received a copy of the GNU General Public License
189
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
190
+ EOH
191
+ end
192
+ if comment
193
+ # Ensure there's no trailing whitespace
194
+ result.gsub(/^(.+)$/, "#{comment} \\1").gsub(/^$/, "#{comment}")
195
+ else
196
+ result
197
+ end
198
+ end
199
+ end
200
+
201
+ end
202
+ end