Skip to content

Commit

Permalink
Merge pull request #90 from bmjen/version-info
Browse files Browse the repository at this point in the history
(SDK-274) Adds --version option
  • Loading branch information
scotje committed Jun 19, 2017
2 parents 8c6745a + a5f4deb commit 9eae594
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/pdk/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'pdk/i18n'
require 'pdk/logger'
require 'pdk/report'
require 'pdk/util/version'

module PDK::CLI
def self.run(args)
Expand Down Expand Up @@ -41,6 +42,11 @@ def self.template_url_option(dsl)
description _('The shortest path to better modules.')
default_subcommand 'help'

flag nil, :version, _('show version of pdk') do |_, _|
puts PDK::Util::Version.version_string
exit 0
end

flag :h, :help, _('show help for this command') do |_, c|
puts c.help
exit 0
Expand Down
3 changes: 3 additions & 0 deletions lib/pdk/generators/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require 'pdk/cli/exec'
require 'pdk/cli/input'
require 'pdk/util'
require 'pdk/util/version'

module PDK
module Generate
Expand All @@ -35,6 +36,8 @@ def self.invoke(opts = {})

module_interview(metadata, opts) unless opts[:'skip-interview'] # @todo Build way to get info by answers file

metadata.update!('pdk-version' => PDK::Util::Version.version_string)

temp_target_dir = PDK::Util.make_tmpdir_name('pdk-module-target')

prepare_module_directory(temp_target_dir)
Expand Down
34 changes: 34 additions & 0 deletions lib/pdk/util/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'pdk/version'
require 'pdk/cli/exec'

module PDK
module Util
module Version
def self.version_string
"#{PDK::VERSION} #{pdk_ref}".strip.freeze
end

def self.pdk_ref
ref = "#{pkg_sha} #{git_ref}".strip
ref.empty? ? nil : "(#{ref})"
end

def self.pkg_sha
version_file = File.join(File.expand_path('../../..', File.dirname(__FILE__)), 'VERSION')

if File.exist? version_file
ver = File.read(version_file)
sha = ver.strip.split('.')[-1] unless ver.nil?
end

sha
end

def self.git_ref
ref_result = PDK::CLI::Exec.git('--git-dir', File.join(File.expand_path('../../..', File.dirname(__FILE__)), '.git'), 'describe', '--all', '--long')

ref_result[:stdout].strip if ref_result[:exit_code].zero?
end
end
end
end

0 comments on commit 9eae594

Please sign in to comment.