module SctCore class Helper WINDOWS = "Windows" MAC_OS = "MacOS" UBUNTU = "Ubuntu" def self.to_hash(str) Hash[ str.split("\n").map{|i|i.split('=')} ] end def self.operatingSystem proc = `uname -a` case proc.downcase when /microsoft/ os = WINDOWS when /darwin/ os = MAC_OS else os = UBUNTU end return os end def self.homePath user = ENV["SUDO_USER"] || ENV["USER"] if self.operatingSystem == MAC_OS home = "/Users" else home = "/home" end return "#{home}/#{user}" end def self.windowsHomePath if self.is_windows? return self.convertWindowsToWSLPath(`cmd.exe /c echo %userprofile%`) end return nil end def self.convertWindowsToWSLPath(path) if self.is_windows? return path.gsub(/C:\\/, '/mnt/c/').gsub(/\\\\/, "/").gsub(/\\/, '/').gsub(/\r\n?/, '') end return path end def self.convertWSLToWindowsPath(path) if self.is_windows? return path.gsub(/\/mnt\/c/, 'C:/').gsub(/\/\//, '/').gsub(/\\\\/, "/").gsub(/\r\n?/, '') end return path end def self.is_sudo? return !ENV["SUDO_USER"].nil? && !ENV["SUDO_USER"].empty? end def self.is_windows? return self.operatingSystem == WINDOWS end def self.is_windows_administrator? # https://serverfault.com/a/95464 return `/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -c "(New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)"` == "True\r\n" end def self.ensure_sudo if !self.is_sudo? UI.error "This command needs to be run with sudo." exit 1 end end def self.ensure_windows_administrator if self.is_windows? && !self.is_windows_administrator? UI.error "This command needs to be run in an admin shell." exit 1 end end def self.ensure_permissions self.ensure_sudo self.ensure_windows_administrator end end end