feat: add support for configuration file
This commit is contained in:
parent
f87920a393
commit
fdc80bfadc
4 changed files with 69 additions and 35 deletions
16
misc/code_preloader.sample.yaml
Normal file
16
misc/code_preloader.sample.yaml
Normal file
|
@ -0,0 +1,16 @@
|
|||
---
|
||||
|
||||
ignore_list:
|
||||
- .git
|
||||
- .code_preloader.yml
|
||||
- bin
|
||||
- LICENSE
|
||||
- prompts
|
||||
|
||||
output_file_path: null
|
||||
|
||||
header_prompt_file_path: null
|
||||
|
||||
footer_prompt_file_path: prompts/footer.txt
|
||||
|
||||
#
|
37
src/cli.cr
37
src/cli.cr
|
@ -26,6 +26,8 @@ module CodePreloader
|
|||
footer_prompt = ""
|
||||
__header_prompt_file_path = @config.header_prompt_file_path
|
||||
__footer_prompt_file_path = @config.footer_prompt_file_path
|
||||
__output_file_path = @output_file_path
|
||||
__repository_path_list = @config.repository_path_list
|
||||
|
||||
if !__header_prompt_file_path.nil?
|
||||
STDERR.puts "Loading header prompt from: #{__header_prompt_file_path}"
|
||||
|
@ -37,13 +39,9 @@ module CodePreloader
|
|||
footer_prompt = File.read(__footer_prompt_file_path)
|
||||
end
|
||||
|
||||
STDERR.puts "Processing repository: #{@config.repository_path}"
|
||||
|
||||
__output_file_path = @output_file_path
|
||||
__repository_path = @config.repository_path
|
||||
|
||||
abort("@output_file_path should be non-nil here") if __output_file_path.nil?
|
||||
abort("@repository_path should be non-nil here") if __repository_path.nil?
|
||||
abort("@repository_path should be non-empty here") if __repository_path_list.empty?
|
||||
|
||||
invalid_output_file = true
|
||||
output_file = STDOUT
|
||||
|
@ -55,7 +53,10 @@ module CodePreloader
|
|||
|
||||
output_file.puts header_prompt if @config.header_prompt_file_path
|
||||
|
||||
process_repository(__repository_path, output_file)
|
||||
STDERR.puts "Processing repository: #{@config.repository_path_list}"
|
||||
__repository_path_list.each do |repository_path|
|
||||
process_repository(repository_path, output_file)
|
||||
end
|
||||
|
||||
output_file.puts footer_prompt if @config.footer_prompt_file_path
|
||||
|
||||
|
@ -69,19 +70,16 @@ module CodePreloader
|
|||
|
||||
# Processes the specified repository and writes the output to a file.
|
||||
def process_repository(repository_path : String, output_file : IO::FileDescriptor)
|
||||
process_directory(repository_path, output_file)
|
||||
process_directory(repository_path, repository_path, output_file)
|
||||
|
||||
rescue e : IO::Error
|
||||
STDERR.puts "Error processing repository: #{e.message}"
|
||||
exit(1)
|
||||
end
|
||||
|
||||
private def process_directory(path : String, output_file : IO::FileDescriptor)
|
||||
__repository_path = @config.repository_path
|
||||
abort("@repository_path should be non-nil here") if __repository_path.nil?
|
||||
|
||||
Dir.each_child(path) do |child|
|
||||
child_path = File.join(path, child)
|
||||
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)
|
||||
|
||||
ignores = (
|
||||
@config.ignore_list
|
||||
|
@ -91,20 +89,17 @@ module CodePreloader
|
|||
next if !ignores.empty?
|
||||
|
||||
STDERR.puts "File: #{child_path}"
|
||||
child_path = File.join(path, child)
|
||||
child_path = File.join(dir_path, child)
|
||||
if File.directory?(child_path)
|
||||
process_directory(child_path, output_file)
|
||||
process_directory(root_path, child_path, output_file)
|
||||
else
|
||||
process_file(child_path, output_file)
|
||||
process_file(root_path, child_path, output_file)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private def process_file(file_path : String, output_file : IO::FileDescriptor)
|
||||
__repository_path = @config.repository_path
|
||||
abort("@repository_path should be non-nil here") if __repository_path.nil?
|
||||
|
||||
relative_file_path = file_path.sub(/^#{Regex.escape(__repository_path)}/, ".").lstrip
|
||||
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
|
||||
output_file.puts "@@ File \"#{relative_file_path}\""
|
||||
output_file.puts ""
|
||||
output_file.puts(File.read(file_path))
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
require "option_parser"
|
||||
require "yaml"
|
||||
|
||||
require "./models/root_config"
|
||||
|
||||
module CodePreloader
|
||||
class Config
|
||||
property repository_path : String?
|
||||
property repository_path_list : Array(String) = [] of String
|
||||
property ignore_list : Array(String) = [] of String
|
||||
property output_file_path : String?
|
||||
property header_prompt_file_path : String?
|
||||
|
@ -14,7 +16,7 @@ module CodePreloader
|
|||
|
||||
def parse_arguments(args : Array(String))
|
||||
OptionParser.parse(args) do |parser|
|
||||
parser.banner = "Usage: code-preloader [options] ROOT_DIR"
|
||||
parser.banner = "Usage: code-preloader [options] DIR1 ..."
|
||||
|
||||
parser.on("-c CONFIG_FILE", "--config=CONFIG_FILE", "Load parameters from CONFIG_FILE") do |config_file|
|
||||
load_config(config_file)
|
||||
|
@ -42,10 +44,9 @@ module CodePreloader
|
|||
end
|
||||
|
||||
parser.unknown_args do |remaining_args, _|
|
||||
if remaining_args.size > 1
|
||||
abort("Invalid number of arguments. Expected exactly one argument for ROOT_DIR.")
|
||||
remaining_args.each do |arg|
|
||||
@repository_path_list << arg
|
||||
end
|
||||
@repository_path = remaining_args[0]
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -67,17 +68,15 @@ module CodePreloader
|
|||
end
|
||||
|
||||
private def load_config(config_file_path : String)
|
||||
config_data = YAML.parse(File.read(config_file_path)).as_h
|
||||
config_str = File.read(config_file_path)
|
||||
|
||||
@repository_path = config_data["repository_path"]?.try &.as_s || @repository_path
|
||||
root = Models::RootConfig.from_yaml(config_str)
|
||||
|
||||
if ignore_list_yaml = config_data["ignore_list"]?
|
||||
@ignore_list = ignore_list_yaml.as_a.map(&.as_s)
|
||||
end
|
||||
|
||||
@output_file_path = config_data["output_file_path"]?.try &.as_s || @output_file_path
|
||||
@header_prompt_file_path = config_data["header_prompt_file_path"]?.try &.as_s || @header_prompt_file_path
|
||||
@footer_prompt_file_path = config_data["footer_prompt_file_path"]?.try &.as_s || @footer_prompt_file_path
|
||||
@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
|
||||
|
||||
rescue ex
|
||||
STDERR.puts "Failed to load config file: #{ex.message}"
|
||||
|
|
24
src/models/root_config.cr
Normal file
24
src/models/root_config.cr
Normal file
|
@ -0,0 +1,24 @@
|
|||
|
||||
require "yaml"
|
||||
|
||||
module CodePreloader::Models
|
||||
class RootConfig
|
||||
include YAML::Serializable
|
||||
include YAML::Serializable::Strict
|
||||
|
||||
@[YAML::Field(key: "repository_path_list")]
|
||||
getter repository_path_list : Array(String)?
|
||||
|
||||
@[YAML::Field(key: "output_file_path")]
|
||||
getter output_file_path : String?
|
||||
|
||||
@[YAML::Field(key: "header_prompt_file_path")]
|
||||
getter header_prompt_file_path : String?
|
||||
|
||||
@[YAML::Field(key: "footer_prompt_file_path")]
|
||||
getter footer_prompt_file_path : String?
|
||||
|
||||
@[YAML::Field(key: "ignore_list")]
|
||||
getter ignore_list : Array(String)?
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue