Skip to content

Commit

Permalink
Add commit type (#6)
Browse files Browse the repository at this point in the history
Add option to specify commit type
  • Loading branch information
Robert Murray committed Aug 28, 2016
1 parent 433a6f6 commit 35d85e3
Show file tree
Hide file tree
Showing 12 changed files with 505 additions and 208 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
@@ -1,9 +1,11 @@
language: ruby
cache: bundler
sudo: false
rvm:
- 1.9.3
- 2.0.0
- 2.1.0
- 2.2.0
- 2.3.0
- ruby-head
script: 'bundle exec rake'
3 changes: 2 additions & 1 deletion Gemfile
@@ -1,4 +1,5 @@
source 'https://rubygems.org'
# frozen_string_literal: true
source "https://rubygems.org"

# Specify your gem's dependencies in jekyll_version_plugin.gemspec
gemspec
59 changes: 50 additions & 9 deletions README.md
Expand Up @@ -5,35 +5,44 @@

### Description

A Liquid tag plugin for [Jekyll](http://jekyllrb.com/) that renders a version identifier for your Jekyll site sourced from the `git` repository containing your code. Great if you want to display the version of your project on your site dynamically each time your site is built.
A Liquid tag plugin for [Jekyll](http://jekyllrb.com/) that renders a version identifier for your Jekyll site sourced from the `git` repository containing your code. Great if you want to display the version of your project on your site automatically each time your site is built.

Identify and highlight the build of your project by calling the tag from your Jekyll project.

##### This Jekyll view code will generate ...

Given a `git` repo with the latest tag of *3.0.0* and *5* commits since tagged, the commit `ga189420` being the latest.

```ruby
# this Jekyll view code will generate ...
<p>Build: {% project_version %}</p>
```
##### This html
```html
<!-- this html -->
<p>Build: 3.0.0-5-ga189420</p>
```
### Features
Stand back, hold onto your hats... the **jekyll-version-plugin** has 1x feature.
The **jekyll-version-plugin** has 1x feature with a few options;
* Render a version of your project via `git`.
* Use the `git-describe` command with *long* or *short* option
* Use the `git rev-parse` on **HEAD** with *long* or *short* option
### Requirements
* Your project is version controlled in `git`.
### Usage
As mentioned by [Jekyll's documentation](http://jekyllrb.com/docs/plugins/#installing-a-plugin) you have two options; manually import the source file or require the plugin as a `gem`.
#### Require gem
Add the `jekyll_version_plugin` to your site `_config.yml` file for Jekyll to import the plugin as a gem.
Expand All @@ -42,53 +51,85 @@ Add the `jekyll_version_plugin` to your site `_config.yml` file for Jekyll to im
gems: ['jekyll_version_plugin']
```
#### Manual import
Just download the source file into your `_plugins` directory, e.g.
```bash
# Create the _plugins dir if needed and download project_version_tag plugin
$ mkdir -p _plugins && cd _plugins
$ wget https://raw.githubusercontent.com/rob-murray/jekyll-version-plugin/master/lib/project_version_tag.rb
$ wget https://raw.githubusercontent.com/rob-murray/jekyll-version-plugin/master/lib/jekyll_version_plugin.rb
```
#### Plugin tag usage
Wherever you want to display the version, just add the snippet below in your content and the version will be rendered when the Jekyll project is built.
The plugin can be called anywhere in your template with or without parameters;
```ruby
{% project_version *params %}
{% project_version type format %}
```
* **type** is what to use for versioning, either `tag` or `commit` - default of `tag`
* **format** is the format of the output, `short` or `long` - defaults to `short`
Wherever you want to display the version, just add one of the snippets below in your content and the version will be rendered when the Jekyll project is built.
```ruby
{% project_version %}
# Default options of `tag short`
{% project_version %} # => will run `git describe --tags --always`

# To render a short tag description like `3.0.0`
{% project_version tag short %} # => will run `git describe --tags --always`
# To render a more details tag description like `3.0.0-5-ga189420`
{% project_version tag long %} # => will run `git describe --tags --always --long`

# To render a short commitish like `151ebce`
{% project_version commit short %} # => will run `git rev-parse --short HEAD`
# To render a longer commitsh like `151ebce149c5886bcf2923db18d73742901eb976`
{% project_version commit long %} # => will run `git rev-parse HEAD`
```

This will simply output the version number, you can then apply your own styling as necessary. Or just `html` comment it out if you don't want it visible.


### Output

Happy path is a `git` repo with releases that are tagged, if this is the case then the output will be something like this;

`3.0.0` or `3.0.0-5-ga189420`

If the repository does not have any `tags` then it will grab the short sha of the last commit, for example;
If you select the *commit* option then the output will be something like this;

`a189420`

If you have selected the *tags* option and the repository does not have any `tags` then it will grab the short sha of the last commit, see above;

If for any reason this fails then the output will just be a *sorry* message. Sorry about that.

`Sorry, could not read project version at the moment`


### Technical wizardry

The plugin just calls `git describe` - For more information see [git documentation](http://git-scm.com/docs/git-describe).
The plugin just calls `git describe` or `git rev-parse HEAD` - For more information see [git-describe](http://git-scm.com/docs/git-describe) and [git rev-parse](https://git-scm.com/docs/git-rev-parse).

```bash
$ git describe --tags --long
$ git rev-parse --short HEAD
```


### Contributions

Please use the GitHub pull-request mechanism to submit contributions.


### License

This project is available for use under the MIT software license.
Expand Down
7 changes: 4 additions & 3 deletions Rakefile
@@ -1,8 +1,9 @@
# frozen_string_literal: true
require "bundler/gem_tasks"
require 'rspec/core'
require 'rspec/core/rake_task'
require "rspec/core"
require "rspec/core/rake_task"

task :default => :spec
task default: :spec

desc "Run all specs in spec directory (excluding plugin specs)"
RSpec::Core::RakeTask.new(:spec)
26 changes: 14 additions & 12 deletions jekyll_version_plugin.gemspec
@@ -1,22 +1,24 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
# frozen_string_literal: true
lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

Gem::Specification.new do |spec|
spec.name = 'jekyll_version_plugin'
spec.version = '1.0.2'
spec.authors = ['Rob Murray']
spec.email = ['robmurray17@gmail.com']
spec.summary = %q{A Liquid tag plugin for Jekyll that renders a version identifier for your Jekyll site, sourced from the git repository.}
spec.homepage = 'https://github.com/rob-murray/jekyll-version-plugin'
spec.license = 'MIT'
spec.name = "jekyll_version_plugin"
spec.version = "1.0.2"
spec.authors = ["Rob Murray"]
spec.email = ["robmurray17@gmail.com"]
spec.summary = "A Liquid tag plugin for Jekyll that renders a version identifier for your Jekyll site, sourced from the git repository."
spec.homepage = "https://github.com/rob-murray/jekyll-version-plugin"
spec.license = "MIT"
spec.required_ruby_version = ">= 1.9.3"

spec.files = `git ls-files -z`.split("\x0")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']
spec.require_paths = ["lib"]

spec.add_development_dependency 'bundler', '~> 1.6'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency "bundler", "~> 1.6"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec", "~> 3.5"
end
105 changes: 103 additions & 2 deletions lib/jekyll_version_plugin.rb
@@ -1,2 +1,103 @@
# Want to use a different name for the tag class than the gem name :)
require 'project_version_tag'
# frozen_string_literal: true
module Jekyll
module VersionPlugin
# A Jekyll Tag type that renders a version identifier for your Jekyll site
# sourced from the `git` repository containing your code.
#
class Tag < Liquid::Tag
NO_GIT_MESSAGE = "Oops, are you sure this is a git project?".freeze
UNABLE_TO_PARSE_MESSAGE = "Sorry, could not read the project version at the moment".freeze
OPTION_NOT_SPECIFIED = nil

attr_writer :system_wrapper # for testing

# A wrapper around system calls; mock/stub this in testing
class SystemWrapper
def run(command)
`#{command}`
end

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

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

def initialize(_name, params, _tokens)
super
args = params.split(/\s+/).map(&:strip)
# TODO: When min Ruby version is >=2.0 just use `to_h`
@params = Hash[[:type, :format].zip(args)]
end

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

private

attr_reader :params

# for testing
def system_wrapper
@system_wrapper ||= SystemWrapper.new
end

def current_version
@_current_version ||= begin
version = case params.fetch(:type, "tag")
when "tag", OPTION_NOT_SPECIFIED
git_describe || parse_head
when "commit"
parse_head
end

version || UNABLE_TO_PARSE_MESSAGE
end
end

def git_describe
tagged_version = case params.fetch(:format, "short")
when "short", OPTION_NOT_SPECIFIED
run("git describe --tags --always")
when "long"
run("git describe --tags --always --long")
end

tagged_version if command_succeeded?
end

def parse_head
head_commitish = case params.fetch(:format, "short")
when "short", OPTION_NOT_SPECIFIED
run("git rev-parse --short HEAD")
when "long"
run("git rev-parse HEAD")
end

head_commitish if command_succeeded?
end

def run(command)
system_wrapper.run(command)
end

def git_repo?
system_wrapper.git_repo?
end

def command_succeeded?
system_wrapper.command_succeeded?
end
end
end
end

Liquid::Template.register_tag("project_version", Jekyll::VersionPlugin::Tag)
51 changes: 0 additions & 51 deletions lib/project_version_tag.rb

This file was deleted.

0 comments on commit 35d85e3

Please sign in to comment.