2023-12-29 15:53:54 +01:00
|
|
|
require "option_parser"
|
|
|
|
require "yaml"
|
|
|
|
|
2023-12-29 16:31:17 +01:00
|
|
|
require "./models/root_config"
|
|
|
|
|
2023-12-29 15:53:54 +01:00
|
|
|
module CodePreloader
|
|
|
|
class Config
|
2023-12-29 16:31:17 +01:00
|
|
|
property repository_path_list : Array(String) = [] of String
|
2023-12-29 15:53:54 +01:00
|
|
|
property ignore_list : Array(String) = [] of String
|
|
|
|
property output_file_path : String?
|
|
|
|
property header_prompt_file_path : String?
|
|
|
|
property footer_prompt_file_path : String?
|
|
|
|
|
|
|
|
def initialize()
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_arguments(args : Array(String))
|
|
|
|
OptionParser.parse(args) do |parser|
|
2023-12-29 16:31:17 +01:00
|
|
|
parser.banner = "Usage: code-preloader [options] DIR1 ..."
|
2023-12-29 15:53:54 +01:00
|
|
|
|
2024-01-02 12:32:00 +01:00
|
|
|
parser.on(
|
|
|
|
"-c CONFIG_FILE",
|
|
|
|
"--config=CONFIG_FILE",
|
|
|
|
"Load parameters from CONFIG_FILE"
|
|
|
|
) do |config_file|
|
2023-12-29 15:53:54 +01:00
|
|
|
load_config(config_file)
|
|
|
|
end
|
|
|
|
|
2024-01-02 12:32:00 +01:00
|
|
|
parser.on(
|
|
|
|
"-i IGNORE_PATH",
|
|
|
|
"--ignore=IGNORE_PATH",
|
|
|
|
"Ignore file or directory"
|
|
|
|
) do |ignore_file|
|
2023-12-29 15:53:54 +01:00
|
|
|
@ignore_list << ignore_file
|
|
|
|
end
|
|
|
|
|
2024-01-02 12:32:00 +01:00
|
|
|
parser.on(
|
|
|
|
"-o OUTPUT_FILE",
|
|
|
|
"--output=OUTPUT_FILE",
|
|
|
|
"Write output to OUTPUT_FILE"
|
|
|
|
) do |output_file|
|
2023-12-29 15:53:54 +01:00
|
|
|
@output_file_path = output_file
|
|
|
|
end
|
|
|
|
|
2024-01-02 12:32:00 +01:00
|
|
|
parser.on(
|
|
|
|
"-H HEADER_PROMPT_FILE",
|
|
|
|
"--header-prompt=HEADER_PROMPT_FILE",
|
|
|
|
"Load header prompt from HEADER_PROMPT_FILE"
|
|
|
|
) do |header_prompt_file|
|
2023-12-29 15:53:54 +01:00
|
|
|
@header_prompt_file_path = header_prompt_file
|
|
|
|
end
|
|
|
|
|
2024-01-02 12:32:00 +01:00
|
|
|
parser.on(
|
|
|
|
"-F FOOTER_PROMPT_FILE",
|
|
|
|
"--footer-prompt=FOOTER_PROMPT_FILE",
|
|
|
|
"Load footer prompt from FOOTER_PROMPT_FILE"
|
|
|
|
) do |footer_prompt_file|
|
2023-12-29 15:53:54 +01:00
|
|
|
@footer_prompt_file_path = footer_prompt_file
|
|
|
|
end
|
|
|
|
|
|
|
|
parser.on("-h", "--help", "Show this help") do
|
|
|
|
STDERR.puts parser
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
|
|
|
parser.unknown_args do |remaining_args, _|
|
2023-12-29 16:31:17 +01:00
|
|
|
remaining_args.each do |arg|
|
|
|
|
@repository_path_list << arg
|
2023-12-29 15:53:54 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-01-02 12:32:00 +01:00
|
|
|
validate
|
2023-12-29 15:53:54 +01:00
|
|
|
end
|
|
|
|
|
2024-01-02 12:32:00 +01:00
|
|
|
private def validate
|
2023-12-31 13:47:16 +01:00
|
|
|
abort("Missing repository path.") if @repository_path_list.empty?
|
2024-01-02 12:32:00 +01:00
|
|
|
|
2023-12-29 15:53:54 +01:00
|
|
|
STDERR.puts("Output file path not specified (using STDOUT)") if @output_file_path.nil? || @output_file_path.try(&.empty?)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Reads and returns a list of paths to ignore from the given file.
|
|
|
|
def self.get_ignore_list(ignore_file_path : String) : Array(String)
|
|
|
|
File.exists?(ignore_file_path) ? File.read_lines(ignore_file_path).map(&.strip) : [] of String
|
|
|
|
rescue e : IO::Error
|
|
|
|
STDERR.puts "Error reading ignore file: #{e.message}"
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
private def load_config(config_file_path : String)
|
2023-12-29 16:31:17 +01:00
|
|
|
config_str = File.read(config_file_path)
|
2023-12-29 15:53:54 +01:00
|
|
|
|
2023-12-29 16:31:17 +01:00
|
|
|
root = Models::RootConfig.from_yaml(config_str)
|
2023-12-29 15:53:54 +01:00
|
|
|
|
2023-12-29 16:31:17 +01:00
|
|
|
@repository_path = root.repository_path_list || @repository_path_list
|
|
|
|
@ignore_list = root.ignore_list || @ignore_list
|
|
|
|
@output_file_path = root.output_file_path || @output_file_path
|
|
|
|
@header_prompt_file_path = root.header_prompt_file_path || @header_prompt_file_path
|
|
|
|
@footer_prompt_file_path = root.footer_prompt_file_path || @footer_prompt_file_path
|
2023-12-29 15:53:54 +01:00
|
|
|
|
2024-01-02 12:32:00 +01:00
|
|
|
rescue ex : Exception
|
2023-12-29 15:53:54 +01:00
|
|
|
STDERR.puts "Failed to load config file: #{ex.message}"
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|