Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor in a backwards compatible way, add System::CPU.count

  • Loading branch information...
commit 8084cbd503fb3908e2c4982b143cac945900482b 1 parent 3cf7aa9
Ryan Scott Lewis RyanScottLewis authored
Showing with 79 additions and 27 deletions.
  1. +79 −27 lib/system.rb
106 lib/system.rb
View
@@ -1,5 +1,5 @@
# system includes
-require 'singleton'
+# require 'singleton'
require 'rbconfig'
# java includes
@@ -8,36 +8,88 @@
import 'java.lang.System'
end
-module HostSystem
-
- def self.os
- return @os if defined? @os # if we already know then pass it
- os_name = RUBY_PLATFORM
- os_name = RbConfig::CONFIG['host_os'] if defined? RbConfig::CONFIG
- os_name = System.getProperty('os.name').downcase if jruby?
- @os = case os_name
- when /linux/ then :linux
- when /win/ then :windows
- 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
- else
- :unknown
+# Note: Must be a Class for cross-implementation because System exists within JRuby
+class System
+
+ 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
-
- def self.java?
- !(/java/.match(RUBY_PLATFORM).nil?)
+
+ 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
- def self.jruby?
- if HostSystem::java?
- /jruby/.match(RUBY_ENGINE)
- else
- false
+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
-end
+p System::OS.name
+p System::CPU.count
Please sign in to comment.
Something went wrong with that request. Please try again.