-
Notifications
You must be signed in to change notification settings - Fork 6
For funsies: Crystal implementation #13
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,6 @@ | |
/pkg/ | ||
/spec/reports/ | ||
/tmp/ | ||
/crystal_build/ | ||
|
||
Gemfile.lock |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,23 @@ | ||
require "bundler/gem_tasks" | ||
require "rake/testtask" | ||
require "rake/clean" | ||
|
||
CLEAN << "crystal_build" | ||
|
||
Rake::TestTask.new(:test) do |t| | ||
t.libs << "test" | ||
t.libs << "lib" | ||
t.test_files = FileList["test/**/*_test.rb"] | ||
end | ||
|
||
task :default => :test | ||
namespace :crystal do | ||
directory "crystal_build" | ||
file "crystal_build/cfme-versions" => "crystal_build" do | ||
sh "crystal build --release lib/cfme-versions.cr -o crystal_build/cfme-versions" | ||
end | ||
|
||
desc "Build the crystal " | ||
task :build => "crystal_build/cfme-versions" | ||
end | ||
|
||
task :default => ["crystal:build", :test] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
module CFME | ||
class Versions | ||
FIELDS = [ | ||
"ManageIQ", "", "CFME", "CloudForms", "CP4MCM", "Ruby", "Rails", "PostgreSQL" | ||
] | ||
VERSIONS = [ | ||
%w[ N/A N/A 5.1.z 2.0 N/A N/A N/A N/A ], | ||
%w[ N/A N/A 5.2.z 3.0 N/A N/A N/A N/A ], | ||
%w[ Anand 1.y.z 5.3.z 3.1 N/A N/A N/A N/A ], | ||
%w[ Botvinnik 2.y.z 5.4.z 3.2 N/A N/A N/A N/A ], | ||
%w[ Capablanca 3.y.z 5.5.z 4.0 N/A 2.2.z 4.2.z 9.4.z ], | ||
%w[ Darga 4.y.z 5.6.z 4.1 N/A 2.2.z 5.0.z 9.4.z ], | ||
%w[ Euwe 5.y.z 5.7.z 4.2 N/A 2.3.z 5.0.z 9.5.z ], | ||
%w[ Fine 6.y.z 5.8.z 4.5 N/A 2.3.z 5.0.z 9.5.z ], | ||
%w[ Gaprindashvili 7.y.z 5.9.z 4.6 N/A 2.3.z 5.0.z 9.5.z ], | ||
%w[ Hammer 8.y.z 5.10.z 4.7 N/A 2.4.z 5.0.z 9.5.z ], | ||
%w[ Ivanchuk 9.y.z 5.11.z 5.0 1.2,1.3 2.5.z 5.1.z 10.y ], | ||
%w[ Jansa 10.y.z N/A N/A 2.0 2.5.z 5.2.z 10.y ], | ||
%w[ Kasparov 11.y.z N/A N/A N/A 2.6.z 5.2.z 10.y ] | ||
] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder what There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was apparently a harder question to answer than I was expecting... Ran the following: $ crystal tool format lib/cfme-versions.cr
$ git diff
# nothing changed Thought that was surprising (my crystal writing can't be "THAT" good...) Ran it with And then proceeded to go on a wild goose chase to figure out where this is implemented... with still no answer. Unless it won't change it, then in that case, "it won't change a thing". Edit: Guess I will have to grok this monster to be able to figure it out... |
||
|
||
def self.run(argv = ARGV) | ||
until argv.empty? | ||
arg = argv.shift | ||
|
||
# The `return` statements are there for specs | ||
case arg | ||
when "--version" then return print_version | ||
when "--help" then return print_help | ||
end | ||
end | ||
|
||
CFME::Versions.print_table | ||
end | ||
|
||
def self.version | ||
VERSIONS.last[1].split(".").first + ".0.0" | ||
end | ||
|
||
def self.print_help | ||
puts <<-HELP + "\n\n" | ||
usage: cfme-versions [OPTION]... | ||
|
||
Options: | ||
|
||
--version Prints the version number and exits | ||
--help This help | ||
HELP | ||
exit | ||
end | ||
|
||
def self.print_table | ||
# Print Header | ||
puts printable_row(FIELDS) | ||
puts printable_row(spacings.map { |size| "-" * size }) | ||
|
||
# Print version data | ||
VERSIONS.each do |version| | ||
version_data = version.map { |column| column == "N/A" ? "" : column } # remove N/A values | ||
puts printable_row(version_data) | ||
end | ||
end | ||
|
||
def self.print_version | ||
puts CFME::Versions.version | ||
exit | ||
end | ||
|
||
private def self.printable_row(data) | ||
"| #{data.map_with_index { |header, index| header.ljust(spacings[index]) }.join(" | ")} |" | ||
end | ||
|
||
@@spacings = [] of Int32 | ||
|
||
# Column width based on Miq::Versions.raw_data | ||
private def self.spacings | ||
spacings = @@spacings | ||
return spacings unless spacings.empty? | ||
|
||
spacings = FIELDS.map(&.size) | ||
VERSIONS.each do |version| | ||
version.each.with_index do |col, index| | ||
spacings[index] = [spacings[index].to_i, col.size].max | ||
end | ||
end | ||
|
||
@@spacings = spacings | ||
end | ||
end | ||
end | ||
|
||
CFME::Versions.run |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require "test_helper" | ||
|
||
class CrystalImplementation < Minitest::Test | ||
def cfme_versions_cr *args | ||
`crystal_build/cfme-versions #{args.join " "}` | ||
end | ||
|
||
def test_output_is_correct | ||
expected, _ = capture_io { CFME::Versions.print_help } | ||
assert_equal expected, cfme_versions_cr('--help') | ||
end | ||
|
||
def test_version_flag_is_correct | ||
expected, _ = capture_io { CFME::Versions.print_version } | ||
assert_equal expected, cfme_versions_cr('--version') | ||
end | ||
|
||
def test_help_text_is_correct | ||
expected, _ = capture_io { CFME::Versions.print_table } | ||
assert_equal expected, cfme_versions_cr | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably should do some kind of fallback if
crystal
doesn't exist on the system... but this is more like a POC so...