fix: remove ./ prefix in paths
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Glenn Y. Rolland 2024-01-04 16:35:39 +01:00
parent d2d311a866
commit a1ff045da3
7 changed files with 179 additions and 76 deletions

View file

@ -1,15 +1,15 @@
--- ---
ignore_list: ignore_list:
- ^\./\.git/.* - ^\.git/.*
- ^\./\.vagrant/.* - ^\.vagrant/.*
- ^\./misc/.* - ^misc/.*
- ^\./bin/.* - ^bin/.*
- ^\./lib/.* - ^lib/.*
- ^\./misc/.* - ^misc/.*
- ^\./prompts/.* - ^prompts/.*
- ^\./LICENSE - ^LICENSE
- ^\./Makefile - ^Makefile
- .*\.svg$ - .*\.svg$
output_file_path: null output_file_path: null

View file

@ -0,0 +1,21 @@
---
# Example configuration for Code-Preloader
# List of repository paths to preload
# repository_path_list:
# - "path/to/repo1"
# - "path/to/repo2"
# List of patterns to ignore during preloading
ignore_list:
- complex/ignore1
- complex/ignore2
# Path to the output file (if null, output to STDOUT)
output_file_path: complex_output.txt
# Optional: Path to a file containing the header prompt
header_prompt_file_path: null
# Optional: Path to a file containing the footer prompt
footer_prompt_file_path: null

View file

@ -0,0 +1,20 @@
---
# Example configuration for Code-Preloader
# List of repository paths to preload
# repository_path_list:
# - "path/to/repo1"
# - "path/to/repo2"
# List of patterns to ignore during preloading
ignore_list:
- simple/ignore
# Path to the output file (if null, output to STDOUT)
output_file_path: simple_output.txt
# Optional: Path to a file containing the header prompt
header_prompt_file_path: null
# Optional: Path to a file containing the footer prompt
footer_prompt_file_path: null

View file

@ -4,67 +4,109 @@ require "../src/config"
CONFIG_FILE_SIMPLE = "spec/config_data/simple_config.yml" CONFIG_FILE_SIMPLE = "spec/config_data/simple_config.yml"
CONFIG_FILE_COMPLEX = "spec/config_data/complex_config.yml" CONFIG_FILE_COMPLEX = "spec/config_data/complex_config.yml"
alias Config = CodePreloader::Config
describe CodePreloader::Config do describe CodePreloader::Config do
context "Initialization" do context "Initialization" do
it "initializes with default values" do it "initializes with default values" do
config = CodePreloader::Config.new config = Config.new
config.repository_path_list.should eq [] of String config.pack_options.should be_nil
config.ignore_list.should eq [] of String config.init_options.should be_nil
config.output_file_path.should be_nil
config.header_prompt_file_path.should be_nil
config.footer_prompt_file_path.should be_nil
end end
end end
context "Parse Arguments" do context "Handles global arguments" do
it "parses repository paths correctly" do it "parses version option correctly" do
args = ["path/to/repo1", "path/to/repo2"] args = ["--version"]
config = CodePreloader::Config.new config = Config.new
config.parse_arguments(args) config.parse_arguments(args)
config.repository_path_list.should eq ["path/to/repo1", "path/to/repo2"] config.subcommand == Config::Subcommand::Version
config.pack_options.should be_nil
config.init_options.should be_nil
end
it "parses help options correctly" do
args = ["-h"]
config = Config.new
config.parse_arguments(args)
config.subcommand.should eq(Config::Subcommand::Help)
config.pack_options.should be_nil
config.init_options.should be_nil
args = ["--help"]
config = Config.new
config.parse_arguments(args)
config.subcommand.should eq(Config::Subcommand::Help)
config.pack_options.should be_nil
config.init_options.should be_nil
end
end
context "Handles pack arguments" do
it "parses repository paths correctly" do
args = ["pack", "path/to/repo1", "path/to/repo2"]
config = Config.new
config.parse_arguments(args)
config.subcommand.should eq(Config::Subcommand::Pack)
config.pack_options.should be_truthy
config.pack_options.try do |opts|
opts.repository_path_list.should eq ["path/to/repo1", "path/to/repo2"]
end
end end
it "parses ignore paths correctly" do it "parses ignore paths correctly" do
args = ["-i", "path/to/ignore", "path/to/repo"] args = ["pack", "-i", "path/to/ignore", "path/to/repo"]
config = CodePreloader::Config.new config = Config.new
config.parse_arguments(args) config.parse_arguments(args)
config.ignore_list.should eq ["path/to/ignore"] config.subcommand.should eq(Config::Subcommand::Pack)
config.pack_options.should be_truthy
config.pack_options.try do |opts|
opts.ignore_list.should eq ["path/to/ignore"]
end
end end
it "parses output file path correctly" do it "parses output file path correctly" do
args = ["-o", "output.txt", "path/to/repo"] args = ["pack", "-o", "output.txt", "path/to/repo"]
config = CodePreloader::Config.new config = Config.new
config.parse_arguments(args) config.parse_arguments(args)
config.output_file_path.should eq "output.txt" config.subcommand.should eq(Config::Subcommand::Pack)
config.pack_options.should be_truthy
config.pack_options.try do |opts|
opts.output_file_path.should eq "output.txt"
end
end end
end end
context "Config File Loading" do context "loads config file" do
it "loads settings from a simple config file" do it "loads settings from a simple config file" do
config = CodePreloader::Config.new config = Config.new
args = ["-c", CONFIG_FILE_SIMPLE, "path/to/repo"] args = ["pack", "-c", CONFIG_FILE_SIMPLE, "path/to/repo"]
config.parse_arguments(args) config.parse_arguments(args)
# Assuming the simple_config.yml has specific settings # Assuming the simple_config.yml has specific settings
config.repository_path_list.should eq ["simple/repo/path"] config.pack_options.should be_truthy
config.ignore_list.should eq ["simple/ignore"] config.pack_options.try do |opts|
config.output_file_path.should eq "simple_output.txt" opts.repository_path_list.should eq ["path/to/repo"]
# ... assertions for other properties if needed ... opts.ignore_list.should eq ["simple/ignore"]
opts.output_file_path.should eq "simple_output.txt"
end
end end
it "loads settings from a complex config file" do it "loads settings from a complex config file" do
repo_path ="path/to/repo" repo_path ="path/to/repo"
config = CodePreloader::Config.new config = Config.new
args = ["-c", CONFIG_FILE_COMPLEX, repo_path] args = ["pack", "-c", CONFIG_FILE_COMPLEX, repo_path]
config.parse_arguments(args) config.parse_arguments(args)
# Assuming the complex_config.yml has specific settings # Assuming the complex_config.yml has specific settings
config.repository_path_list.should eq [repo_path] config.pack_options.should be_truthy
config.ignore_list.should eq ["complex/ignore1", "complex/ignore2"] config.pack_options.try do |opts|
config.output_file_path.should eq "complex_output.txt" opts.repository_path_list.should eq [repo_path]
opts.ignore_list.should eq ["complex/ignore1", "complex/ignore2"]
opts.output_file_path.should eq "complex_output.txt"
end
end end
end end

View file

@ -1,6 +1,7 @@
# vim: set ts=2 sw=2 et ft=crystal: # vim: set ts=2 sw=2 et ft=crystal:
require "colorize"
require "file" require "file"
require "option_parser" require "option_parser"
require "magic" require "magic"
@ -56,7 +57,7 @@ module CodePreloader
"", "",
"# List of patterns to ignore during preloading", "# List of patterns to ignore during preloading",
"ignore_list:", "ignore_list:",
" - \"^\\./\\.git/.*\"", " - ^\\.git/.*",
"", "",
"# Path to the output file (if null, output to STDOUT)", "# Path to the output file (if null, output to STDOUT)",
"output_file_path: null", "output_file_path: null",
@ -99,6 +100,8 @@ module CodePreloader
header_prompt_file_path = pack_options.header_prompt_file_path header_prompt_file_path = pack_options.header_prompt_file_path
footer_prompt_file_path = pack_options.footer_prompt_file_path footer_prompt_file_path = pack_options.footer_prompt_file_path
regular_output_file = false regular_output_file = false
header_prompt = ""
footer_prompt = ""
filelist = FileList.new() filelist = FileList.new()
filelist.add(repository_path_list) filelist.add(repository_path_list)
@ -107,37 +110,37 @@ module CodePreloader
end end
if !header_prompt_file_path.nil? if !header_prompt_file_path.nil?
STDERR.puts "Loading header prompt from: #{header_prompt_file_path}" STDERR.puts "Loading header prompt from: #{header_prompt_file_path}".colorize(:yellow)
header_prompt = File.read(header_prompt_file_path) header_prompt = File.read(header_prompt_file_path)
end end
if !footer_prompt_file_path.nil? if !footer_prompt_file_path.nil?
STDERR.puts "Loading footer prompt from: #{footer_prompt_file_path}" STDERR.puts "Loading footer prompt from: #{footer_prompt_file_path}".colorize(:yellow)
footer_prompt = File.read(footer_prompt_file_path) footer_prompt = File.read(footer_prompt_file_path)
end end
output_file = STDOUT
output_file_path.try do |path| output_file_path.try do |path|
break if path.empty? break if path.empty?
break if path == "-" break if path == "-"
regular_output_file = true regular_output_file = true
output_file = File.open(path, "w") output_file = File.open(path, "w")
end end
STDERR.puts "Writing output to: #{regular_output_file ? output_file_path : "stdout" }".colorize(:yellow)
output_file = STDOUT
header_prompt = ""
footer_prompt = ""
output_file.puts header_prompt if header_prompt_file_path header_prompt_file_path.try { output_file.puts header_prompt }
STDERR.puts "Processing repository: #{repository_path_list}" STDERR.puts "Processing repository: #{repository_path_list}".colorize(:yellow)
filelist.each do |file_path| filelist.each do |file_path|
STDERR.puts "Processing file: #{file_path}".colorize(:yellow)
process_file(file_path, output_file) process_file(file_path, output_file)
end end
output_file.puts footer_prompt if footer_prompt_file_path footer_prompt_file_path.try { output_file.puts footer_prompt }
output_file.close if regular_output_file output_file.close if regular_output_file
STDERR.puts "Processing completed. Output written to: #{regular_output_file ? output_file_path : "stdout" }" STDERR.puts "Processing completed.".colorize(:yellow)
rescue e : Exception rescue e : Exception
STDERR.puts "An error occurred during execution: #{e.message}" STDERR.puts "An error occurred during execution: #{e.message}"
@ -145,13 +148,23 @@ module CodePreloader
end end
private def process_file(file_path : String, output_file : IO::FileDescriptor) private def process_file(file_path : String, output_file : IO::FileDescriptor)
fh = File.open(file_path) mime = ""
mime = Magic.mime_type.of(fh) clean_content = ""
File.open(file_path) do |fh|
mime = Magic.mime_type.of(fh)
clean_content = (
fh.gets_to_end
.strip
.gsub(/\n\s*\n\s*\n/,"\n\n")
)
end
output_file.puts "@@ File \"#{file_path}\" (Mime-Type: #{mime.inspect})" output_file.puts "@@ File \"#{file_path}\" (Mime-Type: #{mime.inspect})"
output_file.puts "" output_file.puts ""
output_file.puts(fh.gets_to_end) if clean_content !~ /^\s*$/
output_file.puts "" output_file.puts(clean_content)
fh.close output_file.puts ""
end
end end
end end
end end

View file

@ -207,14 +207,9 @@ module CodePreloader
end end
private def validate_pack private def validate_pack
abort("No pack options defined!") if @pack_options.nil? opts = @pack_options
@pack_options.try do |opts| abort("No pack options defined!") if opts.nil?
abort("Missing repository path.") if opts.repository_path_list.empty? abort("Missing repository path.") if opts.repository_path_list.empty?
if opts.output_file_path.nil? || opts.output_file_path.try(&.empty?)
STDERR.puts("Output file path not specified (using STDOUT)")
end
end
end end
# Reads and returns a list of paths to ignore from the given file. # Reads and returns a list of paths to ignore from the given file.
@ -226,17 +221,27 @@ module CodePreloader
end end
private def load_pack_config(config_file_path : String) private def load_pack_config(config_file_path : String)
config_str = File.read(config_file_path) opts = @pack_options
abort("FIXME") if opts.nil?
config_str = File.read(config_file_path)
root = Models::RootConfig.from_yaml(config_str) root = Models::RootConfig.from_yaml(config_str)
@pack_options.try do |opts| opts.config_file_path = config_file_path
opts.config_file_path = config_file_path if opts.repository_path_list.nil? || opts.repository_path_list.try &.empty?
opts.repository_path_list = root.repository_path_list || opts.repository_path_list root.repository_path_list.try { |value| opts.repository_path_list = value }
opts.ignore_list = root.ignore_list || opts.ignore_list end
opts.output_file_path = root.output_file_path || opts.output_file_path if opts.ignore_list.nil? || opts.ignore_list.try &.empty?
opts.header_prompt_file_path = root.header_prompt_file_path || opts.header_prompt_file_path root.ignore_list.try { |value| opts.ignore_list = value }
opts.footer_prompt_file_path = root.footer_prompt_file_path || opts.footer_prompt_file_path end
if opts.output_file_path.nil?
opts.output_file_path = root.output_file_path
end
if opts.header_prompt_file_path.nil?
root.header_prompt_file_path.try { |value| opts.header_prompt_file_path = value }
end
if opts.footer_prompt_file_path.nil?
root.footer_prompt_file_path.try { |value| opts.footer_prompt_file_path = value }
end end
rescue ex : Exception rescue ex : Exception

View file

@ -56,15 +56,16 @@ module CodePreloader
keep = true keep = true
must_select = false must_select = false
must_reject = false must_reject = false
clean_path = path.to_s.gsub(/^\.\//,"")
@filters_in.each do |filter_in| @filters_in.each do |filter_in|
must_select = must_select || filter_in.call(path.to_s) must_select = must_select || filter_in.call(clean_path)
end end
keep = keep && must_select if @filters_in.any? keep = keep && must_select if @filters_in.any?
keep = keep || is_dir keep = keep || is_dir
@filters_out.each do |filter_out| @filters_out.each do |filter_out|
must_reject = must_reject || filter_out.call(path.to_s) must_reject = must_reject || filter_out.call(clean_path)
end end
keep = keep && !must_reject if @filters_out.any? keep = keep && !must_reject if @filters_out.any?
@ -72,13 +73,14 @@ module CodePreloader
end end
walker.each do |path| walker.each do |path|
next if File.directory? path clean_path = path.to_s.gsub(/^\.\//,"")
next if File.directory? clean_path
path = File.realpath(path) if File.symlink? path path = File.realpath(path) if File.symlink? clean_path
next if seen.includes? path.to_s next if seen.includes? clean_path
seen << path.to_s seen << clean_path
yield path.to_s yield clean_path
end end
end end
end end