Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(SDK-274) Adds --version option #90

Merged
merged 4 commits into from
Jun 19, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debating flipping this prioritization. I think if someone installs the official pdk package, but modifies the underlying code (with an initialized git repo), we should be aware of that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case it would be good to know both, so maybe they should just each be conditionally added if available?

ref.nil? ? 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
Copy link
Contributor

@scotje scotje Jun 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems likesha is going to be an undefined var if the version file doesn't exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the version file doesn't exist, I'm expecting it to return nil so that we can fall back to git_ref up above.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, ruby hoists vars out of if/else blocks.

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