Class: Aggkit::Env

Inherits:
Object
  • Object
show all
Defined in:
lib/aggkit/env.rb

Defined Under Namespace

Classes: Pathfinder, Project

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Env

Returns a new instance of Env.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/aggkit/env.rb', line 47

def initialize(path)
  @project = if File.directory?(File.expand_path(path))
    @name = File.basename(File.realpath(File.expand_path(path)))
    Project.new(path)
  else
    @name = path.strip
    Project.new(Dir.pwd)
  end

  unless self.class.list.include?(@name)
    raise "There no env #{@name.inspect} in project: #{project.project_root.inspect}"
  end

  @env_root = File.join(project_root, 'envs', @name)
end

Instance Attribute Details

#env_rootObject

Returns the value of attribute env_root.



24
25
26
# File 'lib/aggkit/env.rb', line 24

def env_root
  @env_root
end

#environmentObject

Returns the value of attribute environment.



24
25
26
# File 'lib/aggkit/env.rb', line 24

def environment
  @environment
end

#nameObject

Returns the value of attribute name.



24
25
26
# File 'lib/aggkit/env.rb', line 24

def name
  @name
end

#projectObject

Returns the value of attribute project.



24
25
26
# File 'lib/aggkit/env.rb', line 24

def project
  @project
end

Class Method Details

.list(path = Dir.pwd) ⇒ Object



26
27
28
29
# File 'lib/aggkit/env.rb', line 26

def self.list(path = Dir.pwd)
  root = Project.new(path).project_root
  Pathfinder.new(root).each_env
end

.with_env(env) ⇒ Object



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

def self.with_env(env)
  current_env = ENV.to_h
  env.each_pair do |k, v|
    ENV[k.to_s] = v.to_s
  end
  yield(env)
ensure
  (env.keys.map(&:to_s) - current_env.keys).each do |key|
    ENV.delete(key.to_s)
  end

  current_env.each_pair do |k, v|
    ENV[k.to_s] = v.to_s
  end
end

Instance Method Details

#config_fileObject



99
100
101
# File 'lib/aggkit/env.rb', line 99

def config_file
  [File.join(env_root, 'config.yml'), File.join(env_root, 'config.yaml')].compact.select{|f| File.exist?(f) }.uniq.first
end

#core_dot_fileObject



87
88
89
90
91
92
93
# File 'lib/aggkit/env.rb', line 87

def core_dot_file
  begin
    File.join(core_root, '.env')
  rescue StandardError
    nil
  end
end

#core_rootObject



75
76
77
# File 'lib/aggkit/env.rb', line 75

def core_root
  project.core_root
end

#dot_fileObject



79
80
81
# File 'lib/aggkit/env.rb', line 79

def dot_file
  File.join(env_root, '.env')
end

#env_filesObject



95
96
97
# File 'lib/aggkit/env.rb', line 95

def env_files
  [core_dot_file, project_dot_file, dot_file].compact.select{|f| File.exist?(f) }.uniq
end

#prepare(release: false) ⇒ Object



63
64
65
# File 'lib/aggkit/env.rb', line 63

def prepare(release: false)
  @environment = prepare_environment(release: release)
end

#project_dot_fileObject



83
84
85
# File 'lib/aggkit/env.rb', line 83

def project_dot_file
  File.join(project_root, '.env')
end

#project_rootObject



71
72
73
# File 'lib/aggkit/env.rb', line 71

def project_root
  project.project_root
end

#showObject



103
104
105
106
107
108
109
110
111
112
# File 'lib/aggkit/env.rb', line 103

def show
  {
    name:         name,
    project_root: project_root,
    core_root:    core_root,
    envfiles:     env_files,
    config_file:  config_file,
    envs:         environment
  }
end

#with_env(&block) ⇒ Object



67
68
69
# File 'lib/aggkit/env.rb', line 67

def with_env(&block)
  Env.with_env(environment) & block
end