class EnvironmentInformation::Queries::SimpleVersion

Public Class Methods

new( for_this_program = nil, run_already = true ) { || ... } click to toggle source
#

initialize

The first argument is mandatory.

#
# File lib/environment_information/queries/simple_version.rb, line 27
def initialize(
    for_this_program = nil,
    run_already      = true
  )
  reset
  set_program_name(
    for_this_program
  )
  # ======================================================================= #
  # === Handle blocks given next
  # ======================================================================= #
  if block_given?
    yielded = yield
    if yielded.is_a? Hash
      if yielded.has_key? :prefix_to_use
        @prefix_to_use << yielded.delete(:prefix_to_use)
      end
    end
  end
  run if run_already
end

Public Instance Methods

program_name?() click to toggle source
#

program_name?

#
# File lib/environment_information/queries/simple_version.rb, line 77
def program_name?
  @program_name
end
program_version?() click to toggle source
#

program_version?

#
# File lib/environment_information/queries/simple_version.rb, line 84
def program_version?
  @program_version
end
Also aliased as: version?
reset() click to toggle source
#

reset

#
# File lib/environment_information/queries/simple_version.rb, line 52
def reset
  # ======================================================================= #
  # === @program_name
  # ======================================================================= #
  @program_name    = ''.dup
  # ======================================================================= #
  # === @program_version
  # ======================================================================= #
  @program_version = nil
  # ======================================================================= #
  # === @prefix_to_use
  # ======================================================================= #
  @prefix_to_use = ''.dup
end
return_version_of( this_program = program_name? ) click to toggle source
#

return_version_of

#
# File lib/environment_information/queries/simple_version.rb, line 91
def return_version_of(
    this_program = program_name?
  )
  # ======================================================================= #
  # The next variable will store the Hash containing all individual
  # queries for the registered programs. For instance:
  #
  #    :busybox=>:custom_busybox, :bzip2=>:"bzip2recover --version",
  #    :cairo=>:pkgconfig, :ccache=>:version, :check=>:pkgconfig,
  #    :clang=>:version, :clutter=>:"pkg-config --modversion clutter-1.0",
  #    :lighttpd=>:version   # and so forth
  #
  # ======================================================================= #
  _ = ::EnvironmentInformation.query_to_use_for_the_individual_components?
  if _.nil?
    _ = ::EnvironmentInformation.load_file_query_to_use_for_the_individual_components
  end
  if _.has_key?(this_program) # Check whether the program is registered.
    result = _[this_program]
    case result
    # ===================================================================== #
    # === :version
    #
    # This is the simplest entry point; we will try the commandline flag
    # "--version" here. If you have to use "-v" then use :short_version
    # instead.
    # ===================================================================== #
    when :version
      @program_version = `#{@prefix_to_use}#{this_program} --version #{ERROR_LINE}`.strip
    # ===================================================================== #
    # === :short_version
    # ===================================================================== #
    when :short_version,
         :simple_version
      @program_version = `#{@prefix_to_use}#{this_program} -v #{ERROR_LINE}`.strip
    end
  end
  if @program_version
    if @program_version.include? N
      @program_version = @program_version.split(N).first
    end
    if @program_version.include? '--'
      @program_version.gsub!(/--/,'') # For strace specifically.
    end
    # ===================================================================== #
    # Next format entries such as:
    #
    #   "lighttpd/1.4.71 - a light and fast webserver"
    #
    # This may fail, though - see vim:
    #
    #   "VIM - Vi IMproved 9.0 (2022 Jun 28, compiled Mar 15 2024 11:08:54)"
    #
    # ===================================================================== #
    if @program_version.include? ' - '
      unless this_program.to_s.include?('vim')
        @program_version = @program_version.split(' - ').first.to_s
      end
    end
    # if @program_version.include? 'GNOME Document Viewer'
    #   @program_version.sub!(/GNOME Document Viewer/,'') # For evince specifically.
    # end
    if @program_version.include? 'GNU'
      @program_version.gsub!(/GNU/,'')
    end
    # ===================================================================== #
    # Next we will specifically handle vim:
    # ===================================================================== #
    if @program_version =~ / (\d{1,2}\.\d{1,2}) \(/
      @program_version = $1.to_s.dup
    end
    if @program_version.include? ' built on linux-gnu.'
      @program_version.gsub!(/ built on linux-gnu\./, '') # Fixing wget specifically.
    end
    if @program_version.include?('Midnight Commander')
      @program_version.gsub!(/Midnight Commander/, '')
    end
    if @program_version.include?('[') and @program_version.include?(']')
      @program_version.gsub!(/\[.+\]/, '')
    end
    @program_version.strip!
    if @program_version.start_with? "#{this_program} "
      # For example: "ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]"
      @program_version.sub!(/#{this_program} /, '')
    elsif @program_version.start_with? "#{this_program.capitalize} "
      # For example: "Wget 1.20.3 built on linux-gnu."
      @program_version.sub!(/#{this_program.capitalize} /, '')
    elsif @program_version.start_with? "#{this_program.upcase} "
      # For example: "NASM version 2.15.04 compiled on Aug 27 2020"
      @program_version.sub!(/#{this_program.upcase} /, '')
    elsif @program_version.downcase.start_with? "#{this_program.downcase} "
      # For example: "IceWM", has a unusual start.
      @program_version.sub!(/#{this_program} /i, '')
    elsif @program_version.start_with? 'Awk'
      @program_version.sub!(/Awk /i, '') # Ad-hox fix here.
    end
    if @program_version.include? '- Vi IMproved '
      @program_version.sub!(/- Vi IMproved /, '') # Fixing vim specifically.
    end
    if @program_version.include? '(v'
      # This is specifically for perl:
      #   "This is perl 5, version 32, subversion 0 (v5.32.0) built for x86_64-linux-thread-multi"
      @program_version = @program_version.scan(
                           /\(v(\d{0,2}\.?\d{0,2}\.\d{0,2})\)/
                         ).flatten.first.to_s.dup
    end
    # ===================================================================== #
    # The next check for "Version" should come after we eliminated
    # the leading program name.
    # ===================================================================== #
    if @program_version.include? 'Version '
      @program_version = @program_version.scan(
                           /Version (\d{0,2}\.?\d{0,2}\.\d{0,2})/
                         ).flatten.first.to_s.dup
    elsif @program_version.include? 'version '
      # This here should work for most programs, save for perl.
      @program_version = @program_version.scan(
                           /version (\d{0,2}\.?\d{0,2}\.\d{0,2})/
                         ).flatten.first.to_s.dup
    end
    if @program_version.include? ' patchlevel '
      # =================================================================== #
      # Gnuplot has this specifically: "5.4 patchlevel 0"
      # =================================================================== #
      @program_version = @program_version.sub(/ patchlevel /,'.').strip
    end
    # ===================================================================== #
    # Next we kill everything inside (). This may sometimes be
    # problematic, which is why this check comes quite late.
    # ===================================================================== #
    if @program_version.include?('(') and @program_version.include?(')')
      @program_version.gsub!(/\(.+\)/, '')
    end
    # ===================================================================== #
    # Handle: "IceWM 1.8.0, Copyright 1997-2003 Marko Macek, 2001 Mathias Hasselmann."
    # ===================================================================== #
    if @program_version.include?(',')
      @program_version = @program_version.split(',').first
    end
    if @program_version.include?('/')
      # =================================================================== #
      # Assume something like "lighttpd/1.4.71".
      # =================================================================== #
      @program_version = @program_version.split('/').last
    end
    # ===================================================================== #
    # Next, if a '-' is included, we split it and use the last part.
    # This handles cases such as "valgrind-3.16.0".
    # ===================================================================== #
    if @program_version.include?('-')
      @program_version = @program_version.split('-').last
    end
    # ===================================================================== #
    # Last but not least, for "node" specifically.
    # ===================================================================== #
    if @program_version.start_with? 'v'
      @program_version[0,1] = ''
    end
    if @program_version.include? ':' # e. g. for glslang.
      @program_version = @program_version.split(':').last.to_s.strip
    end
    if @program_version.include? COMMAND_NOT_FOUND
      @program_version = nil
    end
  end
  return @program_version
end
run() click to toggle source
#

run (run tag)

#
# File lib/environment_information/queries/simple_version.rb, line 262
def run
  return_version_of
end
set_program_name(i) click to toggle source
#

set_program_name

#
# File lib/environment_information/queries/simple_version.rb, line 70
def set_program_name(i)
  @program_name = i.to_sym if i
end
version?()
Alias for: program_version?