Class: Penchant::Gemfile

Inherits:
Object
  • Object
show all
Defined in:
lib/penchant/gemfile.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = Dir.pwd) ⇒ Gemfile

Returns a new instance of Gemfile.



35
36
37
38
# File 'lib/penchant/gemfile.rb', line 35

def initialize(path = Dir.pwd)
  @path = path
  @env = environment
end

Instance Attribute Details

#is_deploymentObject (readonly)

Returns the value of attribute is_deployment.



3
4
5
# File 'lib/penchant/gemfile.rb', line 3

def is_deployment
  @is_deployment
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/penchant/gemfile.rb', line 3

def path
  @path
end

Class Method Details

.available_environmentsObject



25
26
27
# File 'lib/penchant/gemfile.rb', line 25

def self.available_environments
  new.available_environments
end

.defined_git_reposObject



29
30
31
# File 'lib/penchant/gemfile.rb', line 29

def self.defined_git_repos
  new.defined_git_repos
end

.do_full_env_switch!(env, deployment = false) ⇒ Object



5
6
7
8
9
# File 'lib/penchant/gemfile.rb', line 5

def self.do_full_env_switch!(env, deployment = false)
  return false if !(gemfile = pre_switch(env, deployment))

  gemfile.switch_to!(env, deployment)
end

.pre_switch(env, deployment = false) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/penchant/gemfile.rb', line 17

def self.pre_switch(env, deployment = false)
  gemfile = new
  return false if !gemfile.has_processable_gemfile?
  gemfile.run_dot_penchant!(env, deployment)

  gemfile
end

.switch_back!(fallback_env) ⇒ Object



11
12
13
14
15
# File 'lib/penchant/gemfile.rb', line 11

def self.switch_back!(fallback_env)
  return false if !(gemfile = pre_switch(fallback_env))

  gemfile.switch_back!(fallback_env)
end

Instance Method Details

#available_environmentsObject



76
77
78
79
# File 'lib/penchant/gemfile.rb', line 76

def available_environments
  process
  builder.available_environments
end

#current_envObject



33
# File 'lib/penchant/gemfile.rb', line 33

def current_env ; @env ; end

#defined_git_reposObject



81
82
83
84
# File 'lib/penchant/gemfile.rb', line 81

def defined_git_repos
  process(FileProcessor::ANY_ENVIRONMENT)
  builder.defined_git_repos
end

#deployment?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/penchant/gemfile.rb', line 72

def deployment?
  gemfile_header['deployment mode'] != nil
end

#environmentObject



68
69
70
# File 'lib/penchant/gemfile.rb', line 68

def environment
  gemfile_header.strip[%r{environment: ([^, ]*)}, 1]
end

#gemfile_pathObject



40
41
42
# File 'lib/penchant/gemfile.rb', line 40

def gemfile_path
  file_in_path('Gemfile')
end

#gemfile_penchant_pathObject



52
53
54
# File 'lib/penchant/gemfile.rb', line 52

def gemfile_penchant_path
  file_in_path('Gemfile.penchant')
end

#has_dot_penchant?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/penchant/gemfile.rb', line 48

def has_dot_penchant?
  File.file?('.penchant')
end

#has_gemfile?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/penchant/gemfile.rb', line 44

def has_gemfile?
  File.file?(gemfile_path)
end

#has_gemfile_penchant?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/penchant/gemfile.rb', line 56

def has_gemfile_penchant?
  File.file?(gemfile_penchant_path)
end

#has_processable_gemfile?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/penchant/gemfile.rb', line 60

def has_processable_gemfile?
  has_gemfile_penchant?
end

#headerObject



98
99
100
101
102
103
104
105
106
# File 'lib/penchant/gemfile.rb', line 98

def header
  header = [ "# generated by penchant, environment: #{current_env}" ]

  if is_deployment
    header << ", deployment mode (was #{environment})"
  end

  header.join
end

#prior_environmentObject



108
109
110
# File 'lib/penchant/gemfile.rb', line 108

def prior_environment
  gemfile_header[%r{\(was (.+)\)}, 1]
end

#processable_gemfile_pathObject



64
65
66
# File 'lib/penchant/gemfile.rb', line 64

def processable_gemfile_path
  gemfile_penchant_path
end

#run_dot_penchant!(env, deployment) ⇒ Object



94
95
96
# File 'lib/penchant/gemfile.rb', line 94

def run_dot_penchant!(env, deployment)
  DotPenchant.run(env || environment, deployment) if has_dot_penchant?
end

#switch_back!(fallback_env) ⇒ Object



112
113
114
# File 'lib/penchant/gemfile.rb', line 112

def switch_back!(fallback_env)
  switch_to!(prior_environment || fallback_env)
end

#switch_to!(gemfile_env = nil, deployment = false) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/penchant/gemfile.rb', line 86

def switch_to!(gemfile_env = nil, deployment = false)
  @env, @is_deployment = gemfile_env, deployment

  output = [ header, process ]

  File.open(gemfile_path, 'wb') { |fh| fh.print output.join("\n") }
end