excavator 0.0.1 → 0.0.2

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.
@@ -1,10 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+
1
3
  require 'excavator'
4
+
2
5
  module Excavator
6
+
7
+ # Public: Runner is the entry point into an Excavator script library. When
8
+ # running an Excavator script from the command line, a Runner object is
9
+ # created.
10
+ #
11
+ # Runner duties:
12
+ #
13
+ # * Load all Excavator commands.
14
+ # * Provides the object that Excavator::DSL manipulates.
15
+ # * Finds the command from the command line arguments and executes.
16
+ # * Creates a useful help message to list all commands defined in the library.
17
+ #
3
18
  class Runner
4
19
 
5
- # Runner specific vars
6
- attr_accessor :command_paths
7
- attr_accessor :commands
20
+ # Public: An Array of filesystem paths where Excavator commands are found.
21
+ attr_writer :command_paths
22
+
23
+ # Public: A pointer to the current namespace.
8
24
  attr_reader :current_namespace
9
25
 
10
26
  def initialize
@@ -12,35 +28,57 @@ module Excavator
12
28
  @current_namespace = @namespace
13
29
  end
14
30
 
31
+ # Public: Return the current working directory for the script.
15
32
  def cwd
16
33
  Excavator.cwd
17
34
  end
18
35
 
19
- def namespaces
20
- @namespaces
21
- end
22
-
23
- def namespace
24
- @namespace
25
- end
26
-
36
+ # Public: A pointer to the current namespace being worked on.
27
37
  def current_namespace
28
38
  @current_namespace
29
39
  end
30
40
 
41
+ # Public: An Array of filesystem paths where Excavator commands are found.
42
+ # Defaults to the current directory's "command/" directory.
43
+ def command_paths
44
+ @command_paths = Excavator.command_paths || [cwd.join("commands")]
45
+ end
46
+
47
+ # Public: A helper to move the current_namespace pointer to new namespace,
48
+ # yield, and then return the pointer back to the previous namespace. If the
49
+ # namespace argument already exists (e.g., created by another file), then
50
+ # this points to the previously created namespace.
51
+ #
52
+ # name - A String/Symbol of the namespace to move the current_namespace
53
+ # pointer to.
54
+ #
55
+ # Examples
56
+ #
57
+ # runner = Runner.new
58
+ # runner.in_namespace("test") do
59
+ # runner.current_namespace
60
+ # # => <Namespace "test">
61
+ # end
62
+ #
63
+ # Returns nothing.
31
64
  def in_namespace(name)
32
- ns = @current_namespace.namespace(name)
33
- ns = @current_namespace << Excavator.namespace_class.new(name) unless ns
65
+ ns = @current_namespace.namespace(name) ||
66
+ Excavator.namespace_class.new(name)
67
+ @current_namespace << ns
68
+
34
69
  @current_namespace = ns
35
70
  yield
36
71
  @current_namespace = ns.parent
37
72
  end
38
73
 
39
- def clear_commands!
40
- self.commands = {}
41
- end
42
-
43
- def run(*args)
74
+ # Public: The entry point into all commands. This will load all commands
75
+ # from any external Excavator files, find the command from the command line,
76
+ # and execute the command.
77
+ #
78
+ # This method is never called directly. See Excavator.run.
79
+ #
80
+ # Returns value returned from the Command.
81
+ def run(args = ARGV)
44
82
  args.flatten!
45
83
 
46
84
  name = args.delete_at(0)
@@ -55,6 +93,12 @@ module Excavator
55
93
  command.execute *args
56
94
  end
57
95
 
96
+ # Public: Find a command given it's full name.
97
+ #
98
+ # cmd - A String/Symbol full name of the Command to look up.
99
+ # (e.g., "server:test:integration")
100
+ #
101
+ # Returns a Command.
58
102
  def find_command(cmd)
59
103
  *namespaces, command_name = cmd.to_s.split(':').collect {|c| c.to_sym }
60
104
  cur_namespace = current_namespace
@@ -65,10 +109,21 @@ module Excavator
65
109
  cur_namespace.command(command_name)
66
110
  end
67
111
 
68
- def display_help?(command_name)
69
- ["-h", "-?", "--help", "help"].include?(command_name)
112
+ # Public: The last command being worked on. This is designed for use
113
+ # with Excavator::DSL methods.
114
+ def last_command
115
+ @last_command ||= Excavator.command_class.new(
116
+ self, :namespace => current_namespace
117
+ )
70
118
  end
71
119
 
120
+ # Public: Clear the last command being worked on. This is designed for use
121
+ # with Excavator::DSL methods.
122
+ def clear_last_command!
123
+ @last_command = nil
124
+ end
125
+
126
+ # Internal: Display the list of all command names and their descriptions.
72
127
  def display_help
73
128
  table_view = Excavator::TableView.new do |t|
74
129
  t.title "#{File.basename($0)} commands:\n"
@@ -84,24 +139,23 @@ module Excavator
84
139
  puts table_view
85
140
  end
86
141
 
87
- def load_commands
88
- command_paths.each do |path|
89
- Dir["#{path.to_s}/**/*.rb"].each { |file| load file }
90
- end
142
+ # Internal: The root namespace for all other commands and namespaces.
143
+ def namespace
144
+ @namespace
91
145
  end
92
146
 
93
- def last_command
94
- @last_command ||= Excavator.command_class.new(
95
- self, :namespace => current_namespace
96
- )
97
- end
147
+ protected
98
148
 
99
- def clear_last_command!
100
- @last_command = nil
149
+ def display_help?(command_name)
150
+ ["-h", "-?", "--help", "help"].include?(command_name)
101
151
  end
102
152
 
103
- def command_paths
104
- @command_paths = Excavator.command_paths || [cwd.join("commands")]
153
+ # Internal: Loop through each command path and load all *.rb files.
154
+ def load_commands
155
+ command_paths.each do |path|
156
+ Dir["#{path.to_s}/**/*.rb"].each { |file| load file }
157
+ end
105
158
  end
159
+
106
160
  end # Runner
107
161
  end
@@ -1,3 +1,5 @@
1
+ # -*- encoding: utf-8 -*-
2
+
1
3
  require 'excavator'
2
4
  module Excavator
3
5
  class TableView
@@ -1,3 +1,5 @@
1
+ # -*- encoding: utf-8 -*-
2
+
1
3
  module Excavator
2
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2"
3
5
  end
@@ -1,3 +1,5 @@
1
+ # -*- encoding: utf-8 -*-
2
+
1
3
  require 'pathname'
2
4
  basedir = Pathname.new(File.dirname(__FILE__)).join("..").expand_path
3
5
  $LOAD_PATH.unshift(basedir.join("lib").to_s)
@@ -71,5 +71,26 @@ context "Command" do
71
71
  end
72
72
  assert error.params.include?(:server)
73
73
  end
74
+
75
+ test "#full_name includes namespace" do
76
+ ns = Namespace.new(:this_is_not_used_in_full_name)
77
+ ns1 = Namespace.new(:servers)
78
+ ns << ns1
79
+ cmd = Command.new(Excavator.runner)
80
+ cmd.name = :create
81
+ cmd.namespace = ns1
82
+
83
+ assert_equal "servers:create", cmd.full_name
84
+ end
85
+
86
+ test "#full_name does not include namespace if namespace's parent is nil" do
87
+ cmd = Command.new(Excavator.runner)
88
+ cmd.name = :create
89
+ cmd.namespace = Namespace.new(:servers)
90
+
91
+ assert cmd.namespace.parent.nil?
92
+ assert_equal "create", cmd.full_name
93
+ end
94
+
74
95
  end # Command
75
96
 
@@ -4,6 +4,7 @@ context "Namespace" do
4
4
  include Excavator
5
5
 
6
6
  setup do
7
+ Excavator.reset!
7
8
  @namespace = Namespace.new
8
9
  end
9
10
 
@@ -39,17 +40,23 @@ context "Namespace" do
39
40
  end
40
41
 
41
42
  test "#commands_and_descriptions returns names and description" do
42
- cmd = MiniTest::Mock.new
43
- cmd.expect(:name, "command")
44
- cmd.expect(:desc, "command description")
43
+ cmd = Command.new(
44
+ Excavator.runner,
45
+ :name => "command",
46
+ :desc => "command description",
47
+ :namespace => @namespace
48
+ )
45
49
  @namespace << cmd
46
50
 
47
51
  ns1 = Namespace.new("first")
48
52
  @namespace << ns1
49
53
 
50
- inner_cmd = MiniTest::Mock.new
51
- inner_cmd.expect(:name, "inner_command")
52
- inner_cmd.expect(:desc, "inner command description")
54
+ inner_cmd = Command.new(
55
+ Excavator.runner,
56
+ :name => "inner_command",
57
+ :desc => "inner command description",
58
+ :namespace => ns1
59
+ )
53
60
  ns1 << inner_cmd
54
61
 
55
62
  expected = [
@@ -94,7 +94,7 @@ test description
94
94
  USAGE: test-command [options]
95
95
 
96
96
  REQUIRED:
97
- -s, --server_id=SERVER_ID Server instance id
97
+ -s, --server-id=SERVER_ID Server instance id
98
98
 
99
99
  OPTIONAL:
100
100
  -r, --region=REGION A region to use
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: excavator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-06 00:00:00.000000000Z
12
+ date: 2012-02-08 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
16
- requirement: &70321668148280 !ruby/object:Gem::Requirement
16
+ requirement: &70295484482360 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 2.10.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70321668148280
24
+ version_requirements: *70295484482360
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: ruby-debug19
27
- requirement: &70321668147020 !ruby/object:Gem::Requirement
27
+ requirement: &70295484481400 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70321668147020
35
+ version_requirements: *70295484481400
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &70321668145740 !ruby/object:Gem::Requirement
38
+ requirement: &70295484480420 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70321668145740
46
+ version_requirements: *70295484480420
47
47
  description: ! 'Excavator is a scripting framework for writing multi-command executables
48
48
  for the
49
49
 
@@ -52,15 +52,15 @@ description: ! 'Excavator is a scripting framework for writing multi-command exe
52
52
  '
53
53
  email:
54
54
  - peter@paydrotalks.com
55
- executables:
56
- - excavator
55
+ executables: []
57
56
  extensions: []
58
57
  extra_rdoc_files: []
59
58
  files:
60
59
  - .gitignore
61
60
  - Gemfile
61
+ - LICENSE
62
+ - README.md
62
63
  - Rakefile
63
- - bin/excavator
64
64
  - excavator.gemspec
65
65
  - lib/excavator.rb
66
66
  - lib/excavator/command.rb
@@ -1,3 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'excavator'
3
- Excavator.run(ARGV)