Sha256: 722f88ca2cf7c9adad6b563f0c88381fed87d9af0ca30cafc7d28feb3bf762d1

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

module Jekyll
  class ProjectVersionTag < Liquid::Tag
    NO_GIT_MESSAGE = 'Oops, are you sure this is a git project?'
    UNABLE_TO_PARSE_MESSAGE = 'Sorry, could not read project version at the moment'

    def render(context)
      if git_repo?
        current_version.chomp
      else
        NO_GIT_MESSAGE
      end
    end

    private

    def current_version
      @_current_version ||= begin
        # attempt to find the latest tag, falling back to last commit
        version = git_describe || parse_head

        version || UNABLE_TO_PARSE_MESSAGE
      end
    end

    def git_describe
      tagged_version = %x{ git describe --tags --always }

      if command_succeeded?
        tagged_version
      end
    end

    def parse_head
      head_commitish = %x{ git rev-parse --short HEAD }

      if command_succeeded?
        head_commitish
      end
    end

    def command_succeeded?
      !$?.nil? && $?.success?
    end

    def git_repo?
      system('git rev-parse')
    end
  end
end

Liquid::Template.register_tag('project_version', Jekyll::ProjectVersionTag)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jekyll_version_plugin-1.0.2 lib/project_version_tag.rb