Class: P4ApiVersion

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
ext/P4/extconf.rb

Overview

This captures the version information of the P4API C++ library we’re building against. This is mostly parsed into this structure and then spit out into a header file we compile into the Ruby API.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(major, minor = nil) ⇒ P4ApiVersion

Returns a new instance of P4ApiVersion.



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'ext/P4/extconf.rb', line 298

def initialize(major, minor = nil)
  if (major.kind_of?(String) && !minor)
    if (major =~ /(\d+)\.(\d+)/)
      major = $1
      minor = $2
    else
      raise("Bad API version: #{major}")
    end
  end

  @major = major.to_i
  @minor = minor.to_i
  @type = nil

  @patchlevel = nil
  @suppdate = nil
end

Instance Attribute Details

#majorObject (readonly)

Returns the value of attribute major.



323
324
325
# File 'ext/P4/extconf.rb', line 323

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor.



323
324
325
# File 'ext/P4/extconf.rb', line 323

def minor
  @minor
end

#patchlevelObject

Returns the value of attribute patchlevel.



322
323
324
# File 'ext/P4/extconf.rb', line 322

def patchlevel
  @patchlevel
end

#suppdateObject

Returns the value of attribute suppdate.



322
323
324
# File 'ext/P4/extconf.rb', line 322

def suppdate
  @suppdate
end

#typeObject (readonly)

Returns the value of attribute type.



323
324
325
# File 'ext/P4/extconf.rb', line 323

def type
  @type
end

Class Method Details

.load(dir) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'ext/P4/extconf.rb', line 263

def P4ApiVersion.load(dir)
  #
  # 2007.2 and later APIs put the Version file in the 'sample'
  # subdirectory. Look there if we can't find it in the API root
  #
  ver_file = dir + "/Version"
  unless File.exist?(ver_file)
    ver_file = dir + "/sample/Version"
    return nil unless File.exist?(ver_file)
  end

  re = Regexp.new('^RELEASE = (\d+)\s+(\d+)\s+(\w*\S*)\s*;')
  rp = Regexp.new('^PATCHLEVEL = (.*)\s*;')
  rs = Regexp.new('^SUPPDATE = (.*)\s*;')

  p4api_version = nil

  File.open(ver_file, "r") do
  |f|
    f.each_line do
    |line|
      if md = re.match(line)
        p4api_version = P4ApiVersion.new(md[1], md[2])
        p4api_version.set_type(md[3])
      elsif md = rp.match(line)
        p4api_version.patchlevel = md[1]
      elsif md = rs.match(line)
        p4api_version.suppdate = md[1]
      end
    end
  end
  puts("Found #{p4api_version} Perforce API in #{dir}")
  return p4api_version
end

Instance Method Details

#<=>(other) ⇒ Object



339
340
341
342
343
344
# File 'ext/P4/extconf.rb', line 339

def <=>(other)
  hi = @major <=> other.major
  lo = @minor <=> other.minor

  return hi == 0 ? lo : hi
end

#set_type(type) ⇒ Object



316
317
318
319
320
# File 'ext/P4/extconf.rb', line 316

def set_type(type)
  if (type.kind_of?(String))
    @type = type
  end
end

#to_iObject



335
336
337
# File 'ext/P4/extconf.rb', line 335

def to_i
  major << 8 | minor
end

#to_sObject



327
328
329
330
331
332
333
# File 'ext/P4/extconf.rb', line 327

def to_s
  if (@type and not @type.empty?)
    "#{major}.#{minor}.#{@type.upcase}"
  else
    "#{major}.#{minor}"
  end
end