module PatternPark class ExecutionError < StandardError; end ############################# # User class class User @@user = nil def User.new(os=nil, impl=nil) if(os.nil? && impl.nil? && @@user) return @@user end if(os.nil?) os = Platform::OS end if(impl.nil?) impl = Platform::IMPL end if(os == :win32 && impl == :vista) @@user = VistaUser.new elsif(os == :win32) @@user = WinUser.new elsif(os == :unix && impl == :cygwin) @@user = CygwinUser.new elsif(os == :unix && impl == :macosx) @@user = OSXUser.new elsif(os == :unix && impl == :linux) @@user = UnixUser.new else @@user = UnixUser.new end end def User.user=(user) @@user = user end def User.home=(path) User.new().home = path end def User.home User.new().home end def User.application_home(name) return User.new().application_home(name) end def User.library return User.new().library end def User.execute(tool, options='') return User.new().execute(tool, options) end def User.execute_thread(tool, options='') if(Log.debug) return ThreadMock.new else return User.new().execute_thread(tool, options) end end def User.clean_path(path) return User.new().clean_path(path) end # Called from Sprout.init_values... def User.init_values @@user = nil @@home = nil end end ############################# # UnixUser class class UnixUser def initialize @home = nil end def home=(path) @home = path end def home if(@home) return @home end ["HOME", "USERPROFILE"].each do |homekey| return @home = ENV[homekey] if ENV[homekey] end if ENV["HOMEDRIVE"] && ENV["HOMEPATH"] return @home = "#{ENV["HOMEDRIVE"]}:#{ENV["HOMEPATH"]}" end begin return @home = File.expand_path("~") rescue StandardError => ex if File::ALT_SEPARATOR return @home = "C:\\" else return @home = "/" end end end def library return home end def platform if(Platform::OS == :win32) return :win32 elsif(Platform::IMPL == :macosx) return :macosx else return Platform::IMPL end end def execute(tool, options='') tool = Sprout.load(tool) target = tool.archive_path Log.puts(">> Execute: #{File.basename(target)} #{options}") runner = CommandRunner.new("#{clean_path(target)} #{options}") runner.run result = runner.read error = runner.readError.to_s if(result.size > 0) Log.puts result end if(error.size > 0) raise ExecutionError.new("[ERROR] #{error}") end end def execute_thread(tool, options='') return Thread.new { execute(tool, options) } end def clean_path(path) if(path.index(' ')) return %{'#{path}'} end return path end def application_home(name) return File.join(library, format_application_name(name.to_s)); end def format_application_name(name) if(name.index('.') != 0) name = '.' + name end return name.split(" ").join("_").downcase end end class OSXUser < UnixUser @@LIBRARY = 'Library' def library lib = File.join(home, @@LIBRARY) if(File.exists?(lib)) return lib else return super end end def format_application_name(name) return name.capitalize end end class WinUser < UnixUser @@LOCAL_SETTINGS = "Local\ Settings" @@APPLICATION_DATA = "Application\ Data" def home usr = super if(usr.index "My Documents") usr = File.dirname(usr) end return usr end def library # For some reason, my homepath returns inside 'My Documents'... application_data = File.join(home, @@LOCAL_SETTINGS, @@APPLICATION_DATA) if(File.exists?(application_data)) return application_data else return super end end def clean_path(path) path = path.split('/').join(File::SEPARATOR) if(path.index(' ')) return %{"#{path}"} end return path end def format_application_name(name) return name.capitalize end end class CygwinUser < WinUser end class VistaUser < WinUser def home profile = ENV['USERPROFILE'] if(profile) return profile end return super end end class ThreadMock def alive? return false end end end