<commit_before># rubocop:disable Metrics/LineLength

module Fastlane
  class Setup
    def run
      raise "Fastlane already set up at path #{folder}".yellow if (FastlaneFolder.setup? and not Helper.is_test?)

      show_infos
      response = agree('Do you want to get started? This will move your Deliverfile and Snapfile (if they exist) (y/n)'.yellow, true)
      return unless response
      response = agree('Do you have everything commited in version control? If not please do so! (y/n)'.yellow, true)
      return unless response

      begin
        FastlaneFolder.create_folder! unless Helper.is_test?
        copy_existing_files
        generate_app_metadata
        detect_installed_tools # after copying the existing files
        ask_to_enable_other_tools
        FileUtils.mkdir(File.join(folder, 'actions'))
        generate_fastfile
        Helper.log.info 'Successfully finished setting up fastlane'.green
      rescue Exception => ex # this will also be caused by Ctrl + C
        # Something went wrong with the setup, clear the folder again
        # and restore previous files
        Helper.log.fatal 'Error occured with the setup program! Reverting changes now!'.red
        restore_previous_state
        raise ex
      end
    end

    def show_infos
      Helper.log.info 'This setup will help you get up and running in no time.'.green
      Helper.log.info 'First, it will move the config files from `deliver` and `snapshot`'.green
      Helper.log.info "into the subfolder `fastlane`.\n".green
      Helper.log.info "Fastlane will check what tools you're already using and set up".green
      Helper.log.info 'the tool automatically for you. Have fun! '.green
    end

    def files_to_copy
      ['Deliverfile', 'Snapfile', 'deliver', 'snapshot.js', 'snapshot-iPad.js', 'SnapshotHelper.js', 'screenshots']
    end

    def copy_existing_files
      files_to_copy.each do |current|
        current = File.join(File.expand_path('..', FastlaneFolder.path), current)
        next unless File.exist?(current)
        file_name = File.basename(current)
        to_path = File.join(folder, file_name)
        Helper.log.info "Moving '#{current}' to '#{to_path}'".green
        FileUtils.mv(current, to_path)
      end
    end

    def generate_app_metadata
      Helper.log.info '------------------------------'
      Helper.log.info 'To not re-enter your username and app identifier every time you run one of the fastlane tools or fastlane, these will be stored from now on.'.green
      app_identifier = ask('App Identifier (com.krausefx.app): '.yellow)
      apple_id = ask('Your Apple ID: '.yellow)
      template = File.read("#{Helper.gem_path('fastlane')}/lib/assets/AppfileTemplate")
      template.gsub!('[[APP_IDENTIFIER]]', app_identifier)
      template.gsub!('[[APPLE_ID]]', apple_id)
      path = File.join(folder, 'Appfile')
      File.write(path, template)
      Helper.log.info "Created new file '#{path}'. Edit it to manage your preferred app metadata information.".green
    end

    def detect_installed_tools
      @tools = {}
      @tools[:deliver] = File.exist?(File.join(folder, 'Deliverfile'))
      @tools[:snapshot] = File.exist?(File.join(folder, 'Snapfile'))
      @tools[:xctool] = File.exist?(File.join(File.expand_path('..', folder), '.xctool-args'))
      @tools[:cocoapods] = File.exist?(File.join(File.expand_path('..', folder), 'Podfile'))
      @tools[:sigh] = false
    end

    def ask_to_enable_other_tools
      unless @tools[:deliver]
        if agree("Do you want to setup 'deliver', which is used to upload app screenshots, app metadata and app updates to the App Store or Apple TestFlight? (y/n)".yellow, true)
          Helper.log.info "Loading up 'deliver', this might take a few seconds"
          require 'deliver'
          Deliver::DeliverfileCreator.create(folder)
          @tools[:deliver] = true
        end
      else<commit_msg>Apple ID clarification<commit_after># rubocop:disable Metrics/LineLength

module Fastlane
  class Setup
    def run
      raise "Fastlane already set up at path #{folder}".yellow if (FastlaneFolder.setup? and not Helper.is_test?)

      show_infos
      response = agree('Do you want to get started? This will move your Deliverfile and Snapfile (if they exist) (y/n)'.yellow, true)
      return unless response
      response = agree('Do you have everything commited in version control? If not please do so! (y/n)'.yellow, true)
      return unless response

      begin
        FastlaneFolder.create_folder! unless Helper.is_test?
        copy_existing_files
        generate_app_metadata
        detect_installed_tools # after copying the existing files
        ask_to_enable_other_tools
        FileUtils.mkdir(File.join(folder, 'actions'))
        generate_fastfile
        Helper.log.info 'Successfully finished setting up fastlane'.green
      rescue Exception => ex # this will also be caused by Ctrl + C
        # Something went wrong with the setup, clear the folder again
        # and restore previous files
        Helper.log.fatal 'Error occured with the setup program! Reverting changes now!'.red
        restore_previous_state
        raise ex
      end
    end

    def show_infos
      Helper.log.info 'This setup will help you get up and running in no time.'.green
      Helper.log.info 'First, it will move the config files from `deliver` and `snapshot`'.green
      Helper.log.info "into the subfolder `fastlane`.\n".green
      Helper.log.info "Fastlane will check what tools you're already using and set up".green
      Helper.log.info 'the tool automatically for you. Have fun! '.green
    end

    def files_to_copy
      ['Deliverfile', 'Snapfile', 'deliver', 'snapshot.js', 'snapshot-iPad.js', 'SnapshotHelper.js', 'screenshots']
    end

    def copy_existing_files
      files_to_copy.each do |current|
        current = File.join(File.expand_path('..', FastlaneFolder.path), current)
        next unless File.exist?(current)
        file_name = File.basename(current)
        to_path = File.join(folder, file_name)
        Helper.log.info "Moving '#{current}' to '#{to_path}'".green
        FileUtils.mv(current, to_path)
      end
    end

    def generate_app_metadata
      Helper.log.info '------------------------------'
      Helper.log.info 'To not re-enter your username and app identifier every time you run one of the fastlane tools or fastlane, these will be stored from now on.'.green
      app_identifier = ask('App Identifier (com.krausefx.app): '.yellow)
      apple_id = ask('Your Apple ID (<EMAIL>): '.yellow)
      template = File.read("#{Helper.gem_path('fastlane')}/lib/assets/AppfileTemplate")
      template.gsub!('[[APP_IDENTIFIER]]', app_identifier)
      template.gsub!('[[APPLE_ID]]', apple_id)
      path = File.join(folder, 'Appfile')
      File.write(path, template)
      Helper.log.info "Created new file '#{path}'. Edit it to manage your preferred app metadata information.".green
    end

    def detect_installed_tools
      @tools = {}
      @tools[:deliver] = File.exist?(File.join(folder, 'Deliverfile'))
      @tools[:snapshot] = File.exist?(File.join(folder, 'Snapfile'))
      @tools[:xctool] = File.exist?(File.join(File.expand_path('..', folder), '.xctool-args'))
      @tools[:cocoapods] = File.exist?(File.join(File.expand_path('..', folder), 'Podfile'))
      @tools[:sigh] = false
    end

    def ask_to_enable_other_tools
      unless @tools[:deliver]
        if agree("Do you want to setup 'deliver', which is used to upload app screenshots, app metadata and app updates to the App Store or Apple TestFlight? (y/n)".yellow, true)
          Helper.log.info "Loading up 'deliver', this might take a few seconds"
          require 'deliver'
          Deliver::DeliverfileCreator.create(folder)
          @tools[:deliver] = true
        end
      else
{'fraction_non_alphanumeric': 0.1116077129357681, 'fraction_numerical': 0.0, 'mean_word_length': 2.9675785207700103, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, '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': '7110780', 'n_tokens_mistral': 2314, 'n_tokens_neox': 2258, 'n_words': 704}