Skip to content

Commit

Permalink
Create lists of facts
Browse files Browse the repository at this point in the history
Based on #262

YARD build not tested
  • Loading branch information
yakatz committed May 16, 2024
1 parent 7b73308 commit 487155c
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ vendor/
.yardoc
packer_cache/
*.box
database.md
doc/
reports/
hugo/public
4 changes: 2 additions & 2 deletions .yardopts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
--list-undoc
--files CHANGELOG.md,database.md
--title 'FacterDB Documentation'
--files CHANGELOG.md,reports/*
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ gem 'facter', ENV.fetch('FACTER_GEM_VERSION', nil), require: false
group :development do
gem 'faraday-retry', require: false
gem 'github_changelog_generator', '>= 1.16.4', require: false
gem 'yard'
gem 'redcarpet'
gem 'yard'
end
83 changes: 76 additions & 7 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ rescue LoadError
# No yard
else
YARD::Rake::YardocTask.new
task :yard => :database
task yard: :database # rubocop:disable Rake/Desc
end

# Generate a human-friendly OS label based on a given factset
Expand Down Expand Up @@ -108,9 +108,36 @@ def factset_to_os_label(fs)
label
end

def factset_hash_to_markdown(h, caption1: nil, caption2: nil, caption3: nil)
content = ''

h.each do |label1, data1|
indent = ''
content += "#{indent} - "
content += "#{caption1} " if caption1
content += "#{label1}\n"
indent += ' '
data1.each do |label2, data2|
content += "#{indent}- "
content += "#{caption2} " if caption2
content += "#{label2}:"

data2.each do |label3|
content += " #{caption3}" if caption3
content += " #{label3}"
end
content += "\n"
end
end

content
end

desc 'generate a markdown table of Facter/OS coverage'
task :database do
require_relative 'lib/facterdb'
FileUtils.mkdir_p 'reports'

# Turn on the source injection
old_env = ENV.fetch('FACTERDB_INJECT_SOURCE', nil)
ENV['FACTERDB_INJECT_SOURCE'] = 'true'
Expand All @@ -121,15 +148,42 @@ task :database do
facter_versions = factsets.map do |x|
Gem::Version.new(x[:facterversion].split('.')[0..1].join('.'))
end.uniq.sort.map(&:to_s)

# Old table
os_facter_matrix = {}

# New lists
os_facter_arch_list = {}
os_arch_facter_list = {}
facter_os_arch_list = {}
# Can't think of any reason facter_arch_os_list would be useful
arch_os_facter_list = {}
# Can't think of any reason arch_facter_os_list would be useful

# Process the facts and create a hash of all the OS and facter combinations
factsets.each do |facts|
fv = facts[:facterversion].split('.')[0..1].join('.')
arch = facts[:hardwaremodel] || 'Missing'
label = factset_to_os_label(facts)
os_facter_matrix[label] ||= {}
os_facter_matrix[label][fv] ||= 0
os_facter_matrix[label][fv] += 1

os_facter_arch_list[label] ||= {}
os_facter_arch_list[label][fv] ||= []
os_facter_arch_list[label][fv] << arch

os_arch_facter_list[label] ||= {}
os_arch_facter_list[label][arch] ||= []
os_arch_facter_list[label][arch] << fv

facter_os_arch_list[fv] ||= {}
facter_os_arch_list[fv][label] ||= []
facter_os_arch_list[fv][label] << arch

arch_os_facter_list[arch] ||= {}
arch_os_facter_list[arch][label] ||= []
arch_os_facter_list[arch][label] << fv
end
# Extract the OS list
os_versions = os_facter_matrix.keys.uniq.sort_by do |label|
Expand All @@ -139,22 +193,21 @@ task :database do
string_pieces.zip(number_pieces).flatten.compact
end


# Write out a nice table
os_version_width = (os_versions.map{|x| x.size } + [17]).max
os_version_width = (os_versions.map { |x| x.size } + [17]).max
facter_width = 3

rows = [
['operating system'.center(os_version_width)] + facter_versions,
['-' * os_version_width] + ['-' * facter_width] * facter_versions.length,
['-' * os_version_width] + (['-' * facter_width] * facter_versions.length),
]

os_versions.each do |label|
fvs = facter_versions.map do |facter_version|
version = os_facter_matrix[label][facter_version] || 0
version > 0 ? version.to_s : ''
(version > 0) ? version.to_s : ''
end
rows << [label.ljust(os_version_width)] + fvs.map { |fv| fv.center(facter_width) }
rows << ([label.ljust(os_version_width)] + fvs.map { |fv| fv.center(facter_width) })
end

content = "# Facter version and Operating System coverage\n\n"
Expand All @@ -163,7 +216,23 @@ task :database do
end
content += "\n\nWhere the number (1, 2 etc.) are the number of factsets for that OS and facter combination (e.g., x86_64 and i386 architectures).\n"

File.write(File.join(__dir__, 'database.md'), content)
File.write(File.join(__dir__, 'reports', 'database.md'), content)

content = "# Available Facts Sorted By OS -> Facter\n\n"
content += factset_hash_to_markdown(os_facter_arch_list, caption2: 'Facter')
File.write(File.join(__dir__, 'reports', 'os_facter_arch.md'), content)

content = "# Available Facts Sorted By OS -> Arch\n\n"
content += factset_hash_to_markdown(os_arch_facter_list)
File.write(File.join(__dir__, 'reports', 'os_arch_facter.md'), content)

content = "# Available Facts Sorted By Facter -> OS\n\n"
content += factset_hash_to_markdown(facter_os_arch_list, caption1: 'Facter')
File.write(File.join(__dir__, 'reports', 'facter_os_arch.md'), content)

content = "# Available Facts Sorted By Arch -> OS\n\n"
content += factset_hash_to_markdown(arch_os_facter_list)
File.write(File.join(__dir__, 'reports', 'arch_os_facter.md'), content)
end

begin
Expand Down
4 changes: 2 additions & 2 deletions facterdb.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ Gem::Specification.new do |s|
s.description = 'Contains facts from many Facter version on many Operating Systems'
s.licenses = 'Apache-2.0'

s.files = `git ls-files`.split("\n") + ['database.md']
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.files = `git ls-files`.split("\n") + Dir['reports/*']
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }

# we have that configured in our CI file
s.required_ruby_version = Gem::Requirement.new('>= 2.7.0')
Expand Down

0 comments on commit 487155c

Please sign in to comment.