<commit_before>class String
  def undent
    gsub(/^.{#{slice(/^ +/).length}}/, '')
  end
end

module Kosmos
  module UserInterface
    class << self
      def init(args)
        ksp_path = extract_ksp_path_from_args(args)

        unless ksp_path
          Util.log <<-EOS.undent
            Error: You did not specify what folder you keep KSP in.

            Before doing anything else, please execute the command:

              kosmos init ksp-folder

            Where "ksp-folder" is the name of the folder where you keep KSP.
          EOS

          return
        end

        Util.log "Initializing Kosmos into #{ksp_path} (This will take a sec) ..."

        Kosmos::Versioner.init_repo(ksp_path)
        Kosmos.save_ksp_path(ksp_path)

        Util.log <<-EOS.undent
          Done! You're ready to begin installing mods.

          Install your first mod by running the command:

              kosmos install [name-of-the-mod]
        EOS
      end

      def install(args)
        return unless check_initialized!

        ksp_path = Kosmos.load_ksp_path

        packages = load_packages(args)
        return unless packages

        return unless check_installed_packages(ksp_path, packages)

        Util.log "Kosmos is about to install #{packages.count} package(s):"
        pretty_print_list(packages.map(&:title))

        packages.each do |package|
          Util.log "Installing package #{package.title} ..."
          package.new.install!(ksp_path)
          Util.log "Done!"
        end
      end

      def uninstall(args)
        return unless check_initialized!

        ksp_path = Kosmos.load_ksp_path

        package_name = args.shift
        unless package_name
          Util.log <<-EOS.undent
            Error: You need to specify what package to uninstall. Example:
                kosmos uninstall name-of-the-mod"
          EOS

          return
        end

        package = Kosmos::Package.find(package_name)

        if package
          Util.log "Preparing to uninstall #{package.title} ..."
          Kosmos::Versioner.uninstall_package(ksp_path, package)
          Util.log "Done! Just uninstalled: #{package.title}."
        else
          Util.log "Error: Kosmos couldn't find any packages with the name #{package_name.inspect}."
        end
      end

      def list(args)
        return unless check_initialized!

        ksp_path = Kosmos.load_ksp_path

        packages = Kosmos::Versioner.installed_packages(ksp_path)
        Util.log "You have installed #{packages.length} mod(s) using Kosmos:"
        pretty_print_list(packages)
      end

      def server(args)
        Web::App.start!
      end

      private

      # Extracts the path to init to from the user-supplied arguments. If the
      # user passed arguments via the commandline, then spaces in paths are
      # auto-handled beacuse they come from ARGV.
      #
      # If, however, the user passed arguments from the server, then these
      # arguments will still be separated by spaces. This method will then re-
      # join them.
      def extract_ksp_path_from_args(args)<commit_msg>Prevent double-uninstalls.<commit_after>class String
  def undent
    gsub(/^.{#{slice(/^ +/).length}}/, '')
  end
end

module Kosmos
  module UserInterface
    class << self
      def init(args)
        ksp_path = extract_ksp_path_from_args(args)

        unless ksp_path
          Util.log <<-EOS.undent
            Error: You did not specify what folder you keep KSP in.

            Before doing anything else, please execute the command:

              kosmos init ksp-folder

            Where "ksp-folder" is the name of the folder where you keep KSP.
          EOS

          return
        end

        Util.log "Initializing Kosmos into #{ksp_path} (This will take a sec) ..."

        Kosmos::Versioner.init_repo(ksp_path)
        Kosmos.save_ksp_path(ksp_path)

        Util.log <<-EOS.undent
          Done! You're ready to begin installing mods.

          Install your first mod by running the command:

              kosmos install [name-of-the-mod]
        EOS
      end

      def install(args)
        return unless check_initialized!

        ksp_path = Kosmos.load_ksp_path

        packages = load_packages(args)
        return unless packages

        return unless check_installed_packages(ksp_path, packages)

        Util.log "Kosmos is about to install #{packages.count} package(s):"
        pretty_print_list(packages.map(&:title))

        packages.each do |package|
          Util.log "Installing package #{package.title} ..."
          package.new.install!(ksp_path)
          Util.log "Done!"
        end
      end

      def uninstall(args)
        return unless check_initialized!

        ksp_path = Kosmos.load_ksp_path

        package_name = args.shift
        unless package_name
          Util.log <<-EOS.undent
            Error: You need to specify what package to uninstall. Example:
                kosmos uninstall name-of-the-mod"
          EOS

          return
        end

        package = Kosmos::Package.find(package_name)
        installed_packages = Kosmos::Versioner.installed_packages(ksp_path)

        if !package
          Util.log "Error: Kosmos couldn't find any packages with the name #{package_name.inspect}."
        elsif !installed_packages.include?(package.title)
          Util.log <<-EOS.undent
            Error: #{package.title} is not currently installed.

            There are three reasons you may get this error:

              1. You have already uninstalled #{package.title}.
              2. You have never previously installed #{package.title}.
              3. You did not use Kosmos to install #{package.title}.
          EOS
        else
          Util.log "Preparing to uninstall #{package.title} ..."
          Kosmos::Versioner.uninstall_package(ksp_path, package)
          Util.log "Done! Just uninstalled: #{package.title}."
        end
      end

      def list(args)
        return unless check_initialized!

        ksp_path = Kosmos.load_ksp_path

        packages = Kosmos::Versioner.installed_packages(ksp_path)
        Util.log "You have installed #{packages.length} mod(s) using Kosmos:"
        pretty_print_list(packages)
      end

      def server(args)
        Web::App.start!
      end

      private

      # Extracts the path to init to from the user-supplied arguments. If the
      # user passed arguments via the commandline, then spaces in paths are
      # auto-handled beacuse they come from ARGV.
      #
      # If, however, the user passed arguments from the server, then these
      # arguments will still be separated by spaces. This method will then re-
      # join them.
      def extract_ksp_path_from_args(args)
{'fraction_non_alphanumeric': 0.0773111737646535, 'fraction_numerical': 0.00044516990651431964, 'mean_word_length': 2.060853769300636, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3801769', 'n_tokens_mistral': 1950, 'n_tokens_neox': 1872, 'n_words': 582}