Class: Squared::Repo::Project::Base

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

Direct Known Subclasses

Git

Constant Summary collapse

0
@@tasks =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common::Task

collect_args, invoke, invoked?

Methods included from Common

#__get__, #__set__, #as_a, #finalize!, #message

Constructor Details

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

Returns a new instance of Base.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/squared/repo/project/base.rb', line 46

def initialize(name, path, workspace, *, group: nil, log: nil, common: true, **kwargs)
  @name = name.to_s
  @path = workspace.root_path(path.to_s)
  @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]
  @warning = workspace.warning
  @theme = if !workspace.verbose
             {}
           elsif common
             workspace.theme
           else
             __get__(:theme)[:project][to_sym] ||= {}
           end
  log = { file: log } unless log.is_a?(::Hash)
  if (logfile = env('LOG_FILE')).nil? && (auto = env('LOG_AUTO'))
    logfile = 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 logfile ||= log[:file]
    logfile = Date.today.strftime(logfile)
    logfile = (dir = env('LOG_DIR')) ? Pathname.new(dir).join(logfile) : Pathname.new(logfile)
    begin
      logfile.realdirpath
    rescue StandardError => e
      logfile = nil
      warn e if @warning
    end
  end
  @log = Logger.new(logfile, progname: @name, level: env('LOG_LEVEL') || log[:level] || Logger::INFO)
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.



43
44
45
# File 'lib/squared/repo/project/base.rb', line 43

def group
  @group
end

#logObject (readonly)

Returns the value of attribute log.



43
44
45
# File 'lib/squared/repo/project/base.rb', line 43

def log
  @log
end

#nameObject (readonly)

Returns the value of attribute name.



43
44
45
# File 'lib/squared/repo/project/base.rb', line 43

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



43
44
45
# File 'lib/squared/repo/project/base.rb', line 43

def path
  @path
end

#projectObject (readonly)

Returns the value of attribute project.



43
44
45
# File 'lib/squared/repo/project/base.rb', line 43

def project
  @project
end

#themeObject (readonly)

Returns the value of attribute theme.



43
44
45
# File 'lib/squared/repo/project/base.rb', line 43

def theme
  @theme
end

#warningObject

Returns the value of attribute warning.



44
45
46
# File 'lib/squared/repo/project/base.rb', line 44

def warning
  @warning
end

#workspaceObject (readonly)

Returns the value of attribute workspace.



43
44
45
# File 'lib/squared/repo/project/base.rb', line 43

def workspace
  @workspace
end

Class Method Details

.as_path(val) ⇒ Object



25
26
27
28
29
# File 'lib/squared/repo/project/base.rb', line 25

def as_path(val)
  return val if val.is_a?(::Pathname)

  val.is_a?(::String) ? Pathname.new(val) : nil
end

.populateObject



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

def populate(*); end

.tasksObject



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

def tasks
  [].freeze
end

.to_sObject



31
32
33
# File 'lib/squared/repo/project/base.rb', line 31

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

.to_symObject



35
36
37
# File 'lib/squared/repo/project/base.rb', line 35

def to_sym
  to_s.downcase.to_sym
end

Instance Method Details

#base_path(*args) ⇒ Object



208
209
210
# File 'lib/squared/repo/project/base.rb', line 208

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

#build(*args) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/squared/repo/project/base.rb', line 130

def build(*args)
  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, exception: workspace.exception, banner: banner)
end

#build?Boolean

Returns:

  • (Boolean)


232
233
234
# File 'lib/squared/repo/project/base.rb', line 232

def build?
  !!@output[0]
end

#cleanObject



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/squared/repo/project/base.rb', line 180

def clean
  return unless @clean

  if @clean.is_a?(::String)
    run_s @clean
  else
    @clean.each do |val|
      if (val = val.to_s).match?(%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)


256
257
258
# File 'lib/squared/repo/project/base.rb', line 256

def clean?
  !!@clean
end

#copyObject



168
169
170
# File 'lib/squared/repo/project/base.rb', line 168

def copy(*)
  run_s @copy
end

#copy?Boolean

Returns:

  • (Boolean)


252
253
254
# File 'lib/squared/repo/project/base.rb', line 252

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

#dependObject



164
165
166
# File 'lib/squared/repo/project/base.rb', line 164

def depend(*)
  run(@depend, exception: workspace.exception) if @depend
end

#depend?Boolean

Returns:

  • (Boolean)


240
241
242
# File 'lib/squared/repo/project/base.rb', line 240

def depend?
  !!@depend
end

#docObject



172
173
174
# File 'lib/squared/repo/project/base.rb', line 172

def doc
  build @doc if @doc
end

#doc?Boolean

Returns:

  • (Boolean)


244
245
246
# File 'lib/squared/repo/project/base.rb', line 244

def doc?
  !!@doc
end

#enabled?Boolean

Returns:

  • (Boolean)


224
225
226
# File 'lib/squared/repo/project/base.rb', line 224

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

#has?(method) ⇒ Boolean

Returns:

  • (Boolean)


228
229
230
# File 'lib/squared/repo/project/base.rb', line 228

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

#initialize_build(ref, **kwargs) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/squared/repo/project/base.rb', line 90

def initialize_build(ref, **kwargs)
  initialize_script(ref)
  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 = workspace.dev?
  end
end

#initialize_script(ref) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/squared/repo/project/base.rb', line 107

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



212
213
214
# File 'lib/squared/repo/project/base.rb', line 212

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

#populateObject



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

def populate(*)
  namespace name do
    workspace.series.each_key do |key|
      next unless Workspace::TASK_KEYS.include?(key) ? has?(key) : workspace.task_defined?(self, key)

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

#refreshObject



154
155
156
157
158
159
160
161
162
# File 'lib/squared/repo/project/base.rb', line 154

def refresh(*)
  build
  key = "#{name}:copy"
  if ::Rake::Task.task_defined?(key)
    invoke key
  else
    copy
  end
end

#refresh?Boolean

Returns:

  • (Boolean)


236
237
238
# File 'lib/squared/repo/project/base.rb', line 236

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

#testObject



176
177
178
# File 'lib/squared/repo/project/base.rb', line 176

def test
  build @test if @test
end

#test?Boolean

Returns:

  • (Boolean)


248
249
250
# File 'lib/squared/repo/project/base.rb', line 248

def test?
  !!@test
end

#to_sObject



216
217
218
# File 'lib/squared/repo/project/base.rb', line 216

def to_s
  path.to_s
end

#to_symObject



220
221
222
# File 'lib/squared/repo/project/base.rb', line 220

def to_sym
  name.to_sym
end