Class: Squared::Workspace::Project::Ruby

Inherits:
Git
  • Object
show all
Defined in:
lib/squared/workspace/project/ruby.rb

Constant Summary

Constants inherited from Base

Base::SEM_VER

Constants included from Common

Common::KEY

Instance Attribute Summary collapse

Attributes inherited from Base

#exception, #group, #name, #parent, #path, #pipe, #project, #theme, #verbose, #workspace

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Git

#checkout, #commit, #diff, #fetch, #files, #pull, #rebase, #refs, #reset, #restore, #rev, #stash, #status

Methods inherited from Base

#add, as_path, #base_path, #build, #build?, #clean, #clean?, #color, #depend?, #dev?, #doc, #doc?, #enabled?, #has?, #initialize_build, #initialize_env, #initialize_logger, #initialize_ref, #inspect, #log, #prod?, ref, #ref?, #refresh, #refresh?, #script?, #test, #test?, to_s, #to_s, #to_sym

Constructor Details

#initialize(version: nil, autodetect: false, **kwargs) ⇒ Ruby

Returns a new instance of Ruby.



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
# File 'lib/squared/workspace/project/ruby.rb', line 43

def initialize(*, version: nil, autodetect: false, **kwargs)
  super
  if @pass.include?(Ruby.ref)
    initialize_ref(Ruby.ref)
    initialize_logger(**kwargs)
  else
    initialize_build(Ruby.ref, **kwargs)
    initialize_env(**kwargs)
  end
  @version = env('BUILD', version, suffix: 'VERSION')
  @autodetect = autodetect
  index = GEMFILE.index { |file| base_path(file).exist? } || 0
  @gemfile = base_path(GEMFILE[index])
  return if !@output[0].nil? || !@copy.nil? || @version || @autodetect || (file = rakefile).nil?

  begin
    File.foreach(file) do |line|
      next unless line =~ %r{\brequire\s+(["'])bundler/gem_tasks\1}

      @output[0] = 'bundle exec rake build'
      @copy = 'bundle exec rake install'
      @clean = 'bundle exec rake clean' if @clean.nil?
      break
    end
  rescue StandardError => e
    log.error e
  end
end

Instance Attribute Details

#gemfileObject (readonly)

Returns the value of attribute gemfile.



41
42
43
# File 'lib/squared/workspace/project/ruby.rb', line 41

def gemfile
  @gemfile
end

Class Method Details

.is_a?(val) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
# File 'lib/squared/workspace/project/ruby.rb', line 22

def is_a?(val)
  if (val = as_path(val))
    RUBY_DIR.any? { |file| val.join(file).exist? }
  else
    super
  end
end

.populateObject



16
# File 'lib/squared/workspace/project/ruby.rb', line 16

def populate(*); end

.tasksObject



18
19
20
# File 'lib/squared/workspace/project/ruby.rb', line 18

def tasks
  nil
end

Instance Method Details

#bundle(flag, cmd) ⇒ Object



337
338
339
340
# File 'lib/squared/workspace/project/ruby.rb', line 337

def bundle(flag, cmd)
  guard_params('bundle', flag, args: cmd)
  run_s "bundle #{flag} #{cmd.join(' ')}"
end

#copy(from: 'lib', glob: '**/*', gemdir: @gemdir, override: false) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/squared/workspace/project/ruby.rb', line 195

def copy(from: 'lib', glob: '**/*', gemdir: @gemdir, override: false)
  if @copy && !override
    return super if runnable?(@copy)

    from = @copy[:from] if @copy.key?(:from)
    glob = @copy[:glob] if @copy.key?(:glob)
    gemdir = @copy[:gemdir] if @copy.key?(:gemdir)
  end
  return unless gemdir

  dest = Pathname.new(gemdir).realpath
  print_item unless @output[0]
  glob = as_a(glob)
  as_a(from).each_with_index do |val, i|
    a = base_path(val)
    b = dest.join(val)
    c = glob[i] || '**/*'
    log.warn "cp #{a.join(c)} #{b}"
    copy_d(a, b, glob: c, verbose: verbose)
  end
end

#copy?Boolean

Returns:

  • (Boolean)


365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/squared/workspace/project/ruby.rb', line 365

def copy?
  return true if super || (@copy.is_a?(::Hash) && copy.fetch(:gemdir, nil))
  return gemdir? if @gemdir

  if @version && (val = ENV['GEM_HOME'])
    @gemdir = Pathname.new(val).join(gempath)
    return true if gemdir?
  end
  return false unless @autodetect

  unsafe = ->(hint) { raise_error('failed to parse', hint: hint) }
  out = `gem -C #{shell_quote(path)} list --local -d #{project}`
  data = /#{Regexp.escape(project)} \(([^)]+)\)/.match(out)
  unsafe.('version') unless data
  ver = data[1].split(/\s*,\s*/)
  ver.unshift(@version).uniq! if @version
  ver.each do |v|
    next unless (data = /\(#{Regexp.escape(v)}(?:,[^)]+|\b)\):([^\n]+)/.match(out))

    log.warn "using version #{v} (given #{@version})" if @version && @version != v
    @version = v
    break
  end
  unsafe.('path') unless data
  @gemdir = Pathname.new(data[1].strip).join(gempath)
rescue StandardError => e
  log.error e
  @version = nil
  @gemdir = nil
  @autodetect = false
else
  gemdir?
end

#depend(flag = nil, opts: []) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/squared/workspace/project/ruby.rb', line 157

def depend(flag = nil, opts: [])
  if @depend && !flag
    super
  elsif outdated?
    case flag
    when :gem
      gem_session 'install'
      append_value opts
    when :with, :without
      gem_session 'install'
      append_repeat flag, opts
    when :redownload, :local, :'prefer-local'
      cmd = bundle_session 'install', "--#{flag}"
      if (val = env('BUNDLE_TRUST_POLICY'))
        cmd << "--trust-policy=#{case val
                                 when '0'
                                   'NoSecurity'
                                 when '1'
                                   'AlmostNoSecurity'
                                 when '2'
                                   'LowSecurity'
                                 when '3'
                                   'MediumSecurity'
                                 when '4'
                                   'HighSecurity'
                                 else
                                   val
                                 end}"
      end
      append_bundle opts, OPT_INSTALL
    else
      bundle_session 'install'
      append_bundle
    end
    run_rb(sync: invoked_sync?('depend', flag))
  end
end

#outdated(rev = nil, opts: []) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/squared/workspace/project/ruby.rb', line 217

def outdated(rev = nil, opts: [])
  cmd = bundle_session 'outdated', rev && "--#{rev}"
  append_bundle opts, OPT_OUTDATED
  cmd = close_session(cmd)
  log.info cmd
  banner = format_banner(cmd)
  if invoked_sync?('outdated', rev)
    print_item banner
    banner = nil
  end
  start = 0
  found = 0
  major = 0
  pwd_set do
    IO.popen("#{cmd} --no-color").each do |line|
      if start > 0
        unless stdin?
          data = line.scan(SEM_VER)
          cur = data.shift
          lat = data.shift
          if cur && lat
            semver(cur)
            semver(lat)
            c = cur.join
            l = lat.join
            styles = []
            data.each do |val|
              break unless (req = /(>=?|=|~>|!=|<=?) (#{Regexp.escape(val.join)})/.match(line))

              v = semver(val).join
              case req[1]
              when '>', '>='
                if semmajor(cur, lat)
                  styles = %i[green bold]
                  major += 1
                else
                  styles[0] = cur[2] == lat[2] ? :yellow : :green
                end
              when '<', '<='
                if c <= v
                  if semmajor(cur, lat)
                    styles = %i[green bold]
                    major += 1
                  else
                    styles[0] = :yellow
                  end
                end
              when '!='
                if c == l
                  styles.clear
                else
                  styles[1] = :bold
                end
              when '~>'
                if c < v && cur[0] == val[0] && !semmajor(cur, val)
                  styles[0] = :yellow
                elsif semmajor(val, lat)
                  styles[1] = :underline
                else
                  styles[1] = :bold
                end
              end
            end
            unless styles.empty?
              case styles[0]
              when :green
                line = sub_style(line, pat: /^(\S+)(.+)$/, styles: theme[styles[1] == :bold ? :major : :active])
                found += 1
              when :yellow
                found += 1
              end
              styles = styles.compact
                             .map { |s| s == :green || s == :yellow ? color(s) : s }
                             .flatten
              line = sub_style(line, pat: /^((?:\S+\s+){2})(#{Regexp.escape(l)})(.+)$/, styles: styles,
                                     index: 2)
            end
          end
        end
        puts "#{start.to_s.rjust(2)}. #{line}"
        start += 1
      elsif line =~ /^Gem /
        print_item banner if banner
        unless stdin?
          puts print_footer(" #  #{line.chomp}", reverse: true, sub: [
            { pat: /^(.+)(Gem)(.+)$/, styles: theme[:header], index: 2 },
            { pat: /^(.+)(Latest)(.+)$/, styles: theme[:header], index: 2 }
          ])
        end
        start += 1
      end
    end
    if found > 0
      puts print_footer(empty_status('Updates are available', 'major', major, always: true))
    elsif start == 0
      puts 'No updates were found'
    end
  end
end

#outdated?Boolean

Returns:

  • (Boolean)


399
400
401
# File 'lib/squared/workspace/project/ruby.rb', line 399

def outdated?
  gemfile.exist?
end

#populateObject



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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/squared/workspace/project/ruby.rb', line 76

def populate(*)
  super
  return unless outdated? && ref?(Ruby.ref)

  namespace name do
    @@tasks[Ruby.ref].each do |action, flags|
      if flags.nil?
        case action
        when :exec, :config
          desc format_desc(action, nil, 'command+')
          task action, [:command] do |_, args|
            bundle(action, args.to_a)
          end
        when :rake
          next unless rakefile

          desc format_desc(action, nil, 'command*')
          task action, [:command] do |_, args|
            rake(*args.to_a)
          end
        end
      else
        namespace action do
          flags.each do |flag|
            case action
            when :install
              case flag
              when :gem
                desc format_desc(action, flag, 'name+')
                task flag, [:name] do |_, args|
                  name = args.to_a
                  guard_params(action, flag, args: name)
                  depend(flag, opts: name)
                end
              when :with, :without
                desc format_desc(action, flag, 'group+')
                task flag, [:group] do |_, args|
                  group = args.to_a
                  guard_params(action, flag, args: group)
                  depend(flag, opts: group)
                end
              else
                desc format_desc(action, flag, OPT_INSTALL)
                task flag, [:opts] do |_, args|
                  depend(flag, opts: args.to_a)
                end
              end
            when :update, :outdated
              desc format_desc(action, flag, action == :update ? OPT_UPDATE : OPT_OUTDATED)
              task flag, [:opts] do |_, args|
                __send__(action, flag, opts: args.to_a)
              end
            when :pristine
              case flag
              when :gem
                desc format_desc(action, flag, 'name+')
                task flag, [:name] do |_, args|
                  name = args.to_a
                  guard_params(action, flag, args: name)
                  pristine(flag, opts: name)
                end
              when :all
                desc format_desc(action, flag, 'skip*')
                task flag, [:skip] do |_, args|
                  pristine(flag, opts: args.to_a)
                end
              when :version
                desc format_desc(action, flag, 'version?')
                task flag, [:version] do |_, args|
                  guard_params(action, flag, args: args, key: :version)
                  pristine(flag, version: args.version)
                end
              end
            end
          end
        end
      end
    end
  end
end

#pristine(flag, version: nil, opts: []) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/squared/workspace/project/ruby.rb', line 323

def pristine(flag, version: nil, opts: [])
  cmd = gem_session 'pristine'
  case flag
  when :gem
    append_value opts
  when :all
    cmd << '--all'
    append_repeat 'skip', opts
  when :version
    cmd << project << "--version=#{shell_escape(version)}"
  end
  run_rb
end

#rake(*cmd) ⇒ Object



342
343
344
# File 'lib/squared/workspace/project/ruby.rb', line 342

def rake(*cmd)
  run_s(cmd.empty? ? 'rake' : cmd.map { |val| "rake #{val}" })
end

#rakefileObject



346
347
348
349
# File 'lib/squared/workspace/project/ruby.rb', line 346

def rakefile
  file = ::Rake::Application::DEFAULT_RAKEFILES.find { |val| base_path(val).exist? }
  base_path(file) if file
end

#refObject



72
73
74
# File 'lib/squared/workspace/project/ruby.rb', line 72

def ref
  Ruby.ref
end

#update(flag, opts: []) ⇒ Object



317
318
319
320
321
# File 'lib/squared/workspace/project/ruby.rb', line 317

def update(flag, opts: [])
  bundle_session 'update', "--#{flag}"
  append_bundle opts, OPT_UPDATE
  run_rb
end

#variable_allObject



360
361
362
363
# File 'lib/squared/workspace/project/ruby.rb', line 360

def variable_all
  ret = super + VAR_SET
  ret.freeze
end

#variable_set(key, *val) ⇒ Object



351
352
353
354
355
356
357
358
# File 'lib/squared/workspace/project/ruby.rb', line 351

def variable_set(key, *val, **)
  case key
  when :gemfile
    @gemfile = base_path(val.first)
  else
    super
  end
end