bake 0.3.0 → 0.4.0

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
  SHA256:
3
- metadata.gz: a957f57bf8396c2e06cb82745bf11c87ac287097c77b28500dc14a0abd9accf5
4
- data.tar.gz: a4292acbdecb68e4d77b926c01347f09564b59db01d88e8705696ad5d2baab92
3
+ metadata.gz: 190cf7469e9cbb8f4db68447334aa9797050e06da5b351755b6d8580e70a8b71
4
+ data.tar.gz: b0ccbec0d89acf14fdd81a503736d0a66242deb4a18f07601c2bcf92fb25c4a6
5
5
  SHA512:
6
- metadata.gz: 3f1af90dd73b4f104f09d8594ff3a36a84ddfbe24038c84207a3e714d850cae0b9d719bb6fc6875ede1cf3908f4b26ab02ec1562ae518e3dd2ab7fed1f0a9c2c
7
- data.tar.gz: d1639bd8ca01c56450406055f51a22a70d46bffc8b999a04ea35e80180ed672a6ae7ab5bc37046359c563c1989e5a82aa04932521ad1b4f4ca119d8a8ad63883
6
+ metadata.gz: 78493f41f9267d11ff32e5280fff448fdd0541f6527d2cf59059f2650ca60c3e1436f40388721c2e9d3263b8ada72b5f2b49fd30cc36853723208c7c93e88506
7
+ data.tar.gz: d922e66e49de98e5c01f5504f42e80064e435ffe9f357b4d900a8dbe6d4d942be9dc2d661b0a8d5c54e84ef73e42288aaf3fce4843916b11aef2fd4dd1344dfc
@@ -33,29 +33,11 @@ module Bake
33
33
  @parent.bakefile
34
34
  end
35
35
 
36
- def update_working_directory
37
- if bakefile = self.bakefile
38
- current_directory = Dir.pwd
39
- working_directory = File.dirname(bakefile)
40
-
41
- if working_directory != current_directory
42
- Console.logger.debug(self) {"Changing working directory to #{working_directory.inspect}."}
43
- Dir.chdir(working_directory)
44
-
45
- return current_directory
46
- end
47
- end
48
-
49
- return nil
50
- end
51
-
52
36
  many :commands, "The commands & arguments to invoke.", default: ["default"]
53
37
 
54
38
  def call
55
39
  context = @parent.context
56
40
 
57
- self.update_working_directory
58
-
59
41
  context.call(*@commands)
60
42
  end
61
43
  end
@@ -24,6 +24,8 @@ require 'set'
24
24
  module Bake
25
25
  module Command
26
26
  class List < Samovar::Command
27
+ PARAMETER = /@param\s+(?<name>.*?)\s+\[(?<type>.*?)\]\s+(?<details>.*?)\Z/
28
+
27
29
  def format_parameters(parameters, terminal)
28
30
  parameters.each do |type, name|
29
31
  case type
@@ -53,16 +55,22 @@ module Bake
53
55
  def print_scope(terminal, scope)
54
56
  format_recipe = self.method(:format_recipe).curry
55
57
 
56
- scope.recipes do |recipe|
58
+ scope.recipes.sort_by(&:command).each do |recipe|
57
59
  terminal.print_line
58
60
  terminal.print_line("\t", format_recipe[recipe])
59
61
 
60
62
  recipe.description.each do |line|
61
- terminal.print_line("\t\t", :description, line)
63
+ if match = line.match(PARAMETER)
64
+ terminal.print_line("\t\t",
65
+ :parameter, match[:name], :reset, " [",
66
+ :type, match[:type], :reset, "] ",
67
+ :description, match[:details]
68
+ )
69
+ else
70
+ terminal.print_line("\t\t", :description, line)
71
+ end
62
72
  end
63
73
  end
64
-
65
- terminal.print_line
66
74
  end
67
75
 
68
76
  def call
@@ -70,7 +78,9 @@ module Bake
70
78
  terminal = @parent.terminal
71
79
  context = @parent.context
72
80
 
73
- print_scope(terminal, context.scope)
81
+ if scope = context.scope
82
+ print_scope(terminal, context.scope)
83
+ end
74
84
 
75
85
  context.loaders.each do |loader|
76
86
  terminal.print_line(:loader, loader)
@@ -29,27 +29,9 @@ module Bake
29
29
  class Top < Samovar::Command
30
30
  self.description = "Execute tasks using Ruby."
31
31
 
32
- def self.bakefile_path(current = Dir.pwd)
33
- while current
34
- bakefile_path = File.join(current, "bake.rb")
35
-
36
- if File.exist?(bakefile_path)
37
- return bakefile_path
38
- end
39
-
40
- parent = File.dirname(current)
41
-
42
- if current == parent
43
- break
44
- else
45
- current = parent
46
- end
47
- end
48
- end
49
-
50
32
  options do
51
33
  option '-h/--help', 'Show help.'
52
- option '-b/--bakefile <path>', 'Path to the bakefile to use.', default: Top.bakefile_path
34
+ option '-b/--bakefile <path>', 'Override the path to the bakefile to use.'
53
35
  end
54
36
 
55
37
  nested :command, {
@@ -69,29 +51,17 @@ module Bake
69
51
  terminal[:keyreq] = terminal.style(:red, nil, :bold)
70
52
  terminal[:keyrest] = terminal.style(:green)
71
53
 
54
+ terminal[:parameter] = terminal[:opt]
55
+
72
56
  return terminal
73
57
  end
74
58
 
75
59
  def bakefile
76
- @options[:bakefile]
77
- end
78
-
79
- def working_directory
80
- if bakefile = self.bakefile
81
- File.dirname(self.bakefile)
82
- else
83
- Dir.pwd
84
- end
60
+ @options[:bakefile] || Dir.pwd
85
61
  end
86
62
 
87
63
  def context
88
- if bakefile = self.bakefile
89
- return Context.load(self.bakefile)
90
- else
91
- raise "Cannot find `bake.rb` file."
92
- end
93
-
94
- return context
64
+ Context.load(self.bakefile)
95
65
  end
96
66
 
97
67
  def call
@@ -21,36 +21,71 @@
21
21
  require_relative 'base'
22
22
 
23
23
  module Bake
24
+ BAKEFILE = "bake.rb"
25
+
24
26
  class Context
25
- def self.load(file_path, loaders = nil)
26
- scope = Scope.load(file_path)
27
+ # If path points to a file, assume it's a `bake.rb` file. Otherwise, recursively search up the directory tree starting from `path` to find the specified bakefile.
28
+ # @return [String, nil] the path to the bakefile if it could be found.
29
+ def self.bakefile_path(path, bakefile: BAKEFILE)
30
+ if File.file?(path)
31
+ return path
32
+ end
33
+
34
+ current = path
27
35
 
28
- unless loaders
29
- if scope.respond_to?(:loaders)
30
- loaders = scope.loaders
36
+ while current
37
+ bakefile_path = File.join(current, BAKEFILE)
38
+
39
+ if File.exist?(bakefile_path)
40
+ return bakefile_path
41
+ end
42
+
43
+ parent = File.dirname(current)
44
+
45
+ if current == parent
46
+ break
31
47
  else
32
- working_directory = File.dirname(file_path)
33
- loaders = Loaders.default(working_directory)
48
+ current = parent
34
49
  end
35
50
  end
36
51
 
37
- self.new(scope, loaders)
52
+ return nil
38
53
  end
39
54
 
40
- def initialize(scope, loaders, **options)
41
- base = Base.derive
42
- base.prepend(scope)
55
+ def self.load(path)
56
+ if bakefile_path = self.bakefile_path(path)
57
+ scope = Scope.load(bakefile_path)
58
+
59
+ working_directory = File.dirname(bakefile_path)
60
+ loaders = Loaders.default(working_directory)
61
+ else
62
+ scope = nil
63
+
64
+ working_directory = path
65
+ loaders = Loaders.default(working_directory)
66
+ end
43
67
 
68
+ return self.new(loaders, scope, working_directory)
69
+ end
70
+
71
+ def initialize(loaders, scope = nil, root = nil)
44
72
  @loaders = loaders
45
73
 
46
74
  @stack = []
47
75
 
48
- @scopes = Hash.new do |hash, key|
49
- hash[key] = scope_for(key)
76
+ @instances = Hash.new do |hash, key|
77
+ hash[key] = instance_for(key)
50
78
  end
51
79
 
52
- @scope = base.new(self)
53
- @scopes[[]] = @scope
80
+ @scope = scope
81
+ @root = root
82
+
83
+ if @scope
84
+ base = Base.derive
85
+ base.prepend(@scope)
86
+
87
+ @instances[[]] = base.new(self)
88
+ end
54
89
 
55
90
  @recipes = Hash.new do |hash, key|
56
91
  hash[key] = recipe_for(key)
@@ -58,6 +93,7 @@ module Bake
58
93
  end
59
94
 
60
95
  attr :scope
96
+ attr :root
61
97
  attr :loaders
62
98
 
63
99
  def call(*commands)
@@ -80,20 +116,20 @@ module Bake
80
116
  def recipe_for(command)
81
117
  path = command.split(":")
82
118
 
83
- if scope = @scopes[path]
84
- return scope.recipe_for(path.last)
119
+ if instance = @instances[path]
120
+ return instance.recipe_for(path.last)
85
121
  else
86
122
  *path, name = *path
87
123
 
88
- if scope = @scopes[path]
89
- return scope.recipe_for(name)
124
+ if instance = @instances[path]
125
+ return instance.recipe_for(name)
90
126
  end
91
127
  end
92
128
 
93
129
  return nil
94
130
  end
95
131
 
96
- def scope_for(path)
132
+ def instance_for(path)
97
133
  if base = base_for(path)
98
134
  return base.new(self)
99
135
  end
@@ -113,13 +149,5 @@ module Bake
113
149
 
114
150
  return base
115
151
  end
116
-
117
- def with!(scope)
118
- @stack << scope
119
-
120
- yield
121
- ensure
122
- @scope.pop
123
- end
124
152
  end
125
153
  end
@@ -23,6 +23,7 @@ module Bake
23
23
  def initialize(scope, name, method = nil)
24
24
  @scope = scope
25
25
  @name = name
26
+ @command = nil
26
27
  @description = nil
27
28
 
28
29
  @method = method
@@ -52,15 +53,7 @@ module Bake
52
53
  end
53
54
 
54
55
  def command
55
- path = @scope.path
56
-
57
- if path.empty?
58
- @name.to_s
59
- elsif path.last.to_sym == @name
60
- path.join(':')
61
- else
62
- (path + [@name]).join(':')
63
- end
56
+ @command ||= compute_command
64
57
  end
65
58
 
66
59
  def to_s
@@ -118,6 +111,18 @@ module Bake
118
111
 
119
112
  private
120
113
 
114
+ def compute_command
115
+ path = @scope.path
116
+
117
+ if path.empty?
118
+ @name.to_s
119
+ elsif path.last.to_sym == @name
120
+ path.join(':')
121
+ else
122
+ (path + [@name]).join(':')
123
+ end
124
+ end
125
+
121
126
  def read_description
122
127
  file, line_number = self.method.source_location
123
128
 
@@ -43,6 +43,8 @@ module Bake
43
43
  end
44
44
 
45
45
  def recipes
46
+ return to_enum(:recipes) unless block_given?
47
+
46
48
  names = self.instance_methods
47
49
 
48
50
  names.each do |name|
@@ -19,5 +19,5 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  module Bake
22
- VERSION = "0.3.0"
22
+ VERSION = "0.4.0"
23
23
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bake
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-16 00:00:00.000000000 Z
11
+ date: 2020-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: samovar