Sha256: f0587526287cfb79712e88c59e526a2ca40399bae56c9b83470286c32ef166ba

Contents?: true

Size: 1.01 KB

Versions: 4

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

module RSGem
  module Tasks
    class AddDependency
      attr_reader :context, :dependency

      def initialize(context:, dependency:)
        @context = context
        @dependency = dependency
      end

      def add
        return if already_installed?

        add_dependency
        write_to_gemspec
        dependency.install(context)
      end

      private

      def add_dependency
        gemspec_file.gsub!(/end\n\z/, code)
        gemspec_file << "\nend"
      end

      def already_installed?
        gemspec_file.match? Regexp.new("('|\")#{dependency.name}('|\")")
      end

      def code
        text = ["  spec.add_#{dependency.mode}_dependency '#{dependency.name}'", dependency.version]
        text.compact.join(', ')
      end

      def gemspec_file
        @gemspec_file ||= File.read(context.gemspec_path)
      end

      def write_to_gemspec
        File.open(context.gemspec_path, 'w') do |file|
          file.puts gemspec_file
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rsgem-0.1.3 lib/rsgem/tasks/add_dependency.rb
rsgem-0.1.2 lib/rsgem/tasks/add_dependency.rb
rsgem-0.1.1 lib/rsgem/tasks/add_dependency.rb
rsgem-0.1.0 lib/rsgem/tasks/add_dependency.rb