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



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
95
96
# File 'lib/cupper/environment.rb', line 46

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.



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

def cupperfile_name
  @cupperfile_name
end

#cwdObject (readonly)

The ‘cwd` that this environment represents



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

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)


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

def data_dir
  @data_dir
end

#gems_pathObject (readonly)

The path where the plugins are stored (gems)



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

def gems_path
  @gems_path
end

#local_data_pathObject (readonly)

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



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

def local_data_path
  @local_data_path
end

#tmp_pathObject (readonly)

The directory where temporary files for Cupper go.



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

def tmp_path
  @tmp_path
end

Instance Method Details

#check_env(ex, root_path) ⇒ Object



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

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

#cupperfileObject



150
151
152
# File 'lib/cupper/environment.rb', line 150

def cupperfile
  @cupperfile ||= Cupper::Cupperfile.new(find_cupperfile(@root_path))
end

#environment(cupperfile, **opts) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cupper/environment.rb', line 106

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



154
155
156
157
158
159
160
161
162
# File 'lib/cupper/environment.rb', line 154

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



122
123
124
125
126
127
128
129
# File 'lib/cupper/environment.rb', line 122

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)


102
103
104
# File 'lib/cupper/environment.rb', line 102

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

#root_pathObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/cupper/environment.rb', line 131

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



146
147
148
# File 'lib/cupper/environment.rb', line 146

def unload
  hook(:environment_unload)
end