Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Move helper methods into a module #55

Closed
wants to merge 2 commits into from

2 participants

@phil-monroe

This extracts all helper methods into a module, wich will allow others to use the Powder gem as a library. Specifically, it will help me with Powser ;)

Will probably conflict a bit with the other pull request that is out right now... Just let me know if you want me to wait and merge his commits into the module.

@phil-monroe

Any chance this will make it in?

@rodreegez
Owner

I'm gonna close this pull req, not because it's no good but because:

  • I left it way way way too long to look at it. Sorry.
  • I really want to refactor powder's internals properly. (by which I significantly different from how they are now)
@rodreegez rodreegez closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 29, 2011
  1. @phil-monroe

    modularizing

    phil-monroe authored
  2. @phil-monroe
This page is out of date. Refresh to see the latest.
Showing with 108 additions and 99 deletions.
  1. +4 −97 bin/powder
  2. +2 −2 lib/powder.rb
  3. +102 −0 lib/powder/helpers.rb
View
101 bin/powder
@@ -4,7 +4,8 @@ require 'rubygems'
require 'thor'
require 'fileutils'
require 'net/https'
-require 'powder/version'
+require 'powder'
+
module Powder
class CLI < Thor
@@ -20,11 +21,6 @@ module Powder
map '-v' => 'version'
map 'update' => 'install'
- POWDER_CONFIG = ".powder"
- POW_PATH = "#{ENV['HOME']}/.pow"
- POW_DAEMON_PLIST_PATH="#{ENV['HOME']}/Library/LaunchAgents/cx.pow.powd.plist"
- POW_FIREWALL_PLIST_PATH = "/Library/LaunchDaemons/cx.pow.firewall.plist"
-
desc "up", "Enable pow"
def up
if File.exists? POW_FIREWALL_PLIST_PATH
@@ -123,12 +119,7 @@ module Powder
desc "list", "List current pows"
def list
- pows = Dir[POW_PATH + "/*"].map do |link|
- realpath = File.readlink(link)
- app_is_current = (realpath == Dir.pwd) ? '*' : ' '
- [app_is_current, File.basename(link), realpath.gsub(ENV['HOME'], '~')]
- end
- print_table(pows)
+ print_table(pow_apps)
end
desc "open", "Open a pow in the browser"
@@ -241,92 +232,8 @@ module Powder
end
private
+ include Powder::Helpers
- def current_dir_name
- File.basename(%x{pwd}.chomp)
- end
-
- def configured_pow_name
- return nil unless File.exists?(POWDER_CONFIG)
-
- File.foreach(POWDER_CONFIG) do |line|
- next if line =~ /^($|#)/
- return line.chomp
- end
-
- return nil
- end
-
- def current_dir_pow_name
- current_dir_name.tr('_', '-')
- end
-
- def get_pow_name
- configured_pow_name || current_dir_pow_name
- end
-
- def write_pow_config(name=nil)
- return if ! name || options[:"no-config"]
- powder_exists = File.exists?(POWDER_CONFIG)
- configured_name = get_pow_name
-
- unlink if powder_exists && configured_name != name
-
- if options[:force] || ! powder_exists
- File.open(POWDER_CONFIG, "w") do |f|
- f.puts(name)
- end
- say "Created powder config #{POWDER_CONFIG}"
- elsif !options[:force] && powder_exists && configured_name != name
- say "Cowardly refusing to overwrite #{POWDER_CONFIG}"
- exit 1
- end
- end
-
- def is_powable?
- if File.exists?('config.ru') || File.exists?('public/index.html')
- true
- elsif legacy = (is_rails2_app? || is_radiant_app?)
- say "This appears to be a #{legacy} application. You need a config.ru file."
- if yes? "Do you want to autogenerate a basic config.ru for #{legacy}?"
- uri = URI.parse("https://raw.github.com/gist/909308")
- http = Net::HTTP.new(uri.host, uri.port)
- http.use_ssl = true
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
- request = Net::HTTP::Get.new(uri.request_uri)
- create_file "config.ru", http.request(request).body
- return true
- else
- say "Did not create config.ru"
- return false
- end
- else
- say "This does not appear to be a rack app as there is no config.ru."
- say "Pow can also host static apps if there is an index.html in public/"
- return false
- end
- end
-
- def is_rails2_app?
- File.exists?('config/environment.rb') &&
- !`grep RAILS_GEM_VERSION config/environment.rb`.empty? ? 'Rails 2' : nil
- end
-
- def is_radiant_app?
- File.exists?('config/environment.rb') &&
- !`grep Radiant::Initializer config/environment.rb`.empty? ? 'Radiant' : nil
- end
-
- def domain
- if File.exists? File.expand_path('~/.powconfig')
- returned_domain = %x{source ~/.powconfig; echo $POW_DOMAINS}.gsub("\n", "").split(",").first
- returned_domain = %x{source ~/.powconfig; echo $POW_DOMAIN}.gsub("\n", "") if returned_domain.nil? || returned_domain.empty?
- returned_domain = 'dev' if returned_domain.nil? || returned_domain.empty?
- returned_domain
- else
- 'dev'
- end
- end
end
end
Powder::CLI.start
View
4 lib/powder.rb
@@ -1,2 +1,2 @@
-module Powder
-end
+require 'powder/version'
+require 'powder/helpers'
View
102 lib/powder/helpers.rb
@@ -0,0 +1,102 @@
+module Powder
+ module Helpers
+ POWDER_CONFIG = ".powder"
+ POW_PATH = "#{ENV['HOME']}/.pow"
+ POW_DAEMON_PLIST_PATH="#{ENV['HOME']}/Library/LaunchAgents/cx.pow.powd.plist"
+ POW_FIREWALL_PLIST_PATH = "/Library/LaunchDaemons/cx.pow.firewall.plist"
+
+ def current_dir_name
+ File.basename(%x{pwd}.chomp)
+ end
+
+ def configured_pow_name
+ return nil unless File.exists?(POWDER_CONFIG)
+
+ File.foreach(POWDER_CONFIG) do |line|
+ next if line =~ /^($|#)/
+ return line.chomp
+ end
+
+ return nil
+ end
+
+ def current_dir_pow_name
+ current_dir_name.tr('_', '-')
+ end
+
+ def get_pow_name
+ configured_pow_name || current_dir_pow_name
+ end
+
+ def write_pow_config(name=nil)
+ return if ! name || options[:"no-config"]
+ powder_exists = File.exists?(POWDER_CONFIG)
+ configured_name = get_pow_name
+
+ unlink if powder_exists && configured_name != name
+
+ if options[:force] || ! powder_exists
+ File.open(POWDER_CONFIG, "w") do |f|
+ f.puts(name)
+ end
+ say "Created powder config #{POWDER_CONFIG}"
+ elsif !options[:force] && powder_exists && configured_name != name
+ say "Cowardly refusing to overwrite #{POWDER_CONFIG}"
+ exit 1
+ end
+ end
+
+ def is_powable?
+ if File.exists?('config.ru') || File.exists?('public/index.html')
+ true
+ elsif legacy = (is_rails2_app? || is_radiant_app?)
+ say "This appears to be a #{legacy} application. You need a config.ru file."
+ if yes? "Do you want to autogenerate a basic config.ru for #{legacy}?"
+ uri = URI.parse("https://raw.github.com/gist/909308")
+ http = Net::HTTP.new(uri.host, uri.port)
+ http.use_ssl = true
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
+ request = Net::HTTP::Get.new(uri.request_uri)
+ create_file "config.ru", http.request(request).body
+ return true
+ else
+ say "Did not create config.ru"
+ return false
+ end
+ else
+ say "This does not appear to be a rack app as there is no config.ru."
+ say "Pow can also host static apps if there is an index.html in public/"
+ return false
+ end
+ end
+
+ def is_rails2_app?
+ File.exists?('config/environment.rb') &&
+ !`grep RAILS_GEM_VERSION config/environment.rb`.empty? ? 'Rails 2' : nil
+ end
+
+ def is_radiant_app?
+ File.exists?('config/environment.rb') &&
+ !`grep Radiant::Initializer config/environment.rb`.empty? ? 'Radiant' : nil
+ end
+
+ def domain
+ if File.exists? File.expand_path('~/.powconfig')
+ returned_domain = %x{source ~/.powconfig; echo $POW_DOMAINS}.gsub("\n", "").split(",").first
+ returned_domain = %x{source ~/.powconfig; echo $POW_DOMAIN}.gsub("\n", "") if returned_domain.nil? || returned_domain.empty?
+ returned_domain = 'dev' if returned_domain.nil? || returned_domain.empty?
+ returned_domain
+ else
+ 'dev'
+ end
+ end
+
+ def pow_apps
+ Dir[POW_PATH + "/*"].map do |link|
+ realpath = File.readlink(link)
+ app_is_current = (realpath == Dir.pwd) ? '*' : ' '
+ [app_is_current, File.basename(link), realpath.gsub(ENV['HOME'], '~')]
+ end
+ end
+ end
+end
Something went wrong with that request. Please try again.