Sutto-perennial 0.2.2.3 → 0.2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,6 +15,29 @@ module Perennial
15
15
  klass.new.apply(*arguments)
16
16
  end
17
17
 
18
+ def yes?(question)
19
+ result = Readline.readline("#{question.to_s.strip} (y/n) ")
20
+ result.downcase[0] == ?y
21
+ end
22
+
23
+ def ask(question, default)
24
+ result = Readline.readline("#{question.to_s.strip} (default: #{default}) ")
25
+ result.blank? ? default : result
26
+ end
27
+
28
+ def ask_password(question)
29
+ system "stty -echo"
30
+ line = Readline.readline("#{question.to_s.strip} ").strip
31
+ system "stty echo"
32
+ print "\n"
33
+ return line
34
+ end
35
+
36
+ def die!(message)
37
+ $stderr.puts message
38
+ exit! 1
39
+ end
40
+
18
41
  end
19
42
 
20
43
  attr_accessor :options, :banner, :command_env
@@ -44,19 +67,20 @@ module Perennial
44
67
  end
45
68
  end
46
69
 
47
- def controller!(controller, description)
70
+ def controller!(controller, description, opts = {})
48
71
  return unless defined?(Loader)
49
72
  add_default_options!
50
73
  option :kill, "Kill any runninng instances"
51
74
  controller_name = controller.to_s.underscore
52
75
  controller = controller.to_sym
53
76
  command_name = controller_name.gsub("_", "-")
54
- add("#{command_name} [PATH]", description) do |*args|
77
+ add("#{command_name} #{"[PATH]" if !opts[:skip_path]}".strip, description) do |*args|
55
78
  options = args.extract_options!
56
79
  path = File.expand_path(args[0] || ".")
57
80
  Settings.root = path
58
81
  if options.delete(:kill)
59
- puts "Attempting to kill processess..."
82
+ attempt_showing_banner
83
+ puts "Attempting to kill processess for #{command_name}"
60
84
  Daemon.kill_all(controller)
61
85
  else
62
86
  Loader.run!(controller, options)
@@ -86,35 +110,32 @@ module Perennial
86
110
  if @commands.has_key?(command)
87
111
  execute_command(command, arguments)
88
112
  else
89
- puts "Unknown command '#{command}', please try again."
90
- return usage
113
+ show_error "Unknown command '#{command}', please try again."
114
+ usage(true)
91
115
  end
92
116
  end
93
117
 
94
- def usage
95
- if banner.present?
96
- puts banner
97
- puts ""
98
- end
118
+ def usage(skip_banner = false)
119
+ attempt_showing_banner unless skip_banner
99
120
  puts "Usage:"
100
121
  max_width = @banners.values.map { |b| b.length }.max
101
122
  @commands.keys.sort.each do |command|
102
123
  next unless @descriptions.has_key?(command)
103
124
  formatted_command = "#{@banners[command]} [OPTIONS]".ljust(max_width + 10)
104
- command = "%s - %s" % [formatted_command, @descriptions[command]]
125
+ command = " %s - %s" % [formatted_command, @descriptions[command]]
105
126
  puts command
106
127
  end
128
+ puts ""
129
+ puts "Please note: you can pass -h / --help to any command for more specific help"
107
130
  end
108
131
 
109
- def help_for(command)
110
- if banner.present?
111
- puts banner
112
- puts ""
113
- end
132
+ def help_for(command, skip_banner = false)
133
+ attempt_showing_banner unless skip_banner
114
134
  puts @descriptions[command]
135
+ puts ""
115
136
  puts "Usage: #{$0} #{@banners[command]} [options]"
116
137
  puts "Options:"
117
- puts @option_parsers[command].summary
138
+ puts pad_left(@option_parsers[command].summary)
118
139
  exit
119
140
  end
120
141
 
@@ -128,6 +149,13 @@ module Perennial
128
149
  application.execute args
129
150
  end
130
151
 
152
+ def attempt_showing_banner
153
+ if banner.present?
154
+ puts banner
155
+ puts ""
156
+ end
157
+ end
158
+
131
159
  protected
132
160
 
133
161
  def execute_command(command, arguments)
@@ -137,7 +165,7 @@ module Perennial
137
165
  args << opts
138
166
  @command_env.execute(command_proc, args)
139
167
  else
140
- usage
168
+ help_for(command, true)
141
169
  end
142
170
  end
143
171
 
@@ -160,14 +188,28 @@ module Perennial
160
188
  needed_count = blk.arity - 1
161
189
  provided_count = arguments.size
162
190
  if needed_count > 0 && needed_count != provided_count
163
- puts "You didn't provide the correct number of arguments (needed #{needed_count}, provided #{provided_count})"
191
+ attempt_showing_banner
192
+ show_error "You didn't provide the correct number of arguments (needed #{needed_count}, provided #{provided_count})"
164
193
  elsif needed_count < 0 && (-needed_count - 2) > provided_count
165
- puts "You didn't provide enough arguments - a minimum of #{-needed_count} are needed."
194
+ show_error "You didn't provide enough arguments - a minimum of #{-needed_count} are needed."
166
195
  else
167
196
  return true
168
197
  end
169
198
 
170
199
  end
171
200
 
201
+ def show_error(text)
202
+ attempt_showing_banner
203
+ text = "Error: #{text}".strip
204
+ puts "--#{"-" * text.length}"
205
+ puts " #{text} "
206
+ puts "--#{"-" * text.length}"
207
+ puts ""
208
+ end
209
+
210
+ def pad_left(text, spacing = 2)
211
+ text.split("\n").map { |l| "#{" " * spacing}#{l}" }.join("\n")
212
+ end
213
+
172
214
  end
173
215
  end
@@ -1,6 +1,7 @@
1
1
  require 'open-uri'
2
2
  require 'fileutils'
3
3
  require 'erb'
4
+ require 'readline'
4
5
 
5
6
  module Perennial
6
7
  class Generator
@@ -32,12 +33,46 @@ module Perennial
32
33
  def initialize(destination, opts = {})
33
34
  @destination_path = destination
34
35
  @template_path = opts[:template_path] || File.join(Settings.library_root, "templates")
35
- puts "Starting generator for #{destination}"
36
+ @silent = !!opts[:silent]
37
+ describe "Initializing generator in #{destination}"
36
38
  end
37
39
 
38
- def download(from, to)
40
+ # Helpers for testing file state
41
+
42
+ def fu
43
+ FileUtils
44
+ end
45
+
46
+ def chmod(permissions, path)
47
+ describe "Changing permissions for #{path} to #{permissions}"
48
+ FileUtils.chmod(permissions, expand_destination_path(path))
49
+ end
50
+
51
+ def file?(path)
52
+ describe "Checking if #{path} is a file"
53
+ File.file?(expand_destination_path(path))
54
+ end
55
+
56
+ def executable?(path)
57
+ describe "Checking if #{path} is an executable"
58
+ File.executable?(expand_destination_path(path))
59
+ end
60
+
61
+ def directory?(path)
62
+ describe "Checking if #{path} is a directory"
63
+ File.directory?(expand_destination_path(path))
64
+ end
65
+
66
+ alias folder? directory?
67
+
68
+ def exists?(path)
69
+ describe "Checking if #{path} exists"
70
+ File.exits?(expand_destination_path(path))
71
+ end
72
+
73
+ def download(from, to, append = false)
39
74
  describe "Downloading #{from}"
40
- file to, open(from).read
75
+ file to, open(from).read, append
41
76
  end
42
77
 
43
78
  def folders(*args)
@@ -47,20 +82,20 @@ module Perennial
47
82
  end
48
83
  end
49
84
 
50
- def file(name, contents)
85
+ def file(name, contents, append = false)
51
86
  dest_folder = File.dirname(name)
52
87
  folders(dest_folder) unless File.directory?(expand_destination_path(dest_folder))
53
88
  describe "Creating file #{name}"
54
- File.open(expand_destination_path(name), "w+") do |f|
89
+ File.open(expand_destination_path(name), "#{append ? "a" : "w"}+") do |f|
55
90
  f.write(contents)
56
91
  end
57
92
  end
58
93
 
59
- def template(source, destination, environment = {})
94
+ def template(source, destination, environment = {}, append = false)
60
95
  describe "Processing template #{source}"
61
96
  raw_template = File.read(expand_template_path(source))
62
97
  processed_template = ERB.new(raw_template).result(binding_for(environment))
63
- file destination, processed_template
98
+ file destination, processed_template, append
64
99
  end
65
100
 
66
101
  protected
@@ -82,7 +117,7 @@ module Perennial
82
117
  end
83
118
 
84
119
  def describe(action)
85
- puts "- #{action}"
120
+ puts "[generator] #{action}" unless @silent
86
121
  end
87
122
 
88
123
  end
@@ -53,7 +53,10 @@ module Perennial
53
53
  end
54
54
 
55
55
  def current_controller
56
- @current_controller ||= @@controllers[@@current_type.to_sym]
56
+ @current_controller ||= begin
57
+ c = @@controllers[@@current_type.to_sym]
58
+ c.is_a?(String) ? eval(c) : c
59
+ end
57
60
  end
58
61
 
59
62
  protected
@@ -19,9 +19,18 @@ module Perennial
19
19
  setup!
20
20
  end
21
21
 
22
+ def default_logger_path=(value)
23
+ @@default_logger_path = value
24
+ # Rereun setup if setup is already done.
25
+ setup! if setup?
26
+ end
27
+
28
+ def default_logger_path
29
+ @@default_logger_path ||= (Settings.root / "log" / @@log_name.to_str)
30
+ end
31
+
22
32
  def setup!
23
- log_path = Settings.root / "log" / @@log_name.to_str
24
- @@logger = new(log_path, Settings.log_level, Settings.verbose?)
33
+ @@logger = new(self.default_logger_path, Settings.log_level, Settings.verbose?)
25
34
  @@setup = true
26
35
  end
27
36
 
@@ -39,16 +39,25 @@ module Perennial
39
39
  @@setup ||= false
40
40
  end
41
41
 
42
+ def default_settings_path
43
+ @@default_settings_path ||= (root / "config" / "settings.yml")
44
+ end
45
+
46
+ def default_settings_path=(value)
47
+ @@default_settings_path = value
48
+ setup! if setup?
49
+ end
50
+
42
51
  def setup(options = {})
43
52
  self.setup!(options) unless setup?
44
53
  end
45
54
 
46
55
  def setup!(options = {})
47
56
  @@configuration = {}
48
- settings_file = root / "config" / "settings.yml"
57
+ settings_file = self.default_settings_path
49
58
  if File.exist?(settings_file)
50
59
  loaded_yaml = YAML.load(File.read(settings_file))
51
- @@configuration.merge! loaded_yaml["default"]
60
+ @@configuration.merge!(loaded_yaml["default"] || {})
52
61
  end
53
62
  @@configuration.merge! options
54
63
  @@configuration.symbolize_keys!
data/lib/perennial.rb CHANGED
@@ -9,7 +9,7 @@ require 'perennial/exceptions'
9
9
 
10
10
  module Perennial
11
11
 
12
- VERSION = "0.2.2.3"
12
+ VERSION = "0.2.3.0"
13
13
 
14
14
  has_libary :dispatchable, :hookable, :loader, :logger,
15
15
  :loggable, :manifest, :settings, :argument_parser,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Sutto-perennial
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2.3
4
+ version: 0.2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Darcy Laycock
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-04 00:00:00 -07:00
12
+ date: 2009-09-06 00:00:00 -07:00
13
13
  default_executable: perennial
14
14
  dependencies: []
15
15