summaryrefslogtreecommitdiff
path: root/lib/autorequire/file.rb
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-12-06 23:50:13 +0200
committerPaul Buetow <paul@buetow.org>2024-12-06 23:50:13 +0200
commit36c00a4509e745e4b29b1194faca4dca830a9b95 (patch)
tree4f1a2c47db34ed38ea89c5631e01b5ea3c6b2e13 /lib/autorequire/file.rb
parent7686fe830946ae36957501f1656bb429c694b09e (diff)
more on this
Diffstat (limited to 'lib/autorequire/file.rb')
-rw-r--r--lib/autorequire/file.rb37
1 files changed, 32 insertions, 5 deletions
diff --git a/lib/autorequire/file.rb b/lib/autorequire/file.rb
index 3233015..35df33f 100644
--- a/lib/autorequire/file.rb
+++ b/lib/autorequire/file.rb
@@ -1,30 +1,48 @@
+require 'erb'
require 'fileutils'
+
+require_relative 'options'
require_relative 'log'
module RCM
# Managing files
class File
- attr_reader :path
+ 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
+ def create_parent_directory
@create_parent = true
+ self
end
- def to_s
- @path
+ 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}"
@@ -32,10 +50,19 @@ module RCM
end
info "Creating file #{@path}"
+ debug content if option :debug
+
tmp_path = "#{@path}.tmp"
- ::File.write(tmp_path, @content)
+ ::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