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/.*
- ^\.vagrant/.*
- ^misc/.*

View file

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

View file

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

View file

@ -13,13 +13,13 @@ module CodePreloader
end
@sources : Array(String)
@filters_in : Array(Filter)
@filters_out : Array(Filter)
@include_filters : Array(Filter)
@exclude_filters : Array(Filter)
def initialize(dirs = [] of String)
@sources = [] of String
@filters_in = [] of Filter
@filters_out = [] of Filter
@include_filters = [] of Filter
@exclude_filters = [] of Filter
dirs.each { |dir| self.add(dir) }
end
@ -34,11 +34,11 @@ module CodePreloader
end
def select(&filter : Filter)
@filters_in << filter
@include_filters << filter
end
def reject(&filter : Filter)
@filters_out << filter
@exclude_filters << filter
end
def each(&block)
@ -56,16 +56,16 @@ module CodePreloader
must_reject = false
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)
end
keep = keep && must_select if @filters_in.any?
keep = keep && must_select if @include_filters.any?
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)
end
keep = keep && !must_reject if @filters_out.any?
keep = keep && !must_reject if @exclude_filters.any?
keep
end

View file

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

View file

@ -6,9 +6,14 @@
# - "path/to/repo1"
# - "path/to/repo2"
# List of patterns to ignore during preloading
ignore_list:
# List of patterns to exclude (= ignore) during preloading
exclude_list:
- ^\.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)
output_path: null