<commit_before>require 'optparse'
require 'yaml'

require_relative '../lib/cli'
require_relative '../lib/app_generator'

module Moto

  class Parser
  
    def self.run(argv)
      begin
        # TODO Generate app / Change the way parsing options goes so it doesnt generate them if they`re not needed
        case argv[0]
          when '--version' then puts Moto::VERSION
          when 'run' then Moto::Cli.run run_parse(argv)
          when 'help' then show_help
          when 'generate' then Moto::AppGenerator.run generate_parse(argv)
          else puts "Command '#{argv[0]}' not recognized. Type help for list of supported commands."
        end
      rescue Exception => e
        puts e.message
      end
    end
    
    def self.run_parse(argv)

      # TODO: fix this dumb verification of current working directory.
      unless File.exists? "#{MotoApp::DIR}/config/moto.rb"
        msg = "Config file (config/moto.rb) not present.\n"
        msg << 'Does current working directory contain Moto application?'
        raise msg
      end    

      require 'bundler/setup'
      Bundler.require
      
      # Default options
      options = {}
      options[:reporters] = []
      # TODO Mandatory env var in app config
      options[:config] = eval(File.read("#{MotoApp::DIR}/config/moto.rb"))
      options[:environments] = []
      options[:name] = ""
         
      # Parse arguments
      # TODO eval ?
      # TODO const 
      # TODO reporters should be consts - not strings
      OptionParser.new do |opts|       
        opts.on('-t', '--tests Tests', Array) { |v| options[:tests ] = v }
        opts.on('-g', '--tags Tags', Array) { |v| options[:tags ] = v }
        opts.on('-r', '--reporters Reporters', Array) { |v| options[:reporters] = v }
        opts.on('-e', '--environments Environment', Array) { |v| options[:environments] = v }
        opts.on('-c', '--const Const') { |v| options[:const] = v }
        opts.on('-n', '--name Name') { |v| options[:name] = v }
        opts.on('-f', '--config Config') { |v| options[:config] = options[:config].merge( eval( v ) ) }
      end.parse!

      if options[:name].empty?
        options[:name] = evaluate_name(options[:tags], options[:tests])
      end

      if options[ :config ][ :moto ][ :runner ][ :mandatory_environment ] && options[ :environments ].empty?
        puts 'Environment is mandatory for this project.'
        exit 1
      end

      return options
    end
    
    def self.evaluate_name(tags, tests)
      tags ||= ""
      tests ||= ""<commit_msg>fix: loading custom config from file<commit_after>require 'optparse'
require 'yaml'

require_relative '../lib/cli'
require_relative '../lib/app_generator'

module Moto

  class Parser
  
    def self.run(argv)
      begin
        # TODO Generate app / Change the way parsing options goes so it doesnt generate them if they`re not needed
        case argv[0]
          when '--version' then puts Moto::VERSION
          when 'run' then Moto::Cli.run run_parse(argv)
          when 'help' then show_help
          when 'generate' then Moto::AppGenerator.run generate_parse(argv)
          else puts "Command '#{argv[0]}' not recognized. Type help for list of supported commands."
        end
      rescue Exception => e
        puts e.message
      end
    end
    
    def self.run_parse(argv)

      # TODO: fix this dumb verification of current working directory.
      unless File.exists? "#{MotoApp::DIR}/config/moto.rb"
        msg = "Config file (config/moto.rb) not present.\n"
        msg << 'Does current working directory contain Moto application?'
        raise msg
      end    

      require 'bundler/setup'
      Bundler.require
      
      # Default options
      options = {}
      options[:reporters] = []
      # TODO Mandatory env var in app config
      options[:config] = eval(File.read("#{MotoApp::DIR}/config/moto.rb"))
      options[:environments] = []
      options[:name] = ""
         
      # Parse arguments
      # TODO eval ?
      # TODO const 
      # TODO reporters should be consts - not strings
      OptionParser.new do |opts|       
        opts.on('-t', '--tests Tests', Array) { |v| options[:tests ] = v }
        opts.on('-g', '--tags Tags', Array) { |v| options[:tags ] = v }
        opts.on('-r', '--reporters Reporters', Array) { |v| options[:reporters] = v }
        opts.on('-e', '--environments Environment', Array) { |v| options[:environments] = v }
        opts.on('-c', '--const Const') { |v| options[:const] = v }
        opts.on('-n', '--name Name') { |v| options[:name] = v }
        opts.on('-f', '--config Config') { |v| options[:config].deep_merge!( eval( File.read(v) ) ) }
      end.parse!

      if options[:name].empty?
        options[:name] = evaluate_name(options[:tags], options[:tests])
      end

      if options[ :config ][ :moto ][ :runner ][ :mandatory_environment ] && options[ :environments ].empty?
        puts 'Environment is mandatory for this project.'
        exit 1
      end

      return options
    end
    
    def self.evaluate_name(tags, tests)
      tags ||= ""
      tests ||= ""
{'fraction_non_alphanumeric': 0.1374926657539605, 'fraction_numerical': 0.001173479366321142, 'mean_word_length': 2.292981326464907, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '709515', 'n_tokens_mistral': 1531, 'n_tokens_neox': 1508, 'n_words': 448}