Skip to content

Commit

Permalink
Removed need for logger and slop
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBakerEffendi committed Oct 31, 2024
1 parent 4dc4f80 commit 353e3a5
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 57 deletions.
4 changes: 0 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
62 changes: 42 additions & 20 deletions exe/ruby_ast_gen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
46 changes: 18 additions & 28 deletions lib/ruby_ast_gen.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
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
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)

Expand All @@ -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
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
4 changes: 2 additions & 2 deletions lib/ruby_ast_gen/node_handling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions sig/ruby_ast_gen.rbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
module RubyAstGen
VERSION: String
@logger: Logger

def self.logger: -> Logger
end

0 comments on commit 353e3a5

Please sign in to comment.