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

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

Instance Attribute Summary

Attributes inherited from Base

#group, #name, #path, #project, #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 included from Common::Format

#check_style, #emphasize, #enable_aixterm, #log_message, #log_title, #sub_style

Methods included from Common

#as_a, #finalize!, #get!, #message, #set!

Methods inherited from Base

#base_path, #build, #build?, #clean, #clean?, #doc, #doc?, #enabled?, #has?, #initialize_build, #initialize_script, #inspect, #refresh, #refresh?, #styles, #test, #test?, to_s, #to_s, to_sym, #to_sym

Methods included from Common::Task

#collect_args, #invoke, #invoked?

Methods included from Common::System

#copy_d, #copy_f, #shell

Constructor Details

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

Returns a new instance of Ruby.



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

def initialize(name, project, 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
  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
    warn e
  end
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.is_a?(::Pathname) || (val.is_a?(::String) && (val = Pathname.new(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



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

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] || '**/*'
    warn "cp #{a.join(c)} #{b}"
    copy_d(a, b, glob: c, verbose: verbose?)
  end
end

#copy?Boolean

Returns:

  • (Boolean)


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

def copy?
  return true if @copy && (@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

  error = ->(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)
    error.('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

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

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



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

def depend(flag = nil, opts: [], group: [])
  if @depend
    super
  else
    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_opts 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)}" }
    else
      cmd = bundle_session 'install'
    end
    run(banner: banner?, exception: workspace.exception)
  end
end

#depend?Boolean

Returns:

  • (Boolean)


246
247
248
# File 'lib/squared/repo/project/ruby.rb', line 246

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

#dev?Boolean

Returns:

  • (Boolean)


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

def dev?
  @dev
end

#outdatedObject



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

def outdated(*); end

#outdated?Boolean

Returns:

  • (Boolean)


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

def outdated?
  GEMFILE.any? { |file| base_path(file).exist? }
end

#populateObject



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

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|
                  install(flag, group: collect_args(args, :group))
                end
              else
                desc format_desc(action, flag, OPT_INSTALL)
                task flag, [:opts] do |_, args|
                  install(flag, opts: collect_args(args, :opts))
                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



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/squared/repo/project/ruby.rb', line 180

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



192
193
194
195
196
197
198
# File 'lib/squared/repo/project/ruby.rb', line 192

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

#rakefileObject



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

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

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



170
171
172
173
174
175
176
177
178
# File 'lib/squared/repo/project/ruby.rb', line 170

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