Class: Raykit::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/raykit/console.rb

Overview

The implementation for the raykit console application

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = nil) ⇒ Console

def self.get_opts



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/raykit/console.rb', line 21

def initialize(opts = nil)
  if opts.nil?
    @opts = Console.get_opts
  else
    @opts = opts
  end
  #@opts = Slop.parse do |o|

  #  o.string "-t", "--task", "rake task", default: "default"

  #  o.bool "-v", "--verbose", "print detailed output", default: false

  #  o.bool "-q", "--quiet", "minimal output", default: false

  #  o.bool "-s", "--stop", "stop on error", default: true

  #end


  #if opts.verbose?

  #  puts "options: #{Rainbow(@opts.to_hash).yellow.bright}"

  #  puts "arguments:#{Rainbow(@opts.arguments).yellow.bright}"

  #end

end

Instance Attribute Details

#opts ⇒ Object

Returns the value of attribute opts.



10
11
12
# File 'lib/raykit/console.rb', line 10

def opts
  @opts
end

Class Method Details

.get_opts ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/raykit/console.rb', line 12

def self.get_opts
  Slop.parse do |o|
    o.string "-t", "--task", "rake task", default: "default"
    o.bool "-v", "--verbose", "print detailed output", default: false
    o.bool "-q", "--quiet", "minimal output", default: false
    o.bool "-s", "--stop", "stop on error", default: true
  end
end

Instance Method Details

#add ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/raykit/console.rb', line 111

def add
  if @opts.arguments.length < 2
    puts "add requires a url"
    return 1
  end
  url = @opts.arguments[1]
  if REPOSITORIES.include?(url)
    puts "url #{url} already exists."
  else
    REPOSITORIES << url
    REPOSITORIES.save(REPOSITORIES.filename)
    puts "url #{url} added."
  end
end

#clean ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/raykit/console.rb', line 271

def clean
  pattern = ""
  pattern = @opts.arguments[1] if @opts.arguments.length > 1
  REPOSITORIES.matches(pattern).each do |url|
    repo = Raykit::Git::Repository.new(url)
    work = Raykit::Git::Directory.new(repo.get_dev_dir("work"))
    next unless Dir.exist?(work.directory)

    # TODO: run a git clean command in the directory

    puts "cleaning #{work.directory}"
    Dir.chdir(work.directory) do
      cmd = Command.new("git clean -xdf")
      cmd.summary if @opts.verbose?
      if cmd.exitstatus != 0
        cmd.details
        abort Rainbow(cmd.summary).blue.bright if @opts.quit?
      end
    end
  end
  Dir::remove_empty_directories(Raykit::Environment.get_dev_dir("work"))
end

#clobber ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/raykit/console.rb', line 293

def clobber
  pattern = ""
  pattern = @opts.arguments[1] if @opts.arguments.length > 1
  REPOSITORIES.matches(pattern).each do |url|
    repo = Raykit::Git::Repository.new(url)
    work = Raykit::Git::Directory.new(repo.get_dev_dir("work"))
    next unless Dir.exist?(work.directory)

    puts "removing #{work.directory}"
    begin
      FileUtils.rm_rf(work.directory, secure: true)
    rescue StandardError
      puts "error removing #{work.directory}"
    end
  end
  Dir::remove_empty_directories(Raykit::Environment.get_dev_dir("work"))
end

#copy ⇒ Object



311
312
313
314
315
316
317
318
319
320
# File 'lib/raykit/console.rb', line 311

def copy
  if @opts.arguments.length < 3
    puts "source and destination arguments are required for copy"
    return 1
  end
  source = @opts.arguments[1]
  dest = @opts.arguments[2]
  FileUtils.cp(source, dest)
  puts "copied #{source} to #{dest}"
end

#import ⇒ Object



261
262
263
264
265
266
267
268
269
# File 'lib/raykit/console.rb', line 261

def import
  pattern = ""
  pattern = @opts.arguments[1] if @opts.arguments.length > 1
  puts "scanning..."
  count = REPOSITORIES.length
  REPOSITORIES.import(pattern)
  new_count = REPOSITORIES.length - count
  puts "imported #{new_count} git repositories"
end

#list ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/raykit/console.rb', line 145

def list
  pattern = ""
  pattern = @opts.arguments[1] if @opts.arguments.length > 1
  REPOSITORIES.matches(pattern).each do |url|
    repo = Raykit::Git::Repository.new(url)
    puts repo.to_s
    #puts url

  end
end

#pull ⇒ Object



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
# File 'lib/raykit/console.rb', line 230

def pull
  pattern = ""
  pattern = @opts.arguments[1] if @opts.arguments.length > 1
  REPOSITORIES.matches(pattern).each do |url|
    repo = Raykit::Git::Repository.new(url)
    work = Raykit::Git::Directory.new(repo.get_dev_dir("work"))
    if Dir.exist?(work.directory) && !work.directory == ".git"
      Dir.chdir(work.directory) do
        diff = `git diff`.strip
        status = `git status`.strip
        if diff.length.zero? && status.include?("nothing to commit")
          cmd = Command.new("git pull")
          cmd.summary if @opts.verbose?

          if cmd.exitstatus != 0
            cmd.details
            abort Rainbow(cmd.summary).blue.bright if @opts.quit?
          end
        end
      end
    end
  rescue StandardError => e
    issue = "error occurred for pull of #{url} #{e}"
    if @opts.quit?
      abort Rainbow(issue).blue.bright
    else
      puts Rainbow(issue).blue.bright
    end
  end
end

#rake(hash) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/raykit/console.rb', line 322

def rake(hash)
  REPOSITORIES.each do |remote|
    next unless remote.include?(hash[:pattern])

    begin
      puts "remote: #{remote}"
      cmd = Raykit::Rake.run(remote, "master")
      elapsed_str = Timer.get_elapsed_str(cmd.elapsed)
      if cmd.exitstatus.zero?
        puts "#{elapsed_str} #{Rainbow(SECRETS.hide(cmd.command)).yellow.bright} (#{cmd.directory})"
      else
        puts "\r\n#{cmd.command}\r\n"
        puts "\r\n#{cmd.output}\r\n"
        puts "\r\n#{cmd.error}\r\n"
        puts ""
        puts "#{Rainbow(elapsed_str).red.bright} #{Rainbow(cmd.command).white}"
      end
    rescue StandardError
      puts "rescued..."
    end
  end
end

#remove ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/raykit/console.rb', line 126

def remove
  if @opts.arguments.length < 2
    puts "remove requires a url or pattern"
    return 1
  end
  pattern = ""
  pattern = @opts.arguments[1] if @opts.arguments.length > 1
  remove_urls = REPOSITORIES.matches(pattern)
  if remove_urls.length.zero?
    puts "no matching urls found."
  else
    remove_urls.each do |url|
      REPOSITORIES.delete(url)
      puts "url #{url} removed."
    end
    REPOSITORIES.save(REPOSITORIES.filename)
  end
end

#run ⇒ Object



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
68
69
70
71
72
# File 'lib/raykit/console.rb', line 40

def run
  verb = "usage"
  verb = @opts.arguments[0] if @opts.arguments.length.positive?
  case verb
  when "usage"
    usage
  when "add"
    add
  when "remove"
    remove
  when "list"
    list
  when "show"
    show
  when "work"
    work
  when "import"
    import
  when "clean"
    clean
  when "clobber"
    clobber
  when "sync_version"
    sync_version
  when "copy"
    copy
  when "pull"
    pull
  else
    puts "unrecognized command #{verb}"
    1
  end
end

#show ⇒ Object



155
156
157
158
159
160
161
# File 'lib/raykit/console.rb', line 155

def show
  pattern = ""
  pattern = @opts.arguments[1] if @opts.arguments.length > 1
  REPOSITORIES.matches(pattern).each do |url|
    puts url
  end
end

#usage ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/raykit/console.rb', line 101

def usage
  puts "Usage: raykit VERB [GIT_URL|PATTERN] [OPTIONS]"
  verb_descriptions.each do |k, v|
    puts "#{k.ljust(15, " ")} - #{v}"
  end
  # puts @opts

  # puts "verbs: " + verbs.to_s

  0
end

#verb_descriptions ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/raykit/console.rb', line 74

def verb_descriptions
  { "add" => "add a git url",
    "remove" => "remove one or more git urls",
    "list" => "list summaries of repositories",
    "show" => "show git urls matching a specific pattern",
    "work" => "clone and rake a git repository",
    "import" => "import git urls matching a specific pattern",
    "clean" => "clean one or more working directories",
    "clobber" => "clobber one or more working directories",
    "pull" => "preform git pull on one or more working directories",
    "sync_version" => "synchronize the version number between two files",
    "copy" => "copy a file" }
end

#verb_usage ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/raykit/console.rb', line 88

def verb_usage
  { "add" => "add GIT_URL",
    "remove" => "remove URL_PATTERN",
    "show" => "show URL_PATTERN",
    "work" => "work URL_PATTERN [--task RAKE_TASK]",
    "import" => "import URL_PATTERN",
    "clean" => "clean URL_PATTERN",
    "clobber" => "clobber URL_PATTERN",
    "pull" => "pull URL_PATTERN",
    "sync_version" => "sync_version SOURCE_FILENAME DESTINATION_FILENAME",
    "copy" => "copy SOURCE DESTINATION" }
end

#work ⇒ Object



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
# File 'lib/raykit/console.rb', line 163

def work
  pattern = ""
  pattern = @opts.arguments[1] if @opts.arguments.length > 1
  logger = Logger.new(STDOUT)
  # Set the formatter to output just the message

  #logger.formatter = proc do |_severity, _datetime, _progname, msg|

  #  "#{msg}\n"

  #end

  #logger.level = Logger::DEBUG

  logger.debug("work: pattern: #{pattern}")

  work_repositories = REPOSITORIES.matches(pattern)
  errors = []
  logger.info("work: found #{work_repositories.length} matching urls")
  #puts "work: found #{work_repositories.length} matching urls"

  work_repositories.each do |url|
    default_command = "rake default"
    logger.info("work: #{url} #{default_command}")
    #puts "work: #{url} #{default_command}"

    repo = Raykit::Git::Repository.new(url)
    cmd = repo.work default_command
    logger.info(cmd.to_s)
    #puts cmd.to_s

    #exitstatus = work_url(url)

    #puts " " if @opts.verbose?

    #return exitstatus if @opts[:stop] && exitstatus != 0

  end
  0
end

#work_url(url) ⇒ Object

puts "\nwork "rake default"" cmd = repo.work "rake default" puts cmd.to_s



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
# File 'lib/raykit/console.rb', line 197

def work_url(url)
  return 0 if url == "https://gitlab.com/gems-rb/raykit.git"

  puts Rainbow(url).yellow.bright if @opts.verbose?
  repo = Raykit::Git::Repository.new(url)
  work = Raykit::Git::Directory.new(repo.get_dev_dir("work"))
  unless Dir.exist?(work.directory)
    clone = Command.new("git clone #{url} #{work.directory} --recursive")
    puts clone.summary unless @opts.quiet?
    if clone.exitstatus != 0
      clone.details
      return clone.exitstatus
    end
  end
  if Dir.exist?(work.directory)
    Dir.chdir(work.directory) do
      if File.exist?("rakefile.rb")
        rake = Raykit::Command.new("rake #{@opts[:task]}")
        rake.summary(true) if !@opts.quiet? || rake.exitstatus != 0
        if rake.exitstatus != 0
          rake.details
          rake.summary true
          return rake.exitstatus
        end
      else
        puts("rakefile.rb not found in #{work.directory}") if @opts.verbose?
        return 0
      end
    end
  end
  0
end