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

Instance Attribute Summary collapse

Attributes inherited from Base

#group, #name, #path, #project, #theme, #warning, #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

as_path, #base_path, #build, #build?, #clean, #clean?, #depend?, #dev?, #doc, #doc?, #enabled?, #has?, #initialize_build, #initialize_logger, #initialize_script, #inspect, #log, ref, #refresh, #refresh?, #test, #test?, to_s, #to_s, #to_sym

Constructor Details

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

Returns a new instance of Ruby.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/squared/workspace/project/ruby.rb', line 41

def initialize(*, version: nil, autodetect: false, **kwargs)
  super
  initialize_build(Ruby.ref, **kwargs)
  @version = env('BUILD', version, suffix: 'VERSION', strict: true)
  @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.



39
40
41
# File 'lib/squared/workspace/project/ruby.rb', line 39

def gemfile
  @gemfile
end

Class Method Details

.is_a?(val) ⇒ Boolean

Returns:

  • (Boolean)


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

def is_a?(val)
  if (val = as_path(val))
    [*GEMFILE, *::Rake::Application::DEFAULT_RAKEFILES, 'README.rdoc'].any? { |file| val.join(file).exist? }
  else
    super
  end
end

.populateObject



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

def populate(*); end

.tasksObject



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

def tasks
  nil
end

Instance Method Details

#bundle(flag, cmd) ⇒ Object



332
333
334
335
# File 'lib/squared/workspace/project/ruby.rb', line 332

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



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/squared/workspace/project/ruby.rb', line 183

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

    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)


350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/squared/workspace/project/ruby.rb', line 350

def copy?
  return true if @copy.is_a?(::String) || @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) }
  begin
    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*/)
    if @version
      ver.unshift(@version)
      ver.uniq!
    end
    ver.each do |v|
      data = /\(#{Regexp.escape(v)}(?:,[^)]+|\b)\):([^\n]+)/.match(out)
      next unless data

      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
end

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



145
146
147
148
149
150
151
152
153
154
155
156
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
# File 'lib/squared/workspace/project/ruby.rb', line 145

def depend(flag = nil, opts: [], override: false)
  if @depend && !override
    super
  elsif outdated?
    case flag
    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
    when :gem
      gem_session 'install'
      append_value opts
    when :with, :without
      gem_session 'install'
      append_repeat flag, opts
    else
      bundle_session 'install'
      append_bundle
    end
    run_rb
  end
end

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



205
206
207
208
209
210
211
212
213
214
215
216
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
# File 'lib/squared/workspace/project/ruby.rb', line 205

def outdated(rev = nil, opts: [])
  cmd = bundle_session 'outdated', rev && "--#{rev}"
  append_bundle opts, OPT_OUTDATED
  cmd = cmd.done
  log.info cmd
  print_item format_banner(cmd) if store_pwd || invoked_sync?("outdated#{rev ? ":#{rev}" : ''}")
  start = 0
  found = 0
  major = 0
  IO.popen("#{cmd} --no-color").each do |line|
    if start > 0
      unless pipe?
        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 styles && (req = /(>?=|~>|!=|<=?) (#{Regexp.escape(val.join)})/.match(line))

            v = semver(val).join
            case req[1]
            when '>', '>='
              if c == l
                styles = nil
              elsif l < v
                styles = %i[red underline]
                break
              elsif semmajor(cur, lat)
                styles = %i[green underline]
                major += 1
              elsif cur[3] != lat[3]
                styles = %i[green bold]
              else
                styles[0] = :green
              end
            when '<', '<='
              if c <= v
                if semmajor(cur, lat)
                  styles = %i[green underline]
                  major += 1
                else
                  styles[0] = :yellow
                end
              elsif l >= v
                styles = nil
              end
            when '!='
              if c == v
                styles = %i[red underline]
                break
              elsif l == v
                styles = nil
              else
                styles[1] = :bold
              end
            when '~>'
              if c < v && cur[0] == val[0] && !semmajor(cur, val)
                styles[0] = :yellow
              elsif semmajor(lat, val)
                styles = nil
              else
                styles[1] = :bold
              end
            when '='
              if c == v
                styles[1] = :underline
              else
                styles[0] = :red
              end
            end
          end
          next unless styles && !styles.empty?

          case styles[0]
          when :yellow, :green
            line = sub_style(line, pat: /^(\S+)(.+)$/, styles: theme[:active])
            found += 1
          end
          line = sub_style(line, pat: /^((?:\S+\s+){2})(#{Regexp.escape(l)})(.+)$/,
                                 styles: styles.compact, index: 2)
        end
      end
      puts "#{start.to_s.rjust(2)}. #{line}"
      start += 1
    elsif line =~ /^Gem /
      unless pipe?
        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
  store_pwd true
end

#outdated?Boolean

Returns:

  • (Boolean)


390
391
392
# File 'lib/squared/workspace/project/ruby.rb', line 390

def outdated?
  gemfile.exist?
end

#populateObject



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

def populate(*)
  super
  return unless outdated? && !@exclude.include?(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, override: true)
                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, override: true)
                end
              else
                desc format_desc(action, flag, OPT_INSTALL)
                task flag, [:opts] do |_, args|
                  depend(flag, opts: args.to_a, override: true)
                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



318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/squared/workspace/project/ruby.rb', line 318

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



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

def rake(cmd)
  if cmd.empty?
    run_s 'rake'
  else
    cmd.each { |val| run_s "rake #{val}" }
  end
end

#rakefileObject



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

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

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



312
313
314
315
316
# File 'lib/squared/workspace/project/ruby.rb', line 312

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