Class: Squared::Workspace::Application

Inherits:
Object
  • Object
show all
Includes:
Format, Rake::DSL, Common, Repo
Defined in:
lib/squared/workspace/application.rb

Constant Summary collapse

WORKSPACE_KEYS =
TASK_BASE.keys.freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Repo

#repo

Constructor Details

#initialize(main, common: true, exception: false, pipe: false, verbose: nil) ⇒ Application

Returns a new instance of Application.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/squared/workspace/application.rb', line 57

def initialize(main, *, common: true, exception: false, pipe: false, verbose: nil, **)
  @main = main.to_s
  @home = Pathname.pwd
  @root = @home.parent
  @series = Series.new(TASK_BASE, self)
  @project = {}
  @extensions = []
  @script = {
    group: {},
    ref: {},
    build: nil,
    dev: nil,
    prod: nil
  }
  @theme = common && @verbose ? __get__(:theme)[:workspace] : {}
  @exception = exception.is_a?(String) ? !env(exception, ignore: '0').nil? : exception
  @pipe = pipe.is_a?(String) ? !env(pipe, ignore: '0').nil? : pipe
  @verbose = verbose.nil? ? !@pipe : verbose
  @warning = true
end

Class Attribute Details

.project_kindObject (readonly)

Returns the value of attribute project_kind.



49
50
51
# File 'lib/squared/workspace/application.rb', line 49

def project_kind
  @project_kind
end

Instance Attribute Details

#exceptionObject

Returns the value of attribute exception.



55
56
57
# File 'lib/squared/workspace/application.rb', line 55

def exception
  @exception
end

#homeObject (readonly)

Returns the value of attribute home.



54
55
56
# File 'lib/squared/workspace/application.rb', line 54

def home
  @home
end

#mainObject (readonly)

Returns the value of attribute main.



54
55
56
# File 'lib/squared/workspace/application.rb', line 54

def main
  @main
end

#pipeObject

Returns the value of attribute pipe.



55
56
57
# File 'lib/squared/workspace/application.rb', line 55

def pipe
  @pipe
end

#rootObject (readonly)

Returns the value of attribute root.



54
55
56
# File 'lib/squared/workspace/application.rb', line 54

def root
  @root
end

#seriesObject (readonly)

Returns the value of attribute series.



54
55
56
# File 'lib/squared/workspace/application.rb', line 54

def series
  @series
end

#themeObject (readonly)

Returns the value of attribute theme.



54
55
56
# File 'lib/squared/workspace/application.rb', line 54

def theme
  @theme
end

#verboseObject

Returns the value of attribute verbose.



55
56
57
# File 'lib/squared/workspace/application.rb', line 55

def verbose
  @verbose
end

#warningObject (readonly)

Returns the value of attribute warning.



54
55
56
# File 'lib/squared/workspace/application.rb', line 54

def warning
  @warning
end

Class Method Details

.find(path: nil, ref: nil) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/squared/workspace/application.rb', line 37

def find(path: nil, ref: nil)
  if ref.is_a?(::Symbol) || ref.is_a?(::String)
    ret = project_kind.find { |proj| proj.to_sym == ref.to_sym }
    return ret if ret
  end
  project_kind.find { |proj| proj.is_a?(path) } if path
end

.implement(*objs) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/squared/workspace/application.rb', line 25

def implement(*objs)
  objs.each do |obj|
    next unless obj < Project::Base

    project_kind.unshift(obj)
    obj.tasks&.each do |val|
      TASK_BASE[val] ||= []
      (TASK_EXTEND[val] ||= []).push(obj)
    end
  end
end

.to_sObject



45
46
47
# File 'lib/squared/workspace/application.rb', line 45

def to_s
  super.to_s.match(/[^:]+$/)[0]
end

Instance Method Details

#add(name, path = nil, ref: nil, **kwargs) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/squared/workspace/application.rb', line 130

def add(name, path = nil, ref: nil, **kwargs)
  path = root_path((path || name).to_s)
  project = if !ref.is_a?(::Class)
              Application.find(path: path, ref: ref)
            elsif ref < Project::Base
              ref
            end
  instance = (project || Project::Base).new(name, path, self, **kwargs)
  @project[n = name.to_sym] = instance
  __get__(:project)[n] = instance unless kwargs[:private]
  self
end

#apply(&blk) ⇒ Object



169
170
171
172
# File 'lib/squared/workspace/application.rb', line 169

def apply(&blk)
  instance_eval(&blk)
  self
end

#build(**kwargs) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/squared/workspace/application.rb', line 78

def build(**kwargs)
  return unless enabled?

  @project.each_value do |proj|
    next unless proj.enabled?

    series.populate(proj)
    proj.populate(**kwargs)
  end

  Application.project_kind.each { |obj| obj.populate(self, **kwargs) }
  @extensions.each { |ext| __send__(ext, **kwargs) }

  series[:refresh].clear if series[:refresh].all? { |val| val.end_with?(':build') }

  task default: kwargs[:default] if series.has?(kwargs[:default]) && !task_defined?('default')

  if series.has?('build')
    init = ['depend', dev? && series.has?('refresh') ? 'refresh' : 'build']

    task default: init[1] unless task_defined?('default')

    if series.has?(init[0]) && !task_defined?('init')
      desc 'init'
      task init: init
    end
  end
  series.finalize!(kwargs.fetch(:parallel, []))

  yield self if block_given?
end

#clean(script, group: nil, ref: nil) ⇒ Object



118
119
120
# File 'lib/squared/workspace/application.rb', line 118

def clean(script, group: nil, ref: nil)
  script_command :clean, script, group, ref
end

#compose(name, &blk) ⇒ Object



164
165
166
167
# File 'lib/squared/workspace/application.rb', line 164

def compose(name, &blk)
  namespace(name, &blk)
  self
end

#depend(script, group: nil, ref: nil) ⇒ Object



114
115
116
# File 'lib/squared/workspace/application.rb', line 114

def depend(script, group: nil, ref: nil)
  script_command :depend, script, group, ref
end

#dev?(script: nil, pat: nil, global: nil) ⇒ Boolean

Returns:

  • (Boolean)


243
244
245
# File 'lib/squared/workspace/application.rb', line 243

def dev?(script: nil, pat: nil, global: nil)
  with?(:dev, script: script, pat: pat, global: global || (pat.nil? && global.nil?))
end

#doc(script, group: nil, ref: nil) ⇒ Object



122
123
124
# File 'lib/squared/workspace/application.rb', line 122

def doc(script, group: nil, ref: nil)
  script_command :doc, script, group, ref
end

#enabled?Boolean

Returns:

  • (Boolean)


225
226
227
# File 'lib/squared/workspace/application.rb', line 225

def enabled?
  !@extensions.empty? || @project.any? { |_, proj| proj.enabled? }
end

#env(key, equals: nil, ignore: nil, default: nil) ⇒ Object



198
199
200
201
202
203
# File 'lib/squared/workspace/application.rb', line 198

def env(key, equals: nil, ignore: nil, default: nil)
  ret = ENV.fetch("#{key}_#{main.gsub(/[^\w]/, '_').upcase}", ENV[key]).to_s
  return ret == equals.to_s unless equals.nil?

  ret.empty? || (ignore && as_a(ignore).any? { |val| ret == val.to_s }) ? default : ret
end

#find_base(obj) ⇒ Object



213
214
215
# File 'lib/squared/workspace/application.rb', line 213

def find_base(obj)
  Application.project_kind.find { |proj| obj.instance_of?(proj) }
end

#group(name, path, **kwargs) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/squared/workspace/application.rb', line 143

def group(name, path, **kwargs)
  root_path(path).children.map do |ent|
    next unless (dir = Pathname.new(ent)).directory?

    index = 0
    basename = dir.basename.to_s.to_sym
    override = kwargs.delete(basename)
    while @project[basename]
      index += 1
      basename = :"#{basename}-#{index}"
    end
    [basename, dir, override]
  end
  .each do |basename, dir, override|
    opts = kwargs.dup
    opts.merge!(override) if override
    add(basename, dir, group: name, **opts)
  end
  self
end

#home_path(*args) ⇒ Object



209
210
211
# File 'lib/squared/workspace/application.rb', line 209

def home_path(*args)
  home.join(*args)
end

#inspectObject



221
222
223
# File 'lib/squared/workspace/application.rb', line 221

def inspect
  "#<#{self.class}: #{main} => #{self}>"
end

#prod?(script: nil, pat: nil, global: false) ⇒ Boolean

Returns:

  • (Boolean)


247
248
249
250
251
# File 'lib/squared/workspace/application.rb', line 247

def prod?(script: nil, pat: nil, global: false)
  return false if global && (@script[:dev] == true || !@script[:prod])

  with?(:prod, script: script, pat: pat, global: global)
end

#root_path(*args) ⇒ Object



205
206
207
# File 'lib/squared/workspace/application.rb', line 205

def root_path(*args)
  root.join(*args)
end

#run(script, group: nil, ref: nil) ⇒ Object



110
111
112
# File 'lib/squared/workspace/application.rb', line 110

def run(script, group: nil, ref: nil)
  script_command :run, script, group, ref
end

#script(group: nil, ref: nil) ⇒ Object



190
191
192
193
194
195
196
# File 'lib/squared/workspace/application.rb', line 190

def script(group: nil, ref: nil)
  if group || ref
    (group && @script[:group][group.to_sym]) || (ref && @script[:ref][ref.to_sym])
  else
    @script[:build]
  end
end

#style(name, *args, target: nil, empty: false) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/squared/workspace/application.rb', line 174

def style(name, *args, target: nil, empty: false)
  data = nil
  if target
    as_a(target).each_with_index do |val, i|
      if i == 0
        break unless (data = __get__(:theme)[val.to_sym])
      else
        data = data[val.to_sym] ||= {}
      end
    end
    return unless data
  end
  apply_style(data || theme, name, *args, empty: empty)
  self
end

#task_base?(key, val) ⇒ Boolean

Returns:

  • (Boolean)


229
230
231
232
233
# File 'lib/squared/workspace/application.rb', line 229

def task_base?(key, val)
  return val if task_defined?(key)

  val if key == :refresh && series[:build].include?(val = "#{val.split(':').first}:build")
end

#task_defined?(key) ⇒ Boolean

Returns:

  • (Boolean)


239
240
241
# File 'lib/squared/workspace/application.rb', line 239

def task_defined?(key)
  ::Rake::Task.task_defined?(key)
end

#task_include?(obj, key) ⇒ Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/squared/workspace/application.rb', line 235

def task_include?(obj, key)
  TASK_EXTEND.fetch(key, []).any? { |item| obj.is_a?(item) }
end

#test(script, group: nil, ref: nil) ⇒ Object



126
127
128
# File 'lib/squared/workspace/application.rb', line 126

def test(script, group: nil, ref: nil)
  script_command :test, script, group, ref
end

#to_sObject



217
218
219
# File 'lib/squared/workspace/application.rb', line 217

def to_s
  root.to_s
end