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-31 13:47:16 +01:00
|
|
|
require "magic"
|
2023-12-29 14:13:20 +01:00
|
|
|
|
2023-12-29 15:53:54 +01:00
|
|
|
require "./config"
|
2024-01-02 12:32:00 +01:00
|
|
|
require "./filelist"
|
2023-12-29 15:53:54 +01:00
|
|
|
|
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()
|
2024-01-04 11:53:50 +01:00
|
|
|
@config.detect_config()
|
2023-12-29 15:53:54 +01:00
|
|
|
@config.parse_arguments(args)
|
2023-12-29 14:13:20 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Executes the main functionality of the CLI application.
|
|
|
|
def exec
|
2024-01-04 11:53:50 +01:00
|
|
|
case @config.subcommand
|
|
|
|
when Config::Subcommand::Init then exec_init(@config.init_options)
|
|
|
|
when Config::Subcommand::Pack then exec_pack(@config.pack_options)
|
|
|
|
when Config::Subcommand::Version then exec_version
|
|
|
|
when Config::Subcommand::Help then exec_help
|
|
|
|
when Config::Subcommand::None then exec_none
|
|
|
|
else
|
|
|
|
abort("Unknown subcommand #{@config.subcommand}!")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def exec_init(init_options)
|
|
|
|
abort("Unexpected nil value for init_options!") if init_options.nil?
|
|
|
|
abort("FIXME: Not implemented!")
|
|
|
|
end
|
|
|
|
|
|
|
|
def exec_version
|
|
|
|
abort("FIXME: Not implemented!")
|
|
|
|
end
|
|
|
|
|
|
|
|
def exec_none
|
|
|
|
abort("No command specified!")
|
|
|
|
end
|
|
|
|
|
|
|
|
def exec_help
|
|
|
|
abort("FIXME: Not implemented!")
|
|
|
|
end
|
|
|
|
|
|
|
|
def exec_pack(pack_options)
|
|
|
|
abort("Unexpected nil value for pack_options!") if pack_options.nil?
|
|
|
|
|
|
|
|
output_file_path = pack_options.output_file_path
|
|
|
|
repository_path_list = pack_options.repository_path_list
|
|
|
|
header_prompt_file_path = pack_options.header_prompt_file_path
|
|
|
|
footer_prompt_file_path = pack_options.footer_prompt_file_path
|
|
|
|
regular_output_file = false
|
2024-01-02 12:32:00 +01:00
|
|
|
|
|
|
|
filelist = FileList.new()
|
|
|
|
filelist.add(repository_path_list)
|
2024-01-04 11:53:50 +01:00
|
|
|
pack_options.ignore_list.each do |ignore_pattern|
|
2024-01-02 12:32:00 +01:00
|
|
|
filelist.reject { |path| !!(path =~ Regex.new(ignore_pattern)) }
|
2023-12-29 14:13:20 +01:00
|
|
|
end
|
|
|
|
|
2024-01-02 12:32:00 +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
|
|
|
|
|
2024-01-02 12:32:00 +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)
|
|
|
|
end
|
2023-12-29 14:57:11 +01:00
|
|
|
|
2024-01-04 11:53:50 +01:00
|
|
|
output_file_path.try do |path|
|
|
|
|
break if path.empty?
|
|
|
|
break if path == "-"
|
|
|
|
regular_output_file = true
|
|
|
|
output_file = File.open(path, "w")
|
|
|
|
end
|
2023-12-29 14:57:11 +01:00
|
|
|
|
|
|
|
output_file = STDOUT
|
2024-01-02 12:32:00 +01:00
|
|
|
header_prompt = ""
|
|
|
|
footer_prompt = ""
|
2023-12-29 14:13:20 +01:00
|
|
|
|
2024-01-04 11:53:50 +01:00
|
|
|
output_file.puts header_prompt if header_prompt_file_path
|
2023-12-29 14:57:11 +01:00
|
|
|
|
2024-01-04 11:53:50 +01:00
|
|
|
STDERR.puts "Processing repository: #{repository_path_list}"
|
2024-01-02 12:32:00 +01:00
|
|
|
filelist.each do |file_path|
|
|
|
|
process_file(file_path, output_file)
|
2023-12-29 16:31:17 +01:00
|
|
|
end
|
2023-12-29 14:57:11 +01:00
|
|
|
|
2024-01-04 11:53:50 +01:00
|
|
|
output_file.puts footer_prompt if footer_prompt_file_path
|
2023-12-29 14:57:11 +01:00
|
|
|
|
2024-01-04 11:53:50 +01:00
|
|
|
output_file.close if regular_output_file
|
|
|
|
STDERR.puts "Processing completed. Output written to: #{regular_output_file ? output_file_path : "stdout" }"
|
2023-12-29 14:13:20 +01:00
|
|
|
|
|
|
|
rescue e : Exception
|
|
|
|
STDERR.puts "An error occurred during execution: #{e.message}"
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
|
2024-01-02 12:32:00 +01:00
|
|
|
private def process_file(file_path : String, output_file : IO::FileDescriptor)
|
2023-12-31 13:47:16 +01:00
|
|
|
fh = File.open(file_path)
|
|
|
|
mime = Magic.mime_type.of(fh)
|
2024-01-02 12:32:00 +01:00
|
|
|
output_file.puts "@@ File \"#{file_path}\" (Mime-Type: #{mime.inspect})"
|
2023-12-29 14:13:20 +01:00
|
|
|
output_file.puts ""
|
2023-12-31 13:47:16 +01:00
|
|
|
output_file.puts(fh.gets_to_end)
|
2023-12-29 14:13:20 +01:00
|
|
|
output_file.puts ""
|
2023-12-31 13:47:16 +01:00
|
|
|
fh.close
|
2023-12-29 14:13:20 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|