refactor(cli): enhance file inclusion/exclusion logic
Some checks failed
continuous-integration/drone/push Build is failing

Improves the flexibility and clarity of file processing by introducing
separate include and exclude lists.

- Replace `ignore_list` with `exclude_list` and add `include_list` in
  `.code_preloader.yml`.
- Update `cli.cr` to handle both `include_list` and `exclude_list` for
  file selection.
- Add support for tracing exceptions with a new `trace` configuration
  option.
- Modify `PackOptions` class to include `exclude_list` and
  `include_list` properties.
- Adjust option parsing in `config.cr` to reflect new configuration
  options.

Signed-off-by: Glenn <glenux@glenux.net>
This commit is contained in:
Glenn Y. Rolland 2025-06-16 16:14:09 +02:00
parent e3e091974d
commit aea7979e40
6 changed files with 81 additions and 47 deletions

View file

@ -1,6 +1,9 @@
--- ---
ignore_list: include_list:
- .*cr$
exclude_list:
- ^\.git/.* - ^\.git/.*
- ^\.vagrant/.* - ^\.vagrant/.*
- ^misc/.* - ^misc/.*

View file

@ -92,9 +92,12 @@ module CodePreloader
filelist = FileList.new() filelist = FileList.new()
filelist.add(source_list) filelist.add(source_list)
pack_options.ignore_list.each do |ignore_pattern| pack_options.exclude_list
filelist.reject { |path| !!(path =~ Regex.new(ignore_pattern)) } .map { |exclude_pattern| Regex.new(exclude_pattern) }
end .each { |exclude_regexp| filelist.reject { |path| !!(path =~ exclude_regexp) } }
pack_options.include_list
.map { |include_pattern| Regex.new(include_pattern) }
.each { |include_regexp| filelist.select { |path| !!(path =~ include_regexp) } }
STDERR.puts "Loading template file from: #{prompt_template_path ? prompt_template_path : "<internal>" }".colorize(:yellow) STDERR.puts "Loading template file from: #{prompt_template_path ? prompt_template_path : "<internal>" }".colorize(:yellow)
if prompt_template_path if prompt_template_path
@ -149,6 +152,9 @@ module CodePreloader
rescue e : Exception rescue e : Exception
STDERR.puts "ERROR: #{e.message}" STDERR.puts "ERROR: #{e.message}"
if @config.trace?
STDERR.puts e.backtrace.map(&.gsub(/^/, " ")).join("\n")
end
exit(1) exit(1)
end end

View file

@ -27,14 +27,16 @@ module CodePreloader
class PackOptions class PackOptions
property config_path : String? = nil property config_path : String? = nil
property source_list : Array(String) = [] of String property source_list : Array(String) = [] of String
property ignore_list : Array(String) = [] of String property exclude_list : Array(String) = [] of String
property include_list : Array(String) = [] of String
property output_path : String? property output_path : String?
property prompt_template_path : String? property prompt_template_path : String?
property prompt_header_path : String? property prompt_header_path : String?
property prompt_footer_path : String? property prompt_footer_path : String?
end end
getter verbose : Bool = false getter? verbose : Bool = false
getter? trace : Bool = false
getter parser : OptionParser? getter parser : OptionParser?
getter subcommand : Subcommand = Subcommand::None getter subcommand : Subcommand = Subcommand::None
getter pack_options : PackOptions? getter pack_options : PackOptions?
@ -60,6 +62,7 @@ module CodePreloader
end end
end end
parser.on( parser.on(
"-c FILE", "-c FILE",
"--config=FILE", "--config=FILE",
@ -124,10 +127,18 @@ module CodePreloader
parser.on( parser.on(
"-i REGEXP", "-i REGEXP",
"--ignore=REGEXP", "--include=REGEXP",
"Ignore file or directory. Can be used\nmultiple times (default: none)" "Include file or directory. Can be used\nmultiple times (default: none)"
) do |ignore_file| ) do |include_regexp|
@pack_options.try { |opt| opt.ignore_list << ignore_file } @pack_options.try { |opt| opt.include_list << include_regexp }
end
parser.on(
"-e REGEXP",
"--exclude=REGEXP",
"Exclude file or directory. Can be used\nmultiple times (default: none)"
) do |exclude_regexp|
@pack_options.try { |opt| opt.exclude_list << exclude_regexp }
end end
parser.on( parser.on(
@ -186,11 +197,14 @@ module CodePreloader
@verbose = true @verbose = true
end end
parser.on("-t", "--trace", "Show detailed traces for exceptions") do
@trace = true
end
parser.on("--version", "Show version") do parser.on("--version", "Show version") do
@subcommand = Subcommand::Version @subcommand = Subcommand::Version
end end
parser.separator "\nSubcommands:" parser.separator "\nSubcommands:"
parser.on("init", "Create an example .code_preloader.yml file") do parser.on("init", "Create an example .code_preloader.yml file") do
@ -277,8 +291,11 @@ module CodePreloader
if opts.source_list.nil? || opts.source_list.try &.empty? if opts.source_list.nil? || opts.source_list.try &.empty?
root.source_list.try { |value| opts.source_list = value } root.source_list.try { |value| opts.source_list = value }
end end
if opts.ignore_list.nil? || opts.ignore_list.try &.empty? if opts.exclude_list.nil? || opts.exclude_list.try &.empty?
root.ignore_list.try { |value| opts.ignore_list = value } root.exclude_list.try { |value| opts.exclude_list = value }
end
if opts.include_list.nil? || opts.include_list.try &.empty?
root.include_list.try { |value| opts.include_list = value }
end end
if opts.output_path.nil? if opts.output_path.nil?
opts.output_path = root.output_path opts.output_path = root.output_path

View file

@ -13,13 +13,13 @@ module CodePreloader
end end
@sources : Array(String) @sources : Array(String)
@filters_in : Array(Filter) @include_filters : Array(Filter)
@filters_out : Array(Filter) @exclude_filters : Array(Filter)
def initialize(dirs = [] of String) def initialize(dirs = [] of String)
@sources = [] of String @sources = [] of String
@filters_in = [] of Filter @include_filters = [] of Filter
@filters_out = [] of Filter @exclude_filters = [] of Filter
dirs.each { |dir| self.add(dir) } dirs.each { |dir| self.add(dir) }
end end
@ -34,11 +34,11 @@ module CodePreloader
end end
def select(&filter : Filter) def select(&filter : Filter)
@filters_in << filter @include_filters << filter
end end
def reject(&filter : Filter) def reject(&filter : Filter)
@filters_out << filter @exclude_filters << filter
end end
def each(&block) def each(&block)
@ -56,16 +56,16 @@ module CodePreloader
must_reject = false must_reject = false
clean_path = path.to_s.gsub(/^\.\//,"") clean_path = path.to_s.gsub(/^\.\//,"")
@filters_in.each do |filter_in| @include_filters.each do |filter_in|
must_select = must_select || filter_in.call(clean_path) must_select = must_select || filter_in.call(clean_path)
end end
keep = keep && must_select if @filters_in.any? keep = keep && must_select if @include_filters.any?
keep = keep || is_dir keep = keep || is_dir
@filters_out.each do |filter_out| @exclude_filters.each do |filter_out|
must_reject = must_reject || filter_out.call(clean_path) must_reject = must_reject || filter_out.call(clean_path)
end end
keep = keep && !must_reject if @filters_out.any? keep = keep && !must_reject if @exclude_filters.any?
keep keep
end end

View file

@ -16,7 +16,10 @@ module CodePreloader::Models
@[YAML::Field(key: "prompt")] @[YAML::Field(key: "prompt")]
getter prompt : PromptConfig? getter prompt : PromptConfig?
@[YAML::Field(key: "ignore_list")] @[YAML::Field(key: "exclude_list")]
getter ignore_list : Array(String)? getter exclude_list : Array(String)?
@[YAML::Field(key: "include_list")]
getter include_list : Array(String)?
end end
end end

View file

@ -6,9 +6,14 @@
# - "path/to/repo1" # - "path/to/repo1"
# - "path/to/repo2" # - "path/to/repo2"
# List of patterns to ignore during preloading # List of patterns to exclude (= ignore) during preloading
ignore_list: exclude_list:
- ^\.git/.* - ^\.git/.*
- ".*\\.(png|jpeg|jpg|webp|pdf|mp4|mp3)$"
# List of patterns to include (= limit) during preloading
include_list:
- ".*\\.(md|txt|markdown)$"
# Path to the output file (if null, output to STDOUT) # Path to the output file (if null, output to STDOUT)
output_path: null output_path: null