Sha256: c546a3545fe20c0b4a8ca5fd63963d7fcf549fac8448503277acd02ba19f222b

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require "bundler/ui/shell"
require "fileutils"
require "tocer"

module Gemsmith
  module Rake
    # Provides gem build functionality. Meant to be wrapped in Rake tasks.
    class Builder
      def initialize tocer: Tocer::Writer, shell: Bundler::UI::Shell.new, kernel: Kernel
        @tocer = tocer
        @shell = shell
        @kernel = kernel
      end

      def toc
        readme = File.join Dir.pwd, "README.md"
        tocer.new(readme).write
        shell.confirm "Updated gem table of contents."
      end

      def clean
        FileUtils.rm_rf "pkg"
        shell.confirm "Cleaned gem artifacts."
      end

      def validate
        return if `git status --porcelain`.empty?

        shell.error "Build failed: Gem has uncommitted changes."
        kernel.exit 1
      end

      def build gem_spec
        path = gem_spec.package_path

        if kernel.system "gem build #{gem_spec.name}.gemspec"
          FileUtils.mkdir_p "pkg"
          FileUtils.mv gem_spec.package_file_name, path
          shell.confirm "Built: #{path}."
        else
          shell.error "Unable to build: #{path}."
        end
      end

      def install gem_spec
        gem_name = "#{gem_spec.name} #{gem_spec.version}"

        if kernel.system "gem install #{gem_spec.package_path}"
          shell.confirm "Installed: #{gem_name}."
        else
          shell.error "Unable to install: #{gem_name}."
        end
      end

      private

      attr_reader :tocer, :shell, :kernel
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gemsmith-13.4.0 lib/gemsmith/rake/builder.rb
gemsmith-13.3.0 lib/gemsmith/rake/builder.rb
gemsmith-13.2.0 lib/gemsmith/rake/builder.rb