Module: Sumi::Function::File

Included in:
Sumi::Function
Defined in:
function/lib/sumi/function/file.rb

Overview

File-system methods promoted to Sumi::F. This module organizes the facade implementation. It is not a consumer mixin contract.

Instance Method Summary collapse

Instance Method Details

#changed?(file, content) {|content| ... } ⇒ Boolean

Checks whether a file differs from the supplied content.

Parameters:

  • file (String) —

    file path to compare

  • content (String) —

    proposed file content

Yields:

  • optional normalization filter applied to both old and new content

Yield Parameters:

  • content (String) —

    content to normalize

Yield Returns:

Returns:

  • (Boolean) —

    true when the file is missing or content differs

Raises:

  • (SystemCallError) —

    when the file cannot be inspected or read



18
19
20
21
22
23
24
25
# File 'function/lib/sumi/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

  old_content != content
end

#existing(file, extensions) ⇒ String?

Finds an existing file by exact path or by trying default extensions.

Parameters:

  • file (String) —

    file path or extensionless basename

  • extensions (Array<String>) —

    extensions to try when file has no extension

Returns:

  • (String, nil) —

    matching file path, or nil when no file is found



31
32
33
34
35
36
37
# File 'function/lib/sumi/function/file.rb', line 31

def existing(file, extensions)
  return file if ::File.file?(file)
  return nil unless ::File.extname(file).empty?
  return nil if extensions.empty?

  extensions.map { |ext| "#{file}.#{ext}" }.detect { |file| ::File.file?(file) }
end

#existing!(file, extensions) ⇒ String

Finds an existing file or raises.

Parameters:

  • file (String) —

    file path or extensionless basename

  • extensions (Array<String>) —

    extensions to try when file has no extension

Returns:

  • (String) —

    matching file path

Raises:



44
45
46
47
48
# File 'function/lib/sumi/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.

Parameters:

  • files (Array<String, nil>) —

    file paths or extensionless basenames

  • extensions (Array<String>) (defaults to: []) —

    extensions to try when a file has no extension

Returns:

  • (Hash{String => String, nil}) —

    original file names mapped to found paths



54
55
56
57
58
# File 'function/lib/sumi/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}

Maps each non-nil input file to an existing path lookup result or raises.

Parameters:

  • files (Array<String, nil>) —

    file paths or extensionless basenames

  • extensions (Array<String>) (defaults to: []) —

    extensions to try when a file has no extension

Returns:

  • (Hash{String => String}) —

    original file names mapped to found paths

Raises:



66
67
68
69
70
71
72
73
# File 'function/lib/sumi/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.

Parameters:

  • content (String) —

    output content

  • paths (Array<String>) —

    path components for the output file

Yields:

  • optional normalization filter used for change detection

Yield Parameters:

  • content (String) —

    old or new content

Yield Returns:

Returns:

  • (String, nil) —

    expanded file path when written, otherwise nil

Raises:

  • (SystemCallError) —

    when the destination cannot be inspected, read, or written



83
84
85
86
87
88
89
90
91
92
93
94
# File 'function/lib/sumi/function/file.rb', line 83

def out(content, *paths, &filter)
  if paths.empty?
    ::Kernel.puts(content)
  else
    file = ::File.expand_path(::File.join(*paths))
    output = "#{content.chomp}\n"

    return unless changed?(file, output, &filter)

    file.tap { ::File.write(file, output) }
  end
end

#output_path(value, default:, context: "Output") ⇒ String

Resolves an output path, using the default when value is nil or an existing directory. The default is validated only when used. This method does not create files or directories.

Parameters:

  • value (String, #to_path, nil) —

    explicit output path or directory

  • default (String, #to_path) —

    default output path; its basename is used for a directory target

  • context (String) (defaults to: "Output") —

    public operation named in errors

Returns:

  • (String) —

    expanded output path

Raises:

  • (Sumi::ArgumentError) —

    when a selected path/default is blank, invalid, or cannot be converted



103
104
105
106
107
108
109
110
111
# File 'function/lib/sumi/function/file.rb', line 103

def output_path(value, default:, context: "Output")
  return path(default, context: "#{context} default") if value.nil?

  output = path(value, context: "#{context} path")
  return output unless ::File.directory?(output)

  default = path(default, context: "#{context} default")
  ::File.join(output, ::File.basename(default))
end

#path(value, context: "Path") ⇒ String

Converts a non-blank path to an expanded String without requiring it to exist.

Parameters:

  • value (String, #to_path) —

    raw path value

  • context (String) (defaults to: "Path") —

    error-message subject

Returns:

Raises:



118
119
120
121
122
123
124
125
126
127
128
# File 'function/lib/sumi/function/file.rb', line 118

def path(value, context: "Path")
  path = value.respond_to?(:to_path) ? value.to_path : value
  ArgumentError.("#{context} must be a String or path-like object") unless path.is_a?(::String)
  ArgumentError.("#{context} must be provided") if path.strip.empty?

  ::File.expand_path(path)
rescue ::Sumi::ArgumentError
  raise
rescue ::StandardError => e
  ArgumentError.("#{context} must be a String or path-like object: #{e.message}")
end

#qualify(file, default_extension) ⇒ String

Adds a default extension when a path has no extension.

Parameters:

  • file (String) —

    file path

  • default_extension (String) —

    extension to append without a leading dot

Returns:

  • (String) —

    qualified file path



134
135
136
137
138
# File 'function/lib/sumi/function/file.rb', line 134

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.

Parameters:

  • ext (String, nil) —

    replacement extension, without or with a leading dot

  • paths (Array<String>) —

    path components

Returns:

  • (String) —

    path with the replacement extension



144
145
146
147
148
149
150
151
152
153
# File 'function/lib/sumi/function/file.rb', line 144

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?(".")

  "#{path.delete_suffix(::File.extname(path))}#{ext}"
end

#touch(*paths) ⇒ String

Creates a file and any missing parent directories.

Parameters:

  • paths (Array<String>) —

    path components for the file

Returns:

  • (String) —

    touched file path

Raises:

  • (SystemCallError) —

    when the file or parent directory cannot be created



159
160
161
162
163
164
# File 'function/lib/sumi/function/file.rb', line 159

def touch(*paths)
  ::File.join(*paths).tap do |path|
    ::FileUtils.mkdir_p(::File.dirname(path))
    ::FileUtils.touch(path)
  end
end