Class: Moonshadow::Manifest
- Inherits:
-
ShadowPuppet::Manifest
- Object
- ShadowPuppet::Manifest
- Moonshadow::Manifest
- Defined in:
- lib/moonshadow/manifest.rb
Overview
This is the base Moonshadow Manifest class, which provides a simple system for loading moonshadow recpies from plugins, a template helper, and parses several configuration files:
config/moonshadow.yml
The contents of config/moonshadow.yml
are expected to serialize into a hash, and are loaded into the manifest’s Configatron::Store.
config/database.yml
The contents of your database config are parsed and are available at configuration[:database]
.
If you’d like to create another ‘default rails stack’ using other tools that what Moonshadow::Manifest::Rails uses, subclass this and go nuts.
Direct Known Subclasses
Defined Under Namespace
Classes: Rails
Class Method Summary collapse
-
.deploy_stage ⇒ Object
The current deployment target.
- .initial_configuration ⇒ Object
-
.plugin(name = nil) ⇒ Object
Load a Moonshadow Plugin.
-
.rails_env ⇒ Object
The current Rails environment.
-
.rails_root ⇒ Object
The working directory of the Rails application this manifests describes.
Instance Method Summary collapse
-
#database_environment ⇒ Object
The current environment’s database configuration.
-
#deploy_stage ⇒ Object
The current deployment target.
-
#on_stage(*args) ⇒ Object
Only run tasks on the specified deploy_stage.
-
#rails_env ⇒ Object
The current Rails environment.
- #rails_root ⇒ Object
-
#template(pathname, b = binding) ⇒ Object
Render the ERB template located at
pathname
.
Class Method Details
.deploy_stage ⇒ Object
The current deployment target. Best when used with capistrano-ext’s multistage settings.
65 66 67 |
# File 'lib/moonshadow/manifest.rb', line 65 def self.deploy_stage ENV['DEPLOY_STAGE'] || 'undefined' end |
.initial_configuration ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/moonshadow/manifest.rb', line 136 def self.initial_configuration # config/moonshadow.yml moonshadow_yml = IO.read(File.join(rails_root, 'config', 'moonshadow.yml')) rescue nil configure(YAML::load(ERB.new(moonshadow_yml).result)) if moonshadow_yml # database config database_yml = IO.read(File.join(rails_root, 'config', 'database.yml')) rescue nil configure(:database => YAML::load(ERB.new(database_yml).result)) if database_yml # gems configure(:gems => (YAML.load_file(File.join(rails_root, 'config', 'gems.yml')) rescue nil)) end |
.plugin(name = nil) ⇒ Object
Load a Moonshadow Plugin
class MyManifest < Moonshadow::Manifest
# Evals vendor/plugins/moonshadow_my_app/moonshadow/init.rb
plugin :moonshadow_my_app
# Evals lib/my_recipe.rb
plugin 'lib/my_recipe.rb'
...
end
30 31 32 33 34 35 36 37 38 |
# File 'lib/moonshadow/manifest.rb', line 30 def self.plugin(name = nil) if name.is_a?(Symbol) path = File.join(rails_root, 'vendor', 'plugins', 'moonshadow_' + name.to_s, 'moonshadow', 'init.rb') else path = name end Kernel.eval(File.read(path), binding, path) true end |
.rails_env ⇒ Object
The current Rails environment
50 51 52 |
# File 'lib/moonshadow/manifest.rb', line 50 def self.rails_env ENV["RAILS_ENV"] || 'production' end |
.rails_root ⇒ Object
The working directory of the Rails application this manifests describes.
41 42 43 |
# File 'lib/moonshadow/manifest.rb', line 41 def self.rails_root @rails_root ||= File.(ENV["RAILS_ROOT"] || Dir.getwd) end |
Instance Method Details
#database_environment ⇒ Object
The current environment’s database configuration
60 61 62 |
# File 'lib/moonshadow/manifest.rb', line 60 def database_environment configuration[:database][rails_env.to_sym] end |
#deploy_stage ⇒ Object
The current deployment target. Best when used with capistrano-ext’s multistage settings.
70 71 72 |
# File 'lib/moonshadow/manifest.rb', line 70 def deploy_stage self.class.deploy_stage end |
#on_stage(*args) ⇒ Object
Only run tasks on the specified deploy_stage.
You can call it with the exact stage you want to run on:
on_stage(:my_stage) do
puts "I'm on my_stage"
end
Or you can pass an array of stages to run on:
on_stage(:my_stage, :my_other_stage) do
puts "I'm on one of my stages"
end
Or you can run a task unless it is on a stage:
on_stage(:unless => :my_stage) do
puts "I'm not on my_stage"
end
Or you can run a task unless it is on one of several stages:
on_stage(:unless => [:my_stage, :my_other_stage]) do
puts "I'm not on my stages"
end
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/moonshadow/manifest.rb', line 99 def on_stage(*args) = args. if_opt = [:if] unless_opt = [:unless] unless if_opt || unless_opt if_opt = args end if if_opt && if_opt.is_a?(Array) && if_opt.map {|x| x.to_s}.include?(deploy_stage) yield elsif if_opt && (if_opt.is_a?(String) || if_opt.is_a?(Symbol)) && deploy_stage == if_opt.to_s yield elsif unless_opt && unless_opt.is_a?(Array) && !unless_opt.map {|x| x.to_s}.include?(deploy_stage) yield elsif unless_opt && (unless_opt.is_a?(String) || unless_opt.is_a?(Symbol)) && deploy_stage != unless_opt.to_s yield end end |
#rails_env ⇒ Object
The current Rails environment
55 56 57 |
# File 'lib/moonshadow/manifest.rb', line 55 def rails_env self.class.rails_env end |
#rails_root ⇒ Object
45 46 47 |
# File 'lib/moonshadow/manifest.rb', line 45 def rails_root self.class.rails_root end |
#template(pathname, b = binding) ⇒ Object
Render the ERB template located at pathname
. If a template exists with the same basename at RAILS_ROOT/app/manifests/templates
, it is used instead. This is useful to override templates provided by plugins to customize application configuration files.
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/moonshadow/manifest.rb', line 123 def template(pathname, b = binding) template_contents = nil basename = pathname.index('/') ? pathname.split('/').last : pathname if File.exist?(File.(File.join(rails_root, 'app', 'manifests', 'templates', basename))) template_contents = File.read(File.(File.join(rails_root, 'app', 'manifests', 'templates', basename))) elsif File.exist?(File.(pathname)) template_contents = File.read(File.(pathname)) else raise LoadError, "Can't find template #{pathname}" end ERB.new(template_contents).result(b) end |