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, exception: false, pipe: false, verbose: nil, **kwargs) ⇒ 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
77
# File 'lib/squared/workspace/application.rb', line 57

def initialize(main, *, exception: false, pipe: false, verbose: nil, **kwargs)
  @main = main.to_s
  @home = Pathname.pwd
  @root = @home.parent
  @series = Series.new(TASK_BASE)
  @project = {}
  @extensions = []
  @script = {
    group: {},
    ref: {},
    build: nil,
    dev: nil,
    prod: nil
  }
  @theme = kwargs.key?(:common) && !kwargs[:common] ? {} : __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 = @verbose
  __set__(:no_color, true) if @pipe
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

Returns the value of attribute warning.



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

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.ref == 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, **kwargs) ⇒ Object



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

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

#apply(&blk) ⇒ Object



172
173
174
175
# File 'lib/squared/workspace/application.rb', line 172

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

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

Yields:

  • (_self)

Yield Parameters:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/squared/workspace/application.rb', line 79

def build(**kwargs)
  return unless enabled?

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

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

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

  __build__(**kwargs)
  series.build(**kwargs)

  yield self if block_given?
end

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



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

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

#compose(name, &blk) ⇒ Object



167
168
169
170
# File 'lib/squared/workspace/application.rb', line 167

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

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



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

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

#dev?(global: nil, **kwargs) ⇒ Boolean

Returns:

  • (Boolean)


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

def dev?(global: nil, **kwargs)
  script?(:dev, global: global.nil? ? kwargs[:pat].nil? : global, **kwargs)
end

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



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

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

#enabled?Boolean

Returns:

  • (Boolean)


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

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

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



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

def env(key, default = nil, equals: nil, ignore: 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



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

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

#group(name, path, override: {}, **kwargs) ⇒ Object



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

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

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

#home_path(*args) ⇒ Object



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

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

#inspectObject



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

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

#prod?(global: false, **kwargs) ⇒ Boolean

Returns:

  • (Boolean)


252
253
254
255
256
# File 'lib/squared/workspace/application.rb', line 252

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

  script?(:prod, global: global, **kwargs)
end

#root_path(*args) ⇒ Object



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

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

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



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

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

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



193
194
195
196
197
198
199
200
201
# File 'lib/squared/workspace/application.rb', line 193

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

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



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

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)


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

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)


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

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

#task_include?(obj, key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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



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

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

#to_sObject



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

def to_s
  root.to_s
end

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



98
99
100
101
102
103
104
105
106
107
# File 'lib/squared/workspace/application.rb', line 98

def with(group: nil, ref: nil)
  @group = nil
  @ref = nil
  if group
    @group = group
  elsif ref
    @ref = ref
  end
  self
end