Class: Hank::PathUtils

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/hank/path_utils.rb

Overview

Utilities for path manipulation and file type detection

Class Method Summary collapse

Class Method Details

.flatten_path(path) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hank/path_utils.rb', line 10

def self.flatten_path(path)
  pathname = Pathname.new(path)

  # Handle dotfiles
  basename = pathname.basename.to_s
  basename = "dot--#{basename[1..]}" if basename.start_with?('.')

  # Flatten directory structure
  if pathname.dirname.to_s == '.'
    basename
  else
    dirname = pathname.dirname.to_s.gsub(%r{^/}, '').gsub('/', '-')
    "#{dirname}-#{basename}"
  end
end

.text_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hank/path_utils.rb', line 27

def self.text_file?(path)
  return false unless File.file?(path)

  # Use libmagic to determine if file is text
  begin
    # Create a new instance for each call to avoid allocator warnings
    magic = FileMagic.open(FileMagic::MAGIC_MIME)
    mime_type = magic.file(path)
    magic.close
    mime_type.start_with?('text/') ||
      mime_type.include?('xml') ||
      mime_type.include?('json') ||
      mime_type.include?('script')
  rescue StandardError => e
    puts "Warning: Error detecting file type: #{e.message}".yellow
    # Fall back to simple text check
    File.open(path) { |f| T.must(f.read(1024)).valid_encoding? }
  end
end