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.



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

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

  if !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)

  @environment = prepare_environment
end

Instance Attribute Details

#env_root ⇒ Object

Returns the value of attribute env_root.



21
22
23
# File 'lib/aggkit/env.rb', line 21

def env_root
  @env_root
end

#environment ⇒ Object

Returns the value of attribute environment.



21
22
23
# File 'lib/aggkit/env.rb', line 21

def environment
  @environment
end

#name ⇒ Object

Returns the value of attribute name.



21
22
23
# File 'lib/aggkit/env.rb', line 21

def name
  @name
end

#project ⇒ Object

Returns the value of attribute project.



21
22
23
# File 'lib/aggkit/env.rb', line 21

def project
  @project
end

Class Method Details

.list(path = Dir.pwd) ⇒ Object



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

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

.with_env(env, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/aggkit/env.rb', line 28

def self.with_env env, &block
  current_env = ENV.to_h
  env.each_pair do |k, v|
    ENV[k.to_s] = v.to_s
  end
  yield
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

#core_dot_file ⇒ Object



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

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

#core_root ⇒ Object



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

def core_root
  project.core_root
end

#dot_file ⇒ Object



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

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

#env_files ⇒ Object



86
87
88
# File 'lib/aggkit/env.rb', line 86

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

#project_dot_file ⇒ Object



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

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

#project_root ⇒ Object



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

def project_root
  project.project_root
end

#show ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/aggkit/env.rb', line 90

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

#with_env(&block) ⇒ Object



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

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