Skip to content

Commit

Permalink
v0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Scott Lewis committed Nov 10, 2012
1 parent 3778c90 commit 4d9d23b
Show file tree
Hide file tree
Showing 7 changed files with 265 additions and 88 deletions.
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
0.1.0 0.1.1
106 changes: 19 additions & 87 deletions lib/system.rb
@@ -1,95 +1,27 @@
# system includes # System includes
# require 'singleton'
require 'rbconfig' require 'rbconfig'
require 'pathname'


# java includes # Java includes - Have to load this way to avoid overloading of our classes
if /java/.match(RUBY_PLATFORM) if /java/.match(RUBY_PLATFORM) # TODO: For now, this is a good place for this but it will need abstracting..
require 'java' require 'java'
import 'java.lang.System' import 'java.lang.System'
end end


# Note: Must be a Class for cross-implementation because System exists within JRuby # Prepare LOAD_PATH
class System __LIB__ ||= Pathname.new(__FILE__).dirname

$:.unshift(__LIB__.to_s) unless $:.include?(__LIB__.to_s)
module OS

# Constants for OS names:
%w{Unknown Linux Windows Solaris BSD OSX Darwin}.each { |name| const_set(name, name.downcase.to_sym) }

class << self

def name
return @name if defined?(@name)

os_name = RUBY_PLATFORM
os_name = RbConfig::CONFIG['host_os'] if defined?(RbConfig::CONFIG)
os_name = System.getProperty('os.name').downcase if System::Ruby.jruby?

@name = case os_name
when /linux/ then Linux
when /solaris/ then Solaris
when /bsd/ then BSD
when /darwin/ then
defined?(RbConfig::CONFIG) && RbConfig::CONFIG['build_vendor'] == 'apple' ? OSX : Darwin
when /mac.*?os.*?x/ then OSX
when /win/ then Windows # TODO: "win" is in the word "darwin"... had to move down here but need better examples of Windows OS name results...
else
Unknown
end
end

end

end

module Ruby
class << self

def java?
!(/java/.match(RUBY_PLATFORM).nil?)
end

def jruby?
java? ? /jruby/.match(RUBY_ENGINE) : false
end

end
end

class << self

# Delegator for backwards compatibility
def os
System::OS.name
end
alias_method :operating_system, :os

end

end

class System

class CPU

class << self

def count
return Java::Java.lang.Runtime.getRuntime.availableProcessors if System::Ruby.java? # defined? Java::Java
return File.read('/proc/cpuinfo').scan(/^processor\s*:/).size if File.exist?('/proc/cpuinfo')
require 'win32ole'
WIN32OLE.connect("winmgmts://").ExecQuery("select * from Win32_ComputerSystem").NumberOfProcessors
rescue LoadError
Integer `sysctl -n hw.ncpu 2>/dev/null` rescue 1
end

end

end

end


HostSystem = System # Backwards compatibility # Local includes
require 'system/version'
require 'system/ruby'
require 'system/os'
require 'system/cpu'
require 'system/backwards_compatibility'


p System::OS.name # System is a cross-platform and cross-implementation interface to gather system information from the current host.
p System::CPU.count #
# System offers a simple to use interface to gather an array of information including; OS, CPU, Filesystem, etc..
#
# @since 0.1.0
class System; end
42 changes: 42 additions & 0 deletions lib/system/backwards_compatibility.rb
@@ -0,0 +1,42 @@
require 'system/version'

class System
class << self

# Get the name of the operating system running on the current host.
#
# Delegates to System::OS.name for backwards compatibility with 0.1.0.
#
# @return [Symbol] The name of the operating system.
# @since 0.1.0
def os
System::OS.name
end
alias_method :operating_system, :os

# Check if Ruby is using Java.
#
# Delegates to System::Ruby.java? for backwards compatibility with 0.1.0.
#
# @return [TrueClass, FalseClass] Is the current Ruby implementation using Java?
# @since 0.1.0
def java?
System::Ruby.java?
end

# Check if Ruby is JRuby.
#
# Delegates to System::Ruby.jruby? for backwards compatibility with 0.1.0.
#
# @return [TrueClass, FalseClass] Is the current Ruby implementation JRuby?
# @since 0.1.0
def jruby?
System::Ruby.jruby?
end

end
end

# Constant for backwards compatibility with 0.1.0.
# @since 0.1.0
HostSystem = System
25 changes: 25 additions & 0 deletions lib/system/cpu.rb
@@ -0,0 +1,25 @@
require 'system/version'

class System

# Information about the current CPU(s) running on the current system.
#
# @since 0.1.1
class CPU
class << self

# The CPU count of the system, including individual CPU cores.
#
# @return [Integer] Count of CPU cores.
def count
return Java::Java.lang.Runtime.getRuntime.availableProcessors if System::Ruby.java? # defined? Java::Java
return File.read('/proc/cpuinfo').scan(/^processor\s*:/).size if File.exist?('/proc/cpuinfo')
require 'win32ole'
WIN32OLE.connect("winmgmts://").ExecQuery("select * from Win32_ComputerSystem").NumberOfProcessors
rescue LoadError
Integer `sysctl -n hw.ncpu 2>/dev/null` rescue 1
end

end
end
end
71 changes: 71 additions & 0 deletions lib/system/os.rb
@@ -0,0 +1,71 @@
require 'system/version'

class System

# Information about the current Operating System (OS) running on the current system.
#
# @since 0.1.1
module OS

# Unknown OS
# @since 0.1.1
Unknown = :unknown

# Any OS running the Linux kernel
# @since 0.1.1
Linux = :linux

# Any Windows OS
# @since 0.1.1
Windows = :windows

# Any Solaris OS
# @since 0.1.1
Solaris = :solaris

# Any OS running the BSD kernel
# @since 0.1.1
BSD = :bsd

# Any OSX OS
# @since 0.1.1
OSX = :osx

# Any OS running the Darwin kernel
# @since 0.1.1
Darwin = :darwin

class << self

# Get the name of the operating system running on the current host.
#
# @return [Symbol] The name of the operating system.
# @example Return OS name
# System::OS.name # => :osx
# @example Assert current OS
# System::OS.name == System::OS::OSX # => true
# @since 0.1.1
def name
return @name if defined?(@name)

os_name = System::Ruby.platform
os_name = RbConfig::CONFIG['host_os'] if defined?(RbConfig::CONFIG)
os_name = System.getProperty('os.name').downcase if System::Ruby.jruby?

@name = case os_name
when /linux/ then Linux
when /solaris/ then Solaris
when /bsd/ then BSD
when /darwin/ then
defined?(RbConfig::CONFIG) && RbConfig::CONFIG['build_vendor'] == 'apple' ? OSX : Darwin
when /mac.*?os.*?x/ then OSX
when /win/ then Windows # TODO: "win" is in the word "darwin"... had to move down here but need better examples of Windows OS name results...
else
Unknown
end
end

end

end
end
102 changes: 102 additions & 0 deletions lib/system/ruby.rb
@@ -0,0 +1,102 @@
require 'system/version'

class System

# Information about the current Ruby implementation.
#
# @since 0.1.1
module Ruby
class << self

# @method copyright
# Return the current Ruby implementation's copyright.
#
# @since 0.1.1
# @return [String] The current Ruby implementation's copyright.
# @example
# System::Ruby.copyright # => "ruby - Copyright (C) 1993-2012 Yukihiro Matsumoto"

# @method description
# Return the current Ruby implementation's description which includes the engine, version,
# release date, revision, and platform.
#
# @since 0.1.1
# @return [String] The current Ruby implementation's engine.
# @example
# System::Ruby.copyright # => "ruby"

# @method engine
# Return the current Ruby implementation's engine.
#
# @since 0.1.1
# @return [String] The current Ruby implementation's engine.
# @example
# System::Ruby.copyright # => "ruby"

# @method patchlevel
# Return the current Ruby implementation's patch-level.
#
# @since 0.1.1
# @return [Integer] The current Ruby implementation's patch-level.
# @example
# System::Ruby.patchlevel # => 286

# @method platform
# Return the system platform the current Ruby implementation is running on.
#
# @since 0.1.1
# @return [String] The system platform the current Ruby implementation is running on.
# @example
# System::Ruby.platform # => "x86_64-darwin12.2.0"

# @method revision
# Return the current Ruby implementation's revision.
#
# @since 0.1.1
# @return [Integer] The current Ruby implementation's revision.
# @example
# System::Ruby.revision # => 37165

# @method release_date
# Return the current Ruby implementation's release date.
#
# @since 0.1.1
# @return [String] The current Ruby implementation's release date.
# @example
# System::Ruby.release_date # => "2012-10-12"

# @method version
# Return the current Ruby implementation's version.
#
# @since 0.1.1
# @return [String] The current Ruby implementation's version.
# @example
# System::Ruby.version # => "1.9.3"

Object.constants.grep(/^RUBY_/).each do |const_name|
method_name = const_name.to_s.gsub(/^RUBY_/, '').downcase.to_sym

define_method(method_name) { Object.const_get(const_name) }
end

# Check if Ruby is using Java.
# Delegates to System::Ruby.java? for backwards compatibility.
#
# @return [TrueClass, FalseClass] Is the current Ruby implementation using Java?
# @since 0.1.1
def java?
!(/java/.match(platform).nil?)
end

# Check if Ruby is JRuby.
# Delegates to System::Ruby.jruby? for backwards compatibility.
#
# @return [TrueClass, FalseClass] Is the current Ruby implementation JRuby?
# @since 0.1.1
def jruby?
java? ? !!(/jruby/.match(engine)) : false
end

end
end
end
5 changes: 5 additions & 0 deletions lib/system/version.rb
@@ -0,0 +1,5 @@
require 'version'

class System
is_versioned
end

0 comments on commit 4d9d23b

Please sign in to comment.