Class: KXI::Platform

Inherits:
Object
  • Object
show all
Defined in:
lib/kxi/platform.rb

Overview

Represents an platform of execution

Constant Summary collapse

WINDOWS =

Microsoft Windows

'windows'
OSX =

Apple macOS

'osx'
LINUX =

Linux-based system

'linux'
@@this =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nm, ver, ds, ps, cs, *libs) ⇒ Platform

Instantiates the KXI::Platform class

Parameters:

  • nm (string)

    Name of platform

  • ver (string)

    Version of platform

  • ds (string)

    Directory separator

  • ps (string)

    Path separator

  • cs (string)

    Determines whether paths are case-sensitive

  • libs (string)

    File extensions of libraries



57
58
59
60
61
62
63
64
# File 'lib/kxi/platform.rb', line 57

def initialize(nm, ver, ds, ps, cs, *libs)
	@name    = nm
	@version = KXI::Application::Version.parse(ver)
	@ds      = ds
	@ps      = ps
	@cs      = cs
	@libs    = libs
end

Class Method Details

.env(key) ⇒ string

Gets an environment variable

Returns:

  • (string)

    Value of environment variable if found; otherwise nil



96
97
98
99
100
101
102
# File 'lib/kxi/platform.rb', line 96

def self.env(key)
	key = key.downcase
	ENV.each_pair do |k, v|
		return v if k.downcase == key
	end
	return nil
end

.thisKXI::Platform

Gets the current platform

Returns:



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/kxi/platform.rb', line 68

def self.this
	return @@this if @@this != nil
	if /cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM
		@@this = Platform.new(WINDOWS, `wmic os get Version /value`.split('=').last.strip.chomp, '\\', ';', false, 'dll')
	elsif /darwin/ =~ RUBY_PLATFORM
		@@this = Platform.new(OSX, `defaults read loginwindow SystemVersionStampAsString`, '/', ':', true, 'dylib')
	else
		@@this = Platform.new(LINUX, `uname -r`.split('-')[0], '/', ':', true, 'so')
	end
	return @@this
end

Instance Method Details

#directory_separatorstring

Gets the separator of directories within a path

Returns:

  • (string)

    Separator of directories



40
41
42
# File 'lib/kxi/platform.rb', line 40

def directory_separator
	@ds
end

#exec(name, *dir) ⇒ string?

Finds an executable within the PATH of platform

Returns:

  • (string, nil)

    Path of executable if found; otherwise nil



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/kxi/platform.rb', line 82

def exec(name, *dir)
	dir = Platform.env('path').split(@ps) if dir.length == 0
	ext = Platform.env('pathext')
	(ext != nil ? ext.split(@ps) + [''] : ['']).each do |e|
		dir.each do |d|
			f = File.join(d, "#{name}#{e}")
			return f if File.exists?(f)
		end
	end
	return nil
end

#library_extensionsArray<string>

Gets the file extensions of libraries

Returns:

  • (Array<string>)

    File extensions of libraries



28
29
30
# File 'lib/kxi/platform.rb', line 28

def library_extensions
	@libs
end

#namestring

Gets the name of platform

Returns:

  • (string)

    Name of platform



16
17
18
# File 'lib/kxi/platform.rb', line 16

def name
	@name
end

#path_case_sensitive?bool

Determines whether paths are case-sensitive

Returns:

  • (bool)

    True if paths are case-sensitive; false otherwise



46
47
48
# File 'lib/kxi/platform.rb', line 46

def path_case_sensitive?
	@cs
end

#path_separatorstring

Gets the separator of paths

Returns:

  • (string)

    Separator of paths



34
35
36
# File 'lib/kxi/platform.rb', line 34

def path_separator
	@ps
end

#versionKXI::Application::Version

Gets the version of platform

Returns:



22
23
24
# File 'lib/kxi/platform.rb', line 22

def version
	@version
end