Class: Cupper::Environment

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = nil) ⇒ Environment

Initializes a new environment with the given options. The options is a hash where the main available key is ‘cwd`, which defines where the environment represents. There are other options available but they shouldn’t be used in general. If ‘cwd` is nil, then it defaults to the `Dir.pwd` (which is the cwd of the executing process).



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cupper/environment.rb', line 44

def initialize(opts=nil)
  opts = {
    cwd:              nil,
    local_data_path:  nil,
    cupperfile_name: nil,
  }.merge(opts || {})

  # Set the default working directory to look for the cupperfile
  opts[:cwd] ||= ENV["cupper_CWD"] if ENV.key?("cupper_CWD")
  opts[:cwd] ||= Dir.pwd
  opts[:cwd] = Pathname.new(opts[:cwd])
  if !opts[:cwd].directory?
    raise Errors::EnvironmentNonExistentCWD, cwd: opts[:cwd].to_s
  end
  opts[:cwd] = opts[:cwd].expand_path
  opts[:cupperfile_name] = 'Cupperfile'

  # Set the Cupperfile name up. We append "Cupperfile" and "cupperfile" so that
  # those continue to work as well, but anything custom will take precedence.
  opts[:cupperfile_name] ||= ENV["cupper_cupperFILE"] if \
    ENV.key?("cupper_cupperFILE")
  opts[:cupperfile_name] = [opts[:cupperfile_name]] if \
    opts[:cupperfile_name] && !opts[:cupperfile_name].is_a?(Array)

  # Set instance variables for all the configuration parameters.
  @cwd              = opts[:cwd]
  @cupperfile_name = opts[:cupperfile_name]


  # Run checkpoint in a background thread on every environment
  # initialization. The cache file will cause this to mostly be a no-op
  # most of the time.
  @checkpoint_thr = Thread.new do
    Thread.current[:result] = nil

    # If we disabled state and knowing what alerts we've seen, then
    # disable the signature file.
    signature_file = @data_dir.join("checkpoint_signature")
    if ENV["cupper_CHECKPOINT_NO_STATE"].to_s != ""
      signature_file = nil
    end

    Thread.current[:result] = Checkpoint.check(
      product: "cupper",
      version: VERSION,
      signature_file: signature_file,
      cache_file: @data_dir.join("checkpoint_cache"),
    )
  end

end

Instance Attribute Details

#cupperfile_nameObject (readonly)

The valid name for a Cupperfile for this environment.



19
20
21
# File 'lib/cupper/environment.rb', line 19

def cupperfile_name
  @cupperfile_name
end

#cwdObject (readonly)

The ‘cwd` that this environment represents



9
10
11
# File 'lib/cupper/environment.rb', line 9

def cwd
  @cwd
end

#data_dirPathname (readonly)

The persistent data directory where global data can be stored. It is up to the creator of the data in this directory to properly remove it when it is no longer needed.

Returns:

  • (Pathname)


16
17
18
# File 'lib/cupper/environment.rb', line 16

def data_dir
  @data_dir
end

#gems_pathObject (readonly)

The path where the plugins are stored (gems)



29
30
31
# File 'lib/cupper/environment.rb', line 29

def gems_path
  @gems_path
end

#local_data_pathObject (readonly)

The directory to the directory where local, environment-specific data is stored.



23
24
25
# File 'lib/cupper/environment.rb', line 23

def local_data_path
  @local_data_path
end

#tmp_pathObject (readonly)

The directory where temporary files for Cupper go.



26
27
28
# File 'lib/cupper/environment.rb', line 26

def tmp_path
  @tmp_path
end

Instance Method Details

#check_env(ex, root_path) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/cupper/environment.rb', line 31

def check_env(ex, root_path)
  begin
    raise ex if !root_path 
  rescue ex => ex
    puts "#{ex.message}".red
  end
end

#config_loaderObject



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/cupper/environment.rb', line 104

def config_loader
  return @config_loader if @config_loader

  root_cupperfile = nil
  if root_path
    root_cupperfile = find_cupperfile(root_path, @cupperfile_name)
  end

  @config_loader = Config::Loader.new(
    Config::VERSIONS, Config::VERSIONS_ORDER)
  @config_loader.set(:root, root_cupperfile) if root_cupperfile
  @config_loader
end

#cupperfileObject



162
163
164
# File 'lib/cupper/environment.rb', line 162

def cupperfile
  @cupperfile ||= cupperfile.new(config_loader, [:home, :root])
end

#environment(cupperfile, **opts) ⇒ Object



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

def environment(cupperfile, **opts)
  path = File.expand_path(cupperfile, root_path)
  file = File.basename(path)
  path = File.dirname(path)

  Util::SilenceWarnings.silence! do
    Environment.new({
      child:     true,
      cwd:       path,
      home_path: home_path,
      ui_class:  ui_class,
      cupperfile_name: file,
    }.merge(opts))
  end
end

#find_cupperfile(search_path, filenames = nil) ⇒ Object



166
167
168
169
170
171
172
173
174
# File 'lib/cupper/environment.rb', line 166

def find_cupperfile(search_path, filenames=nil)
  filenames ||= ["cupperfile", "cupperfile"]
  filenames.each do |cupperfile|
    current_path = search_path.join(cupperfile)
    return current_path if current_path.file?
  end

  nil
end

#hook(name, opts = nil) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/cupper/environment.rb', line 134

def hook(name, opts=nil)
  opts ||= {}
  opts[:callable] ||= Action::Builder.new
  opts[:runner] ||= action_runner
  opts[:action_name] = name
  opts[:env] = self
  opts.delete(:runner).run(opts.delete(:callable), opts)
end

#inspectString

Return a human-friendly string for pretty printed or inspected instances.

Returns:

  • (String)


100
101
102
# File 'lib/cupper/environment.rb', line 100

def inspect
  "#<#{self.class}: #{@cwd}>".encode('external')
end

#root_pathObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/cupper/environment.rb', line 143

def root_path
  return @root_path if defined?(@root_path)

  root_finder = lambda do |path|
    # Note: To remain compatible with Ruby 1.8, we have to use
    # a `find` here instead of an `each`.
    vf = find_cupperfile(path, @cupperfile_name)
    return path if vf
    return nil if path.root? || !File.exist?(path)
    root_finder.call(path.parent)
  end

  @root_path = root_finder.call(cwd)
end

#unloadObject



158
159
160
# File 'lib/cupper/environment.rb', line 158

def unload
  hook(:environment_unload)
end