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)


112
113
114
115
# File 'lib/raykit/environment.rb', line 112

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

.bin_dirObject



70
71
72
# File 'lib/raykit/environment.rb', line 70

def self.bin_dir
  get_dev_dir("bin")
end

.get_dev_dir(name) ⇒ Object

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



75
76
77
78
79
80
81
82
83
# File 'lib/raykit/environment.rb', line 75

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



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

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

.get_work_dir(url) ⇒ Object



85
86
87
# File 'lib/raykit/environment.rb', line 85

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

.home_dirObject

The user home directory



61
62
63
64
# File 'lib/raykit/environment.rb', line 61

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

.infoObject



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/raykit/environment.rb', line 8

def self.info
  ljust = 35
  puts "machine".ljust(ljust) + Rainbow("#{machine}").yellow.bright
  puts "user".ljust(ljust) + Rainbow("#{user}").yellow.bright
  puts "root_dir".ljust(ljust) + Rainbow("#{root_dir}").yellow.bright
  puts "home_dir".ljust(ljust) + Rainbow("#{home_dir}").yellow.bright
  puts "bin_dir".ljust(ljust) + Rainbow("#{bin_dir}").yellow.bright
  puts "log_dir".ljust(ljust) + Rainbow("#{log_dir}").yellow.bright
  puts "local_application_data".ljust(ljust) + Rainbow("#{local_application_data}").yellow.bright
  puts "admin".ljust(ljust) + Rainbow("#{admin?}").yellow.bright
  self
end

.local_application_dataObject



108
109
110
# File 'lib/raykit/environment.rb', line 108

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

.log_dirObject



66
67
68
# File 'lib/raykit/environment.rb', line 66

def self.log_dir
  get_dev_dir("log")
end

.machineObject



96
97
98
99
100
101
102
# File 'lib/raykit/environment.rb', line 96

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



22
23
24
25
26
27
28
# File 'lib/raykit/environment.rb', line 22

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

.normalize_unix(name) ⇒ Object



30
31
32
# File 'lib/raykit/environment.rb', line 30

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

.normalize_windows(name) ⇒ Object



34
35
36
# File 'lib/raykit/environment.rb', line 34

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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/raykit/environment.rb', line 45

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



104
105
106
# File 'lib/raykit/environment.rb', line 104

def self.user
  ENV["USERNAME"]
end

.which(name) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/raykit/environment.rb', line 117

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)


38
39
40
# File 'lib/raykit/environment.rb', line 38

def self.windows?
  Gem.win_platform?
end