Sha256: 20587918409f1ff08ce6a7ef8d842d7eea5b8e2aaf299b6c8d22e7275ae9fcee

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

require 'debsacker/system_gateway'

module Debsacker
  module Package
    class Version
      attr_accessor :add_distro, :add_branch

      def initialize(version_type)
        @version_type = version_type
      end

      def generate
        begin
          case @version_type
            when 'tag', nil
              version = tag
            when 'commit'
              version = commit
            when 'datetime'
              version = datetime
            else
              version = @version_type
          end
        rescue
          version = '0.1.0'
        end

        version << "-#{ branch_name }" if add_branch
        version << "-#{ distro_name }" if add_distro

        version
      end

    protected

      def distro_name
        @_distro_name ||= SystemGateway.perform('lsb_release -s -c').strip || 'unknown'
      end

      def branch_name
        @_branch_name ||= SystemGateway.perform('git rev-parse --abbrev-ref HEAD').strip.sub(/([\/\\])/,'-') || 'unknown'
      end

      def tag
        last_ref = SystemGateway.perform('git symbolic-ref -q --short HEAD || git describe --tags --exact-match').strip
        tags = SystemGateway.perform('git tag').split($/)

        if tags.include? last_ref
          last_ref
        else
          tags.last
        end
      end

      def commit
        SystemGateway.perform('git log -n 1 --pretty=format:"%H"').strip
      end

      def datetime
        Time.now.strftime('%Y%m%d%H%M%S')
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
debsacker-0.1.5 lib/debsacker/package/version.rb
debsacker-0.1.3 lib/debsacker/package/version.rb