<commit_before>namespace :deploy do
namespace :appdata do
  require_relative '../../lib/optimacms/appdata/settings'


  task :check do
    on roles(:app) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :rake, "appdata:check"
        end
      end
    end
  end

  task :setup do
    on roles(:app) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :rake, "appdata:setup_all"
        end
      end
    end
  end


  ###
=begin
  task :upload do
    # variant. upload using local cache of git repo
    d_repo = Optimacms::Appdata::Settings.appdata_repo_path(release_path, app_env)
    git_repo = Optimacms::Appdata::Settings.repo_app_site_data(app_env)

    # init local repo
    %x(mkdir -p #{d_repo})
    %x(cd #{d_repo} && git init)
    %x(cd #{d_repo} && git remote add origin #{git_repo})

    # pull
    %x(cd #{d_repo} && git pull origin master)

    # copy
    Optimacms::Appdata::Settings.site_app_data_dirs.each do |d|
      d_server_base = File.dirname(d)

      #puts "from #{d} to #{d_server_base}"
      #exit
      upload!(d+"/", "#{current_path}/"+d_server_base+"/", :recursive => true)
    end




  end
=end

  # variant1. upload directly to server
=begin
    on roles(:app) do
      AppSettings.site_app_data_dirs.each do |d|
        d_server_base = File.dirname(d)

        #puts "from #{d} to #{d_server_base}"
        #exit
        upload!(d+"/", "#{current_path}/"+d_server_base+"/", :recursive => true)
      end

      # commit to remote repo
      Rake::Task["deploy:appdata:server_save"].invoke

      #
      Rake::Task["deploy:restart"].invoke

    end
=end


  task :server_save, [:name] => :environment do |t, args|
    on roles(:app) do
      within release_path do
        #with fetch(:bundle_env_variables, {}) do
        with rails_env: fetch(:rails_env) do
          execute :rake, "appdata:save['#{args[:name]}']"
        end
      end
    end
  end


  task :server_update, [:name] => :environment do |t, args|
    on roles(:app) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :rake, "appdata:update['#{args[:name]}']"
          #execute :bundle, 'exec', "rake appdata:update"
          #execute "cd #{release_path} && RAILS_ENV=#{e} bundle exec rake appdata:update"
          #execute "RAILS_ENV=#{e} rake appdata:update"
        end
      end
    end
  end




#### OLD
=begin
  task :server_save do
    on roles(:app) do
      p = File.join(shared_path, '..', 'repo-data')
<commit_msg>appdata<commit_after>namespace :deploy do
namespace :appdata do
  require_relative '../../lib/optimacms/appdata/settings'


  task :check do
    on roles(:app) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :rake, "appdata:check"
        end
      end
    end
  end

  task :setup do
    on roles(:app) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :rake, "appdata:setup_all"
        end
      end
    end
  end


  ###
=begin
  task :upload do
    # variant. upload using local cache of git repo
    d_repo = Optimacms::Appdata::Settings.appdata_repo_path(release_path, app_env)
    git_repo = Optimacms::Appdata::Settings.repo_app_site_data(app_env)

    # init local repo
    %x(mkdir -p #{d_repo})
    %x(cd #{d_repo} && git init)
    %x(cd #{d_repo} && git remote add origin #{git_repo})

    # pull
    %x(cd #{d_repo} && git pull origin master)

    # copy
    Optimacms::Appdata::Settings.site_app_data_dirs.each do |d|
      d_server_base = File.dirname(d)

      #puts "from #{d} to #{d_server_base}"
      #exit
      upload!(d+"/", "#{current_path}/"+d_server_base+"/", :recursive => true)
    end




  end
=end

  # variant1. upload directly to server
=begin
    on roles(:app) do
      AppSettings.site_app_data_dirs.each do |d|
        d_server_base = File.dirname(d)

        #puts "from #{d} to #{d_server_base}"
        #exit
        upload!(d+"/", "#{current_path}/"+d_server_base+"/", :recursive => true)
      end

      # commit to remote repo
      Rake::Task["deploy:appdata:server_save"].invoke

      #
      Rake::Task["deploy:restart"].invoke

    end
=end


  task :server_save do
    name = ENV['name']

    on roles(:app) do
      within release_path do
        #with fetch(:bundle_env_variables, {}) do
        with rails_env: fetch(:rails_env) do
          execute :rake, "appdata:save['#{name}']"
        end
      end
    end
  end


  task :server_update do
    name = ENV['name']

    on roles(:app) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :rake, "appdata:update['#{name}']"
          #execute :bundle, 'exec', "rake appdata:update"
          #execute "cd #{release_path} && RAILS_ENV=#{e} bundle exec rake appdata:update"
          #execute "RAILS_ENV=#{e} rake appdata:update"
        end
      end
    end
  end




#### OLD
=begin
  task :server_save do
    on roles(:app) do
      p = File.join(shared_path, '..', 'repo-data')

{'fraction_non_alphanumeric': 0.13094769956744004, 'fraction_numerical': 0.00039323633503735744, 'mean_word_length': 2.564821303433777, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '641337', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1715, 'n_words': 436}