<commit_before>require 'find'

SCM = ENV['SCM'] || 'svn'

if !(SCM == 'svn' || SCM == 'git' || SCM == 'git_with_svn')
  puts "#{SCM} is not supported. Please use svn or git."
  exit
end

INTEGRATION_TASKS = %w( 
    scm:status:check
    log:clear
    tmp:clear
    backup:local
    scm:update
    db:migrate
    test:units
    test:functionals
    test:integration
    spec:lib
    spec:models
    spec:helpers
    spec:controllers
    spec:views
    test:rcov:units
    test:rcov:units:verify
    test:rcov:functionals
    test:rcov:functionals:verify
    spec:rcov
    spec:rcov:verify
    test:plugins:selected
    spec:plugins:selected
    test:selenium:server:start
    test_acceptance
    test:selenium:server:stop
    scm:commit            
)

# Extract project name.
def project_name
  File.expand_path(RAILS_ROOT).split("/").last
end

# Run coverage test to check project coverage.
def rcov_verify
  sh "ruby #{File.expand_path(File.dirname(__FILE__) + '/../test/coverage_test.rb')}" 
end

# Stop mongrel server.
def stop_mongrel
    sh "mongrel_rails stop" if FileTest.exists?('log/mongrel.pid') 
end

# Print message with separator.
def p80(message)
  puts "-"*80
  puts message if message
  yield if block_given?
end

# Extract environment parameters
def environment_parameters(key)
  return ENV[key].split(/\s*\,\s*/) if ENV[key]
  []
end

# Check if need to skip task.
def skip_task?(task)
  environment_parameters('SKIP_TASKS').include?(task)  
end

# Remove old backups
def remove_old_backups(backup_dir)
  backups_to_keep = ENV['NUMBER_OF_BACKUPS_TO_KEEP'] || 30
  backups = []
  Find.find(backup_dir) { |file_name| backups << file_name if !File.directory?(file_name) && file_name =~ /.*\.tar.gz$/ }
  backups.sort!
  (backups - backups.last(backups_to_keep - 1)).each do |file_name|
    puts "Removing #{file_name}..."
    FileUtils.rm(file_name)
  end
end

namespace :backup do
  desc 'Creates a backup of the project in the local disk.'
  task :local do
    backup_dir = '../backup-' + project_name
    sh "mkdir #{backup_dir}" if !FileTest.exists?(backup_dir)
    remove_old_backups(backup_dir)
    sh "tar cfz #{backup_dir}/#{project_name}-#{Time.now.strftime('%Y%m%d-%H%M%S')}.tar.gz ../#{project_name}"
  end
end

namespace :scm do
  namespace :status do
    desc 'Check if project can be committed to the repository.'
    task :check do
      Rake::Task["#{SCM}:status:check"].invoke 
    end
  end

  desc 'Update files from repository.'
  task :update do
    Rake::Task["svn:update"].invoke if SCM == 'svn' 
    Rake::Task["git:pull"].invoke if SCM == 'git'
    Rake::Task["git_with_svn:rebase"].invoke if SCM == 'git_with_svn' 
  end
  
  desc 'Commit project.'
  task :commit do
    Rake::Task["svn:commit"].invoke if SCM == 'svn' 
    if SCM == 'git' 
      Rake::Task["git:push"].invoke 
    end
    Rake::Task["git_with_svn:dcommit"].invoke if SCM == 'git_with_svn'
  end
end


namespace :svn do
  namespace :status do
    desc 'Check if project can be committed to the repository.'
    task :check do
      files_out_of_sync = `svn status | grep -e '[?|!]'`
      if files_out_of_sync.size > 0
        puts "Files out of sync:"
        files_out_of_sync.each { |filename| puts filename }
        puts 
        exit
      end
    end
  end

  desc 'Update files from repository.'
  task :update do
    sh "svn update"
  end
  
  desc 'Commit project.'
  task :commit do
    message = ''
    message = "-m ''" if ENV['SKIP_COMMIT_MESSAGES']
    sh "svn commit #{message}"
  end
end

namespace :git do
  namespace :status do
    desc 'Check if project can be committed to the repository.'
    task :check do
      result = `git status`
      if !result.include?('nothing to commit')
        puts "Files out of sync:"
        puts result
        exit
      end
    end
  end

  desc 'Update files from repository.'
  task :pull do
    sh "git pull"
  end
  
  desc 'Commit project.'
  task :commit do
    message = ''
    message = "-m ''" if ENV['SKIP_COMMIT_MESSAGES']
    sh "git commit -a -v #{message}"
  end
  
  desc 'Push project.'
  task :push do
    sh "git push"
  end
end

namespace :git_with_svn do
  namespace :status do
    task :check do
      Rake::Task["git:status:check"].invoke
    end
  end
  
  desc 'Rebase the git project from svn repository'
  task :rebase do
    sh "git svn rebase"
  end
  
  desc 'Send all changes to svn repository'
  task :dcommit do
    sh "git svn dcommit"
  end<commit_msg>Fazendo com que o comportamento do integration para uso com o git seja mais parecido do que existia antes, com o uso do subversion.<commit_after>require 'find'

SCM = ENV['SCM'] || 'svn'

if !(SCM == 'svn' || SCM == 'git' || SCM == 'git_with_svn')
  puts "#{SCM} is not supported. Please use svn or git."
  exit
end

INTEGRATION_TASKS = %w( 
    scm:status:check
    log:clear
    tmp:clear
    backup:local
    scm:update
    db:migrate
    test:units
    test:functionals
    test:integration
    spec:lib
    spec:models
    spec:helpers
    spec:controllers
    spec:views
    test:rcov:units
    test:rcov:units:verify
    test:rcov:functionals
    test:rcov:functionals:verify
    spec:rcov
    spec:rcov:verify
    test:plugins:selected
    spec:plugins:selected
    test:selenium:server:start
    test_acceptance
    test:selenium:server:stop
    scm:commit            
)

# Extract project name.
def project_name
  File.expand_path(RAILS_ROOT).split("/").last
end

# Run coverage test to check project coverage.
def rcov_verify
  sh "ruby #{File.expand_path(File.dirname(__FILE__) + '/../test/coverage_test.rb')}" 
end

# Stop mongrel server.
def stop_mongrel
    sh "mongrel_rails stop" if FileTest.exists?('log/mongrel.pid') 
end

# Print message with separator.
def p80(message)
  puts "-"*80
  puts message if message
  yield if block_given?
end

# Extract environment parameters
def environment_parameters(key)
  return ENV[key].split(/\s*\,\s*/) if ENV[key]
  []
end

# Check if need to skip task.
def skip_task?(task)
  environment_parameters('SKIP_TASKS').include?(task)  
end

# Remove old backups
def remove_old_backups(backup_dir)
  backups_to_keep = ENV['NUMBER_OF_BACKUPS_TO_KEEP'] || 30
  backups = []
  Find.find(backup_dir) { |file_name| backups << file_name if !File.directory?(file_name) && file_name =~ /.*\.tar.gz$/ }
  backups.sort!
  (backups - backups.last(backups_to_keep - 1)).each do |file_name|
    puts "Removing #{file_name}..."
    FileUtils.rm(file_name)
  end
end

namespace :backup do
  desc 'Creates a backup of the project in the local disk.'
  task :local do
    backup_dir = '../backup-' + project_name
    sh "mkdir #{backup_dir}" if !FileTest.exists?(backup_dir)
    remove_old_backups(backup_dir)
    sh "tar cfz #{backup_dir}/#{project_name}-#{Time.now.strftime('%Y%m%d-%H%M%S')}.tar.gz ../#{project_name}"
  end
end

namespace :scm do
  namespace :status do
    desc 'Check if project can be committed to the repository.'
    task :check do
      Rake::Task["#{SCM}:status:check"].invoke 
    end
  end

  desc 'Update files from repository.'
  task :update do
    Rake::Task["svn:update"].invoke if SCM == 'svn' 
    Rake::Task["git:pull"].invoke if SCM == 'git'
    Rake::Task["git_with_svn:rebase"].invoke if SCM == 'git_with_svn' 
  end
  
  desc 'Commit project.'
  task :commit do
    Rake::Task["svn:commit"].invoke if SCM == 'svn' 
    if SCM == 'git' 
      Rake::Task["git:push"].invoke 
    end
    Rake::Task["git_with_svn:dcommit"].invoke if SCM == 'git_with_svn'
  end
end


namespace :svn do
  namespace :status do
    desc 'Check if project can be committed to the repository.'
    task :check do
      files_out_of_sync = `svn status | grep -e '[?|!]'`
      if files_out_of_sync.size > 0
        puts "Files out of sync:"
        files_out_of_sync.each { |filename| puts filename }
        puts 
        exit
      end
    end
  end

  desc 'Update files from repository.'
  task :update do
    sh "svn update"
  end
  
  desc 'Commit project.'
  task :commit do
    message = ''
    message = "-m ''" if ENV['SKIP_COMMIT_MESSAGES']
    sh "svn commit #{message}"
  end
end

namespace :git do
  namespace :status do
    desc 'Check if project can be committed to the repository.'
    task :check do
      result = `git status`
      if result.include?('Untracked files:') || result.include?('unmerged:')
        puts "Files out of sync:"
        puts result
        exit
      end
    end
  end

  desc 'Update files from repository.'
  task :pull do
    sh "git pull"
  end
  
  desc 'Commit project.'
  task :commit do
    message = ''
    message = "-m 'Committed by integration plugin.'" if ENV['SKIP_COMMIT_MESSAGES']
    sh "git commit -a -v #{message}"
  end
  
  desc 'Push project.'
  task :push do
    Rake::Task['git:commit'].invoke 
    sh "git push"
  end
end

namespace :git_with_svn do
  namespace :status do
    task :check do
      Rake::Task["git:status:check"].invoke
    end
  end
  
  desc 'Rebase the git project from svn repository'
  task :rebase do
    sh "git svn rebase"
  end
  
  desc 'Send all changes to svn repository'
  task :dcommit do
    sh "git svn dcommit"
  end
{'fraction_non_alphanumeric': 0.11687314987391734, 'fraction_numerical': 0.0017541936191207105, 'mean_word_length': 3.0614425645592163, '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': '2717833', 'n_tokens_mistral': 3211, 'n_tokens_neox': 3185, 'n_words': 901}