2023-12-29 15:53:54 +01:00
|
|
|
|
2023-12-29 14:13:20 +01:00
|
|
|
# vim: set ts=2 sw=2 et ft=crystal:
|
|
|
|
|
|
|
|
require "file"
|
|
|
|
require "option_parser"
|
|
|
|
|
2023-12-29 15:53:54 +01:00
|
|
|
require "./config"
|
|
|
|
|
2023-12-29 14:13:20 +01:00
|
|
|
# The CodePreloader module organizes classes and methods related to preloading code files.
|
|
|
|
module CodePreloader
|
|
|
|
# The Cli class handles command-line interface operations for the CodePreloader.
|
|
|
|
class Cli
|
2023-12-29 15:53:54 +01:00
|
|
|
@config : Config
|
2023-12-29 14:13:20 +01:00
|
|
|
|
|
|
|
# Initializes the Cli class with default values.
|
2023-12-29 15:53:54 +01:00
|
|
|
def initialize(args)
|
2023-12-29 14:13:20 +01:00
|
|
|
@output_file_path = ""
|
2023-12-29 15:53:54 +01:00
|
|
|
@config = Config.new()
|
|
|
|
@config.parse_arguments(args)
|
2023-12-29 14:13:20 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# Executes the main functionality of the CLI application.
|
|
|
|
def exec
|
2023-12-29 14:57:11 +01:00
|
|
|
header_prompt = ""
|
|
|
|
footer_prompt = ""
|
2023-12-29 15:53:54 +01:00
|
|
|
__header_prompt_file_path = @config.header_prompt_file_path
|
|
|
|
__footer_prompt_file_path = @config.footer_prompt_file_path
|
2023-12-29 16:31:17 +01:00
|
|
|
__output_file_path = @output_file_path
|
|
|
|
__repository_path_list = @config.repository_path_list
|
2023-12-29 14:57:11 +01:00
|
|
|
|
|
|
|
if !__header_prompt_file_path.nil?
|
|
|
|
STDERR.puts "Loading header prompt from: #{__header_prompt_file_path}"
|
|
|
|
header_prompt = File.read(__header_prompt_file_path)
|
2023-12-29 14:13:20 +01:00
|
|
|
end
|
|
|
|
|
2023-12-29 14:57:11 +01:00
|
|
|
if !__footer_prompt_file_path.nil?
|
|
|
|
STDERR.puts "Loading footer prompt from: #{__footer_prompt_file_path}"
|
|
|
|
footer_prompt = File.read(__footer_prompt_file_path)
|
2023-12-29 14:13:20 +01:00
|
|
|
end
|
|
|
|
|
2023-12-29 14:57:11 +01:00
|
|
|
|
|
|
|
abort("@output_file_path should be non-nil here") if __output_file_path.nil?
|
2023-12-29 16:31:17 +01:00
|
|
|
abort("@repository_path should be non-empty here") if __repository_path_list.empty?
|
2023-12-29 14:57:11 +01:00
|
|
|
|
|
|
|
invalid_output_file = true
|
|
|
|
output_file = STDOUT
|
|
|
|
|
|
|
|
unless __output_file_path.nil? || __output_file_path.try(&.empty?) || (__output_file_path != "-")
|
|
|
|
output_file = File.open(__output_file_path, "w")
|
|
|
|
invalid_output_file = false
|
|
|
|
end
|
2023-12-29 14:13:20 +01:00
|
|
|
|
2023-12-29 15:53:54 +01:00
|
|
|
output_file.puts header_prompt if @config.header_prompt_file_path
|
2023-12-29 14:57:11 +01:00
|
|
|
|
2023-12-29 16:31:17 +01:00
|
|
|
STDERR.puts "Processing repository: #{@config.repository_path_list}"
|
|
|
|
__repository_path_list.each do |repository_path|
|
|
|
|
process_repository(repository_path, output_file)
|
|
|
|
end
|
2023-12-29 14:57:11 +01:00
|
|
|
|
2023-12-29 15:53:54 +01:00
|
|
|
output_file.puts footer_prompt if @config.footer_prompt_file_path
|
2023-12-29 14:57:11 +01:00
|
|
|
|
|
|
|
output_file.close if !invalid_output_file
|
|
|
|
STDERR.puts "Processing completed. Output written to: #{invalid_output_file ? "stdout" : __output_file_path}"
|
2023-12-29 14:13:20 +01:00
|
|
|
|
|
|
|
rescue e : Exception
|
|
|
|
STDERR.puts "An error occurred during execution: #{e.message}"
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Processes the specified repository and writes the output to a file.
|
2023-12-29 14:57:11 +01:00
|
|
|
def process_repository(repository_path : String, output_file : IO::FileDescriptor)
|
2023-12-29 16:31:17 +01:00
|
|
|
process_directory(repository_path, repository_path, output_file)
|
2023-12-29 14:13:20 +01:00
|
|
|
|
|
|
|
rescue e : IO::Error
|
|
|
|
STDERR.puts "Error processing repository: #{e.message}"
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
|
2023-12-29 16:31:17 +01:00
|
|
|
private def process_directory(root_path, dir_path : String, output_file : IO::FileDescriptor)
|
|
|
|
Dir.each_child(dir_path) do |child|
|
|
|
|
child_path = File.join(dir_path, child)
|
2023-12-29 14:13:20 +01:00
|
|
|
|
|
|
|
ignores = (
|
2023-12-29 15:53:54 +01:00
|
|
|
@config.ignore_list
|
2023-12-29 14:13:20 +01:00
|
|
|
.map{ |prefix| [prefix, File.expand_path(child_path) =~ /^#{File.expand_path(prefix)}/] }
|
|
|
|
.reject!{ |item| item[1].nil? }
|
|
|
|
)
|
|
|
|
next if !ignores.empty?
|
|
|
|
|
2023-12-29 14:57:11 +01:00
|
|
|
STDERR.puts "File: #{child_path}"
|
2023-12-29 16:31:17 +01:00
|
|
|
child_path = File.join(dir_path, child)
|
2023-12-29 14:13:20 +01:00
|
|
|
if File.directory?(child_path)
|
2023-12-29 16:31:17 +01:00
|
|
|
process_directory(root_path, child_path, output_file)
|
2023-12-29 14:13:20 +01:00
|
|
|
else
|
2023-12-29 16:31:17 +01:00
|
|
|
process_file(root_path, child_path, output_file)
|
2023-12-29 14:13:20 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-29 16:31:17 +01:00
|
|
|
private def process_file(root_path : String, file_path : String, output_file : IO::FileDescriptor)
|
|
|
|
relative_file_path = file_path.sub(/^#{Regex.escape(root_path)}/, ".").lstrip
|
2023-12-29 14:13:20 +01:00
|
|
|
output_file.puts "@@ File \"#{relative_file_path}\""
|
|
|
|
output_file.puts ""
|
|
|
|
output_file.puts(File.read(file_path))
|
|
|
|
output_file.puts ""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|