Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Move helper methods into a module #55

Closed
wants to merge 2 commits into from

2 participants

Phil Monroe Adam Rogers
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?

Adam Rogers
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)
Adam Rogers rodreegez closed this August 22, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
101  bin/powder
@@ -4,7 +4,8 @@ require 'rubygems'
4 4
 require 'thor'
5 5
 require 'fileutils'
6 6
 require 'net/https'
7  
-require 'powder/version'
  7
+require 'powder'
  8
+
8 9
 
9 10
 module Powder
10 11
   class CLI < Thor
@@ -20,11 +21,6 @@ module Powder
20 21
     map '-v' => 'version'
21 22
     map 'update' => 'install'
22 23
 
23  
-    POWDER_CONFIG = ".powder"
24  
-    POW_PATH = "#{ENV['HOME']}/.pow"
25  
-    POW_DAEMON_PLIST_PATH="#{ENV['HOME']}/Library/LaunchAgents/cx.pow.powd.plist"
26  
-    POW_FIREWALL_PLIST_PATH = "/Library/LaunchDaemons/cx.pow.firewall.plist"
27  
-
28 24
     desc "up", "Enable pow"
29 25
     def up
30 26
       if File.exists? POW_FIREWALL_PLIST_PATH
@@ -123,12 +119,7 @@ module Powder
123 119
 
124 120
     desc "list", "List current pows"
125 121
     def list
126  
-      pows = Dir[POW_PATH + "/*"].map do |link|
127  
-        realpath = File.readlink(link)
128  
-        app_is_current = (realpath == Dir.pwd) ? '*' : ' '
129  
-        [app_is_current, File.basename(link), realpath.gsub(ENV['HOME'], '~')]
130  
-      end
131  
-      print_table(pows)
  122
+      print_table(pow_apps)
132 123
     end
133 124
 
134 125
     desc "open", "Open a pow in the browser"
@@ -241,92 +232,8 @@ module Powder
241 232
     end
242 233
 
243 234
     private
  235
+    include Powder::Helpers
244 236
 
245  
-    def current_dir_name
246  
-      File.basename(%x{pwd}.chomp)
247  
-    end
248  
-
249  
-    def configured_pow_name
250  
-      return nil unless File.exists?(POWDER_CONFIG)
251  
-
252  
-      File.foreach(POWDER_CONFIG) do |line|
253  
-        next if line =~ /^($|#)/
254  
-        return line.chomp
255  
-      end
256  
-
257  
-      return nil
258  
-    end
259  
-
260  
-    def current_dir_pow_name
261  
-      current_dir_name.tr('_', '-')
262  
-    end
263  
-
264  
-    def get_pow_name
265  
-      configured_pow_name || current_dir_pow_name
266  
-    end
267  
-
268  
-    def write_pow_config(name=nil)
269  
-      return if ! name || options[:"no-config"]
270  
-      powder_exists = File.exists?(POWDER_CONFIG)
271  
-      configured_name = get_pow_name
272  
-
273  
-      unlink if powder_exists && configured_name != name
274  
-
275  
-      if options[:force] || ! powder_exists
276  
-        File.open(POWDER_CONFIG, "w") do |f|
277  
-          f.puts(name)
278  
-        end
279  
-        say "Created powder config #{POWDER_CONFIG}"
280  
-      elsif !options[:force] && powder_exists && configured_name != name
281  
-        say "Cowardly refusing to overwrite #{POWDER_CONFIG}"
282  
-        exit 1
283  
-      end
284  
-    end
285  
-
286  
-    def is_powable?
287  
-      if File.exists?('config.ru') || File.exists?('public/index.html')
288  
-        true
289  
-      elsif legacy = (is_rails2_app? || is_radiant_app?)
290  
-        say "This appears to be a #{legacy} application. You need a config.ru file."
291  
-        if yes? "Do you want to autogenerate a basic config.ru for #{legacy}?"
292  
-          uri = URI.parse("https://raw.github.com/gist/909308")
293  
-          http = Net::HTTP.new(uri.host, uri.port)
294  
-          http.use_ssl = true
295  
-          http.verify_mode = OpenSSL::SSL::VERIFY_NONE
296  
-          request = Net::HTTP::Get.new(uri.request_uri)
297  
-          create_file "config.ru",  http.request(request).body
298  
-          return true
299  
-        else
300  
-          say "Did not create config.ru"
301  
-          return false
302  
-        end
303  
-      else
304  
-        say "This does not appear to be a rack app as there is no config.ru."
305  
-        say "Pow can also host static apps if there is an index.html in public/"
306  
-        return false
307  
-      end
308  
-    end
309  
-
310  
-    def is_rails2_app?
311  
-      File.exists?('config/environment.rb') &&
312  
-        !`grep RAILS_GEM_VERSION config/environment.rb`.empty? ? 'Rails 2' : nil
313  
-    end
314  
-
315  
-    def is_radiant_app?
316  
-      File.exists?('config/environment.rb') &&
317  
-        !`grep Radiant::Initializer config/environment.rb`.empty? ? 'Radiant' : nil
318  
-    end
319  
-
320  
-    def domain
321  
-      if File.exists? File.expand_path('~/.powconfig')
322  
-        returned_domain = %x{source ~/.powconfig; echo $POW_DOMAINS}.gsub("\n", "").split(",").first
323  
-        returned_domain = %x{source ~/.powconfig; echo $POW_DOMAIN}.gsub("\n", "") if returned_domain.nil? || returned_domain.empty?
324  
-        returned_domain = 'dev' if returned_domain.nil? || returned_domain.empty?
325  
-        returned_domain
326  
-      else
327  
-        'dev'
328  
-      end
329  
-    end
330 237
   end
331 238
 end
332 239
 Powder::CLI.start
4  lib/powder.rb
... ...
@@ -1,2 +1,2 @@
1  
-module Powder
2  
-end
  1
+require 'powder/version'
  2
+require 'powder/helpers'
102  lib/powder/helpers.rb
... ...
@@ -0,0 +1,102 @@
  1
+module Powder
  2
+  module Helpers
  3
+    POWDER_CONFIG = ".powder"
  4
+    POW_PATH = "#{ENV['HOME']}/.pow"
  5
+    POW_DAEMON_PLIST_PATH="#{ENV['HOME']}/Library/LaunchAgents/cx.pow.powd.plist"
  6
+    POW_FIREWALL_PLIST_PATH = "/Library/LaunchDaemons/cx.pow.firewall.plist"
  7
+    
  8
+    def current_dir_name
  9
+      File.basename(%x{pwd}.chomp)
  10
+    end
  11
+
  12
+    def configured_pow_name
  13
+      return nil unless File.exists?(POWDER_CONFIG)
  14
+
  15
+      File.foreach(POWDER_CONFIG) do |line|
  16
+        next if line =~ /^($|#)/
  17
+        return line.chomp
  18
+      end
  19
+
  20
+      return nil
  21
+    end
  22
+
  23
+    def current_dir_pow_name
  24
+      current_dir_name.tr('_', '-')
  25
+    end
  26
+
  27
+    def get_pow_name
  28
+      configured_pow_name || current_dir_pow_name
  29
+    end
  30
+
  31
+    def write_pow_config(name=nil)
  32
+      return if ! name || options[:"no-config"]
  33
+      powder_exists = File.exists?(POWDER_CONFIG)
  34
+      configured_name = get_pow_name
  35
+
  36
+      unlink if powder_exists && configured_name != name
  37
+
  38
+      if options[:force] || ! powder_exists
  39
+        File.open(POWDER_CONFIG, "w") do |f|
  40
+          f.puts(name)
  41
+        end
  42
+        say "Created powder config #{POWDER_CONFIG}"
  43
+      elsif !options[:force] && powder_exists && configured_name != name
  44
+        say "Cowardly refusing to overwrite #{POWDER_CONFIG}"
  45
+        exit 1
  46
+      end
  47
+    end
  48
+
  49
+    def is_powable?
  50
+      if File.exists?('config.ru') || File.exists?('public/index.html')
  51
+        true
  52
+      elsif legacy = (is_rails2_app? || is_radiant_app?)
  53
+        say "This appears to be a #{legacy} application. You need a config.ru file."
  54
+        if yes? "Do you want to autogenerate a basic config.ru for #{legacy}?"
  55
+          uri = URI.parse("https://raw.github.com/gist/909308")
  56
+          http = Net::HTTP.new(uri.host, uri.port)
  57
+          http.use_ssl = true
  58
+          http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  59
+          request = Net::HTTP::Get.new(uri.request_uri)
  60
+          create_file "config.ru",  http.request(request).body
  61
+          return true
  62
+        else
  63
+          say "Did not create config.ru"
  64
+          return false
  65
+        end
  66
+      else
  67
+        say "This does not appear to be a rack app as there is no config.ru."
  68
+        say "Pow can also host static apps if there is an index.html in public/"
  69
+        return false
  70
+      end
  71
+    end
  72
+
  73
+    def is_rails2_app?
  74
+      File.exists?('config/environment.rb') &&
  75
+        !`grep RAILS_GEM_VERSION config/environment.rb`.empty? ? 'Rails 2' : nil
  76
+    end
  77
+
  78
+    def is_radiant_app?
  79
+      File.exists?('config/environment.rb') &&
  80
+        !`grep Radiant::Initializer config/environment.rb`.empty? ? 'Radiant' : nil
  81
+    end
  82
+
  83
+    def domain
  84
+      if File.exists? File.expand_path('~/.powconfig')
  85
+        returned_domain = %x{source ~/.powconfig; echo $POW_DOMAINS}.gsub("\n", "").split(",").first
  86
+        returned_domain = %x{source ~/.powconfig; echo $POW_DOMAIN}.gsub("\n", "") if returned_domain.nil? || returned_domain.empty?
  87
+        returned_domain = 'dev' if returned_domain.nil? || returned_domain.empty?
  88
+        returned_domain
  89
+      else
  90
+        'dev'
  91
+      end
  92
+    end
  93
+    
  94
+    def pow_apps
  95
+      Dir[POW_PATH + "/*"].map do |link|
  96
+        realpath = File.readlink(link)
  97
+        app_is_current = (realpath == Dir.pwd) ? '*' : ' '
  98
+        [app_is_current, File.basename(link), realpath.gsub(ENV['HOME'], '~')]
  99
+      end
  100
+    end
  101
+  end
  102
+end
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.