Sha256: cca7e76843805ef05b046bcc8a152486de4dd3d5800c2a2511a407b3b3ba2adc

Contents?: true

Size: 1.68 KB

Versions: 5

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

require "bundler"
require "open3"
require "etc"
require "dry/files"
require_relative "./system_call"

module Hanami
  module CLI
    class Bundler
      BUNDLE_GEMFILE = "BUNDLE_GEMFILE"
      private_constant :BUNDLE_GEMFILE

      DEFAULT_GEMFILE_PATH = "Gemfile"
      private_constant :DEFAULT_GEMFILE_PATH

      def self.require(*groups)
        return unless File.exist?(ENV.fetch(BUNDLE_GEMFILE) { DEFAULT_GEMFILE_PATH })

        ::Bundler.require(*groups)
      end

      def initialize(fs: Dry::Files.new, system_call: SystemCall.new)
        @fs = fs
        @system_call = system_call
      end

      def install
        parallelism_level = Etc.nprocessors
        bundle "install --jobs=#{parallelism_level} --quiet --no-color"
      end

      def install!
        install.tap do |result|
          raise "Bundle install failed\n\n\n#{result.err.inspect}" unless result.successful?
        end
      end

      def exec(cmd, env: nil, &blk)
        bundle("exec #{cmd}", env: env, &blk)
      end

      def bundle(cmd, env: nil, &blk)
        bundle_bin = which("bundle")
        hanami_env = "HANAMI_ENV=#{env} " unless env.nil?

        system_call.call("#{hanami_env}#{bundle_bin} #{cmd}",
                         env: {BUNDLE_GEMFILE => fs.expand_path(DEFAULT_GEMFILE_PATH)}, &blk)
      end

      private

      attr_reader :fs

      attr_reader :system_call

      # Adapted from https://stackoverflow.com/a/5471032/498386
      def which(cmd)
        ENV["PATH"].split(File::PATH_SEPARATOR).each do |path|
          exe = fs.join(path, cmd)
          return exe if fs.executable?(exe) && !fs.directory?(exe)
        end

        nil
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
hanami-cli-2.0.0.rc1 lib/hanami/cli/bundler.rb
hanami-cli-2.0.0.beta4 lib/hanami/cli/bundler.rb
hanami-cli-2.0.0.beta3 lib/hanami/cli/bundler.rb
hanami-cli-2.0.0.beta2 lib/hanami/cli/bundler.rb
hanami-cli-2.0.0.beta1 lib/hanami/cli/bundler.rb