From a1ff045da367fe7bcf63ade9532101c6dc3290df Mon Sep 17 00:00:00 2001 From: Glenn Date: Thu, 4 Jan 2024 16:35:39 +0100 Subject: [PATCH] fix: remove ./ prefix in paths --- .code_preloader.yml | 18 ++--- spec/config_data/complex_config.yml | 21 ++++++ spec/config_data/simple_config.yml | 20 ++++++ spec/config_spec.cr | 100 ++++++++++++++++++++-------- src/cli.cr | 43 +++++++----- src/config.cr | 37 +++++----- src/filelist.cr | 16 +++-- 7 files changed, 179 insertions(+), 76 deletions(-) diff --git a/.code_preloader.yml b/.code_preloader.yml index 8630832..422bad0 100644 --- a/.code_preloader.yml +++ b/.code_preloader.yml @@ -1,15 +1,15 @@ --- ignore_list: - - ^\./\.git/.* - - ^\./\.vagrant/.* - - ^\./misc/.* - - ^\./bin/.* - - ^\./lib/.* - - ^\./misc/.* - - ^\./prompts/.* - - ^\./LICENSE - - ^\./Makefile + - ^\.git/.* + - ^\.vagrant/.* + - ^misc/.* + - ^bin/.* + - ^lib/.* + - ^misc/.* + - ^prompts/.* + - ^LICENSE + - ^Makefile - .*\.svg$ output_file_path: null diff --git a/spec/config_data/complex_config.yml b/spec/config_data/complex_config.yml index e69de29..924af8f 100644 --- a/spec/config_data/complex_config.yml +++ b/spec/config_data/complex_config.yml @@ -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 diff --git a/spec/config_data/simple_config.yml b/spec/config_data/simple_config.yml index e69de29..793b6fa 100644 --- a/spec/config_data/simple_config.yml +++ b/spec/config_data/simple_config.yml @@ -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 diff --git a/spec/config_spec.cr b/spec/config_spec.cr index bac4f70..a48e825 100644 --- a/spec/config_spec.cr +++ b/spec/config_spec.cr @@ -4,67 +4,109 @@ require "../src/config" CONFIG_FILE_SIMPLE = "spec/config_data/simple_config.yml" CONFIG_FILE_COMPLEX = "spec/config_data/complex_config.yml" +alias Config = CodePreloader::Config describe CodePreloader::Config do context "Initialization" do it "initializes with default values" do - config = CodePreloader::Config.new - config.repository_path_list.should eq [] of String - config.ignore_list.should eq [] of String - config.output_file_path.should be_nil - config.header_prompt_file_path.should be_nil - config.footer_prompt_file_path.should be_nil + config = Config.new + config.pack_options.should be_nil + config.init_options.should be_nil end end - context "Parse Arguments" do - it "parses repository paths correctly" do - args = ["path/to/repo1", "path/to/repo2"] - config = CodePreloader::Config.new + context "Handles global arguments" do + it "parses version option correctly" do + args = ["--version"] + config = Config.new 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 it "parses ignore paths correctly" do - args = ["-i", "path/to/ignore", "path/to/repo"] - config = CodePreloader::Config.new + args = ["pack", "-i", "path/to/ignore", "path/to/repo"] + config = Config.new 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 it "parses output file path correctly" do - args = ["-o", "output.txt", "path/to/repo"] - config = CodePreloader::Config.new + args = ["pack", "-o", "output.txt", "path/to/repo"] + config = Config.new 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 - context "Config File Loading" do + context "loads config file" do it "loads settings from a simple config file" do - config = CodePreloader::Config.new - args = ["-c", CONFIG_FILE_SIMPLE, "path/to/repo"] + config = Config.new + args = ["pack", "-c", CONFIG_FILE_SIMPLE, "path/to/repo"] config.parse_arguments(args) # Assuming the simple_config.yml has specific settings - config.repository_path_list.should eq ["simple/repo/path"] - config.ignore_list.should eq ["simple/ignore"] - config.output_file_path.should eq "simple_output.txt" - # ... assertions for other properties if needed ... + config.pack_options.should be_truthy + config.pack_options.try do |opts| + opts.repository_path_list.should eq ["path/to/repo"] + opts.ignore_list.should eq ["simple/ignore"] + opts.output_file_path.should eq "simple_output.txt" + end end it "loads settings from a complex config file" do repo_path ="path/to/repo" - config = CodePreloader::Config.new - args = ["-c", CONFIG_FILE_COMPLEX, repo_path] + config = Config.new + args = ["pack", "-c", CONFIG_FILE_COMPLEX, repo_path] config.parse_arguments(args) # Assuming the complex_config.yml has specific settings - config.repository_path_list.should eq [repo_path] - config.ignore_list.should eq ["complex/ignore1", "complex/ignore2"] - config.output_file_path.should eq "complex_output.txt" + config.pack_options.should be_truthy + config.pack_options.try do |opts| + 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 diff --git a/src/cli.cr b/src/cli.cr index d73eaef..405a38c 100644 --- a/src/cli.cr +++ b/src/cli.cr @@ -1,6 +1,7 @@ # vim: set ts=2 sw=2 et ft=crystal: +require "colorize" require "file" require "option_parser" require "magic" @@ -56,7 +57,7 @@ module CodePreloader "", "# List of patterns to ignore during preloading", "ignore_list:", - " - \"^\\./\\.git/.*\"", + " - ^\\.git/.*", "", "# Path to the output file (if null, output to STDOUT)", "output_file_path: null", @@ -99,6 +100,8 @@ module CodePreloader header_prompt_file_path = pack_options.header_prompt_file_path footer_prompt_file_path = pack_options.footer_prompt_file_path regular_output_file = false + header_prompt = "" + footer_prompt = "" filelist = FileList.new() filelist.add(repository_path_list) @@ -107,37 +110,37 @@ module CodePreloader end 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) end 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) end + output_file = STDOUT output_file_path.try do |path| break if path.empty? break if path == "-" regular_output_file = true output_file = File.open(path, "w") 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| + STDERR.puts "Processing file: #{file_path}".colorize(:yellow) process_file(file_path, output_file) 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 - STDERR.puts "Processing completed. Output written to: #{regular_output_file ? output_file_path : "stdout" }" + STDERR.puts "Processing completed.".colorize(:yellow) rescue e : Exception STDERR.puts "An error occurred during execution: #{e.message}" @@ -145,13 +148,23 @@ module CodePreloader end private def process_file(file_path : String, output_file : IO::FileDescriptor) - fh = File.open(file_path) - mime = Magic.mime_type.of(fh) + mime = "" + 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 "" - output_file.puts(fh.gets_to_end) - output_file.puts "" - fh.close + if clean_content !~ /^\s*$/ + output_file.puts(clean_content) + output_file.puts "" + end end end end diff --git a/src/config.cr b/src/config.cr index d979807..06560e5 100644 --- a/src/config.cr +++ b/src/config.cr @@ -207,14 +207,9 @@ module CodePreloader end private def validate_pack - abort("No pack options defined!") if @pack_options.nil? - @pack_options.try do |opts| - 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 + opts = @pack_options + abort("No pack options defined!") if opts.nil? + abort("Missing repository path.") if opts.repository_path_list.empty? end # Reads and returns a list of paths to ignore from the given file. @@ -226,17 +221,27 @@ module CodePreloader end 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) - @pack_options.try do |opts| - opts.config_file_path = config_file_path - opts.repository_path_list = root.repository_path_list || opts.repository_path_list - opts.ignore_list = root.ignore_list || opts.ignore_list - opts.output_file_path = root.output_file_path || opts.output_file_path - opts.header_prompt_file_path = root.header_prompt_file_path || opts.header_prompt_file_path - opts.footer_prompt_file_path = root.footer_prompt_file_path || opts.footer_prompt_file_path + opts.config_file_path = config_file_path + if opts.repository_path_list.nil? || opts.repository_path_list.try &.empty? + root.repository_path_list.try { |value| opts.repository_path_list = value } + end + if opts.ignore_list.nil? || opts.ignore_list.try &.empty? + root.ignore_list.try { |value| opts.ignore_list = value } + 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 rescue ex : Exception diff --git a/src/filelist.cr b/src/filelist.cr index b3cb50f..ef06ed1 100644 --- a/src/filelist.cr +++ b/src/filelist.cr @@ -56,15 +56,16 @@ module CodePreloader keep = true must_select = false must_reject = false + clean_path = path.to_s.gsub(/^\.\//,"") @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 keep = keep && must_select if @filters_in.any? keep = keep || is_dir @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 keep = keep && !must_reject if @filters_out.any? @@ -72,13 +73,14 @@ module CodePreloader end 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 - next if seen.includes? path.to_s + path = File.realpath(path) if File.symlink? clean_path + next if seen.includes? clean_path - seen << path.to_s - yield path.to_s + seen << clean_path + yield clean_path end end end