Class: Raykit::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/raykit/environment.rb

Overview

Provides functionality related to the development environment

Class Method Summary collapse

Class Method Details

.admin?Boolean

Returns:

  • (Boolean)


95
96
97
98
# File 'lib/raykit/environment.rb', line 95

def self.admin?
  rights = `whoami /priv`
  rights.include?("SeCreateGlobalPrivilege")
end

.get_dev_dir(name) ⇒ Object

Get, and create if it does not exist, a specific development directory



58
59
60
61
62
63
64
65
66
# File 'lib/raykit/environment.rb', line 58

def self.get_dev_dir(name)
  dir = Pathname.new("#{Environment.root_dir}/#{name}")
  dir.mkpath
  if (dir.to_s.include?("https:") || dir.to_s.include?("http:"))
    normalize_path(dir.to_s.gsub("https://", ".").gsub("http://", ".").gsub("//", "/"))
  else
    normalize_path(dir.to_s)
  end
end

.get_dir_size(dir) ⇒ Object

Get the size of a directory and its contents



73
74
75
76
77
# File 'lib/raykit/environment.rb', line 73

def self.get_dir_size(dir)
  Dir.glob(File.join(dir, "**", "*"))
     .map { |f| File.size(f) }
     .inject(:+)
end

.get_work_dir(url) ⇒ Object



68
69
70
# File 'lib/raykit/environment.rb', line 68

def self.get_work_dir(url)
  "#{Raykit::Environment.get_dev_dir("work")}/#{url.gsub(".git", "").gsub("://", ".")}"
end

.home_dirObject

The user home directory



48
49
50
51
# File 'lib/raykit/environment.rb', line 48

def self.home_dir
  return normalize_path(ENV["USERPROFILE"]) if ENV.include?("USERPROFILE")
  normalize_path(ENV["HOME"])
end

.local_application_dataObject



91
92
93
# File 'lib/raykit/environment.rb', line 91

def self.local_application_data
  "#{ENV["USERPROFILE"]}/AppData/Local".gsub('\\', "/")
end

.log_dirObject



53
54
55
# File 'lib/raykit/environment.rb', line 53

def self.log_dir
  get_dev_dir("log")
end

.machineObject



79
80
81
82
83
84
85
# File 'lib/raykit/environment.rb', line 79

def self.machine
  return ENV["COMPUTERNAME"] unless ENV["COMPUTERNAME"].nil?

  machine = `hostname`
  machine = machine.split(".")[0] if machine.include?(".")
  machine.strip
end

.normalize_path(name) ⇒ Object

Normalize a directory or filename to use forward slashes



9
10
11
12
13
14
15
# File 'lib/raykit/environment.rb', line 9

def self.normalize_path(name)
  if (windows?)
    normalize_windows(name)
  else
    normalize_unix(name)
  end
end

.normalize_unix(name) ⇒ Object



17
18
19
# File 'lib/raykit/environment.rb', line 17

def self.normalize_unix(name)
  name.gsub('\\', "/")
end

.normalize_windows(name) ⇒ Object



21
22
23
# File 'lib/raykit/environment.rb', line 21

def self.normalize_windows(name)
  name.gsub("/", '\\')
end

.root_dirObject

The root directory for the development environment. May be set using the environment variable DEV_ROOT, otherwise defaults to the user home directory



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/raykit/environment.rb', line 32

def self.root_dir
  if ENV["DEV_ROOT"].nil?
    config = Raykit::Configuration.new
    if (config.root_dir.nil? || config.root_dir.empty?)
      root = Environment.home_dir + File::SEPARATOR + "code"
    else
      root = Environment.home_dir + File::SEPARATOR + config.root_dir
    end
    normalize_path(root)
  else
    root = ENV["DEV_ROOT"].gsub("\\", "/").chomp("/")
    normalize_path(root)
  end
end

.userObject



87
88
89
# File 'lib/raykit/environment.rb', line 87

def self.user
  ENV["USERNAME"]
end

.which(name) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/raykit/environment.rb', line 100

def self.which(name)
  return name if File.exist?(name)

  ["", ".exe", ".bat", ".cmd"].each do |ext|
    aname = name + ext
    return aname if File.exist?(aname)

    ENV["PATH"].split(File::PATH_SEPARATOR).each do |path|
      apath = "#{path.gsub('\\', "/")}/#{aname}".gsub("//", "/")
      return apath if File.exist?(apath)
    end
  end
  ""
end

.windows?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/raykit/environment.rb', line 25

def self.windows?
  Gem.win_platform?
end