Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

For funsies: Crystal implementation #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
/pkg/
/spec/reports/
/tmp/
/crystal_build/

Gemfile.lock
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
---
language: ruby
cache: bundler
# ref: https://github.com/travis-ci/travis-build/blob/543bb7e3/lib/travis/build/script/crystal.rb#L104-L108
before_script:
- travis_apt_get_update
- sudo apt-get install -y gcc pkg-config git tzdata libpcre3-dev libevent-dev libyaml-dev libgmp-dev libssl-dev libxml2-dev 2>&1 > /dev/null
- sudo snap install crystal --classic --channel=latest/stable
rvm:
- 2.5
- 2.6
Expand Down
15 changes: 14 additions & 1 deletion Rakefile
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"
Copy link
Contributor Author

@NickLaMuro NickLaMuro Aug 21, 2020

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...

end

desc "Build the crystal "
task :build => "crystal_build/cfme-versions"
end

task :default => ["crystal:build", :test]
92 changes: 92 additions & 0 deletions lib/cfme-versions.cr
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 ]
]
Copy link
Member

Choose a reason for hiding this comment

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

I wonder what crystal tool format will do to this (many editor implementations run this automatically on save, btw)

Copy link
Contributor Author

@NickLaMuro NickLaMuro Aug 25, 2020

Choose a reason for hiding this comment

The 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 --check... nothing.

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
22 changes: 22 additions & 0 deletions test/crystal_implementation_test.rb
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