Module: Wired::Actions
- Included in:
- AppBuilder
- Defined in:
- lib/wired/actions.rb
Instance Method Summary collapse
- #action_mailer_host(rails_env, host) ⇒ Object
- #concat_file(source, destination) ⇒ Object
-
#download(from, to = from.split("/").last) ⇒ Object
File Management.
- #download_file(uri_string, destination) ⇒ Object
-
#file_from_repo(github_user, repo, sha, filename, to = filename) ⇒ Object
grab an arbitrary file from github.
-
#heroku(command = {}) ⇒ Object
Heroku management Run a command with the Heroku gem.
- #load_from_file_in_template(file_name, parent_binding = nil, file_group = 'default', file_type = :pattern) ⇒ Object
-
#load_pattern(pattern_name, pattern_group = "default", parent_binding = nil) ⇒ Object
Load a pattern from a file, potentially with string interpolation.
-
#load_snippet(snippet_name, snippet_group = "default", parent_binding = nil) ⇒ Object
Load a snippet from a file.
-
#load_template_config_file(config_file_name, config_file_group = template_identifier) ⇒ Object
YAML.load a configuration from a file.
- #replace_in_file(relative_path, find, replace) ⇒ Object
Instance Method Details
#action_mailer_host(rails_env, host) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/wired/actions.rb', line 18 def action_mailer_host(rails_env, host) inject_into_file( "config/environments/#{rails_env}.rb", "\n\n config.action_mailer.default_url_options = { :host => '#{host}' }", :before => "\nend" ) end |
#concat_file(source, destination) ⇒ Object
4 5 6 7 |
# File 'lib/wired/actions.rb', line 4 def concat_file(source, destination) contents = IO.read(find_in_source_paths(source)) append_file destination, contents end |
#download(from, to = from.split("/").last) ⇒ Object
File Management
61 62 63 64 65 66 67 |
# File 'lib/wired/actions.rb', line 61 def download(from, to = from.split("/").last) #run "curl -s -L #{from} > #{to}" file to, open(from).read rescue puts "Can't get #{from} - Internet down?" exit! end |
#download_file(uri_string, destination) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/wired/actions.rb', line 26 def download_file(uri_string, destination) uri = URI.parse(uri_string) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if uri_string =~ /^https/ request = Net::HTTP::Get.new(uri.path) contents = http.request(request).body path = File.join(destination_root, destination) File.open(path, "w") { |file| file.write(contents) } end |
#file_from_repo(github_user, repo, sha, filename, to = filename) ⇒ Object
grab an arbitrary file from github
70 71 72 |
# File 'lib/wired/actions.rb', line 70 def file_from_repo(github_user, repo, sha, filename, to = filename) download("http://github.com/#{github_user}/#{repo}/raw/#{sha}/#{filename}", to) end |
#heroku(command = {}) ⇒ Object
Heroku management Run a command with the Heroku gem.
Examples
heroku :create
heroku :rake => "db:migrate"
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/wired/actions.rb', line 46 def heroku(command = {}) in_root do if command.is_a?(Symbol) log 'running', "heroku #{command}" run "heroku #{command}" else command.each do |command, | log 'running', "heroku #{command} #{options}" run("heroku #{command} #{options}") end end end end |
#load_from_file_in_template(file_name, parent_binding = nil, file_group = 'default', file_type = :pattern) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/wired/actions.rb', line 74 def load_from_file_in_template(file_name, parent_binding = nil, file_group = 'default', file_type = :pattern) base_name = file_name.gsub(/^\./, '') begin if file_type == :config contents = {} else contents = '' end paths = template_paths paths.each do |template_path| full_file_name = File.join(template_path, file_type.to_s.pluralize, file_group, base_name) debug_log "Searching for #{full_file_name} ... " next unless File.exists? full_file_name debug_log "Found!" if file_type == :config contents = open(full_file_name) { |f| YAML.load(f) } else contents = open(full_file_name) { |f| f.read } end if contents && parent_binding contents = eval("\"" + contents.gsub('"','\\"') + "\"", parent_binding) end # file loaded, stop searching break if contents end contents rescue => ex debug_log "Error in load_from_file_in_template #{file_name}" debug_log ex. end end |
#load_pattern(pattern_name, pattern_group = "default", parent_binding = nil) ⇒ Object
Load a pattern from a file, potentially with string interpolation
116 117 118 |
# File 'lib/wired/actions.rb', line 116 def load_pattern(pattern_name, pattern_group = "default", parent_binding = nil) load_from_file_in_template(pattern_name, parent_binding, pattern_group, :pattern) end |
#load_snippet(snippet_name, snippet_group = "default", parent_binding = nil) ⇒ Object
Load a snippet from a file
111 112 113 |
# File 'lib/wired/actions.rb', line 111 def load_snippet(snippet_name, snippet_group = "default", parent_binding = nil) load_from_file_in_template(snippet_name, parent_binding, snippet_group, :snippet) end |
#load_template_config_file(config_file_name, config_file_group = template_identifier) ⇒ Object
YAML.load a configuration from a file
121 122 123 |
# File 'lib/wired/actions.rb', line 121 def load_template_config_file(config_file_name, config_file_group = template_identifier) load_from_file_in_template(config_file_name, nil, config_file_group, :config ) end |
#replace_in_file(relative_path, find, replace) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/wired/actions.rb', line 9 def replace_in_file(relative_path, find, replace) path = File.join(destination_root, relative_path) contents = IO.read(path) unless contents.gsub!(find, replace) raise "#{find.inspect} not found in #{relative_path}" end File.open(path, "w") { |file| file.write(contents) } end |