Class: EnvironmentInformation::Queries::SimpleVersion

Inherits:
Base
  • Object
show all
Defined in:
lib/environment_information/queries/simple_version.rb

Overview

EnvironmentInformation::Queries::SimpleVersion

Instance Method Summary collapse

Methods inherited from Base

#cd, #commandline_arguments?, #e, #esystem, #first_argument?, #gold, #is_on_roebe?, #is_rbt_available?, #lightblue, #log_dir?, #mediumaquamarine, #report_left_right, #set_commandline_arguments

Constructor Details

#initialize(for_this_program = nil, run_already = true) ⇒ SimpleVersion

#

initialize

The first argument is mandatory.

#


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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

Instance Method Details

#program_name?Boolean

#

program_name?

#

Returns:

  • (Boolean)


77
78
79
# File 'lib/environment_information/queries/simple_version.rb', line 77

def program_name?
  @program_name
end

#program_version?Boolean Also known as: version?

#

program_version?

#

Returns:

  • (Boolean)


84
85
86
# File 'lib/environment_information/queries/simple_version.rb', line 84

def program_version?
  @program_version
end

#resetObject

#

reset

#


52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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? ) ⇒ Object

#

return_version_of

#


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# 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

#runObject

#

run (run tag)

#


262
263
264
# File 'lib/environment_information/queries/simple_version.rb', line 262

def run
  return_version_of
end

#set_program_name(i) ⇒ Object

#

set_program_name

#


70
71
72
# File 'lib/environment_information/queries/simple_version.rb', line 70

def set_program_name(i)
  @program_name = i.to_sym if i
end