Class: Squared::Repo::Project::Ruby

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

Instance Attribute Summary collapse

Attributes inherited from Base

#group, #log, #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?, #doc, #doc?, #enabled?, #has?, #initialize_build, #initialize_script, #inspect, #refresh, #refresh?, #test, #test?, to_s, #to_s, to_sym, #to_sym

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, **kwargs) ⇒ Ruby

Returns a new instance of Ruby.



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

def initialize(name, path, workspace, *, **kwargs)
  super
  initialize_build(REF, **kwargs)
  @version = env('BUILD', suffix: 'VERSION', strict: true) || kwargs.delete(:version)
  @autodetect = kwargs.key?(:autodetect) ? kwargs.delete(:autodetect) : false
  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
    pat = %r{\brequire\s+(["'])bundler/gem_tasks\1}
    File.foreach(file) do |line|
      next unless line.match?(pat)

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

Instance Attribute Details

#gemfileObject (readonly)

Returns the value of attribute gemfile.



36
37
38
# File 'lib/squared/repo/project/ruby.rb', line 36

def gemfile
  @gemfile
end

Class Method Details

.is_a?(val) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
# File 'lib/squared/repo/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/repo/project/ruby.rb', line 14

def populate(*); end

.tasksObject



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

def tasks
  nil
end

Instance Method Details

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



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/squared/repo/project/ruby.rb', line 151

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)


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

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

  gempath = -> { "gems#{::File::SEPARATOR}#{project}-#{@version}" }
  if @version && (val = ENV['GEM_HOME'])
    @gemdir = Pathname.new(val).join(gempath.())
    return true if gemdir?
  end
  return false unless @autodetect

  unsafe = ->(hint) { raise ArgumentError, message('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: [], group: [], override: false) ⇒ Object



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

def depend(flag = nil, opts: [], group: [], override: false)
  if @depend && !override
    super
  elsif outdated?
    case flag
    when :redownload, :local, :'prefer-local'
      cmd = bundle_session 'install'
      cmd << "--#{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 :'with-g', :'without-g'
      guard_params('install', flag, args: group)
      cmd = gem_session 'install', '-g'
      opt = flag.to_s.sub('-g', '')
      group.each { |s| cmd << "--#{opt}=#{shell_escape(s, quote: true)}" }
    else
      cmd = bundle_session 'install'
      append_bundle
    end
    run(banner: banner?, exception: workspace.exception)
  end
end

#depend?Boolean

Returns:

  • (Boolean)


251
252
253
# File 'lib/squared/repo/project/ruby.rb', line 251

def depend?
  outdated? || !!@depend
end

#dev?Boolean

Returns:

  • (Boolean)


259
260
261
# File 'lib/squared/repo/project/ruby.rb', line 259

def dev?
  @dev
end

#outdatedObject



173
# File 'lib/squared/repo/project/ruby.rb', line 173

def outdated(*); end

#outdated?Boolean

Returns:

  • (Boolean)


255
256
257
# File 'lib/squared/repo/project/ruby.rb', line 255

def outdated?
  gemfile.exist?
end

#populateObject



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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/squared/repo/project/ruby.rb', line 62

def populate(*)
  super
  return unless outdated?

  namespace name do
    @@tasks[REF].each do |action, flags|
      if flags.nil?
        case action
        when :rake
          next unless rakefile

          desc format_desc(action, nil, 'command*')
          task action, [:command] do |_, args|
            rake collect_args(args, :command)
          end
        end
      else
        namespace action do
          flags.each do |flag|
            case action
            when :install
              case flag
              when :'with-g', :'without-g'
                desc format_desc(action, flag, 'group+')
                task flag, [:group] do |_, args|
                  depend(flag, group: collect_args(args, :group), override: true)
                end
              else
                desc format_desc(action, flag, OPT_INSTALL)
                task flag, [:opts] do |_, args|
                  depend(flag, opts: collect_args(args, :opts), override: true)
                end
              end
            when :update
              desc format_desc(action, flag, OPT_UPDATE)
              task flag, [:opts] do |_, args|
                update(flag, opts: collect_args(args, :opts))
              end
            when :pristine
              desc format_desc(action, flag, 'version?')
              task flag, [:version] do |_, args|
                pristine(flag, version: args.version)
              end
            end
          end
        end
      end
    end
  end
end

#pristine(flag, version: nil) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
# File 'lib/squared/repo/project/ruby.rb', line 185

def pristine(flag, version: nil)
  cmd = gem_session 'pristine', project
  case flag
  when :extensions
    cmd << '--extensions'
  when :version
    guard_params version, 'version'
  end
  cmd << "--version=#{version}" if version
  run(banner: banner?, exception: workspace.exception)
end

#rake(cmd) ⇒ Object



197
198
199
200
201
202
203
# File 'lib/squared/repo/project/ruby.rb', line 197

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

#rakefileObject



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

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

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



175
176
177
178
179
180
181
182
183
# File 'lib/squared/repo/project/ruby.rb', line 175

def update(flag, opts: [])
  cmd = bundle_session 'update'
  case flag
  when :all, :major, :patch, :minor
    cmd << "--#{flag}"
  end
  append_bundle opts, OPT_UPDATE
  run(banner: banner?, exception: workspace.exception)
end