ghcurl 0.5.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +21 -8
  3. data/bin/ghcurl +183 -70
  4. data/lib/ghcurl.rb +4 -3
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b51925892c13aec5ed7e86f931e8319f9b4ddb06c0c14d6c08816776b3e5b922
4
- data.tar.gz: 01ce411ded642b6f0cac664e3a966ad2053f7b0a7f3a5d536a28e27f0531a43a
3
+ metadata.gz: '0069ec92dd2a2638c346d29aec05b591f52556ccee9906c28a3382506d5d5bd6'
4
+ data.tar.gz: db516685965cfeadfb7e7db426ee8b328e5f6267af1158bbdc21c23e2aeddce7
5
5
  SHA512:
6
- metadata.gz: 4194359adb2d54b6fca4069e7d8f67ff205bd2b350ce70648aca80281e7d6a392eb7e46c50257a330341f18be67cf1ccc2a50bcf40becb104b60beb0ffb0367c
7
- data.tar.gz: 8e04c84f09936c07f8c975443933a757452677c58e7613339747f6983a3297cff21642d4633a0e47fe39251648554f47227135eda3dd557acd575faffe465a5b
6
+ metadata.gz: 71a3fd97dc832a34b358f87a1be6b925e07647e83506545d13384229a42a24f34a6ab4c6c362a3b482b914e5bbff620fa8a0f3aa545dc771db371ad7eb366c7a
7
+ data.tar.gz: 3b407e1b7b4ebf046621567d9a1aba7d60cbd55754de54b4e85b7c9af583a57054cd7b50868761249ae385f9aded3eae9d5ca0cc8913245e03b14eaa50bc2f09
data/README.md CHANGED
@@ -12,26 +12,39 @@ gem install ghcurl
12
12
 
13
13
  Download files and install from Github releases.
14
14
 
15
+ It's quite easy to use `ghcurl`, because it can recognize your OS and arch, always give you the best option to download by default. What's more, You can search by regular expression, specify a version, and install it to a path or according to the environment variable `GHCURL_BIN_PATH` or just `/usr/local/bin`. The best of it is to automatically extract a tar/zip file to make the installation process smooth in a terminal.
16
+
17
+ It may be difficult to use it on Windows, welcome contributions!
18
+
15
19
  </div>
16
20
 
17
21
  ## Usage
18
22
 
19
- Download latest deb/rpm package and install, notice the argument `deb` / `rpm` are just regular expressions.
20
- ```bash
21
- ghcurl cli/cli deb -i
22
- ghcurl cli/cli rpm -i
23
- ```
23
+ Things can be easier. We consider these as popular binaries: https://github.com/ibraheemdev/modern-unix
24
+
25
+ Notice: `-i` means to install.
24
26
 
25
- Things can be easier.
26
27
  ```bash
27
28
  # It knows that's sharkdp/fd
28
29
  ghcurl fd -i
30
+
31
+ # Much easier to install rbspy!
32
+ ghcurl rbspy -i
33
+
34
+ # On Linux, it's very handy
35
+ ghcurl cli -i
36
+ ```
37
+
38
+ Download latest deb/rpm package and install, notice the argument `deb` / `rpm` are just regular expressions.
39
+ ```bash
40
+ ghcurl cli/cli deb -i
41
+ ghcurl cli/cli rpm -i
29
42
  ```
30
43
 
31
44
  Normal download
32
45
  ```bash
33
46
  # Download latest rbspy-x86_64-unknown-linux-gnu.tar.gz to ~/.cache/ghcurl
34
- ghcurl rbspy/rbspy x86_64.*linux
47
+ ghcurl rbspy/rbspy 'x86_64.*linux'
35
48
 
36
49
  # Download rbspy version 0.11.1
37
50
  ghcurl rbspy/rbspy 'x86_64.*linux' -v0.11.1
@@ -46,7 +59,7 @@ ghcurl BetaPictoris/timeleft timeleft -i
46
59
  ghcurl BetaPictoris/timeleft timeleft -i ~/tmp/bin
47
60
 
48
61
  # Install and rename it to, here, 'gd' in /usr/local/bin
49
- ghcurl dlvhdr/gh-dash linux-amd64 -i gd
62
+ ghcurl dlvhdr/gh-dash linux-amd64 -i -r 'gd'
50
63
 
51
64
  # or, like this
52
65
  ghcurl dlvhdr/gh-dash linux-amd64 -i ~/tmp/bin/gd
data/bin/ghcurl CHANGED
@@ -3,11 +3,11 @@
3
3
  # File : ghcurl.rb
4
4
  # Authors : ccmywish <ccmywish@qq.com>
5
5
  # Created on : <2022-04-12>
6
- # Last modified : <2022-04-14>
6
+ # Last modified : <2022-04-15>
7
7
  #
8
8
  # ghcurl:
9
9
  #
10
- # Download files (and install) from Github releases
10
+ # Download files and install from Github releases
11
11
  #
12
12
  # ------------------------------------------------------
13
13
 
@@ -16,11 +16,12 @@ require 'nokogiri'
16
16
  require 'open-uri'
17
17
  require 'highline'
18
18
  require 'cliswitch'
19
+ require 'fileutils'
19
20
 
20
21
  module Ghcurl
21
22
 
22
23
  WAREHOUSE = File.expand_path("~/.cache/ghcurl")
23
- BIN_PATH = ENV['GHCURL_BIN_PATH'] || "/usr/local/bin"
24
+ BIN_PATH = ENV['GHCURL_BIN_PATH'].chomp('/') || "/usr/local/bin"
24
25
  HL = HighLine.new
25
26
 
26
27
  class Error < StandardError; end
@@ -37,10 +38,6 @@ module Ghcurl
37
38
 
38
39
 
39
40
  def curl(url, name)
40
- #if !test('d', WAREHOUSE)
41
- # require 'fileutils'
42
- # FileUtils.mkdir_p(WAREHOUSE)
43
- #end
44
41
  cmd = "curl -L #{url} --create-dirs -o #{WAREHOUSE}/#{name}"
45
42
  system cmd
46
43
  log "Downloaded to #{WAREHOUSE}/#{name}"
@@ -58,7 +55,8 @@ module Ghcurl
58
55
 
59
56
  oses = [
60
57
  ['linux'],
61
- ['mac', 'apple'],
58
+ ['freebsd'],
59
+ ['mac', 'apple', 'darwin'],
62
60
  ['windows']
63
61
  ]
64
62
 
@@ -190,43 +188,21 @@ module Ghcurl
190
188
 
191
189
  log "Downloading #{url}"
192
190
 
193
- $downloaded = link.split('/').last
194
-
195
- curl(url, $downloaded)
191
+ download_name = link.split('/').last
196
192
 
193
+ curl(url, download_name)
194
+ return download_name
197
195
  end
198
196
 
199
197
 
200
- def install(ware, place: BIN_PATH)
201
198
 
202
- ware_name = ''
203
- if ware.include?('/')
204
- ware_name = ware.split('/').last
205
- else
206
- ware_name = ware
207
- end
208
-
209
- if (! $downloaded.include?(ware_name)) and (!$downloaded.end_with?('.deb')) and (!$downloaded.end_with?('.rpm'))
210
- log "Do you want to rename or install it to a different path?"
211
- re = HL.ask "Input path or name, or just enter to say no."
212
- if !re.empty?
213
- place = re
214
- end
215
- end
216
-
217
- case RUBY_PLATFORM
218
- when /ucrt/i, /mingw/i
219
- install_on_windows($downloaded, place)
220
- else
221
- install_on_nix($downloaded, place)
222
- end
223
- log "Install finish!"
224
- end
199
+ def install(dl_name, rename_as: nil, install_to: nil)
225
200
 
201
+ if install_to.nil?
202
+ install_to = BIN_PATH
203
+ end
226
204
 
227
- def install_on_nix(ware, place)
228
-
229
- target = "#{WAREHOUSE}/#{ware}"
205
+ target = "#{WAREHOUSE}/#{dl_name}"
230
206
 
231
207
  if target.end_with?('.deb')
232
208
  log "Install deb package for you"
@@ -241,42 +217,177 @@ module Ghcurl
241
217
  end
242
218
 
243
219
 
244
- if test 'd', place
245
- cmd = "sudo install -Dt #{place} -m 755 #{target} "
246
- system cmd
247
- log "Install #{ware} to " + place
220
+ #
221
+ # The zipped file
222
+ #
223
+
224
+ zip_flag = false
225
+ # unzipped to dir
226
+ unzip_dir = ""
227
+ # unzipped files
228
+ files = ""
229
+ # our desired software name
230
+ name = ""
231
+
232
+ if target.match?(/\.zip$/) or target.match?(/\.tar\.(\w){1,3}/)
233
+
234
+ zip_flag = true
235
+
236
+ # unzip
237
+ if target.match? /\.zip$/
238
+ log "Unzip zip file"
239
+ unzip_dir = target.chomp('.zip')
240
+ system "unzip -q #{target} -d #{unzip_dir}"
241
+ end
242
+
243
+ # .gz, .bz2, .xz
244
+ if target.match? /\.tar\.(\w){1,3}/
245
+ log "Unzip tar file"
246
+ unzip_dir = target.split('.')[0..-3].join('.')
247
+ FileUtils.mkdir_p(unzip_dir)
248
+ system "tar xf #{target} --directory=#{unzip_dir}"
249
+ end
250
+
251
+
252
+
253
+ def _iterate_dir(dir)
254
+ result = []
255
+ chd = Dir.children(dir)
256
+ chd_files = chd.select{|f| File.file? "#{dir}/#{f}"}
257
+
258
+ chd_files.each { result << "#{dir}/#{_1}"[2..] }
259
+
260
+ chd_dirs = chd.select{|d| File.directory?("#{dir}/#{d}") && ( d != '.git')}
261
+ chd_dirs.each do |d|
262
+ sub_dir = "#{dir}/#{d}"
263
+ result.concat _iterate_dir(sub_dir)
264
+ end
265
+ result
266
+ end
267
+
268
+
269
+
270
+ Dir.chdir unzip_dir do
271
+ files = _iterate_dir('.')
272
+ end
273
+
274
+
275
+ if files.size > 1
276
+ name = HL.choose do |menu|
277
+ menu.index_color = :rgb_77bbff
278
+ menu.prompt = "Which one do you want to install? "
279
+ menu.choices( *files )
280
+ end
281
+ else
282
+ name = files[0]
283
+ end
284
+
285
+ end # end of zipped file handle
286
+
287
+
288
+ if !zip_flag
289
+ # downloaded name in WAREHOUSE
290
+ name = dl_name
291
+ end
292
+
293
+
294
+ if (name.size > 10 ||
295
+ name.include?('.') ||
296
+ name.include?('-') ||
297
+ name.include?('_')) && (not rename_as)
298
+ log "Do you want to rename the '#{name}'?"
299
+ re = HL.ask "Input name, or just leave it blank to say no."
300
+ if !re.empty?
301
+ rename_as = re
302
+ end
303
+ end
248
304
 
305
+
306
+ if zip_flag
307
+ # add unzip_dir prefix
308
+ name = unzip_dir.delete_prefix(WAREHOUSE + '/') + '/' + name
309
+ end
310
+
311
+ if rename_as
312
+ log "Renamed as '#{rename_as}'"
313
+ end
314
+
315
+
316
+ case RUBY_PLATFORM
317
+ when /ucrt/i, /mingw/i
318
+ install_on_windows(name, install_to, rename_as)
249
319
  else
250
- unless place.include?('/')
251
- # User just give it another name
252
- place = BIN_PATH + '/' + place
320
+ install_on_nix(name, install_to, rename_as)
321
+ end
322
+ end
323
+
324
+
325
+ def install_on_nix(name, install_to, rename_as)
326
+ install_to = install_to.chomp('/')
327
+ target = WAREHOUSE + '/' + name
328
+
329
+ if test 'd', install_to
330
+ log "Ready to install #{name}"
331
+ if rename_as
332
+ dest = "#{install_to}/#{rename_as}"
333
+ system "sudo cp #{target} " + dest
334
+ system "sudo chmod +x " + dest
335
+ log "Installed as " + dest
253
336
  else
254
- # User give it a path and its name
337
+ cmd = "sudo install -Dt #{install_to} -m 755 #{target} "
338
+ system cmd
339
+ log "Install #{name} to " + install_to
255
340
  end
256
- log "Installed as " + place
257
- system "sudo cp #{target} #{place}"
258
- system "sudo chmod +x #{place}"
341
+ log "Install finish!"
342
+ else
343
+ log "#{install_to} is not a directory!"
259
344
  end
260
345
 
261
346
  end
262
347
 
263
348
 
264
349
  def install_on_windows
265
- log "Sorry, not implemented yet!"
350
+ log "Sorry, not implemented yet on Windows! Can you help?"
266
351
  end
267
352
 
268
353
 
354
+ #
355
+ # For -l option
356
+ #
269
357
  def list_wares
270
-
271
- puts blue("In #{WAREHOUSE}")
272
- puts
358
+ puts blue("ghcurl: #{WAREHOUSE}")
273
359
  Dir.children(WAREHOUSE).each_with_index do |dict,i|
274
- puts " #{blue(i+1)}. #{bold(green(dict))}"
360
+ puts "#{blue(i+1)}. #{bold(green(dict))}"
275
361
  end
276
- puts
277
362
  end
278
363
 
279
364
 
365
+ #
366
+ # For -d option
367
+ #
368
+ def delete_wares(name)
369
+ begin
370
+
371
+ if name.nil?
372
+ re = HL.ask "Do you want to delete all downloaded files?"
373
+ case re.downcase
374
+ when '','y','ye','yes','true'
375
+ FileUtils.rm_rf WAREHOUSE
376
+ log "Delete all done"
377
+ end
378
+ else
379
+ FileUtils.rm WAREHOUSE + '/' + name
380
+ log "Delete #{name} done"
381
+ end
382
+
383
+ rescue Exception => e
384
+ puts bold(red("ghcurl: #{e}"))
385
+ list_wares
386
+ end
387
+
388
+ end # end def delete_wares
389
+
390
+
280
391
  def help
281
392
  puts <<~EOC
282
393
  ghcurl (v#{VERSION}): Download files and install from Github releases
@@ -288,14 +399,16 @@ module Ghcurl
288
399
  ghcurl repo [re] -v tag => Download a specific tag version
289
400
  ghcurl repo [deb/rpm] => Download and install deb/rpm package
290
401
  ghcurl repo [re] -i [path] => Download and install to path
291
- ghcurl repo [re] -i name => Download and install as 'name'
402
+ ghcurl repo [re] -i -r name => Download and install as 'name'
292
403
  ghcurl -l => List downloaded files
404
+ ghcurl -d [name] => Delete a downloaded file or all
293
405
  ghcurl -h => Print this help
294
406
 
295
407
  example:
296
408
  ghcurl bat => Search sharkdp/bat the latest
297
409
  ghcurl cli deb -i => Search cli/cli /deb/
298
410
  ghcurl rbspy/rbspy 'x86_64.*linux' -v0.11.1
411
+ ghcurl dlvhdr/gh-dash linux-amd64 -i -r 'gd'
299
412
 
300
413
  EOC
301
414
  end
@@ -311,9 +424,11 @@ extend Ghcurl
311
424
 
312
425
  class Ghcurl::CLI < CliSwitch
313
426
  option name: 'install', short: '-i', arg_required: 'optional'
427
+ option name: 'rename', short: '-r', arg_required: 'required'
314
428
  option name: 'version', short: '-v', arg_required: 'required'
315
- option name: 'help', short: '-h', long: '--help', arg_required: 'noarg'
316
- option name: 'list', short: '-l', arg_required: 'noarg'
429
+ option name: 'help', short: '-h', long: '--help', arg_required: 'noarg'
430
+ option name: 'list', short: '-l', arg_required: 'noarg'
431
+ option name: 'delete', short: '-d', arg_required: 'optional'
317
432
  end
318
433
 
319
434
  args, opts = Ghcurl::CLI.new.parse(ARGV)
@@ -323,16 +438,12 @@ if args.empty? and opts.empty?
323
438
  exit
324
439
  end
325
440
 
326
- if args.size >= 2
327
- repo_or_name, regexp = args[0], args[1]
328
- else
329
- repo_or_name, regexp = args[0], nil
330
- end
331
-
332
441
 
442
+ repo_or_name, regexp = args[0], args[1]
333
443
  version = nil
334
444
  need_install = false
335
445
  install_to = nil
446
+ rename_as = nil
336
447
 
337
448
  opts.each do
338
449
  case _1.name
@@ -342,11 +453,16 @@ opts.each do
342
453
  when 'list'
343
454
  list_wares
344
455
  exit
456
+ when 'delete'
457
+ delete_wares(_1.next_arg)
458
+ exit
345
459
  when 'version'
346
460
  version = _1.next_arg
347
461
  when 'install'
348
462
  need_install = true
349
463
  install_to = _1.next_arg
464
+ when 'rename'
465
+ rename_as = _1.next_arg
350
466
  end
351
467
  end
352
468
 
@@ -355,15 +471,12 @@ end
355
471
  # p regexp
356
472
  # p need_install
357
473
  # p install_to
474
+ # p rename_as
358
475
 
359
476
  begin
360
- download(repo_or_name, regexp, version: version)
477
+ dl_name = download(repo_or_name, regexp, version: version)
361
478
  if need_install
362
- if install_to
363
- install(repo_or_name, place: install_to)
364
- else
365
- install(repo_or_name)
366
- end
479
+ install(dl_name, rename_as: rename_as, install_to: install_to)
367
480
  end
368
481
  rescue Interrupt
369
482
  end
data/lib/ghcurl.rb CHANGED
@@ -2,7 +2,7 @@
2
2
  # File : ghcurl.rb
3
3
  # Authors : ccmywish <ccmywish@qq.com>
4
4
  # Created on : <2022-04-12>
5
- # Last modified : <2022-04-14>
5
+ # Last modified : <2022-04-15>
6
6
  #
7
7
  # ghcurl:
8
8
  #
@@ -12,7 +12,7 @@
12
12
 
13
13
  module Ghcurl
14
14
 
15
- VERSION = "0.5.1"
15
+ VERSION = "0.6.0"
16
16
 
17
17
  end
18
18
 
@@ -61,5 +61,6 @@ Ghcurl::DEFAULT_WARES = {
61
61
  xh: 'ducaale/xh',
62
62
  zoxide: 'ajeetdsouza/zoxide',
63
63
 
64
- scc: 'boyter/scc'
64
+ scc: 'boyter/scc',
65
+ rbspy: 'rbspy/rbspy'
65
66
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ghcurl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ccmywish