Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement Puppet Labs best-practice version handling

Instead of a configuration, or source, file containing a hard-coded version
Puppet Labs uses a pair of methods to determine the version of software:

In the case of pre-release software, software installed from git, or nightly
builds we strongly prefer to use the results of `git describe` to identify a
robust and reproducible version number.

In the case of release software we burn the release version string into the
final product, typically by modifying a `lib/${project}/version.rb` file that
defines a `Project::VERSION` constant that third parties can use.

As with our standards, `require "project/version"` doesn't cause the entire
suite to be loaded, so you can use that to make version-specific decisions
about interacting with the rest of the software suite.

Many of those capabilities are not presently used, or usable, with Project
Razor, but following standards ensures we can do that as we export more usable
code-level APIs.

It does improve reporting of the version that happens for current git
installations of the software.

Signed-off-by: Daniel Pittman <daniel@rimspace.net>
  • Loading branch information...
commit 472062e340a71d1c9ba436e2ac85fc01c54ba08f 1 parent f74276d
@daniel-pittman daniel-pittman authored
View
1  conf/version
@@ -1 +0,0 @@
-v0.7.0
View
1  lib/project_razor.rb
@@ -8,6 +8,7 @@
#puts "Logging path: #{$logging_path}"
require 'set'
+require "project_razor/version"
require "project_razor/object"
require "project_razor/filtering"
require "project_razor/utility"
View
17 lib/project_razor/cli.rb
@@ -12,7 +12,6 @@ class ProjectRazor::CLI
# Create a new instance of the CLI dispatcher, ready to service requests.
def initialize
@obj = ProjectRazor::Object.new
- @version = @obj.get_razor_version
@logger = @obj.get_logger
end
@@ -100,7 +99,7 @@ def call_razor_slice(raw_name, args)
end
def print_available_slices
- print "\n", "Available slices:\n\t"
+ print "\n", "Available slices\n\t".yellow
x = 1
slice_path = File.expand_path(File.join(File.dirname(__FILE__), 'slice', '*.rb'))
slices = Dir.glob(slice_path).map {|f| file2const(File.basename(f,File.extname(f))) }
@@ -120,9 +119,10 @@ def print_available_slices
def get_optparse
OptionParser.new do |opts|
- opts.banner = "\n" + "Razor - #{@version}".bold.green
- opts.separator "Usage: "
- opts.separator "razor [slice name] [command argument] [command argument]...".red
+ opts.version = ProjectRazor::VERSION
+ opts.banner = "#{opts.program_name} - #{opts.version}".green
+ opts.separator "Usage: ".yellow
+ opts.separator " razor [slice name] [command argument] [command argument]...".red
opts.separator ""
opts.separator "Switches".yellow
@@ -146,8 +146,13 @@ def get_optparse
@options[:nocolor] = true
end
+ opts.on_tail('-V', '--version', 'Display the version of Razor'.yellow) do
+ print opts.banner
+ exit
+ end
+
opts.on_tail( '-h', '--help', 'Display this screen'.yellow ) do
- puts opts
+ print opts
print_available_slices
exit
end
View
11 lib/project_razor/utility.rb
@@ -79,17 +79,6 @@ def validate_instance_vars
flag
end
- # Returns the version number as [String] from ./conf/version
- # @return [String]
- def get_razor_version
- file = File.open("#{$razor_root}/conf/version", "rb")
- version = file.read
- file.close
- version
- end
-
-
-
# Returns a true|false on whether the object type is valid
# requires that the instance variables for the object have @type & @hidden
# @param [String] namespace_prefix
View
45 lib/project_razor/version.rb
@@ -0,0 +1,45 @@
+module ProjectRazor
+ # Define the Project Razor version, and stash it in a constant. When we
+ # build a package for shipping we burn the version directly into this file,
+ # by modifying the text on the fly during package building.
+ #
+ # That "burns in" the value, but if that hasn't happened we do our best to
+ # work out a reasonable version number: If we are running from a git
+ # checkout, and we have git installed, determine this with `git describe`,
+ #
+ # If we don't have git, or it fails, but have the metadata, parse out some
+ # useful information directly from the checkout; this isn't great, but does
+ # give some guidance as to where the user was working.
+ #
+ # Finally, fall back to a default version placeholder.
+ #
+ #
+ # The next line is the one that our packaging tools modify, so please make
+ # sure that any change to it is discussed and agreed first.
+ version = "DEVELOPMENT"
+
+ if version == "DEVELOPMENT"
+ root = File.expand_path("../..", File.dirname(__FILE__))
+ if File.directory? File.join(root, ".git")
+ # In theory we can recover if git isn't installed, and read the HEAD and
+ # ref by hand, but that feels like way too much trouble right now.
+ git_version = %x{cd '#{root}' && git describe --tags --dirty --always 2>&1}
+ if $?.success?
+ version = 'v' + git_version
+ else # try to read manually...
+ head = File.read(File.join(root, ".git", "HEAD")) rescue nil
+ if head and match = %r{^ref: (refs/heads/(.[^\n]+))$}.match(head.lines.first)
+ version = 'git-' + match[2]
+ if sha = File.read(File.join(root, ".git", match[1]))[0,8] rescue nil
+ version += '-' + sha
+ end
+ end
+ end
+ end
+ end
+
+ # The running version of Project Razor. Razor follows the tenets of
+ # [semantic versioning](http://semver.org), and this version number reflects
+ # the rules as of SemVer 2.0.0-rc.1
+ VERSION = version
+end
Please sign in to comment.
Something went wrong with that request. Please try again.