squared 0.2.5 → 0.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.
@@ -35,33 +35,44 @@ module Squared
35
35
  end
36
36
  end
37
37
 
38
- def env(key, default = nil, suffix: @envname, equals: nil, ignore: nil)
39
- ret = env_value(key, suffix: suffix)
38
+ def env(key, default = nil, suffix: nil, strict: false, equals: nil, ignore: nil)
39
+ ret = env_value(key, suffix: suffix, strict: strict)
40
40
  return ret == equals.to_s unless equals.nil?
41
41
 
42
42
  ret.empty? || (ignore && as_a(ignore).any? { |val| ret == val.to_s }) ? default : ret
43
43
  end
44
44
 
45
- def env_value(key, default = '', suffix: nil)
46
- suffix && (ret = ENV["#{key}_#{suffix}"]) ? ret : ENV.fetch(key, default)
45
+ def env_value(key, default = '', suffix: nil, strict: false)
46
+ if suffix
47
+ if (ret = ENV["#{key}#{@envname ? "_#{@envname}" : ''}_#{suffix}"])
48
+ return ret
49
+ elsif strict
50
+ return default
51
+ end
52
+ end
53
+ if @envname
54
+ return ret if (ret = ENV["#{key}_#{@envname}"])
55
+ return default if strict
56
+ end
57
+ ENV.fetch(key, default)
47
58
  end
48
59
 
49
- def env_bool(key, default = false, suffix: nil)
60
+ def env_bool(key, default = false, suffix: nil, strict: false, index: false)
50
61
  if key.is_a?(::String)
51
- case env_value(key, suffix: suffix)
62
+ case (val = env_value(key, suffix: suffix, strict: strict))
52
63
  when ''
53
64
  default
54
65
  when '0', 'false'
55
66
  false
56
67
  else
57
- true
68
+ index && val.to_i > 0 ? val.to_i : true
58
69
  end
59
70
  else
60
71
  key.nil? ? default : key
61
72
  end
62
73
  end
63
74
 
64
- def env_pipe(key, default = 1, suffix: nil, root: nil)
75
+ def env_pipe(key, default = 1, suffix: nil, strict: false, root: nil)
65
76
  if default.is_a?(::String)
66
77
  begin
67
78
  default = (root ? root.join(default) : Pathname.new(default)).realdirpath
@@ -72,7 +83,7 @@ module Squared
72
83
  end
73
84
  case key
74
85
  when ::String
75
- case (ret = env_value(key, suffix: suffix))
86
+ case (ret = env_value(key, suffix: suffix, strict: strict))
76
87
  when '0', '1', '2'
77
88
  return ret.to_i
78
89
  end
@@ -82,8 +93,8 @@ module Squared
82
93
  default
83
94
  end
84
95
 
85
- def env_match(key, default = nil, suffix: nil, options: 0, timeout: nil)
86
- case (ret = env_value(key, suffix: suffix))
96
+ def env_match(key, default = nil, suffix: nil, strict: false, options: 0, timeout: nil)
97
+ case (val = env_value(key, suffix: suffix, strict: strict))
87
98
  when ''
88
99
  default
89
100
  when '0'
@@ -91,7 +102,7 @@ module Squared
91
102
  when '1'
92
103
  true
93
104
  else
94
- Regexp.new(ret, options, timeout: timeout)
105
+ Regexp.new(val, options, timeout: timeout)
95
106
  end
96
107
  end
97
108
  end
@@ -33,7 +33,7 @@ module Squared
33
33
  end
34
34
 
35
35
  def to_s
36
- super.match(/[^:]+\z/)[0]
36
+ super[/[^:]+\z/, 0]
37
37
  end
38
38
  end
39
39
 
@@ -48,6 +48,7 @@ module Squared
48
48
  if project && (project.respond_to?(:workspace) || (project = __get__(:project)[project.to_s]))
49
49
  main = project.basepath(main).to_s
50
50
  @project = project
51
+ @envname = project.instance_variable_get(:@envname)
51
52
  @required = true
52
53
  end
53
54
  @name = name || @project&.name
@@ -89,7 +90,7 @@ module Squared
89
90
  @required = true
90
91
  project ? [project, 'not found'] : ['name', 'missing']
91
92
  end
92
- warn log_message(:warn, msg, subject: self.class, hint: hint)
93
+ warn log_message(Logger::WARN, msg, subject: self.class, hint: hint)
93
94
  end
94
95
 
95
96
  def build
@@ -238,7 +239,7 @@ module Squared
238
239
  title = Pathname.new(file)
239
240
  .realpath
240
241
  .to_s
241
- .sub(Regexp.new("\\A#{Regexp.escape(File.join(Dir.pwd, ''))}"), '')
242
+ .sub(/\A#{Regexp.escape(File.join(Dir.pwd, ''))}/, '')
242
243
  emphasize(lines, title: title, sub: unless stdin?
243
244
  [
244
245
  { pat: /\A((?:[^:]|(?<! ):(?! ))+)\z/, styles: theme[:banner] },
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Squared
4
- VERSION = '0.2.5'
4
+ VERSION = '0.3.0'
5
5
  end
@@ -56,7 +56,7 @@ module Squared
56
56
  end
57
57
 
58
58
  def to_s
59
- super.match(/[^:]+\z/)[0]
59
+ super[/[^:]+\z/, 0]
60
60
  end
61
61
 
62
62
  attr_reader :kind_project
@@ -68,7 +68,7 @@ module Squared
68
68
 
69
69
  attr_reader :root, :home, :main, :prefix, :exception, :warning, :pipe, :verbose, :theme, :series, :closed
70
70
 
71
- def initialize(home = Dir.pwd, *, main: nil, prefix: nil,
71
+ def initialize(home = (ARG[:HOME] && ENV[ARG[:HOME]]) || Dir.pwd, *, main: nil, prefix: nil,
72
72
  verbose: ARG[:VERBOSE], common: ARG[:COMMON], pipe: ARG[:PIPE], exception: ARG[:FAIL], **)
73
73
  @home = Pathname.new(home).realdirpath
74
74
  basename = @home.basename.to_s
@@ -78,15 +78,17 @@ module Squared
78
78
  else
79
79
  @main = basename.freeze
80
80
  end
81
+ @home.mkpath rescue nil
81
82
  @root = @home.parent
82
83
  @prefix = prefix
83
84
  @series = Application.impl_series.new(self)
84
85
  @project = {}
85
86
  @kind = {}
86
87
  @extensions = []
88
+ @envname = @main.gsub(/[^\w]+/, '_').upcase.freeze
87
89
  @pipe = env_pipe(pipe, (ARG[:OUT] && env(ARG[:OUT])) || 1, root: @home)
88
90
  @exception = env_bool(exception)
89
- @verbose = env_bool(verbose, verbose.nil? || verbose.is_a?(String) ? @pipe != 0 : verbose)
91
+ @verbose = env_bool(verbose, verbose.nil? || verbose.is_a?(String) ? @pipe != 0 : verbose, index: true)
90
92
  @warning = @verbose != false
91
93
  @closed = false
92
94
  if common
@@ -105,6 +107,12 @@ module Squared
105
107
  group: {},
106
108
  ref: {}
107
109
  }.freeze
110
+ @pass = {
111
+ group: {},
112
+ ref: {},
113
+ global: {},
114
+ pattern: []
115
+ }.freeze
108
116
  @banner = {
109
117
  group: {},
110
118
  ref: {}
@@ -113,14 +121,13 @@ module Squared
113
121
  end
114
122
 
115
123
  def initialize_session
116
- @envname = @main.gsub(/[^\w]+/, '_').upcase.freeze
117
124
  return unless @pipe.is_a?(Pathname)
118
125
 
119
126
  bord = '#' * Project.line_width
120
127
  puts bord, format('Session started on %s by %s', Time.now.to_s, @main), bord
121
128
  end
122
129
 
123
- def build(parallel: [], **kwargs)
130
+ def build(parallel: [], pass: nil, **kwargs)
124
131
  return self unless enabled? && !@closed
125
132
 
126
133
  if kwargs[:pattern].is_a?(Array)
@@ -130,6 +137,7 @@ module Squared
130
137
  kwargs[:parallel] = parallel.reject { |val| val.is_a?(Regexp) && (kwargs[:pattern] << val) }
131
138
  .map(&:to_s)
132
139
  end
140
+ @pass[:pattern].concat(pass.map { |val| val.is_a?(Regexp) ? val : val.to_s }) if pass
133
141
  @project.each_value do |proj|
134
142
  if proj.enabled?
135
143
  proj.populate(series.keys.dup, **kwargs)
@@ -138,7 +146,7 @@ module Squared
138
146
  else
139
147
  next
140
148
  end
141
- series.populate(proj)
149
+ series.populate(proj, **kwargs)
142
150
  end
143
151
 
144
152
  Application.kind_project.each { |obj| obj.populate(self, **kwargs) }
@@ -180,6 +188,10 @@ module Squared
180
188
  script_command :run, script, group, ref, on
181
189
  end
182
190
 
191
+ def script(script, group: @group, ref: @ref, on: nil)
192
+ script_command :script, script, group, ref, on
193
+ end
194
+
183
195
  def depend(script, group: @group, ref: @ref, on: nil)
184
196
  script_command :depend, script, group, ref, on
185
197
  end
@@ -196,6 +208,10 @@ module Squared
196
208
  script_command :doc, script, group, ref, on
197
209
  end
198
210
 
211
+ def lint(script, group: @group, ref: @ref, on: nil)
212
+ script_command :lint, script, group, ref, on
213
+ end
214
+
199
215
  def test(script, group: @group, ref: @ref, on: nil)
200
216
  script_command :test, script, group, ref, on
201
217
  end
@@ -208,6 +224,18 @@ module Squared
208
224
  script_command :exclude, as_a(base, :to_sym).freeze, group, ref
209
225
  end
210
226
 
227
+ def pass(name, group: @group, ref: @ref, &blk)
228
+ data = if group
229
+ @pass[:group][group] ||= {}
230
+ elsif ref
231
+ @pass[:ref][ref] ||= {}
232
+ else
233
+ @pass[:global]
234
+ end
235
+ data[name.to_sym] = blk
236
+ self
237
+ end
238
+
211
239
  def banner(*args, command: true, styles: nil, border: nil, group: @group, ref: @ref)
212
240
  data = { command: command, order: [], styles: check_style(styles, empty: false), border: check_style(border) }
213
241
  args.each do |meth|
@@ -528,6 +556,17 @@ module Squared
528
556
  task_base?(key) ? obj.has?(key, ref || baseref) : task_extend?(obj, key)
529
557
  end
530
558
 
559
+ def task_exclude?(key, obj = nil)
560
+ if obj
561
+ data = obj.group ? @pass[:group][obj.group] : @pass[:ref][obj.ref]
562
+ blk = (data && data[key.to_sym]) || @pass[:global][key.to_sym]
563
+ return true if blk && obj.instance_eval(&blk)
564
+
565
+ key = task_join(task_localname(obj.name), key)
566
+ end
567
+ @pass[:pattern].any? { |item| item.is_a?(Regexp) ? key =~ item : key == item }
568
+ end
569
+
531
570
  def task_defined?(*key)
532
571
  Rake::Task.task_defined?(key.size == 1 ? key.first : task_join(*key))
533
572
  end
@@ -600,9 +639,7 @@ module Squared
600
639
  end
601
640
  items.each do |name|
602
641
  (@script[label][name] ||= {})[task] = val
603
- next unless on.is_a?(Hash)
604
-
605
- (@events[label][name] ||= {})[task] = on
642
+ (@events[label][name] ||= {})[task] = on if on.is_a?(Hash)
606
643
  end
607
644
  self
608
645
  end
@@ -639,15 +676,13 @@ module Squared
639
676
  def root?(path, pass: [])
640
677
  return false unless path.directory?
641
678
 
642
- case path.children.size
643
- when 0
644
- true
645
- when 1
646
- target = path.children.first
647
- target.to_s == __FILE__ || pass.any? { |val| val == target.basename.to_s }
648
- else
649
- false
679
+ path.children.each do |c|
680
+ name = c.basename.to_s
681
+ next if c.to_s == __FILE__ || (@main == name && c.directory? && c.empty?) || pass.any? { |val| val == name }
682
+
683
+ return false
650
684
  end
685
+ true
651
686
  end
652
687
 
653
688
  def script?(state, target: nil, pat: nil, group: nil, ref: baseref, global: false)