<commit_before>require 'optparse'
require 'tempfile'
require_relative 'zip_file_generator'
require_relative 'uploaders/file_uploader'
require_relative 'uploaders/ipa_uploader'
require_relative 'uploaders/apk_uploader'

# -----------------------
# --- functions
# -----------------------

def fail_with_message(message)
  puts "\e[31m#{message}\e[0m"
  exit(1)
end

# ----------------------------
# --- Options

options = {
  build_url: nil,
  api_token: nil,
  is_compress: false,
  deploy_path: nil,
  notify_user_groups: nil,
  notify_email_list: nil,
  is_enable_public_page: true
}

parser = OptionParser.new do|opts|
  opts.banner = 'Usage: step.rb [options]'
  opts.on('-u', '--buildurl URL', 'Build URL') { |u| options[:build_url] = u unless u.to_s == '' }
  opts.on('-t', '--apitoken TOKEN', 'API Token') { |t| options[:api_token] = t unless t.to_s == '' }
  opts.on('-c', '--compress BOOL', 'Is Compress') { |c| options[:is_compress] = true if c.to_s == 'true' }
  opts.on('-d', '--deploypath PATH', 'Deploy Path') { |d| options[:deploy_path] = d unless d.to_s == '' }
  opts.on('-g', '--usergroups ARRAY', 'Notify User Groups') { |g| options[:notify_user_groups] = g unless g.to_s == '' }
  opts.on('-e', '--emaillist ARRAY', 'Notify Email List') { |e| options[:notify_email_list] = e unless e.to_s == '' }
  opts.on('-p', '--publicpage BOOL', 'Enable Public Page') { |p| options[:is_enable_public_page] = false if p.to_s == 'false' }
  opts.on('-h', '--help', 'Displays Help') do
    exit
  end
end
parser.parse!

fail_with_message('No build_url provided') unless options[:build_url]
fail_with_message('No api_token provided') unless options[:api_token]
fail_with_message('No deploy_path provided') unless options[:deploy_path]

options[:deploy_path] = File.absolute_path(options[:deploy_path])

if !Dir.exist?(options[:deploy_path]) && !File.exist?(options[:deploy_path])
  fail_with_message('Deploy source path does not exist at the provided path: ' + options[:deploy_path])
end

puts
puts '========== Configs =========='
puts " * Build URL: #{options[:build_url]}"
puts " * Build's API Token: #{options[:api_token]}"
puts " * is_compress: #{options[:is_compress]}"
puts " * deploy_path: #{options[:deploy_path]}"
puts " * notify_user_groups: #{options[:notify_user_groups]}"
puts " * notify_email_list: #{options[:notify_email_list]}"
puts " * is_enable_public_page: #{options[:is_enable_public_page]}"

# ----------------------------
# --- Main

begin
  public_page_url = ''
  dic = {}
  if File.directory?(options[:deploy_path])
    if options[:is_compress]
      puts
      puts '## Compressing the Deploy directory'
      tempfile = Tempfile.new(::File.basename(options[:deploy_path]))
      begin
        zip_archive_path = tempfile.path + '.zip'
        puts " (i) zip_archive_path: #{zip_archive_path}"
        zip_gen = ZipFileGenerator.new(options[:deploy_path], zip_archive_path)
        zip_gen.write
        tempfile.close

        fail 'Failed to create compressed ZIP file' unless File.exist?(zip_archive_path)

        public_page_url = deploy_file_to_bitrise(zip_archive_path,
                               options[:build_url],
                               options[:api_token]
                              )
      rescue => ex
        raise ex
      ensure
        tempfile.close
        tempfile.unlink
      end
    else
      puts
      puts '## Uploading the content of the Deploy directory separately'
      entries = Dir.entries(options[:deploy_path])
      entries.delete('.')
      entries.delete('..')

      entries = entries
        .map { |e| File.join(options[:deploy_path], e) }
        .select { |e| !File.directory?(e) }

      puts
      puts '======= List of files ======='
      puts ' No files found to deploy' if entries.length == 0
      entries.each { |filepth| puts " * #{filepth}" }
      puts '============================='
      puts

      all_public_urls = ''
      entries.each do |filepth|
        disk_file_path = filepth

        a_public_page_url = ''
        if disk_file_path.match('.*.ipa')
          a_public_page_url = deploy_ipa_to_bitrise(
            disk_file_path,
            options[:build_url],
            options[:api_token],
            options[:notify_user_groups],
            options[:notify_email_list],
            options[:is_enable_public_page]
          )
        elsif disk_file_path.match('.*.apk')
          a_public_page_url = deploy_apk_to_bitrise(disk_file_path,
                                options[:build_url],
                                options[:api_token],
                                options[:notify_user_groups],
                                options[:notify_email_list],
                                options[:is_enable_public_page]
                               )
        else
          a_public_page_url = deploy_file_to_bitrise(disk_file_path,
                                 options[:build_url],
                                 options[:api_token]
                                )
        end

        filename = File.basename(disk_file_path)
        filename_array = filename.split('-')
        station = filename
        if filename_array.count > 2 
          station = filename_array[1]
        end

        if dic[station] == nil
         dic[station] = "\n"
        end
        dic[station] = dic[station] + ">" + filename + ": " + a_public_page_url + "\n"

        all_public_urls = all_public_urls + File.basename(disk_file_path) + " " + a_public_page_url + "\n"
        puts "(i) Public instal page url: #{File.basename(disk_file_path)} (#{a_public_page_url})"

        public_page_url = a_public_page_url if public_page_url == '' && !a_public_page_url.nil? && a_public_page_url != ''
      end
    end
  else
    puts
    puts '## Deploying single file'
    a_public_page_url = ''
    if options[:deploy_path].match('.*.ipa')
      a_public_page_url = deploy_ipa_to_bitrise(
        options[:deploy_path],
        options[:build_url],
        options[:api_token],
        options[:notify_user_groups],
        options[:notify_email_list],
        options[:is_enable_public_page]<commit_msg>fix the output string<commit_after>require 'optparse'
require 'tempfile'
require_relative 'zip_file_generator'
require_relative 'uploaders/file_uploader'
require_relative 'uploaders/ipa_uploader'
require_relative 'uploaders/apk_uploader'

# -----------------------
# --- functions
# -----------------------

def fail_with_message(message)
  puts "\e[31m#{message}\e[0m"
  exit(1)
end

# ----------------------------
# --- Options

options = {
  build_url: nil,
  api_token: nil,
  is_compress: false,
  deploy_path: nil,
  notify_user_groups: nil,
  notify_email_list: nil,
  is_enable_public_page: true
}

parser = OptionParser.new do|opts|
  opts.banner = 'Usage: step.rb [options]'
  opts.on('-u', '--buildurl URL', 'Build URL') { |u| options[:build_url] = u unless u.to_s == '' }
  opts.on('-t', '--apitoken TOKEN', 'API Token') { |t| options[:api_token] = t unless t.to_s == '' }
  opts.on('-c', '--compress BOOL', 'Is Compress') { |c| options[:is_compress] = true if c.to_s == 'true' }
  opts.on('-d', '--deploypath PATH', 'Deploy Path') { |d| options[:deploy_path] = d unless d.to_s == '' }
  opts.on('-g', '--usergroups ARRAY', 'Notify User Groups') { |g| options[:notify_user_groups] = g unless g.to_s == '' }
  opts.on('-e', '--emaillist ARRAY', 'Notify Email List') { |e| options[:notify_email_list] = e unless e.to_s == '' }
  opts.on('-p', '--publicpage BOOL', 'Enable Public Page') { |p| options[:is_enable_public_page] = false if p.to_s == 'false' }
  opts.on('-h', '--help', 'Displays Help') do
    exit
  end
end
parser.parse!

fail_with_message('No build_url provided') unless options[:build_url]
fail_with_message('No api_token provided') unless options[:api_token]
fail_with_message('No deploy_path provided') unless options[:deploy_path]

options[:deploy_path] = File.absolute_path(options[:deploy_path])

if !Dir.exist?(options[:deploy_path]) && !File.exist?(options[:deploy_path])
  fail_with_message('Deploy source path does not exist at the provided path: ' + options[:deploy_path])
end

puts
puts '========== Configs =========='
puts " * Build URL: #{options[:build_url]}"
puts " * Build's API Token: #{options[:api_token]}"
puts " * is_compress: #{options[:is_compress]}"
puts " * deploy_path: #{options[:deploy_path]}"
puts " * notify_user_groups: #{options[:notify_user_groups]}"
puts " * notify_email_list: #{options[:notify_email_list]}"
puts " * is_enable_public_page: #{options[:is_enable_public_page]}"

# ----------------------------
# --- Main

begin
  public_page_url = ''
  dic = {}
  if File.directory?(options[:deploy_path])
    if options[:is_compress]
      puts
      puts '## Compressing the Deploy directory'
      tempfile = Tempfile.new(::File.basename(options[:deploy_path]))
      begin
        zip_archive_path = tempfile.path + '.zip'
        puts " (i) zip_archive_path: #{zip_archive_path}"
        zip_gen = ZipFileGenerator.new(options[:deploy_path], zip_archive_path)
        zip_gen.write
        tempfile.close

        fail 'Failed to create compressed ZIP file' unless File.exist?(zip_archive_path)

        public_page_url = deploy_file_to_bitrise(zip_archive_path,
                               options[:build_url],
                               options[:api_token]
                              )
      rescue => ex
        raise ex
      ensure
        tempfile.close
        tempfile.unlink
      end
    else
      puts
      puts '## Uploading the content of the Deploy directory separately'
      entries = Dir.entries(options[:deploy_path])
      entries.delete('.')
      entries.delete('..')

      entries = entries
        .map { |e| File.join(options[:deploy_path], e) }
        .select { |e| !File.directory?(e) }

      puts
      puts '======= List of files ======='
      puts ' No files found to deploy' if entries.length == 0
      entries.each { |filepth| puts " * #{filepth}" }
      puts '============================='
      puts

      all_public_urls = ''
      entries.each do |filepth|
        disk_file_path = filepth

        a_public_page_url = ''
        if disk_file_path.match('.*.ipa')
          a_public_page_url = deploy_ipa_to_bitrise(
            disk_file_path,
            options[:build_url],
            options[:api_token],
            options[:notify_user_groups],
            options[:notify_email_list],
            options[:is_enable_public_page]
          )
        elsif disk_file_path.match('.*.apk')
          a_public_page_url = deploy_apk_to_bitrise(disk_file_path,
                                options[:build_url],
                                options[:api_token],
                                options[:notify_user_groups],
                                options[:notify_email_list],
                                options[:is_enable_public_page]
                               )
        else
          a_public_page_url = deploy_file_to_bitrise(disk_file_path,
                                 options[:build_url],
                                 options[:api_token]
                                )
        end

        filename = File.basename(disk_file_path)
        filename_array = filename.split('-')
        station = filename
        if filename_array.count > 2 
          station = filename_array[1]
        end

        if dic[station] == nil
         dic[station] = "\n"
        end
        dic[station] = dic[station] + "- " + filename + ": " + a_public_page_url + "\n"

        all_public_urls = all_public_urls + File.basename(disk_file_path) + " " + a_public_page_url + "\n"
        puts "(i) Public instal page url: #{File.basename(disk_file_path)} (#{a_public_page_url})"

        public_page_url = a_public_page_url if public_page_url == '' && !a_public_page_url.nil? && a_public_page_url != ''
      end
    end
  else
    puts
    puts '## Deploying single file'
    a_public_page_url = ''
    if options[:deploy_path].match('.*.ipa')
      a_public_page_url = deploy_ipa_to_bitrise(
        options[:deploy_path],
        options[:build_url],
        options[:api_token],
        options[:notify_user_groups],
        options[:notify_email_list],
        options[:is_enable_public_page]
{'fraction_non_alphanumeric': 0.15276758783728703, 'fraction_numerical': 0.0011412733349637238, 'mean_word_length': 2.4911781445645986, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '577126', 'n_tokens_mistral': 3881, 'n_tokens_neox': 3840, 'n_words': 746}