Compare commits
No commits in common. "fdc80bfadcd83c017bdc4f5d1c81a75151b71684" and "b8e4028d3fe747049bac5fafa826c8d27ba0b8df" have entirely different histories.
fdc80bfadc
...
b8e4028d3f
5 changed files with 39 additions and 74 deletions
|
@ -1,16 +0,0 @@
|
|||
---
|
||||
|
||||
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
|
||||
|
||||
#
|
|
@ -1,9 +1,8 @@
|
|||
---
|
||||
name: code-preloader
|
||||
name: chatgpt-preloader
|
||||
version: 0.1.0
|
||||
|
||||
targets:
|
||||
code-preloader:
|
||||
chatgpt-preloader:
|
||||
main: src/main.cr
|
||||
|
||||
authors:
|
||||
|
|
37
src/cli.cr
37
src/cli.cr
|
@ -26,8 +26,6 @@ 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}"
|
||||
|
@ -39,9 +37,13 @@ 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-empty here") if __repository_path_list.empty?
|
||||
abort("@repository_path should be non-nil here") if __repository_path.nil?
|
||||
|
||||
invalid_output_file = true
|
||||
output_file = STDOUT
|
||||
|
@ -53,10 +55,7 @@ module CodePreloader
|
|||
|
||||
output_file.puts header_prompt if @config.header_prompt_file_path
|
||||
|
||||
STDERR.puts "Processing repository: #{@config.repository_path_list}"
|
||||
__repository_path_list.each do |repository_path|
|
||||
process_repository(repository_path, output_file)
|
||||
end
|
||||
process_repository(__repository_path, output_file)
|
||||
|
||||
output_file.puts footer_prompt if @config.footer_prompt_file_path
|
||||
|
||||
|
@ -70,16 +69,19 @@ 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, repository_path, output_file)
|
||||
process_directory(repository_path, output_file)
|
||||
|
||||
rescue e : IO::Error
|
||||
STDERR.puts "Error processing repository: #{e.message}"
|
||||
exit(1)
|
||||
end
|
||||
|
||||
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)
|
||||
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)
|
||||
|
||||
ignores = (
|
||||
@config.ignore_list
|
||||
|
@ -89,17 +91,20 @@ module CodePreloader
|
|||
next if !ignores.empty?
|
||||
|
||||
STDERR.puts "File: #{child_path}"
|
||||
child_path = File.join(dir_path, child)
|
||||
child_path = File.join(path, child)
|
||||
if File.directory?(child_path)
|
||||
process_directory(root_path, child_path, output_file)
|
||||
process_directory(child_path, output_file)
|
||||
else
|
||||
process_file(root_path, child_path, output_file)
|
||||
process_file(child_path, output_file)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
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
|
||||
output_file.puts "@@ File \"#{relative_file_path}\""
|
||||
output_file.puts ""
|
||||
output_file.puts(File.read(file_path))
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
require "option_parser"
|
||||
require "yaml"
|
||||
|
||||
require "./models/root_config"
|
||||
|
||||
module CodePreloader
|
||||
class Config
|
||||
property repository_path_list : Array(String) = [] of String
|
||||
property repository_path : String?
|
||||
property ignore_list : Array(String) = [] of String
|
||||
property output_file_path : String?
|
||||
property header_prompt_file_path : String?
|
||||
|
@ -16,7 +14,7 @@ module CodePreloader
|
|||
|
||||
def parse_arguments(args : Array(String))
|
||||
OptionParser.parse(args) do |parser|
|
||||
parser.banner = "Usage: code-preloader [options] DIR1 ..."
|
||||
parser.banner = "Usage: code-preloader [options] ROOT_DIR"
|
||||
|
||||
parser.on("-c CONFIG_FILE", "--config=CONFIG_FILE", "Load parameters from CONFIG_FILE") do |config_file|
|
||||
load_config(config_file)
|
||||
|
@ -30,11 +28,11 @@ module CodePreloader
|
|||
@output_file_path = output_file
|
||||
end
|
||||
|
||||
parser.on("-H HEADER_PROMPT_FILE", "--header-prompt=HEADER_PROMPT_FILE", "Load header prompt from HEADER_PROMPT_FILE") do |header_prompt_file|
|
||||
parser.on("--header-prompt=HEADER_PROMPT_FILE", "Load header prompt from HEADER_PROMPT_FILE") do |header_prompt_file|
|
||||
@header_prompt_file_path = header_prompt_file
|
||||
end
|
||||
|
||||
parser.on("-F FOOTER_PROMPT_FILE", "--footer-prompt=FOOTER_PROMPT_FILE", "Load footer prompt from FOOTER_PROMPT_FILE") do |footer_prompt_file|
|
||||
parser.on("--footer-prompt=FOOTER_PROMPT_FILE", "Load footer prompt from FOOTER_PROMPT_FILE") do |footer_prompt_file|
|
||||
@footer_prompt_file_path = footer_prompt_file
|
||||
end
|
||||
|
||||
|
@ -44,9 +42,10 @@ module CodePreloader
|
|||
end
|
||||
|
||||
parser.unknown_args do |remaining_args, _|
|
||||
remaining_args.each do |arg|
|
||||
@repository_path_list << arg
|
||||
if remaining_args.size > 1
|
||||
abort("Invalid number of arguments. Expected exactly one argument for ROOT_DIR.")
|
||||
end
|
||||
@repository_path = remaining_args[0]
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -68,15 +67,17 @@ module CodePreloader
|
|||
end
|
||||
|
||||
private def load_config(config_file_path : String)
|
||||
config_str = File.read(config_file_path)
|
||||
config_data = YAML.parse(File.read(config_file_path)).as_h
|
||||
|
||||
root = Models::RootConfig.from_yaml(config_str)
|
||||
@repository_path = config_data["repository_path"]?.try &.as_s || @repository_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
|
||||
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
|
||||
|
||||
rescue ex
|
||||
STDERR.puts "Failed to load config file: #{ex.message}"
|
||||
|
|
|
@ -1,24 +0,0 @@
|
|||
|
||||
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