Sha256: b320a1f936b3538e10d0c747f7abd84b33bc71abc01c13a63db553947e5ad3bd

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

require 'fileutils'
require 'open-uri'

module PkgForge
  ##
  # Add source methods to Forge
  class Forge
    attr_writer :source

    Contract None => HashOf[Symbol => Any]
    def source
      @source ||= { type: 'git', path: 'upstream' }
    end

    private

    Contract None => nil
    def prepare_source!
      type_method = "#{source[:type]}_prepare_source"
      return send(type_method) if respond_to?(type_method, true)
      raise("Unknown source type: #{source[:type]}")
    end

    Contract None => nil
    def git_prepare_source
      run_local 'git submodule update --init --recursive'
      run_local "git clone --recursive '#{source[:path]}' #{tmpdir(:build)}"
    end

    Contract None => nil
    def tar_prepare_source
      dest_file = tmpfile(:source_tar)
      File.open(dest_file, 'wb') do |fh|
        open(source[:url], 'rb') do |request| # rubocop:disable Security/Open
          fh.write request.read
        end
        verify_file(dest_file, source[:checksum])
      end
      run "tar -xf #{dest_file} --strip-components=1"
    end

    Contract None => nil
    def empty_prepare_source
      # This source type is a no-op
    end
  end

  module DSL
    ##
    # Add source methods to Forge DSL
    class Forge
      Contract HashOf[Symbol => Any] => nil
      def source(params)
        @forge.source = params
        nil
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pkgforge-0.23.0 lib/pkgforge/components/source.rb
pkgforge-0.22.0 lib/pkgforge/components/source.rb
pkgforge-0.21.0 lib/pkgforge/components/source.rb