Class: Squared::Workspace::Project::Base

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL, Shell, Common, System, Task
Defined in:
lib/squared/workspace/project/base.rb

Direct Known Subclasses

Git

Constant Summary collapse

SEM_VER =
/(\d+)(?:(\.)(\d+))?(?:(\.)(\d+))?/.freeze
0
@@tasks =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, path, workspace, group: nil, **kwargs) ⇒ Base

Returns a new instance of Base.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/squared/workspace/project/base.rb', line 49

def initialize(name, path, workspace, *, group: nil, **kwargs)
  @name = name.to_s
  @path = path
  @project = @path.basename.to_s
  @workspace = workspace
  @group = group&.to_s
  @depend = kwargs[:depend]
  @doc = kwargs[:doc]
  @test = kwargs[:test]
  @output = [kwargs[:run], nil]
  @copy = kwargs[:copy]
  @clean = kwargs[:clean]
  @exclude = as_a(kwargs[:exclude])
  @warning = workspace.warning
  @theme = if !workspace.verbose
             {}
           elsif kwargs.fetch(:common, true)
             workspace.theme
           else
             __get__(:theme)[:project][to_sym] ||= {}
           end
  initialize_logger(**kwargs)
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.



46
47
48
# File 'lib/squared/workspace/project/base.rb', line 46

def group
  @group
end

#nameObject (readonly)

Returns the value of attribute name.



46
47
48
# File 'lib/squared/workspace/project/base.rb', line 46

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



46
47
48
# File 'lib/squared/workspace/project/base.rb', line 46

def path
  @path
end

#projectObject (readonly)

Returns the value of attribute project.



46
47
48
# File 'lib/squared/workspace/project/base.rb', line 46

def project
  @project
end

#themeObject (readonly)

Returns the value of attribute theme.



46
47
48
# File 'lib/squared/workspace/project/base.rb', line 46

def theme
  @theme
end

#warningObject

Returns the value of attribute warning.



47
48
49
# File 'lib/squared/workspace/project/base.rb', line 47

def warning
  @warning
end

#workspaceObject (readonly)

Returns the value of attribute workspace.



46
47
48
# File 'lib/squared/workspace/project/base.rb', line 46

def workspace
  @workspace
end

Class Method Details

.as_path(val) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/squared/workspace/project/base.rb', line 25

def as_path(val)
  case val
  when ::Pathname
    val
  when ::String
    Pathname.new(val)
  end
end

.populateObject



19
# File 'lib/squared/workspace/project/base.rb', line 19

def populate(*); end

.refObject



34
35
36
# File 'lib/squared/workspace/project/base.rb', line 34

def ref
  @ref ||= to_s.downcase.to_sym
end

.tasksObject



21
22
23
# File 'lib/squared/workspace/project/base.rb', line 21

def tasks
  [].freeze
end

.to_sObject



38
39
40
# File 'lib/squared/workspace/project/base.rb', line 38

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

Instance Method Details

#base_path(*args) ⇒ Object



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

def base_path(*args)
  path.join(*args)
end

#build(*args, sync: true) ⇒ Object



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

def build(*args, sync: true)
  if args.empty?
    cmd, opts = @output
    opts &&= shell_escape(opts)
  else
    cmd = args.shift
    opts = sanitize_args(*args)
  end
  if cmd
    if opts
      cmd = "#{cmd} #{opts}"
    elsif cmd.is_a?(::Array)
      cmd = cmd.join(' && ')
    end
    banner = verbose?
  else
    return unless respond_to?(:compose)

    cmd = compose(opts)
    banner = env('REPO_BUILD') == 'verbose'
  end
  run(cmd, banner: banner, sync: sync)
end

#build?Boolean

Returns:

  • (Boolean)


254
255
256
# File 'lib/squared/workspace/project/base.rb', line 254

def build?
  !!@output[0]
end

#cleanObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/squared/workspace/project/base.rb', line 195

def clean
  return unless @clean

  if @clean.is_a?(::String)
    run_s(@clean, sync: invoked_sync?('clean'))
  else
    @clean.each do |val|
      val = val.to_s
      if val =~ %r{[\\/]$}
        dir = Pathname.new(val)
        dir = base_path(dir) unless dir.absolute?
        next unless dir.directory?

        log.warn "rm -rf #{dir}"
        dir.rmtree(verbose: true)
      else
        files = val.include?('*') ? Dir[base_path(val)] : [base_path(val)]
        files.each do |file|
          begin
            File.delete(file) if File.file?(file)
          rescue StandardError => e
            log.error e
          end
        end
      end
    end
  end
end

#clean?Boolean

Returns:

  • (Boolean)


278
279
280
# File 'lib/squared/workspace/project/base.rb', line 278

def clean?
  !!@clean
end

#copyObject



183
184
185
# File 'lib/squared/workspace/project/base.rb', line 183

def copy(*)
  run_s(@copy, sync: invoked_sync?('copy')) if @copy
end

#copy?Boolean

Returns:

  • (Boolean)


274
275
276
# File 'lib/squared/workspace/project/base.rb', line 274

def copy?
  @copy.is_a?(::String)
end

#dependObject



179
180
181
# File 'lib/squared/workspace/project/base.rb', line 179

def depend(*)
  run(@depend, sync: invoked_sync?('depend')) if @depend
end

#depend?Boolean

Returns:

  • (Boolean)


262
263
264
# File 'lib/squared/workspace/project/base.rb', line 262

def depend?
  @depend != false && (!@depend.nil? || has?('outdated'))
end

#dev?Boolean

Returns:

  • (Boolean)


282
283
284
# File 'lib/squared/workspace/project/base.rb', line 282

def dev?
  !!@dev
end

#docObject



187
188
189
# File 'lib/squared/workspace/project/base.rb', line 187

def doc
  build(@doc, sync: invoked_sync?('doc')) if @doc
end

#doc?Boolean

Returns:

  • (Boolean)


266
267
268
# File 'lib/squared/workspace/project/base.rb', line 266

def doc?
  !!@doc
end

#enabled?Boolean

Returns:

  • (Boolean)


246
247
248
# File 'lib/squared/workspace/project/base.rb', line 246

def enabled?
  path.directory? && !path.empty?
end

#has?(method) ⇒ Boolean

Returns:

  • (Boolean)


250
251
252
# File 'lib/squared/workspace/project/base.rb', line 250

def has?(method)
  respond_to?(m = :"#{method}?") && __send__(m)
end

#initialize_build(ref, **kwargs) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/squared/workspace/project/base.rb', line 103

def initialize_build(ref, **kwargs)
  initialize_script(ref, **kwargs)
  if (val = env('BUILD', strict: true))
    @output[0] = val
  elsif @script && @output[0] != false
    @output[0] ||= @script[:run]
  end
  @dev = kwargs.delete(:dev)
  if env('BUILD', suffix: 'DEV', equals: '0')
    @dev = false
  elsif env('BUILD', suffix: 'DEV')
    @dev = true
  elsif @dev.nil?
    @dev = !group.nil? && workspace.dev?(group: group, global: true)
  end
end

#initialize_logger(log: nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/squared/workspace/project/base.rb', line 73

def initialize_logger(log: nil, **)
  log = log.is_a?(::Hash) ? log.dup : { file: log }
  if (file = env('LOG_FILE')).nil? && (auto = env('LOG_AUTO'))
    file = case auto
           when 'y', 'year'
             "#{@name}-#{Date.today.year}.log"
           when 'm', 'month'
             "#{@name}-#{Date.today.strftime('%Y-%m')}.log"
           when 'd', 'day', '1'
             "#{@name}-#{Date.today}.log"
           end
  end
  if file ||= log[:file]
    file = Date.today.strftime(file)
    file = (dir = env('LOG_DIR')) ? Pathname.new(dir).join(file) : Pathname.new(file)
    begin
      file = file.realdirpath
    rescue StandardError => e
      raise if @workspace.exception

      file = nil
      warn e if @warning
    end
  end
  log[:progname] = @name
  log[:level] = env('LOG_LEVEL', log[:level] || Logger::INFO, ignore: nil)
  log.delete(:file)
  @log = [file, log]
end

#initialize_script(ref) ⇒ Object



120
121
122
123
124
125
126
127
128
# File 'lib/squared/workspace/project/base.rb', line 120

def initialize_script(ref, **)
  return unless (script = workspace.script(group: group, ref: ref))

  @depend = script[:depend] if @depend.nil?
  @doc = script[:doc] if @doc.nil?
  @test = script[:test] if @test.nil?
  @clean = script[:clean] if @clean.nil?
  @script = script
end

#inspectObject



234
235
236
# File 'lib/squared/workspace/project/base.rb', line 234

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

#logObject



224
225
226
227
228
# File 'lib/squared/workspace/project/base.rb', line 224

def log
  return @log unless @log.is_a?(::Array)

  @log = Logger.new(enabled? ? @log[0] : nil, **@log[1])
end

#populateObject



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

def populate(*)
  return if @exclude.include?(Base.ref)

  namespace name do
    workspace.series.each_key do |key|
      next unless Application::WORKSPACE_KEYS.include?(key) ? has?(key) : workspace.task_include?(self, key)

      desc message(name, key)
      task key do
        __send__(key)
      end
    end
  end
end

#refreshObject



169
170
171
172
173
174
175
176
177
# File 'lib/squared/workspace/project/base.rb', line 169

def refresh(*)
  build(sync: invoked_sync?('depend'))
  key = "#{name}:copy"
  if workspace.task_defined?(key)
    invoke key
  else
    copy
  end
end

#refresh?Boolean

Returns:

  • (Boolean)


258
259
260
# File 'lib/squared/workspace/project/base.rb', line 258

def refresh?
  build? && (copy? || workspace.task_defined?("#{name}:copy"))
end

#testObject



191
192
193
# File 'lib/squared/workspace/project/base.rb', line 191

def test
  build(@test, sync: invoked_sync?('test')) if @test
end

#test?Boolean

Returns:

  • (Boolean)


270
271
272
# File 'lib/squared/workspace/project/base.rb', line 270

def test?
  !!@test
end

#to_sObject



238
239
240
# File 'lib/squared/workspace/project/base.rb', line 238

def to_s
  path.to_s
end

#to_symObject



242
243
244
# File 'lib/squared/workspace/project/base.rb', line 242

def to_sym
  name.to_sym
end