diff --git a/.gitignore b/.gitignore index d5a0040..f7076d6 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,9 @@ capybara-*.html rerun.txt pickle-email-*.html +/test-runs/ +/.idea/ + # TODO Comment out these rules if you are OK with secrets being uploaded to the repo config/initializers/secret_token.rb config/secrets.yml diff --git a/.travis.yml b/.travis.yml index c094d17..66a8c3b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ sudo: required language: ruby rvm: -- 2.4.2 +- 2.7.0 script: - "bundle exec rspec" notifications: diff --git a/cli.rb b/cli.rb index fd3b170..2e1bb2e 100644 --- a/cli.rb +++ b/cli.rb @@ -21,8 +21,44 @@ # You should have received a copy of the GNU Affero General Public # License along with this program. If not, see # . + require 'multi_json' -require_relative 'lib/xa/rules/parse' +require_relative 'lib/xa/rules/parse/content' + +include XA::Rules::Parse::Content + +# Determines if file is a rule or table, and runs relevant library method. +def parse_file(input_file, output_file) + + puts "Parsing #{input_file}" + + if output_file == nil + output_file = "#{input_file}.json" + end + if input_file.end_with?(".rule") + IO.write(output_file, MultiJson.dump(parse_rule(IO.read(input_file)), pretty: true)) + elsif input_file.end_with?(".table") + IO.write(output_file, MultiJson.dump(parse_table(IO.read(input_file)), pretty: true)) + else + raise 'File type not *.rule or *.table' + end +end + +# Executes on program start. For each file in a folder, or a single file, runs parse_file +def cli_run + if ARGV[0] == nil + raise 'Please provide a .rule or .table file to parse' + elsif File.directory?(ARGV[0]) + Dir.foreach(ARGV[0]) do |name| + file = File.join(ARGV[0], name) + if file.end_with?(".table") || file.end_with?(".rule") + parse_file(file, nil) + end + end + else + parse_file(ARGV[0], ARGV[1]) + end +end -include XA::Rules::Parse -IO.write(ARGV[1], MultiJson.dump(parse(IO.read(ARGV[0])), pretty: true)) +# Execute immediately +cli_run