<commit_before>require 'fileutils'

require 'cerberus/utils'
require 'cerberus/constants'
require 'cerberus/config'
require 'cerberus/latch'
require 'cerberus/component_lazy_loader'

module Cerberus
  class AddCommand
    EXAMPLE_CONFIG = File.expand_path(File.dirname(__FILE__) + '/config.example.yml')
    
    def initialize(path, cli_options = {})
      @path, @cli_options = path, HashWithIndifferentAccess.new(cli_options)
    end
    
    def run
      scm_type = @cli_options[:scm] || Cerberus::SCM.guess_type(@path) || 'svn'
      scm = Cerberus::SCM.get(scm_type).new(@path, Config.new(nil, @cli_options))
      say "Can't find any #{scm_type} application under #{@path}" unless scm.url
      
      application_name = @cli_options[:application_name] || extract_project_name(@path)
      
      create_example_config
      
      config_name = "#{HOME}/config/#{application_name}.yml"
      say "Application #{application_name} already present in Cerberus" if File.exists?(config_name)
      
      app_config = { 'scm' => {
          'url' => scm.url,
          'type' =>  scm_type },
          'publisher' => {'mail' => {'recipients' => @cli_options[:recipients]}}
      }
      app_config['builder'] = {'type' => @cli_options[:builder]} if @cli_options[:builder]
      dump_yml(config_name, app_config)
      puts "Application '#{application_name}' has been added to Cerberus successfully" unless @cli_options[:quiet]
    end
    
    private
    def extract_project_name(path)
      path = File.expand_path(path) if test(?d, path)
      File.basename(path).strip
    end
    
    def create_example_config
      FileUtils.mkpath(HOME) unless test(?d, HOME)
      FileUtils.cp(EXAMPLE_CONFIG, CONFIG_FILE) unless test(?f, CONFIG_FILE)
    end
  end
  
  class RemoveCommand
    # DRY: this is ugly and needs refactoring. It duplicates functionality from other 
    # classes a lot.
    
    def initialize(application_name, cli_options = {})
      unless File.exists?("#{HOME}/config/#{application_name}.yml")
        say "Project '#{application_name}' does not present in Cerberus. Type 'cerberus list' to see the list of all active projects."
      end
      @app_root = "#{HOME}/work/#{application_name}"
      
      def_options = {:application_root => @app_root, :application_name => application_name}
      @config = Config.new(application_name, cli_options.merge(def_options))
    end
    
    def run
      application_name = @config[:application_name]
      
      config_name = "#{HOME}/config/#{application_name}.yml"
      
      if not File.exists?(config_name)
        say "Unknown application #{application_name}"
        exit(1) 
      end
      FileUtils.rm_r @config[:application_root]<commit_msg>Fix problem with deletion of unexisted folder<commit_after>require 'fileutils'

require 'cerberus/utils'
require 'cerberus/constants'
require 'cerberus/config'
require 'cerberus/latch'
require 'cerberus/component_lazy_loader'

module Cerberus
  class AddCommand
    EXAMPLE_CONFIG = File.expand_path(File.dirname(__FILE__) + '/config.example.yml')
    
    def initialize(path, cli_options = {})
      @path, @cli_options = path, HashWithIndifferentAccess.new(cli_options)
    end
    
    def run
      scm_type = @cli_options[:scm] || Cerberus::SCM.guess_type(@path) || 'svn'
      scm = Cerberus::SCM.get(scm_type).new(@path, Config.new(nil, @cli_options))
      say "Can't find any #{scm_type} application under #{@path}" unless scm.url
      
      application_name = @cli_options[:application_name] || extract_project_name(@path)
      
      create_example_config
      
      config_name = "#{HOME}/config/#{application_name}.yml"
      say "Application #{application_name} already present in Cerberus" if File.exists?(config_name)
      
      app_config = { 'scm' => {
          'url' => scm.url,
          'type' =>  scm_type },
          'publisher' => {'mail' => {'recipients' => @cli_options[:recipients]}}
      }
      app_config['builder'] = {'type' => @cli_options[:builder]} if @cli_options[:builder]
      dump_yml(config_name, app_config)
      puts "Application '#{application_name}' has been added to Cerberus successfully" unless @cli_options[:quiet]
    end
    
    private
    def extract_project_name(path)
      path = File.expand_path(path) if test(?d, path)
      File.basename(path).strip
    end
    
    def create_example_config
      FileUtils.mkpath(HOME) unless test(?d, HOME)
      FileUtils.cp(EXAMPLE_CONFIG, CONFIG_FILE) unless test(?f, CONFIG_FILE)
    end
  end
  
  class RemoveCommand
    # DRY: this is ugly and needs refactoring. It duplicates functionality from other 
    # classes a lot.
    
    def initialize(application_name, cli_options = {})
      unless File.exists?("#{HOME}/config/#{application_name}.yml")
        say "Project '#{application_name}' does not present in Cerberus. Type 'cerberus list' to see the list of all active projects."
      end
      @app_root = "#{HOME}/work/#{application_name}"
      
      def_options = {:application_root => @app_root, :application_name => application_name}
      @config = Config.new(application_name, cli_options.merge(def_options))
    end
    
    def run
      application_name = @config[:application_name]
      
      config_name = "#{HOME}/config/#{application_name}.yml"
      
      if not File.exists?(config_name)
        say "Unknown application #{application_name}"
        exit(1) 
      end
      FileUtils.rm_rf @config[:application_root]
{'fraction_non_alphanumeric': 0.1187830205866278, 'fraction_numerical': 0.00036436509382401167, 'mean_word_length': 3.3194335169158142, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '2096285', 'n_tokens_mistral': 1733, 'n_tokens_neox': 1703, 'n_words': 373}