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
# 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)
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

#config_file ⇒ Object



92
93
94
# File 'lib/aggkit/env.rb', line 92

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

#core_dot_file ⇒ Object



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

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

#core_root ⇒ Object



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

def core_root
  project.core_root
end

#dot_file ⇒ Object



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

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

#env_files ⇒ Object



88
89
90
# File 'lib/aggkit/env.rb', line 88

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

#prepare(release: false) ⇒ Object



60
61
62
# File 'lib/aggkit/env.rb', line 60

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

#project_dot_file ⇒ Object



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

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

#project_root ⇒ Object



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

def project_root
  project.project_root
end

#show ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/aggkit/env.rb', line 96

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



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

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