diff options
| author | Paul Buetow <paul@buetow.org> | 2024-12-06 23:52:43 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-12-06 23:52:43 +0200 |
| commit | 7c23b27007ca62ff545411aa3d1200fce4eec8c9 (patch) | |
| tree | 86e4215580368b5c6f164bb79c2a0f91f479c7f1 /lib/autorequire/file.rb | |
| parent | 36c00a4509e745e4b29b1194faca4dca830a9b95 (diff) | |
refactor
Diffstat (limited to 'lib/autorequire/file.rb')
| -rw-r--r-- | lib/autorequire/file.rb | 78 |
1 files changed, 0 insertions, 78 deletions
diff --git a/lib/autorequire/file.rb b/lib/autorequire/file.rb deleted file mode 100644 index 35df33f..0000000 --- a/lib/autorequire/file.rb +++ /dev/null @@ -1,78 +0,0 @@ -require 'erb' -require 'fileutils' - -require_relative 'options' -require_relative 'log' - -module RCM - # Managing files - class File - attr_reader :id, :path - - include Options - include Log - - def initialize(path) - @id = "#{self.class}(#{path})" - @path = path - end - - def to_s - id - end - - def content(content = nil) - content.nil? ? @content : @content = content - end - - def create_parent_directory - @create_parent = true - self - end - - def from_file(...) - @from_file = true - self - end - - def from_template(...) - @from_template = true - self - end - - def do! - content = file_content - - dirname = ::File.dirname(@path) - if !::File.directory?(dirname) && @create_parent - info "Creating parent directory #{parent}" - FileUtils.mkdir_p(dirname) - end - - info "Creating file #{@path}" - debug content if option :debug - - tmp_path = "#{@path}.tmp" - ::File.write(tmp_path, content) - ::File.rename(tmp_path, @path) - end - - private - - def file_content - content = @from_file ? ::File.read(@content) : @content - @from_template ? ERB.new(content).result : content - end - end - - # Add file keyword to the DSL - class RCM - def file(path, &block) - return unless @conds_met - - f = File.new(path) - f.instance_eval(&block) - self << f - end - end -end |
