Sha256: a3d6376ea878ac747e71103ccbba2da13f36419daf0daa8d2019f940e1ebe372

Contents?: true

Size: 1.95 KB

Versions: 3

Compression:

Stored size: 1.95 KB

Contents

require_relative '../repo'

module Hillary
  class Repo
    class Version
      BRANCHES = [
        DEV = 'dev',
        MASTER = '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.3 lib/hillary/repo/version.rb
hillary-0.0.2 lib/hillary/repo/version.rb
hillary-0.0.1 lib/hillary/repo/version.rb