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

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
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

#as_a, #get, #message, #set

Constructor Details

#initialize(name, project, workspace, **kwargs) ⇒ Base

Returns a new instance of Base.

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
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
# File 'lib/squared/repo/project/base.rb', line 39

def initialize(name, project, workspace, **kwargs)
  @name = name.to_s
  @path = workspace.root_path(project.to_s)
  @project = @path.basename.to_s
  @workspace = workspace
  @depend = kwargs[:depend]
  @doc = kwargs[:doc]
  @group = kwargs[:group]&.to_s
  run = kwargs[:run]
  opts = env('BUILD', strict: true)
  raise ArgumentError, message("BUILD_#{name.to_s.upcase}", opts) if opts && run.is_a?(::Array)

  @output = [run, opts]
  @copy = kwargs[:copy]
  @clean = kwargs[:clean]
  log = kwargs[:log]
  log = { file: log } unless log.is_a?(::Hash)
  if (logfile = env('LOG_FILE')).nil? && (auto = env('LOG_AUTO'))
    require 'date'
    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"
              else
                log[:file]
              end
  else
    logfile = log[:file]
  end
  if (dir = env('LOG_DIR'))
    logfile &&= Pathname.new(dir).join(logfile)
  end
  @logger = Logger.new(logfile, level: env('LOG_LEVEL') || log[:level] || Logger::INFO)
  @logger.progname = name
  @session = nil
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.



33
34
35
# File 'lib/squared/repo/project/base.rb', line 33

def group
  @group
end

#nameObject (readonly)

Returns the value of attribute name.



33
34
35
# File 'lib/squared/repo/project/base.rb', line 33

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



33
34
35
# File 'lib/squared/repo/project/base.rb', line 33

def path
  @path
end

#projectObject (readonly)

Returns the value of attribute project.



33
34
35
# File 'lib/squared/repo/project/base.rb', line 33

def project
  @project
end

#workspaceObject (readonly)

Returns the value of attribute workspace.



33
34
35
# File 'lib/squared/repo/project/base.rb', line 33

def workspace
  @workspace
end

Class Method Details

.tasksObject



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

def tasks
  [].freeze
end

.to_sObject



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

def to_s
  to_sym.to_s.capitalize
end

.to_symObject



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

def to_sym
  :base
end

Instance Method Details

#base_path(*args) ⇒ Object



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

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

#build(*args) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/squared/repo/project/base.rb', line 96

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)


191
192
193
194
195
# File 'lib/squared/repo/project/base.rb', line 191

def build?
  return false if @output[0] == false

  !doc? || !@output[0].nil?
end

#cleanObject



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

def clean
  return run_s @clean if @clean.is_a?(::String)

  @clean&.each do |path|
    path = path.to_s
    if %r{[\\/]$}.match?(path)
      dir = Pathname.new(path)
      dir = base_path(dir) unless dir.absolute?
      next unless dir.directory?

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

#clean?Boolean

Returns:

  • (Boolean)


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

def clean?
  !@clean.nil?
end

#copyObject



138
139
140
# File 'lib/squared/repo/project/base.rb', line 138

def copy(*)
  run_s @copy
end

#copy?Boolean

Returns:

  • (Boolean)


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

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

#dependObject



130
131
132
# File 'lib/squared/repo/project/base.rb', line 130

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

#depend?Boolean

Returns:

  • (Boolean)


201
202
203
# File 'lib/squared/repo/project/base.rb', line 201

def depend?
  !@depend.nil?
end

#docObject



134
135
136
# File 'lib/squared/repo/project/base.rb', line 134

def doc
  build @doc if @doc
end

#doc?Boolean

Returns:

  • (Boolean)


205
206
207
# File 'lib/squared/repo/project/base.rb', line 205

def doc?
  !@doc.nil?
end

#enabled?Boolean

Returns:

  • (Boolean)


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

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

#has?(method) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#populateObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/squared/repo/project/base.rb', line 79

def populate(*)
  namespace @name do
    @workspace.series.each_key do |key|
      case key
      when :build, :refresh, :depend, :outdated, :doc, :copy, :clean
        next unless has?(key)
      else
        next unless @workspace.task_defined?(self, key)
      end
      desc message(@name, key)
      task key do
        send(key)
      end
    end
  end
end

#refreshObject



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

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

#refresh?Boolean

Returns:

  • (Boolean)


197
198
199
# File 'lib/squared/repo/project/base.rb', line 197

def refresh?
  build?
end

#stylesObject



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

def styles
  @workspace.styles
end

#to_sObject



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

def to_s
  @path.to_s
end

#to_symObject



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

def to_sym
  @name.to_sym
end

#verbose?Boolean

Returns:

  • (Boolean)


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

def verbose?
  @workspace.verbose && !pipe?
end