Class: Squared::Workspace::Project::Ruby
- Defined in:
- lib/squared/workspace/project/ruby.rb
Constant Summary
Constants inherited from Base
Constants included from Common
Instance Attribute Summary collapse
-
#gemfile ⇒ Object
readonly
Returns the value of attribute gemfile.
Attributes inherited from Base
#exception, #group, #name, #parent, #path, #pipe, #project, #theme, #verbose, #workspace
Class Method Summary collapse
Instance Method Summary collapse
- #bundle(flag, cmd) ⇒ Object
- #copy(from: 'lib', glob: '**/*', into: @gemdir, override: false) ⇒ Object
- #copy? ⇒ Boolean
- #depend(flag = nil, opts: [], sync: invoked_sync?('depend', flag)) ⇒ Object
- #depend? ⇒ Boolean
-
#initialize(version: nil, autodetect: false, **kwargs) ⇒ Ruby
constructor
A new instance of Ruby.
- #outdated(rev = nil, opts: []) ⇒ Object
- #outdated? ⇒ Boolean
- #populate ⇒ Object
- #pristine(flag, version: nil, opts: []) ⇒ Object
- #rake(*cmd) ⇒ Object
- #rakefile ⇒ Object
- #ref ⇒ Object
- #update(flag, opts: []) ⇒ Object
- #variable_set(key, *val) ⇒ Object
- #variables ⇒ Object
Methods inherited from Git
#checkout, #commit, #diff, #fetch, #files, #pull, #rebase, #refs, #reset, #restore, #rev, #stash, #status
Methods inherited from Base
#add, #allref, as_path, #base_path, #build, #build?, #clean, #clean?, #color, #dev?, #doc, #doc?, #enabled?, #has?, #initialize_build, #initialize_env, #initialize_logger, #initialize_ref, #inspect, #log, #prod?, ref, #ref?, #script?, #test, #test?, to_s, #to_s, #to_sym, #version, #with
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
#gemfile ⇒ Object (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
.aliasargs ⇒ Object
18 |
# File 'lib/squared/workspace/project/ruby.rb', line 18 def aliasargs(*); end |
.batchargs ⇒ Object
17 |
# File 'lib/squared/workspace/project/ruby.rb', line 17 def batchargs(*); end |
.config?(val) ⇒ Boolean
24 25 26 27 28 |
# File 'lib/squared/workspace/project/ruby.rb', line 24 def config?(val) return false unless (val = as_path(val)) RUBY_DIR.any? { |file| val.join(file).exist? } end |
.populate ⇒ Object
16 |
# File 'lib/squared/workspace/project/ruby.rb', line 16 def populate(*); end |
.tasks ⇒ Object
20 21 22 |
# File 'lib/squared/workspace/project/ruby.rb', line 20 def tasks %i[outdated].freeze end |
Instance Method Details
#bundle(flag, cmd) ⇒ Object
346 347 348 349 |
# File 'lib/squared/workspace/project/ruby.rb', line 346 def bundle(flag, cmd) guard_params('bundle', flag, args: cmd) run_s "bundle #{flag} #{cmd.join(' ')}" end |
#copy(from: 'lib', glob: '**/*', into: @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: '**/*', into: @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) into = @copy[:into] if @copy.key?(:into) end return unless into dest = Pathname.new(into).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.info "cp #{a.join(c)} #{b}" copy_d(a, b, glob: c, verbose: verbose) end end |
#copy? ⇒ Boolean
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/squared/workspace/project/ruby.rb', line 378 def copy? return true if super || (@copy.is_a?(::Hash) && copy.fetch(:into, 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: [], sync: invoked_sync?('depend', flag)) ⇒ 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: [], sync: invoked_sync?('depend', flag)) 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 = option('trust-policy', ignore: false)) 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: sync) end end |
#depend? ⇒ Boolean
374 375 376 |
# File 'lib/squared/workspace/project/ruby.rb', line 374 def depend? @depend != false && (!@depend.nil? || outdated?) 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 316 317 318 319 320 321 322 323 324 |
# 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 = session_done(cmd) log.info cmd = (cmd) if invoked_sync?('outdated', rev) print_item = 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) next unless (cur = data.shift) && (lat = data.shift) semver(cur) semver(lat) c = cur.join l = lat.join styles = [] major_set = lambda do styles = %i[green bold] major += 1 end minor_set = -> { styles[0] = cur[2] == lat[2] ? :yellow : :green } if data.empty? semmajor(cur, lat) ? major_set.() : minor_set.() else data.each do |val| break unless (req = /(>=?|=|~>|!=|<=?) (#{Regexp.escape(val.join)})/.match(line)) v = semver(val).join case req[1] when '>', '>=' semmajor(cur, lat) ? major_set.() : minor_set.() when '<', '<=' if c <= v if semmajor(cur, lat) major_set.() 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 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 puts "#{start.to_s.rjust(2)}. #{line}" start += 1 elsif line =~ /^Gem / print_item if unless stdin? sub = { pat: /^(.+)(?<!\dm)(Gem|Latest)(.+)$/, styles: theme[:header], index: 2 } puts (" # #{line.chomp}", reverse: true, sub: [sub, sub]) end start += 1 end end if found > 0 begin if major == 0 && (data = /\b(?:source\s+(["'])(.+?)\1|remote:\s+(\S+))/.match(@gemfile.read)) status = (data[2] || data[3]).chomp('/') right = true end rescue StandardError => e log.debug e ensure status ||= 'Updates are available' end puts (empty_status(status, 'major', major, always: !right), right: right) elsif start == 0 puts 'No updates were found' end end end |
#outdated? ⇒ Boolean
412 413 414 |
# File 'lib/squared/workspace/project/ruby.rb', line 412 def outdated? gemfile.exist? end |
#populate ⇒ Object
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
332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/squared/workspace/project/ruby.rb', line 332 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
351 352 353 |
# File 'lib/squared/workspace/project/ruby.rb', line 351 def rake(*cmd) run_s(cmd.empty? ? 'rake' : cmd.map { |val| "rake #{val}" }) end |
#rakefile ⇒ Object
355 356 357 358 |
# File 'lib/squared/workspace/project/ruby.rb', line 355 def rakefile file = ::Rake::Application::DEFAULT_RAKEFILES.find { |val| base_path(val).exist? } base_path(file) if file end |
#ref ⇒ Object
72 73 74 |
# File 'lib/squared/workspace/project/ruby.rb', line 72 def ref Ruby.ref end |
#update(flag, opts: []) ⇒ Object
326 327 328 329 330 |
# File 'lib/squared/workspace/project/ruby.rb', line 326 def update(flag, opts: []) bundle_session 'update', "--#{flag}" append_bundle opts, OPT_UPDATE run_rb end |
#variable_set(key, *val) ⇒ Object
360 361 362 363 364 365 366 367 |
# File 'lib/squared/workspace/project/ruby.rb', line 360 def variable_set(key, *val, **) case key when :gemfile @gemfile = base_path(val.first) else super end end |
#variables ⇒ Object
369 370 371 372 |
# File 'lib/squared/workspace/project/ruby.rb', line 369 def variables ret = super + VAR_SET ret.freeze end |