Module: ReleaseHx::WriteOps
- Defined in:
- lib/releasehx/ops/write_ops.rb
Overview
Provides file writing operations and template processing utilities for output generation.
The WriteOps module handles safe file creation with directory management, template resolution
with user/gem fallback paths, and content post-processing for consistent output formatting.
Class Method Summary collapse
-
.append_changes_to_yaml(yaml_file_path, new_changes, config) ⇒ void
Appends new changes to an existing YAML file using template-based formatting.
-
.gem_template_root ⇒ String
Establishes the absolute path to the gem's bundled template directory.
-
.process_template(template_path, vars, config) ⇒ String
Processes a Liquid template file with variable substitution and content post-processing.
-
.resolve_template_path(name, config) ⇒ String
Resolves template file path using hierarchical search with user directory precedence.
-
.safe_write(path, content) ⇒ Symbol
Safely writes content to a file, creating parent directories as needed.
Class Method Details
.append_changes_to_yaml(yaml_file_path, new_changes, config) ⇒ void
This method returns an undefined value.
Appends new changes to an existing YAML file using template-based formatting. Generates properly formatted YAML content for new changes using a template, then appends it to the specified file while maintaining file structure.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/releasehx/ops/write_ops.rb', line 106 def self.append_changes_to_yaml yaml_file_path, new_changes, config # Generate properly formatted YAML content using template context = { 'changes' => new_changes.map(&:to_h), 'config' => config } template_path = resolve_template_path('rhyml-change-append.yaml.liquid', config) append_content = process_template(template_path, { 'vars' => context }, config) # Append to existing file maintaining structure File.open(yaml_file_path, 'a') do |file| file.write(append_content) end ReleaseHx.logger.debug "Appended #{new_changes.size} changes to #{yaml_file_path}" end |
.gem_template_root ⇒ String
Establishes the absolute path to the gem's bundled template directory.
32 33 34 |
# File 'lib/releasehx/ops/write_ops.rb', line 32 def self.gem_template_root File.('../rhyml/templates', __dir__) end |
.process_template(template_path, vars, config) ⇒ String
Processes a Liquid template file with variable substitution and content post-processing.
Loads and renders templates using the TemplateOps system, then applies configurable
post-processing rules such as excess line removal for consistent output formatting.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/releasehx/ops/write_ops.rb', line 69 def self.process_template template_path, vars, config template_content = File.read(template_path) includes_load_paths = [] user_templates_dir = config.dig('paths', 'templates_dir') gem_templates_dir = File.('../../rhyml/templates', __dir__) if user_templates_dir unless Pathname.new(user_templates_dir).absolute? user_templates_dir = File.(user_templates_dir, Dir.pwd) end includes_load_paths << user_templates_dir end includes_load_paths << gem_templates_dir rendered = TemplateOps.render_liquid_string(template_content, vars, config) # Apply configurable post-processing for line spacing control blank_lines = config.dig('modes', 'remove_excess_lines') if blank_lines.zero? rendered = rendered.gsub(/\n{2,}/, "\n") elsif blank_lines rendered = rendered.gsub(/\n{#{blank_lines + 1},}/, "\n" * (blank_lines + 1)) end rendered end |
.resolve_template_path(name, config) ⇒ String
Resolves template file path using hierarchical search with user directory precedence.
Searches for templates first in user-configured directories, then falls back to
gem-bundled templates, providing clear error messages when templates are not found.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/releasehx/ops/write_ops.rb', line 45 def self.resolve_template_path name, config user_dir = config.dig('paths', 'templates_dir') fallback_dir = gem_template_root search_paths = [] search_paths << File.(name, user_dir) if user_dir search_paths << File.join(fallback_dir, name) found = search_paths.find { |p| File.exist?(p) } return found if found raise "Template not found: #{name} (searched #{search_paths.join(' , ')})" end |
.safe_write(path, content) ⇒ Symbol
Safely writes content to a file, creating parent directories as needed.
Ensures parent directory structure exists before writing and provides
logging feedback for successful file creation operations.
21 22 23 24 25 26 27 |
# File 'lib/releasehx/ops/write_ops.rb', line 21 def self.safe_write path, content dirname = File.dirname(path) FileUtils.mkdir_p(dirname) File.write(path, content) ReleaseHx.logger.info "Wrote file: #{path}" :written end |