diff --git a/Gemfile b/Gemfile index c1e2411..5f412c5 100644 --- a/Gemfile +++ b/Gemfile @@ -8,8 +8,4 @@ gem "rspec", "~> 3.0" gem "parser", "~> 3.3" -gem "slop", "~> 4.10" - -gem "logger", "~> 1.6" - gem "ostruct", "~> 0.6.0" diff --git a/exe/ruby_ast_gen.rb b/exe/ruby_ast_gen.rb index 930f483..3d6b36b 100755 --- a/exe/ruby_ast_gen.rb +++ b/exe/ruby_ast_gen.rb @@ -5,28 +5,50 @@ libs = File.expand_path("../../vendor/bundle/ruby/*/gems/**/lib", __FILE__) $LOAD_PATH.unshift *Dir.glob(libs) -require "slop" - require_relative "../lib/ruby_ast_gen" -begin - opts = Slop.parse do |o| - o.string '-i', '--input', 'The input file or directory', required: true - o.string '-o', '--output', 'The output directory', default: '.ast' - o.string '-e', '--exclude', 'The exclusion regex', default: '^(tests?|vendor|spec)' - o.string '-l', '--log', 'The logging level', default: 'warn' - o.on '--version', 'Print the version' do - puts RubyAstGen::VERSION - exit - end - o.on '--help', 'Print usage' do - puts o - exit - end +options = { + input: nil, + output: '.ast', + exclude: '^(tests?|vendor|spec)', +} + +# Parse ARGV manually +i = 0 +while i < ARGV.size + case ARGV[i] + when '-i', '--input' + i += 1 + options[:input] = ARGV[i] + when '-o', '--output' + i += 1 + options[:output] = ARGV[i] + when '-e', '--exclude' + i += 1 + options[:exclude] = ARGV[i] + when '--version' + puts RubyAstGen::VERSION + exit + when '--help' + puts <<-HELP +Usage: + -i, --input The input file or directory (required) + -o, --output The output directory (default: '.ast') + -e, --exclude The exclusion regex (default: '^(tests?|vendor|spec)') + --version Print the version + --help Print usage + HELP + exit + else + puts "Unknown option: #{ARGV[i]}" + exit 1 end -rescue Slop::Error => e - puts e.message - exit 1 # Exit with an error code + i += 1 +end + +if options[:input].nil? + puts "Error: '-i' or '--input' is required." + exit 1 end -RubyAstGen::parse(opts) +RubyAstGen::parse(options) diff --git a/lib/ruby_ast_gen.rb b/lib/ruby_ast_gen.rb index a338f05..f934fad 100644 --- a/lib/ruby_ast_gen.rb +++ b/lib/ruby_ast_gen.rb @@ -1,17 +1,25 @@ require 'parser/current' require 'fileutils' require 'json' -require 'logger' require_relative 'ruby_ast_gen/version' require_relative 'ruby_ast_gen/node_handling' module RubyAstGen - @logger = Logger.new(STDOUT) + module Logger - def self.logger - @logger + def self.info(message) + puts "[INFO] #{message}" + end + + def self.warn(message) + puts "[WARN] #{message}" + end + + def self.error(message) + puts "[ERR] #{message}" + end end # Main method to parse the input and generate the AST output @@ -19,7 +27,6 @@ def self.parse(opts) input_path = opts[:input] output_dir = opts[:output] exclude_regex = Regexp.new(opts[:exclude]) - self.setup_logger(opts[:log]) FileUtils.mkdir_p(output_dir) @@ -28,7 +35,7 @@ def self.parse(opts) elsif File.directory?(input_path) process_directory(input_path, output_dir, exclude_regex) else - puts "Error: #{input_path} is neither a file nor a directory." + RubyAstGen::Logger::error "#{input_path} is neither a file nor a directory." exit 1 end end @@ -42,7 +49,7 @@ def self.process_file(file_path, output_dir, exclude_regex, base_dir) relative_input_path = file_path.sub("#{base_dir}/", '') # Skip if the file matches the exclusion regex if exclude_regex && exclude_regex.match?(relative_input_path) - @logger.info "Excluding: #{relative_input_path}" + RubyAstGen::Logger::info "Excluding: #{relative_input_path}" return end @@ -54,11 +61,10 @@ def self.process_file(file_path, output_dir, exclude_regex, base_dir) output_path = File.join(output_dir, "#{relative_path}.json") - # Write the AST as JSON to the output file File.write(output_path, JSON.pretty_generate(ast)) - @logger.info "Processed: #{relative_input_path} -> #{output_path}" + RubyAstGen::Logger::info "Processed: #{relative_input_path} -> #{output_path}" rescue StandardError => e - @logger.error "'#{relative_input_path}' - #{e.message}" + RubyAstGen::Logger::error "'#{relative_input_path}' - #{e.message}" end end @@ -68,7 +74,7 @@ def self.process_directory(dir_path, output_dir, exclude_regex) relative_dir = path.sub("#{dir_path}/", '') if exclude_regex.match?(relative_dir) - @logger.info "Excluding: #{relative_dir}" + RubyAstGen::Logger::info "Excluding: #{relative_dir}" next end # Create mirrored directory structure in output @@ -92,7 +98,7 @@ def self.parse_file(file_path, relative_input_path) json_ast[:rel_file_path] = relative_input_path json_ast rescue Parser::SyntaxError => e - @logger.error "Failed to parse #{file_path}: #{e.message}" + RubyAstGen::Logger::error "Failed to parse #{file_path}: #{e.message}" nil end @@ -101,20 +107,4 @@ def self.ruby_file?(file_path) ['.rb', '.gemspec', 'Rakefile'].include?(ext) || file_path.end_with?('.rb') end - def self.setup_logger(level) - case level.downcase - when 'debug' - @logger.level = Logger::DEBUG - when 'info' - @logger.level = Logger::INFO - when 'warn' - @logger.level = Logger::WARN - when 'error' - @logger.level = Logger::ERROR - when 'fatal' - @logger.level = Logger::FATAL - else - @logger.level = Logger::WARN - end - end end diff --git a/lib/ruby_ast_gen/node_handling.rb b/lib/ruby_ast_gen/node_handling.rb index 8723388..41d1d35 100644 --- a/lib/ruby_ast_gen/node_handling.rb +++ b/lib/ruby_ast_gen/node_handling.rb @@ -39,7 +39,7 @@ def self.ast_to_json(node, code, current_depth: 0, file_path: nil) code: self.extract_code_snippet(loc, code) } if current_depth >= MAX_NESTING_DEPTH - RubyAstGen::logger.warn "Reached max JSON depth on a #{node.type.to_s} node" + RubyAstGen::Logger::warn "Reached max JSON depth on a #{node.type.to_s} node" return { type: node.type.to_s, meta_data: meta_data, nested: true } end @@ -228,7 +228,7 @@ def self.add_node_properties(node_type, base_map, file_path) base_map[:children] = children else - RubyAstGen::logger.warn "Unhandled AST node type: #{node_type} - #{file_path}" + RubyAstGen::Logger::warn "Unhandled AST node type: #{node_type} - #{file_path}" base_map[:children] = children end end diff --git a/sig/ruby_ast_gen.rbs b/sig/ruby_ast_gen.rbs index 9ed7657..b5f5151 100644 --- a/sig/ruby_ast_gen.rbs +++ b/sig/ruby_ast_gen.rbs @@ -1,6 +1,3 @@ module RubyAstGen VERSION: String - @logger: Logger - - def self.logger: -> Logger end