runfile 0.1.1.pre → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 140227a208cb696ecea61be2900adaa55a2e57c4
4
- data.tar.gz: a8ebcc1b213d108fdb90661b586364860e6dbea3
3
+ metadata.gz: b145578577cc500e75a76396084f5c708f3ffc19
4
+ data.tar.gz: d3f5047c705832d2184c8975b0f55dfb7149354e
5
5
  SHA512:
6
- metadata.gz: f8fc91d59ced071ec86d3bb156937b7f321872fc50f52a3269adfdc4d3227d4d086e05e187140bb397bab920fe326e6c69ce42a19f1dc7a88fed4ec58d4515e6
7
- data.tar.gz: 1e42a00ceea31aa897dcc4905e137b2d9c24dcfcb015735c3000c43b5892835c4f1ec2d6204b676566d6cea7a292e8da2795f71cc2a93d80488f5d3a0c732b97
6
+ metadata.gz: 075be67037b4a3bb1680f0c90188332a4ca757f6f2672052496551a5bd8b8d763928e68d5ae304395e0f96306f3d7e058f21b6d3d310c706fd57811be589d674
7
+ data.tar.gz: 64f564a08e4e93deb4ba3ec4afcb371dc7788a68b7c1bf819efd37ff57f2bf4b526f138bac7aae0f89d76c208aabae1deffd841d6bbd45a58217eef52eed45b3
@@ -1,4 +1,9 @@
1
1
  module Runfile
2
+
3
+ # The Action class represents a single Runfile action.
4
+ # This object holds all the information needed to execute it and
5
+ # show its help text (excluding the options, as they are considered
6
+ # global throughout the application)
2
7
  class Action
3
8
  attr :usage, :help
4
9
 
@@ -8,6 +13,7 @@ module Runfile
8
13
  @block = block
9
14
  end
10
15
 
16
+ # Call the provided block
11
17
  def execute(args)
12
18
  @block.yield args
13
19
  end
@@ -3,6 +3,9 @@ require 'colsole'
3
3
 
4
4
  module Runfile
5
5
  include Colsole
6
+
7
+ # The DocoptMaker class handles the dynamic generation of the docopt
8
+ # document.
6
9
  class DocoptMaker
7
10
  def initialize(version, summary, actions, options)
8
11
  @version = version
@@ -11,6 +14,8 @@ module Runfile
11
14
  @options = options
12
15
  end
13
16
 
17
+ # Generate a document based on all the actions, help messages
18
+ # and options we have collected from the Runfile DSL.
14
19
  def make
15
20
  width, height = detect_terminal_size
16
21
  doc = "Runfile #{@version}\n"
data/lib/runfile/dsl.rb CHANGED
@@ -1,25 +1,41 @@
1
+ # This file defines all the commands supported in a Runfile.
2
+ # All commands are immediately handed over to the Runner instance
3
+ # for handling.
4
+
1
5
  module Runfile
6
+
7
+ # Set the version of your Runfile program
2
8
  def version(ver)
3
9
  Runner.instance.version = ver
4
10
  end
5
11
 
12
+ # Set the one line summary of your Runfile program
13
+ def summary(text)
14
+ Runner.instance.summary = text
15
+ end
16
+
17
+ # Set the usage pattern for the next action
6
18
  def usage(text)
7
19
  Runner.instance.last_usage = text
8
20
  end
9
21
 
22
+ # Set the help message for the next action
10
23
  def help(text)
11
24
  Runner.instance.last_help = text
12
25
  end
13
26
 
14
- def action(name, &block)
15
- Runner.instance.add_action name, &block
27
+ # Add an option/flag to the next action (can be called multiple
28
+ # times)
29
+ def option(flag, text)
30
+ Runner.instance.add_option flag, text
16
31
  end
17
32
 
18
- def summary(text)
19
- Runner.instance.summary = text
33
+ # Define the action
34
+ def action(name, &block)
35
+ Runner.instance.add_action name, &block
20
36
  end
21
37
 
22
- def option(flag, text)
23
- Runner.instance.add_option flag, text
38
+ def command(name)
39
+ Runner.instance.namespace = name
24
40
  end
25
41
  end
@@ -1,47 +1,64 @@
1
1
  require 'docopt'
2
2
 
3
3
  module Runfile
4
+
5
+ # The Runner class is the main workhorse behind Runfile.
6
+ # It handles all the Runfile DSL commands and executes the Runfile.
4
7
  class Runner
5
- attr_writer :last_usage, :last_help, :version, :summary
8
+ attr_accessor :last_usage, :last_help, :version, :summary, :namespace
6
9
 
7
10
  @@instance = nil
8
11
 
12
+ # Initialize all variables to a sensible default.
9
13
  def initialize
10
14
  @last_usage = nil
11
15
  @last_help = nil
16
+ @namespace = nil
12
17
  @actions = {}
13
18
  @options = {}
14
19
  @version = "0.0.0"
15
20
  @summary = false
16
21
  end
17
22
 
23
+ # Return a singleton Runner instance.
18
24
  def self.instance
19
25
  @@instance = self.new if @@instance.nil?
20
26
  @@instance
21
27
  end
22
28
 
29
+ # Load and execute a Runfile call.
23
30
  def execute(argv)
24
31
  File.exist? 'Runfile' or abort "Runfile not found"
25
32
  load 'Runfile'
26
33
  @@instance.run *argv
27
34
  end
28
35
 
36
+ # Add an action to the @actions array, and use the last known
37
+ # usage and help messages sent by the DSL.
29
38
  def add_action(name, &block)
39
+ @last_usage = name if @last_usage.nil?
40
+ if @namespace
41
+ name = "#{namespace}_#{name}".to_sym
42
+ @last_usage = "#{@namespace} #{@last_usage}"
43
+ end
30
44
  @actions[name] = Action.new(block, @last_usage, @last_help)
31
45
  @last_usage = nil
32
46
  @last_help = nil
33
47
  end
34
48
 
49
+ # Add an option flag and its help text.
35
50
  def add_option(flag, text)
36
51
  @options[flag] = text
37
52
  end
38
53
 
54
+ # Run the command. This is a wrapper around docopt. It will
55
+ # generate the docopt document on the fly, using all the
56
+ # information collected so far.
39
57
  def run(*argv)
40
- action = argv[0]
41
- action and action = action.to_sym
58
+ action = find_action argv
42
59
  begin
43
60
  args = Docopt::docopt(docopt, version: @version, argv:argv)
44
- @actions.has_key? action or abort "Runfile error: Action :#{action} is not defined"
61
+ action or abort "Runfile error: Action not found"
45
62
  @actions[action].execute args
46
63
  rescue Docopt::Exit => e
47
64
  puts e.message
@@ -50,9 +67,27 @@ module Runfile
50
67
 
51
68
  private
52
69
 
70
+ # Dynamically generate the docopt document.
53
71
  def docopt
54
72
  maker = DocoptMaker.new(@version, @summary, @actions, @options)
55
73
  maker.make
56
74
  end
75
+
76
+ # Inspect the first two arguments in the argv and look for
77
+ # a matching action or command_action.
78
+ # We give priority to the second form (:make_jam) in order to
79
+ # also allow "overloading" of the command as an action
80
+ # (e.g. also allow a global action called :make).
81
+ def find_action(argv)
82
+ if argv.size >= 2
83
+ action = "#{argv[0]}_#{argv[1]}".to_sym
84
+ return action if @actions.has_key? action
85
+ end
86
+ if argv.size >= 1
87
+ action = argv[0].to_sym
88
+ return action if @actions.has_key? action
89
+ end
90
+ return false
91
+ end
57
92
  end
58
93
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: runfile
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1.pre
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-13 00:00:00.000000000 Z
11
+ date: 2015-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colsole
@@ -51,7 +51,7 @@ files:
51
51
  - lib/runfile/docopt_maker.rb
52
52
  - lib/runfile/dsl.rb
53
53
  - lib/runfile/runner.rb
54
- homepage: http://sector-seven.net
54
+ homepage: https://github.com/DannyBen/runfile
55
55
  licenses:
56
56
  - MIT
57
57
  metadata: {}
@@ -66,9 +66,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
66
66
  version: '0'
67
67
  required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  requirements:
69
- - - ">"
69
+ - - ">="
70
70
  - !ruby/object:Gem::Version
71
- version: 1.3.1
71
+ version: '0'
72
72
  requirements: []
73
73
  rubyforge_project:
74
74
  rubygems_version: 2.4.6