Class: Makit::Environment

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

Overview

This class provide methods for working with the system Environment.

Class Method Summary collapse

Class Method Details

.constants_hashObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/makit/environment.rb', line 30

def self.constants_hash
  constants = {}
  # collect all constants that are all uppercase
  Object.constants.each do |c|
    #	puts "#{c} = #{Object.const_get(c)}" }
    constants[c] = Object.const_get(c) if c == c.upcase
  end
  # Object.constants.each { |c| constants[c] = Object.const_get(c)}#	puts "#{c} = #{Object.const_get(c)}" }
  constants
end

.current_userObject



9
10
11
12
13
# File 'lib/makit/environment.rb', line 9

def self.current_user
  if Makit::Environment.is_windows?
  end
  `whoami`
end

.gem_data_directoryObject



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

def self.gem_data_directory
  File.join(Dir.home, ".makit")
end

.get_code_rootObject



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

def self.get_code_root
  # user home directory  + "code"
  File.join(Dir.home, "code")
end

.get_osObject



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/makit/environment.rb', line 96

def self.get_os
  case RbConfig::CONFIG["host_os"]
  when /linux/
    "Linux"
  when /darwin/
    "macOS"
  when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
    "Windows"
  else
    "Unknown"
  end
end

.get_relative_directory(url) ⇒ Object



63
64
65
66
# File 'lib/makit/environment.rb', line 63

def self.get_relative_directory(url)
  url = url.gsub("https://", "").gsub("http://", "")
  url.gsub("gitlab.com", "gitlab")
end

.get_runtime_identifierObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/makit/environment.rb', line 109

def self.get_runtime_identifier
  os = RbConfig::CONFIG["host_os"]
  cpu = RbConfig::CONFIG["host_cpu"]

  case os
  when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
    os_rid = "win"
  when /darwin|mac os/
    os_rid = "osx"
  when /linux/
    os_rid = "linux"
  when /solaris|bsd/
    os_rid = "unix"
  else
    raise "Unknown operating system: host_os=#{os}"
  end

  case cpu
  when /x86_64|amd64|x64/
    arch_rid = "x64"
  when /i686|i386/
    arch_rid = "x86"
    # when /arm/
    #  arch_rid = "arm"
  when /aarch64|arm64/
    arch_rid = "arm64"
  else
    raise "Unknown architecture: host_cpu=#{cpu}"
  end

  "#{os_rid}-#{arch_rid}"
end

.get_work_base_directoryObject



80
81
82
# File 'lib/makit/environment.rb', line 80

def self.get_work_base_directory
  File.join(Dir.home, ".makit", "work")
end

.get_work_directory(url) ⇒ Object



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

def self.get_work_directory(url)
  raise "invalid url" unless url.include? "https://"

  url.gsub("https://", "").gsub("http://", "")
  # url = url.gsub("gitlab.com","gitlab")
end

.is_linux?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/makit/environment.rb', line 92

def self.is_linux?
  RbConfig::CONFIG["host_os"] =~ /linux/
end

.is_mac?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/makit/environment.rb', line 88

def self.is_mac?
  RbConfig::CONFIG["host_os"] =~ /darwin/
end

.is_windows?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/makit/environment.rb', line 84

def self.is_windows?
  RbConfig::CONFIG["host_os"] =~ /mswin|msys|mingw|cygwin|bccwin|wince|emc/
end

.project_root_directoryObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/makit/environment.rb', line 52

def self.project_root_directory
  if Makit::Environment.rake_file_name.nil?
    Makit::Directory.find_directory_with_pattern(File.dirname(__FILE__), "Rakefile")
    # lass Directory
    #  def self.find_directory_with_pattern(starting_directory, pattern)
    # nil
  else
    File.dirname(Makit::Environment.rake_file_name)
  end
end

.rake_file_nameObject



41
42
43
44
45
46
# File 'lib/makit/environment.rb', line 41

def self.rake_file_name
  caller_locations.each do |location|
    return location.absolute_path if location.absolute_path&.end_with?("Rakefile")
  end
  nil
end

.which(name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/makit/environment.rb', line 15

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