Module: Sevgi::Function::File
- Included in:
- Sevgi::Function
- Defined in:
- lib/sevgi/function/file.rb
Overview
File-system helpers used by build scripts and DSL support code.
Instance Method Summary collapse
-
#changed?(file, content) {|content| ... } ⇒ Boolean
Checks whether a file would change if written with content.
-
#existing(file, extensions) ⇒ String?
Finds an existing file by exact path or by trying default extensions.
-
#existing!(file, extensions) ⇒ String
Finds an existing file or raises.
-
#existing_map(*files, extensions: []) ⇒ Hash{String => String, nil}
Maps each non-nil input file to an existing path lookup result.
-
#existing_map!(*files, extensions: []) ⇒ Hash{String => String}
Maps each non-nil input file to an existing path lookup result or raises.
-
#out(content, *paths) {|content| ... } ⇒ String?
Writes content to a file when it changed, or prints to stdout without a path.
-
#qualify(file, default_extension) ⇒ String
Adds a default extension when a path has no extension.
-
#subext(ext, *paths) ⇒ String
Replaces or removes the extension on a path.
-
#touch(*paths) ⇒ String
Creates a file and any missing parent directories.
Instance Method Details
#changed?(file, content) {|content| ... } ⇒ Boolean
Checks whether a file would change if written with content.
18 19 20 21 22 23 24 25 |
# File 'lib/sevgi/function/file.rb', line 18 def changed?(file, content, &filter) return true unless ::File.exist?(file) old_content = ::File.read(file) old_content, content = [old_content, content].map(&filter) if filter Digest::SHA1.digest(old_content) != Digest::SHA1.digest(content) end |
#existing(file, extensions) ⇒ String?
Finds an existing file by exact path or by trying default extensions.
31 32 33 34 35 36 37 |
# File 'lib/sevgi/function/file.rb', line 31 def existing(file, extensions) return file if ::File.exist?(file) return nil unless ::File.extname(file).empty? return nil if extensions.empty? extensions.map { |ext| "#{file}.#{ext}" }.detect { |file| ::File.exist?(file) } end |
#existing!(file, extensions) ⇒ String
Finds an existing file or raises.
44 45 46 47 48 |
# File 'lib/sevgi/function/file.rb', line 44 def existing!(file, extensions) existing(file, extensions).tap do |found| ArgumentError.("No matching file(s) found: #{file}") unless found end end |
#existing_map(*files, extensions: []) ⇒ Hash{String => String, nil}
Maps each non-nil input file to an existing path lookup result.
54 55 56 57 58 |
# File 'lib/sevgi/function/file.rb', line 54 def existing_map(*files, extensions: []) {}.tap do |found| files.compact.each { |file| found[file] = existing(file, extensions) } end end |
#existing_map!(*files, extensions: []) ⇒ Hash{String => String}
66 67 68 69 70 71 72 73 |
# File 'lib/sevgi/function/file.rb', line 66 def existing_map!(...) found = F.existing_map(...) missings = found.select { |_, match| match.nil? }.keys ArgumentError.("No matching file(s) found: #{missings.join(", ")}") unless missings.empty? found end |
#out(content, *paths) {|content| ... } ⇒ String?
Writes content to a file when it changed, or prints to stdout without a path.
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/sevgi/function/file.rb', line 84 def out(content, *paths, &filter) if paths.empty? ::Kernel.puts(content) else file = ::File.(::File.join(*paths)) output = "#{content.chomp}\n" return unless changed?(file, output, &filter) file.tap { ::File.write(file, output) } end end |
#qualify(file, default_extension) ⇒ String
Adds a default extension when a path has no extension.
101 102 103 104 105 |
# File 'lib/sevgi/function/file.rb', line 101 def qualify(file, default_extension) return file unless ::File.extname(file).empty? "#{file}.#{default_extension}" end |
#subext(ext, *paths) ⇒ String
Replaces or removes the extension on a path.
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/sevgi/function/file.rb', line 111 def subext(ext, *paths) path = ::File.join(*paths) return path if %w[. ..].include?(path) return path unless ext ext = ".#{ext}" unless ext.empty? || ext.start_with?(".") Pathname.new(path).sub_ext(ext).to_s end |
#touch(*paths) ⇒ String
Creates a file and any missing parent directories.
126 127 128 129 130 131 |
# File 'lib/sevgi/function/file.rb', line 126 def touch(*paths) ::File.join(*paths).tap do |path| ::FileUtils.mkdir_p(::File.dirname(path)) ::FileUtils.touch(path) end end |