Module: GemSuit::Application::Utils::InstanceMethods

Defined in:
lib/gem_suit/application/utils.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#root_pathObject

Returns the value of attribute root_path.



27
28
29
# File 'lib/gem_suit/application/utils.rb', line 27

def root_path
  @root_path
end

#validate_root_pathObject

Returns the value of attribute validate_root_path.



27
28
29
# File 'lib/gem_suit/application/utils.rb', line 27

def validate_root_path
  @validate_root_path
end

Instance Method Details

#bundle_installObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/gem_suit/application/utils.rb', line 29

def bundle_install
  return unless bundle_install?
  if verbose
    execute "bundle install", "(this can take several minutes...)"
  else
    puts "Running `bundle install` (this can take several minutes...)".yellow
    puts "(in #{root_path})"
    `cd #{root_path} && bundle install`
  end
end

#bundle_install?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/gem_suit/application/utils.rb', line 40

def bundle_install?
  `cd #{root_path} && bundle check`.any?{|line| line.include? "`bundle install`"}
end

#execute(command, text = "") ⇒ Object



112
113
114
115
116
# File 'lib/gem_suit/application/utils.rb', line 112

def execute(command, text = "")
  return if command.to_s.gsub(/\s/, "").size == 0
  log :executing, "#{command} #{text}"
  `cd #{root_path} && #{command}`
end

#expand_path(path) ⇒ Object



69
70
71
72
73
# File 'lib/gem_suit/application/utils.rb', line 69

def expand_path(path)
  Pathname.new(path).absolute? ?
    path :
    File.expand_path(path, root_path)
end

#log(action, string_or_force = nil, force = nil) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/gem_suit/application/utils.rb', line 118

def log(action, string_or_force = nil, force = nil)
  if %w(TrueClass FalseClass).include? string_or_force.class.name
    string = nil
    force  = string_or_force
  else
    string = string_or_force
    force  = false
  end
  return unless verbose || force
  output = [string || action]
  output.unshift action.to_s.capitalize.ljust(10, " ") unless string.nil?
  puts output.join("  ")
end

#mysql_passwordObject



107
108
109
110
# File 'lib/gem_suit/application/utils.rb', line 107

def mysql_password
  file = File.expand_path("mysql", shared_path)
  "#{File.new(file).read}".strip if File.exists? file
end

#new_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
# File 'lib/gem_suit/application/utils.rb', line 83

def new_file?(file)
  return false unless File.exists? expand_path(".new_files")
  root          = Pathname.new root_path
  relative_path = Pathname.new(file).relative_path_from(root).to_s
  File.readlines(expand_path(".new_files")).any?{|line| line.strip == relative_path}
end

#original(file) ⇒ Object



75
76
77
# File 'lib/gem_suit/application/utils.rb', line 75

def original(file)
  expand_path file.gsub(/\.#{STASHED_EXT}$/, "")
end

#rails_gem_versionObject



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gem_suit/application/utils.rb', line 94

def rails_gem_version
  case rails_version
  when 2
    File.readlines(expand_path("config/environment.rb")).each do |line|
      match = line.match /RAILS_GEM_VERSION\s*=\s*["']([\d\.]+)["']/
      return $1 if match
    end
  when 3
    files = [expand_path(stashed("Gemfile")), expand_path("Gemfile")]
    Gemfile.new(*files).gems["rails"][:version]
  end
end

#rails_versionObject



90
91
92
# File 'lib/gem_suit/application/utils.rb', line 90

def rails_version
  root_path.match(/\/rails-(\d)\//)[1].to_i
end

#shared_pathObject



61
62
63
# File 'lib/gem_suit/application/utils.rb', line 61

def shared_path
  File.expand_path("../../shared", root_path)
end

#stashed(file) ⇒ Object



79
80
81
# File 'lib/gem_suit/application/utils.rb', line 79

def stashed(file)
  expand_path file.match(/\.#{STASHED_EXT}$/) ? file : "#{file}.#{STASHED_EXT}"
end

#templates_pathObject



65
66
67
# File 'lib/gem_suit/application/utils.rb', line 65

def templates_path
  File.expand_path("../../templates", root_path)
end

#validate_root_path!(path) ⇒ Object



44
45
46
47
48
49
# File 'lib/gem_suit/application/utils.rb', line 44

def validate_root_path!(path)
  unless path.match(/rails-\d/)
    log "Running a #{self.class.name} instance from an invalid path: '#{path}' needs to match ".red + "/rails-\\d/".yellow
    exit
  end
end