1# Copyright (c) 2013 Brandon Jones, Colin MacKenzie IV
2# 
3# This software is provided 'as-is', without any express or implied
4# warranty. In no event will the authors be held liable for any damages
5# arising from the use of this software.
6# 
7# Permission is granted to anyone to use this software for any purpose,
8# including commercial applications, and to alter it and redistribute it
9# freely, subject to the following restrictions:
10# 
11#  1. The origin of this software must not be misrepresented; you must not
12#     claim that you wrote the original software. If you use this software
13#     in a product, an acknowledgment in the product documentation would be
14#     appreciated but is not required.
15# 
16#  2. Altered source versions must be plainly marked as such, and must not
17#     be misrepresented as being the original software.
18# 
19#  3. This notice may not be removed or altered from any source distribution.
20
21# Pretty much everything here was ripped from Bundler.
22# https://github.com/carlhuda/bundler/blob/master/lib/bundler/gem_helper.rb
23module GLMatrix::ReleaseHelper
24  module_function
25
26  def release
27    guard_clean
28    guard_already_tagged
29    tag_version {
30      yield if block_given?
31      git_push
32    }
33  end
34
35  def base
36    GLMatrix.base_path.to_s
37  end
38
39  def git_push
40    perform_git_push
41    perform_git_push ' --tags'
42    Bundler.ui.confirm "Pushed git commits and tags"
43  end
44
45  def perform_git_push(options = '')
46    cmd = "git push #{options}"
47    out, code = sh_with_code(cmd)
48    raise "Couldn't git push. `#{cmd}' failed with the following output:\n\n#{out}\n" unless code == 0
49  end
50
51  def guard_already_tagged
52    if sh('git tag').split(/\n/).include?(version_tag)
53      raise("This tag has already been committed to the repo.")
54    end
55  end
56
57  def guard_clean
58    clean? or raise("There are files that need to be committed first.")
59  end
60
61  def clean?
62    sh_with_code("git diff --exit-code")[1] == 0
63  end
64
65  def tag_version
66    sh "git tag -a -m \"Version #{version}\" #{version_tag}"
67    Bundler.ui.confirm "Tagged #{version_tag}"
68    yield if block_given?
69  rescue
70    Bundler.ui.error "Untagged #{version_tag} due to error"
71    sh_with_code "git tag -d #{version_tag}"
72    raise
73  end
74
75  def version
76    GLMatrix::VERSION
77  end
78
79  def version_tag
80    "v#{version}"
81  end
82
83  def name
84    "gl-matrix"
85  end
86
87  def sh(cmd, &block)
88    out, code = sh_with_code(cmd, &block)
89    code == 0 ? out : raise(out.empty? ? "Running `#{cmd}' failed. Run this command directly for more detailed output." : out)
90  end
91
92  def sh_with_code(cmd, &block)
93    cmd << " 2>&1"
94    outbuf = ''
95    Bundler.ui.debug(cmd)
96    Dir.chdir(base) {
97      outbuf = `#{cmd}`
98      if $? == 0
99        block.call(outbuf) if block
100      end
101    }
102    [outbuf, $?]
103  end
104end
105