Sha256: 0e8027e0d5b5457e5c6fcbe4b7a86090967479c1c7c0ad01a4c89309be3a90d0

Contents?: true

Size: 1.99 KB

Versions: 3

Compression:

Stored size: 1.99 KB

Contents

require_relative '../repo'

module Hillary
  class Repo
    class Version
      BRANCHES = [
        DEV = ENV['DEV_BRANCH'] || 'dev',
        MASTER = ENV['MASTER_BRANCH'] || 'master'
      ]

      InvalidRepositoryState = Class.new(StandardError)

      class << self
        def create!(repo = Repo.new('.'), options = {})
          options = {production: ENV['PRODUCTION']}.merge(options)

          version = new(repo, options)

          if version.master?
            if version.production?
              repo.create_production_tag
            else
              repo.create_rc_tag
            end

            version.validate!
          end

          version
        end
      end

      def initialize(repo, options = {})
        @repo = repo
        @options = options
      end

      def name
        master? ? tag : sha
      end

      def branch
        @branch ||= @repo.head.name
      end

      def tag
        @tag ||= production? ? production_tag : rc_tag
      end

      def sha
        @sha ||= @repo.head.commit.sha
      end

      def rc_tag
        @rc_tag ||= begin
          rc_tag = @repo.last_rc_tag
          rc_tag.name if rc_tag && rc_tag.commit.sha == sha
        end
      end
      alias_method :rc_tag?, :rc_tag

      def production_tag
        @production_tag ||= begin
          production_tag = @repo.last_production_tag
          production_tag.name if production_tag && production_tag.commit.sha == @repo.last_rc_tag.commit.sha
        end
      end
      alias_method :production_tag?, :production_tag

      def sluggable?
        BRANCHES.include?(branch)
      end

      def master?
        branch == MASTER
      end

      def dev?
        branch == DEV
      end

      def production?
        @options[:production]
      end

      def validate!
        if !@options[:production] && !rc_tag?
          raise InvalidRepositoryState,
                "Last RC tag '#{@repo.last_rc_tag.name}' did not point to the current head '#{branch}' (#{sha})"
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hillary-0.0.6 lib/hillary/repo/version.rb
hillary-0.0.5 lib/hillary/repo/version.rb
hillary-0.0.4 lib/hillary/repo/version.rb