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, #rev, #stash, #status

Methods included from Common::Format

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

Methods inherited from Base

#base_path, #build, #build?, #clean, #clean?, #doc, #doc?, #enabled?, #has?, #refresh, #refresh?, #styles, to_s, #to_s, #to_sym, #verbose?

Methods included from Common

#as_a, #get, #message, #set

Constructor Details

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

Returns a new instance of Ruby.



37
38
39
40
41
42
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
# File 'lib/squared/repo/project/ruby.rb', line 37

def initialize(name, project, workspace, **kwargs)
  super
  if env('BUILD', suffix: 'DEV', equals: '0')
    @dev = false
  else
    @dev = kwargs.delete(:dev)
    if env('BUILD', suffix: 'DEV')
      @dev = true
    elsif @dev.nil?
      @dev = @workspace.dev?
    end
  end
  @version = env('BUILD', suffix: 'VERSION', strict: true) || kwargs.delete(:version)
  @autodetect = kwargs.key?(:autodetect) ? kwargs.delete(:autodetect) : false
  @gemdir = nil
  return unless @output[0].nil? && @copy.nil? && !@version && !@autodetect && !doc? && (file = rakefile)

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

      @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?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

def is_a?(path)
  if path.is_a?(::String) || path.is_a?(::Pathname)
    base = Pathname.new(path)
    [*GEMFILE, 'Rakefile', 'README.rdoc'].any? { |file| base.join(file).exist? }
  else
    super
  end
end

.tasksObject



13
14
15
# File 'lib/squared/repo/project/ruby.rb', line 13

def tasks
  nil
end

.to_symObject



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

def to_sym
  :ruby
end

Instance Method Details

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



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

def copy(from: 'lib', glob: '**/*', basedir: @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)
    basedir = @copy[:basedir] if @copy.key?(:basedir)
  end
  return unless basedir

  dest = Pathname.new(basedir).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)


196
197
198
199
200
201
202
203
204
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
# File 'lib/squared/repo/project/ruby.rb', line 196

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

  gem_path = -> { "gems#{::File::SEPARATOR}#{@project}-#{@version}" }
  if @version && (val = ENV['GEM_HOME'])
    @gemdir = Pathname.new(val).join(gem_path.())
    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(gem_path.())
  rescue StandardError => e
    warn e
    @version = nil
    @gemdir = nil
    @autodetect = false
  else
    gemdir?
  end
end

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



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/repo/project/ruby.rb', line 108

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)


237
238
239
# File 'lib/squared/repo/project/ruby.rb', line 237

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

#dev?Boolean

Returns:

  • (Boolean)


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

def dev?
  @dev
end

#outdatedObject



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

def outdated(*); end

#outdated?Boolean

Returns:

  • (Boolean)


241
242
243
# File 'lib/squared/repo/project/ruby.rb', line 241

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

#populateObject



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

def populate(*)
  super
  return unless outdated?

  namespace @name do
    @@tasks[:ruby].each do |action, flags|
      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

#pristine(flag, version: nil) ⇒ Object



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

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

#rakefileObject



191
192
193
194
# File 'lib/squared/repo/project/ruby.rb', line 191

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

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



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

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