pdk 1.17.0 → 2.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.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +148 -11
  3. data/README.md +1 -1
  4. data/lib/pdk.rb +1 -1
  5. data/lib/pdk/cli.rb +7 -1
  6. data/lib/pdk/cli/convert.rb +7 -9
  7. data/lib/pdk/cli/env.rb +52 -0
  8. data/lib/pdk/cli/exec/command.rb +11 -1
  9. data/lib/pdk/cli/new.rb +2 -0
  10. data/lib/pdk/cli/new/class.rb +2 -1
  11. data/lib/pdk/cli/new/defined_type.rb +2 -1
  12. data/lib/pdk/cli/new/fact.rb +29 -0
  13. data/lib/pdk/cli/new/function.rb +29 -0
  14. data/lib/pdk/cli/new/provider.rb +2 -1
  15. data/lib/pdk/cli/new/task.rb +2 -1
  16. data/lib/pdk/cli/new/test.rb +2 -1
  17. data/lib/pdk/cli/new/transport.rb +2 -1
  18. data/lib/pdk/cli/release.rb +1 -1
  19. data/lib/pdk/cli/release/publish.rb +11 -1
  20. data/lib/pdk/cli/remove.rb +20 -0
  21. data/lib/pdk/cli/remove/config.rb +80 -0
  22. data/lib/pdk/cli/set.rb +20 -0
  23. data/lib/pdk/cli/set/config.rb +119 -0
  24. data/lib/pdk/cli/update.rb +6 -8
  25. data/lib/pdk/cli/util.rb +1 -0
  26. data/lib/pdk/cli/util/option_validator.rb +6 -0
  27. data/lib/pdk/cli/util/update_manager_printer.rb +82 -0
  28. data/lib/pdk/config.rb +96 -13
  29. data/lib/pdk/context.rb +8 -5
  30. data/lib/pdk/generate/defined_type.rb +25 -32
  31. data/lib/pdk/generate/fact.rb +25 -0
  32. data/lib/pdk/generate/function.rb +48 -0
  33. data/lib/pdk/generate/module.rb +11 -10
  34. data/lib/pdk/generate/provider.rb +15 -64
  35. data/lib/pdk/generate/puppet_class.rb +25 -31
  36. data/lib/pdk/generate/puppet_object.rb +83 -187
  37. data/lib/pdk/generate/task.rb +28 -46
  38. data/lib/pdk/generate/transport.rb +20 -74
  39. data/lib/pdk/module.rb +1 -1
  40. data/lib/pdk/module/convert.rb +43 -23
  41. data/lib/pdk/module/metadata.rb +6 -2
  42. data/lib/pdk/module/release.rb +8 -2
  43. data/lib/pdk/module/update.rb +7 -11
  44. data/lib/pdk/module/update_manager.rb +7 -0
  45. data/lib/pdk/report.rb +3 -3
  46. data/lib/pdk/report/event.rb +8 -2
  47. data/lib/pdk/template.rb +59 -0
  48. data/lib/pdk/template/fetcher.rb +98 -0
  49. data/lib/pdk/template/fetcher/git.rb +85 -0
  50. data/lib/pdk/template/fetcher/local.rb +28 -0
  51. data/lib/pdk/template/renderer.rb +96 -0
  52. data/lib/pdk/template/renderer/v1.rb +25 -0
  53. data/lib/pdk/template/renderer/v1/legacy_template_dir.rb +116 -0
  54. data/lib/pdk/template/renderer/v1/renderer.rb +132 -0
  55. data/lib/pdk/template/renderer/v1/template_file.rb +102 -0
  56. data/lib/pdk/template/template_dir.rb +67 -0
  57. data/lib/pdk/tests/unit.rb +8 -1
  58. data/lib/pdk/util.rb +4 -35
  59. data/lib/pdk/util/bundler.rb +1 -1
  60. data/lib/pdk/util/changelog_generator.rb +20 -3
  61. data/lib/pdk/util/json_finder.rb +85 -0
  62. data/lib/pdk/util/puppet_strings.rb +3 -3
  63. data/lib/pdk/util/puppet_version.rb +2 -2
  64. data/lib/pdk/util/ruby_version.rb +5 -1
  65. data/lib/pdk/util/template_uri.rb +9 -11
  66. data/lib/pdk/util/vendored_file.rb +1 -2
  67. data/lib/pdk/validate.rb +17 -10
  68. data/lib/pdk/validate/control_repo/control_repo_validator_group.rb +23 -0
  69. data/lib/pdk/validate/control_repo/environment_conf_validator.rb +98 -0
  70. data/lib/pdk/validate/invokable_validator.rb +8 -4
  71. data/lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb +1 -1
  72. data/lib/pdk/validate/validator.rb +7 -0
  73. data/lib/pdk/validate/validator_group.rb +1 -0
  74. data/lib/pdk/validate/yaml/yaml_syntax_validator.rb +2 -2
  75. data/lib/pdk/version.rb +1 -1
  76. data/locales/pdk.pot +356 -228
  77. metadata +65 -28
  78. data/lib/pdk/module/template_dir.rb +0 -115
  79. data/lib/pdk/module/template_dir/base.rb +0 -268
  80. data/lib/pdk/module/template_dir/git.rb +0 -91
  81. data/lib/pdk/module/template_dir/local.rb +0 -21
  82. data/lib/pdk/template_file.rb +0 -96
data/lib/pdk/report.rb CHANGED
@@ -104,7 +104,7 @@ module PDK
104
104
  if event.rspec_puppet_coverage?
105
105
  coverage_report = event.to_text
106
106
  else
107
- report << event.to_text unless event.pass?
107
+ report << event.to_text unless event.pass? || event.skipped?
108
108
  end
109
109
  end
110
110
  end
@@ -113,8 +113,8 @@ module PDK
113
113
 
114
114
  if target.is_a?(String)
115
115
  PDK::Util::Filesystem.write_file(target, report.join("\n"))
116
- else
117
- target << report.join("\n")
116
+ elsif !report.empty?
117
+ target << report.join("\n") << "\n"
118
118
  end
119
119
  end
120
120
  end
@@ -111,8 +111,8 @@ module PDK
111
111
  location = nil if location.empty?
112
112
 
113
113
  # TODO: maybe add trace
114
- header = [severity, source, location, message].compact.join(': ')
115
114
  if source == 'rspec'
115
+ header = [severity, source, location, message].compact.join(': ')
116
116
  result = [header, " #{test}"]
117
117
  context = context_lines
118
118
  unless context.nil?
@@ -123,7 +123,13 @@ module PDK
123
123
 
124
124
  result.compact.join("\n")
125
125
  else
126
- header
126
+ output = ['pdk']
127
+ output << "(#{severity.upcase}):" unless severity.nil?
128
+ output << "#{source}:" unless source.nil?
129
+ output << message unless message.nil?
130
+ output << "(#{location})" unless location.nil?
131
+
132
+ output.join(' ')
127
133
  end
128
134
  end
129
135
 
@@ -0,0 +1,59 @@
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module Template
5
+ autoload :Fetcher, 'pdk/template/fetcher'
6
+ autoload :Renderer, 'pdk/template/renderer'
7
+ autoload :TemplateDir, 'pdk/template/template_dir'
8
+
9
+ MODULE_TEMPLATE_TYPE = :module_template
10
+
11
+ # Creates a TemplateDir object with the path or URL to the template
12
+ # and the block of code to run to be run while the template is available.
13
+ #
14
+ # The template directory is only guaranteed to be available on disk
15
+ # within the scope of the block passed to this method.
16
+ #
17
+ # @param uri [PDK::Util::TemplateURI] The path to a directory to use as the
18
+ # template or a URI to a git repository.
19
+ #
20
+ # @param context [PDK::Context::AbstractContext] The context in which the template will render to
21
+ #
22
+ # @yieldparam self [PDK::Template::TemplateDir] The initialised object with
23
+ # the template available on disk.
24
+ #
25
+ # @example Using a git repository as a template
26
+ # PDK::Template.with('https://github.com/puppetlabs/pdk-templates') do |t|
27
+ # t.render_module('module, PDK.context) do |filename, content, status|
28
+ # File.open(filename, 'w') do |file|
29
+ # ...
30
+ # end
31
+ # end
32
+ # end
33
+ #
34
+ # @raise [ArgumentError] If no block is given to this method.
35
+ # @raise [PDK::CLI::FatalError]
36
+ # @raise [ArgumentError]
37
+ #
38
+ # @api public
39
+ def self.with(uri, context)
40
+ unless block_given?
41
+ raise ArgumentError, _('%{class_name}.with must be passed a block.') % { class_name: name }
42
+ end
43
+ unless uri.is_a? PDK::Util::TemplateURI
44
+ raise ArgumentError, _('%{class_name}.with must be passed a PDK::Util::TemplateURI, got a %{uri_type}') % { uri_type: uri.class, class_name: name }
45
+ end
46
+
47
+ Fetcher.with(uri) do |fetcher|
48
+ template_dir = TemplateDir.instance(uri, fetcher.path, context)
49
+ template_dir.metadata = fetcher.metadata
50
+
51
+ template_type = uri.default? ? 'default' : 'custom'
52
+ PDK.analytics.event('TemplateDir', 'initialize', label: template_type)
53
+
54
+ yield template_dir
55
+ end
56
+ nil
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,98 @@
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module Template
5
+ module Fetcher
6
+ autoload :Git, 'pdk/template/fetcher/git'
7
+ autoload :Local, 'pdk/template/fetcher/local'
8
+
9
+ # Returns a Template Fetcher implementation for the given Template URI
10
+ # @param uri [PDK::Util::TemplateURI] The URI of the template to fetch
11
+ # @param options [Hash{Object => Object}] A list of options to pass through to the fetcher.
12
+ #
13
+ # @return [PDK::Template::Fetcher::AbstractTemplateFetcher] An instance of a class which implements the AbstractFetcher class
14
+ def self.instance(uri, options = {})
15
+ return Git.new(uri, options) if Git.fetchable?(uri, options)
16
+ Local.new(uri, options)
17
+ end
18
+
19
+ # Creates an instance of a PDK::Template::Fetcher::AbstractTemplateFetcher object with the path or URL to the template
20
+ # and the block of code to run to be run while the template is fetched.
21
+ #
22
+ # The fetched directory is only guaranteed to be available on disk
23
+ # within the scope of the block passed to this method.
24
+ #
25
+ # @param uri [PDK::Util::TemplateURI] The URI of the template to fetch.
26
+ # @param options [Hash{Object => Object}] A list of options to pass through to the fetcher.
27
+ #
28
+ # @yieldparam fetcher [PDK::Template::Fetcher::AbstractTemplateFetcher] The initialised fetcher with
29
+ # the template already fetched
30
+ #
31
+ # @example Using a git repository as a template
32
+ # PDK::Template::Fetcher.with('https://github.com/puppetlabs/pdk-templates') do |fetcher|
33
+ # end
34
+ #
35
+ # @raise [ArgumentError] If no block is given to this method.
36
+ # @return [void]
37
+ def self.with(uri, options = {})
38
+ raise ArgumentError, _('%{class_name}.with must be passed a block.') % { class_name: name } unless block_given?
39
+ fetcher = instance(uri, options)
40
+
41
+ begin
42
+ fetcher.fetch!
43
+ yield fetcher
44
+ ensure
45
+ # If the the path is temporary, clean it up
46
+ PDK::Util::Filesystem.rm_rf(fetcher.path) if fetcher.temporary
47
+ end
48
+ nil
49
+ end
50
+
51
+ # An abstract class which all Template Fetchers should subclass. This class is responsible for
52
+ # downloading or copying a Template Directory that is pointed to by a Template URI
53
+ #
54
+ # @api private
55
+ # @abstract
56
+ class AbstractFetcher
57
+ # @return [PDK::Util::TemplateURI] The URI of the template that is to be fetched
58
+ attr_reader :uri
59
+
60
+ # @return [String] The local filesystem path of the fetched template
61
+ attr_reader :path
62
+
63
+ # @return [Boolean] Whether the fetched path should be considered temporary and be deleted after use
64
+ attr_reader :temporary
65
+
66
+ # @return [Boolean] Whether the template has been fetched yet
67
+ attr_reader :fetched
68
+
69
+ # @return [Hash] The metadata hash for this template.
70
+ attr_reader :metadata
71
+
72
+ # @param uri [PDK::Util::TemplateURI] The URI of the template to fetch
73
+ # @param options [Hash{Object => Object}] A list of options to pass through to the fetcher.
74
+ def initialize(uri, options)
75
+ @uri = uri
76
+ # Defaults
77
+ @path = nil
78
+ @metadata = {
79
+ 'pdk-version' => PDK::Util::Version.version_string,
80
+ 'template-url' => nil,
81
+ 'template-ref' => nil,
82
+ }
83
+ @fetched = false
84
+ @temporary = false
85
+ @options = options
86
+ end
87
+
88
+ # Fetches the template directory and populates the path property
89
+ #
90
+ # @return [void]
91
+ # @abstract
92
+ def fetch!
93
+ @fetched = true
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,85 @@
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module Template
5
+ module Fetcher
6
+ class Git < PDK::Template::Fetcher::AbstractFetcher
7
+ # Whether the passed uri is fetchable by Git.
8
+ # @see PDK::Template::Fetcher.instance
9
+ # @return [Boolean]
10
+ def self.fetchable?(uri, _options = {})
11
+ PDK::Util::Git.repo?(uri.bare_uri)
12
+ end
13
+
14
+ # @see PDK::Template::Fetcher::AbstractTemplateFetcher.fetch!
15
+ def fetch!
16
+ return if fetched
17
+ super
18
+
19
+ # Default metadata for all git fetching methods
20
+ @metadata['template-url'] = uri.metadata_format
21
+
22
+ # We don't do a checkout of local-path repos. There are lots of edge
23
+ # cases or user un-expectations.
24
+ if PDK::Util::Git.work_tree?(uri.shell_path)
25
+ PDK.logger.warn _("Repository '%{repo}' has a work-tree; skipping git reset.") % {
26
+ repo: uri.shell_path,
27
+ }
28
+ @path = uri.shell_path
29
+ @temporary = false
30
+ @metadata['template-ref'] = describe_path_and_ref(@path)
31
+ return
32
+ end
33
+
34
+ # This is either a bare local repo or a remote. either way it needs cloning.
35
+ # A "remote" can also be git repo on the local filsystem.
36
+ # @todo When switching this over to using rugged, cache the cloned
37
+ # template repo in `%AppData%` or `$XDG_CACHE_DIR` and update before
38
+ # use.
39
+ require 'pdk/util'
40
+ require 'pdk/util/git'
41
+
42
+ temp_dir = PDK::Util.make_tmpdir_name('pdk-templates')
43
+ @temporary = true
44
+ origin_repo = uri.bare_uri
45
+ git_ref = uri.uri_fragment
46
+
47
+ # Clone the repository
48
+ clone_result = PDK::Util::Git.git('clone', origin_repo, temp_dir)
49
+ unless clone_result[:exit_code].zero?
50
+ PDK.logger.error clone_result[:stdout]
51
+ PDK.logger.error clone_result[:stderr]
52
+ raise PDK::CLI::FatalError, _("Unable to clone git repository at '%{repo}' into '%{dest}'.") % { repo: origin_repo, dest: temp_dir }
53
+ end
54
+ @path = PDK::Util.canonical_path(temp_dir)
55
+
56
+ # Checkout the git reference
57
+ if PDK::Util::Git.work_dir_clean?(temp_dir)
58
+ Dir.chdir(temp_dir) do
59
+ full_ref = PDK::Util::Git.ls_remote(temp_dir, git_ref)
60
+ @metadata['template-ref'] = describe_path_and_ref(temp_dir, full_ref)
61
+ reset_result = PDK::Util::Git.git('reset', '--hard', full_ref)
62
+ return if reset_result[:exit_code].zero?
63
+
64
+ PDK.logger.error reset_result[:stdout]
65
+ PDK.logger.error reset_result[:stderr]
66
+ raise PDK::CLI::FatalError, _("Unable to checkout '%{ref}' of git repository at '%{path}'.") % { ref: git_ref, path: temp_dir }
67
+ end
68
+ else
69
+ PDK.logger.warn _("Uncommitted changes found when attempting to checkout '%{ref}' of git repository at '%{path}'; skipping git reset.") % { ref: git_ref, path: temp_dir }
70
+ @metadata['template-ref'] = describe_path_and_ref(temp_dir)
71
+ end
72
+ end
73
+
74
+ private
75
+
76
+ #:nocov: This is a just a wrapper for another method
77
+ def describe_path_and_ref(path, ref = nil)
78
+ require 'pdk/util/git'
79
+ PDK::Util::Git.describe(File.join(path, '.git'), ref)
80
+ end
81
+ #:nocov:
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,28 @@
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module Template
5
+ module Fetcher
6
+ class Local < PDK::Template::Fetcher::AbstractFetcher
7
+ # Whether the passed uri is fetchable. This is a catch-all and all URIs
8
+ # are considered on-disk already.
9
+ #
10
+ # @see PDK::Template::Fetcher.instance
11
+ # @return [Boolean]
12
+ def self.fetchable?(_uri, _options = {})
13
+ true
14
+ end
15
+
16
+ # @see PDK::Template::Fetcher::AbstractTemplateFetcher.fetch!
17
+ def fetch!
18
+ return if fetched
19
+ super
20
+
21
+ @path = uri.shell_path
22
+ @temporary = false
23
+ @metadata['template-url'] = uri.bare_uri
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,96 @@
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module Template
5
+ module Renderer
6
+ autoload :V1, 'pdk/template/renderer/v1'
7
+
8
+ # Returns the most appropriate renderer for the given Template Directory and Context
9
+ #
10
+ # @param template_root [String] The path to where the template exists on disk
11
+ # @param template_uri [PDK::Util::TemplateUri] A URI which points to the source location of the Template
12
+ # @param context [PDK::Context] The context in which the redering will occur in
13
+ #
14
+ # @return [AbstractRenderer, nil] An instance of an AbstractRenderer subclass. Returns nil if no renderer could be found
15
+ def self.instance(template_uri, template_root, context)
16
+ return V1.instance(template_root, template_uri, context) if V1.compatible?(template_root, context)
17
+ nil
18
+ end
19
+
20
+ # An abstract class which all Template Renderers should subclass and implement. This class is responsible for
21
+ # rendering a template or a single item within a template directory
22
+ #
23
+ # To implement a new renderer:
24
+ # 1. Create a new class which subclasses AbstractRenderer and implements the public methods (has_single_item?, render and render_single_item)
25
+ # 2. Add class methods .compatible? and .instance which are used to detect if a template is compatible with the new renderer
26
+ # and create an instance of the new renderer respectively
27
+ # 3. Update the PDK::Template::Renderer.instance method to detect and create an instance of the new renderer (using the .compatible? and .instance methods
28
+ # created in step 2).
29
+ #
30
+ # See the PDK::Template::Renderer::V1 module and classes for an example on how to to this.
31
+ #
32
+ # @api private
33
+ # @abstract
34
+ class AbstractRenderer
35
+ # @return [String] The path to where the template exists on disk
36
+ attr_reader :template_root
37
+
38
+ # @return [PDK::Util::TemplateURI] The URI which points to the source location of the Template
39
+ attr_reader :template_uri
40
+
41
+ # @return context [PDK::Context] The context in which the redering will occur in
42
+ attr_reader :context
43
+
44
+ # @param template_root [String] The path to where the template exists on disk
45
+ # @param template_uri [PDK::Util::TemplateUri] A URI which points to the source location of the Template
46
+ # @param context [PDK::Context] The context in which the redering will occur in
47
+ def initialize(template_root, template_uri, context)
48
+ @template_root = template_root
49
+ @template_uri = template_uri
50
+ @context = context
51
+ end
52
+
53
+ # Whether the renderer supports rendering the a single item called 'item_path'. This is used when rendering things like a new Task or a new Puppet Classes.
54
+ # Rendering a single item is different than redering an entire project, like a entire Puppet Module or Control Repo.
55
+ #
56
+ # @param item_path [String] The path to the item to check
57
+ #
58
+ # @return [Boolean] Whether the renderer can render the item
59
+ # @abstract
60
+ def has_single_item?(_item_path) # rubocop:disable Naming/PredicateName Changing the method name to `single_item?` will convey the wrong intent
61
+ false
62
+ end
63
+
64
+ # Loop through the files in the template type, yielding each rendered file to the supplied block.
65
+ #
66
+ # @param template_type [PDK::Template::*_TEMPLATE_TYPE] The type of template to render
67
+ # @param name [String] The name to use in the rendering process
68
+ # @param options [Hash{Object => Object}] A list of options to pass through to the renderer. See PDK::Template::TemplateDir helper methods for other options
69
+ # @option options [Boolean] :include_first_time Whether to include "first time" items when rendering the project. While it is up to the renderer to implement this
70
+ # the expected behavior is that if the item already exists, it will not be rendererd again. Unlike normal items which
71
+ # are always rendered to keep them in-sync
72
+ #
73
+ # @yieldparam dest_path [String] The path of the destination file, relative to the root of the context.
74
+ # @yieldparam dest_content [String] The rendered content of the destination file.
75
+ # @yieldparam dest_status [Symbol] :unmanage, :delete, :init, :manage
76
+ #
77
+ # @see PDK::Template::TemplateDir.render
78
+ #
79
+ # @return [void]
80
+ # @abstract
81
+ def render(template_type, name, options = {}); end
82
+
83
+ # Render a single item and return the resulting string. This is used when rendering things like a new Task or a new Puppet Classes.
84
+ # Rendering a single item is different than redering an entire project, like a entire Puppet Module or Control Repo. This method is
85
+ # used in conjunction with .has_single_item?
86
+ #
87
+ # @param item_path [String] The path of the single item to render
88
+ # @param template_data_hash [Hash{Object => Object}] A hash of information which will be used in the rendering process
89
+ #
90
+ # @return [String, Nil] The rendered content, or nil of the file could not be rendered
91
+ # @abstract
92
+ def render_single_item(item_path, template_data_hash = {}); end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,25 @@
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module Template
5
+ module Renderer
6
+ module V1
7
+ autoload :LegacyTemplateDir, 'pdk/template/renderer/v1/legacy_template_dir'
8
+ autoload :Renderer, 'pdk/template/renderer/v1/renderer'
9
+ autoload :TemplateFile, 'pdk/template/renderer/v1/template_file'
10
+
11
+ # Whether the template directory and context are valid for the V1 renderer
12
+ # @see PDK::Template::Renderer.instance
13
+ def self.compatible?(template_root, _context)
14
+ %w[moduleroot moduleroot_init].all? { |dir| PDK::Util::Filesystem.directory?(File.join(template_root, dir)) }
15
+ end
16
+
17
+ # Creates an instance of the V1 Renderer
18
+ # @see PDK::Template::Renderer.instance
19
+ def self.instance(template_root, template_uri, context)
20
+ PDK::Template::Renderer::V1::Renderer.new(template_root, template_uri, context)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,116 @@
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module Template
5
+ module Renderer
6
+ module V1
7
+ # The old templating code in the PDK passed through a TemplateDir object. This class mimics the methods
8
+ # of that old class so that existing custom templates will still function with the newer refactor templating code.
9
+ # Methods which have no use in custom templates exist but do no nothing, for example `def render; end`
10
+ #
11
+ # @see https://raw.githubusercontent.com/puppetlabs/pdk/4ffd58062c77ad1e54d2fe16b16015f7207bcee8/lib/pdk/module/template_dir/base.rb
12
+ # :nocov: This class is tested in the packaging and acceptance testing suites
13
+ class LegacyTemplateDir
14
+ attr_accessor :module_metadata
15
+ attr_reader :uri
16
+
17
+ def initialize(context, uri, path, module_metadata = {})
18
+ @uri = uri
19
+ @module_metadata = module_metadata
20
+ @context = context
21
+ @path = path
22
+ end
23
+
24
+ def metadata; end
25
+
26
+ def render; end
27
+
28
+ def object_template_for; end
29
+
30
+ def object_config
31
+ config_for(nil)
32
+ end
33
+
34
+ # Generate a hash of data to be used when rendering the specified
35
+ # template.
36
+ #
37
+ # @param dest_path [String] The destination path of the file that the
38
+ # data is for, relative to the root of the module.
39
+ #
40
+ # @return [Hash] The data that will be available to the template via the
41
+ # `@configs` instance variable.
42
+ #
43
+ # @api private
44
+ def config_for(dest_path, sync_config_path = nil)
45
+ require 'pdk/util'
46
+ require 'pdk/analytics'
47
+
48
+ module_root = PDK::Util.module_root
49
+ sync_config_path ||= File.join(module_root, '.sync.yml') unless module_root.nil?
50
+ config_path = File.join(@path, 'config_defaults.yml')
51
+
52
+ if @config.nil?
53
+ require 'deep_merge'
54
+ conf_defaults = read_config(config_path)
55
+ @sync_config = read_config(sync_config_path) unless sync_config_path.nil?
56
+ @config = conf_defaults
57
+ @config.deep_merge!(@sync_config, knockout_prefix: '---') unless @sync_config.nil?
58
+ end
59
+ file_config = @config.fetch(:global, {})
60
+ file_config['module_metadata'] = @module_metadata
61
+ file_config.merge!(@config.fetch(dest_path, {})) unless dest_path.nil?
62
+ file_config.merge!(@config).tap do |c|
63
+ if uri.default?
64
+ file_value = if c['unmanaged']
65
+ 'unmanaged'
66
+ elsif c['delete']
67
+ 'deleted'
68
+ elsif @sync_config && @sync_config.key?(dest_path)
69
+ 'customized'
70
+ else
71
+ 'default'
72
+ end
73
+
74
+ PDK.analytics.event('TemplateDir', 'file', label: dest_path, value: file_value)
75
+ end
76
+ end
77
+ end
78
+
79
+ # Generates a hash of data from a given yaml file location.
80
+ #
81
+ # @param loc [String] The path of the yaml config file.
82
+ #
83
+ # @warn If the specified path is not a valid yaml file. Returns an empty Hash
84
+ # if so.
85
+ #
86
+ # @return [Hash] The data that has been read in from the given yaml file.
87
+ #
88
+ # @api private
89
+ def read_config(loc)
90
+ if PDK::Util::Filesystem.file?(loc) && PDK::Util::Filesystem.readable?(loc)
91
+ require 'yaml'
92
+
93
+ begin
94
+ YAML.safe_load(PDK::Util::Filesystem.read_file(loc), [], [], true)
95
+ rescue Psych::SyntaxError => e
96
+ PDK.logger.warn _("'%{file}' is not a valid YAML file: %{problem} %{context} at line %{line} column %{column}") % {
97
+ file: loc,
98
+ problem: e.problem,
99
+ context: e.context,
100
+ line: e.line,
101
+ column: e.column,
102
+ }
103
+ {}
104
+ end
105
+ else
106
+ {}
107
+ end
108
+ end
109
+
110
+ def template_path(_uri); end
111
+ end
112
+ # :nocov:
113
+ end
114
+ end
115
+ end
116
+ end