Class: Poepod::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/poepod/processor.rb

Constant Summary collapse

EXCLUDE_DEFAULT =
[
  "node_modules/", ".git/", ".gitignore", ".DS_Store",
  "*.jpg", "*.jpeg", "*.png", "*.svg", "*.gif",
  "*.exe", "*.dll", "*.so", "*.bin", "*.o", "*.a"
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(config_file = nil) ⇒ Processor

Returns a new instance of Processor.



14
15
16
17
# File 'lib/poepod/processor.rb', line 14

def initialize(config_file = nil)
  @failed_files = []
  @config = load_config(config_file)
end

Instance Method Details

#gather_files(directory_path, exclude) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/poepod/processor.rb', line 35

def gather_files(directory_path, exclude)
  exclude += @config["exclude"] if @config["exclude"]
  exclude_pattern = Regexp.union(exclude.map { |ex| Regexp.escape(ex) })

  Dir.glob("#{directory_path}/**/*").reject do |file_path|
    File.directory?(file_path) || file_path.match?(exclude_pattern)
  end.map do |file_path|
    Pathname.new(file_path).expand_path.to_s
  end
end

#load_config(config_file) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/poepod/processor.rb', line 19

def load_config(config_file)
  if config_file && File.exist?(config_file)
    YAML.load_file(config_file)
  else
    {}
  end
end

#process_file(file_path) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/poepod/processor.rb', line 27

def process_file(file_path)
  content = File.read(file_path, encoding: "utf-8")
  [file_path, content, nil]
rescue Encoding::InvalidByteSequenceError, Encoding::UndefinedConversionError
  @failed_files << file_path
  [file_path, nil, "Failed to decode the file, as it is not saved with UTF-8 encoding."]
end

#relative_path(file_path) ⇒ Object



60
61
62
# File 'lib/poepod/processor.rb', line 60

def relative_path(file_path)
  Pathname.new(file_path).relative_path_from(Dir.pwd)
end

#write_directory_structure_to_file(directory_path, output_file_name, exclude = EXCLUDE_DEFAULT) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/poepod/processor.rb', line 64

def write_directory_structure_to_file(directory_path, output_file_name, exclude = EXCLUDE_DEFAULT)
  dir_path = Pathname.new(directory_path)

  dir_path = dir_path.expand_path unless dir_path.absolute?

  file_list = gather_files(dir_path, exclude)
  total_files = file_list.size

  File.open(output_file_name, "w", encoding: "utf-8") do |output_file|
    results = file_list.tqdm(desc: "Progress", unit: " file").map do |file|
      process_file(file)
    end
    write_results_to_file(results, output_file)
  end

  copied_files = total_files - @failed_files.size

  [total_files, copied_files]
end

#write_results_to_file(results, output_file) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/poepod/processor.rb', line 46

def write_results_to_file(results, output_file)
  results.each_with_index do |(file_path, content, error), index|
    relative = relative_path(file_path)
    if content
      output_file.puts "--- START FILE: #{relative} ---"
      output_file.puts content
      output_file.puts "--- END FILE: #{relative} ---"
    elsif error
      output_file.puts "#{relative}\n#{error}"
    end
    output_file.puts if index < results.size - 1 # Add a newline between files
  end
end