Class: Binbundle::GemBins
- Inherits:
-
Object
- Object
- Binbundle::GemBins
- Defined in:
- lib/binbundle/gem_bins.rb
Overview
Main class
Instance Method Summary collapse
- #bins_to_s ⇒ Object
- #gem_command(gem, attrs) ⇒ Object
- #generate ⇒ Object
- #info(options) ⇒ Object
-
#initialize(options = {}) ⇒ GemBins
constructor
A new instance of GemBins.
- #install ⇒ Object
- #local_gems ⇒ Object
- #write_file(output) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ GemBins
Returns a new instance of GemBins.
10 11 12 13 14 15 16 17 18 |
# File 'lib/binbundle/gem_bins.rb', line 10 def initialize( = {}) @include_version = [:include_version] || false @user_install = [:user_install] @sudo = [:sudo] @dry_run = [:dry_run] @file = File.([:file]) @local_gems = local_gems.delete_if { |_, specs| specs.delete_if { |spec| spec.executables.empty? }.empty? } end |
Instance Method Details
#bins_to_s ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/binbundle/gem_bins.rb', line 104 def bins_to_s gems_with_bins = {} @local_gems.each do |g, specs| versions = specs.map { |spec| spec.version.to_s } bins = specs.map(&:executables) gems_with_bins[g] = { version: versions.max, bins: bins.sort.uniq } end gems_with_bins.map { |gem, attrs| gem_command(gem, attrs) }.join("\n\n") end |
#gem_command(gem, attrs) ⇒ Object
97 98 99 100 101 102 |
# File 'lib/binbundle/gem_bins.rb', line 97 def gem_command(gem, attrs) ver = @include_version ? " -v '#{attrs[:version]}'" : '' ui = @user_install ? '--user-install ' : '' sudo = @sudo ? 'sudo ' : '' "# Executables: #{attrs[:bins].join(', ')}\n#{sudo}gem install #{ui}#{gem}#{ver}" end |
#generate ⇒ Object
116 117 118 119 120 121 122 123 124 |
# File 'lib/binbundle/gem_bins.rb', line 116 def generate output = bins_to_s if @dry_run puts output else write_file(output) end end |
#info(options) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/binbundle/gem_bins.rb', line 20 def info() unless File.exist?(@file) || [:local] puts "File #{@file} not found" Process.exit 1 end contents = if [:local] bins_to_s else IO.read(@file) end gem_list = GemList.new(contents, include_version: @include_version) if [:gem_for] gem_list.gem_for_bin([:gem_for]) elsif [:bin_for] gem_list.bins_for_gem([:bin_for]) end end |
#install ⇒ 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/binbundle/gem_bins.rb', line 40 def install unless File.exist?(@file) puts "File #{@file} not found" Process.exit 1 end res = Prompt.yn("Install gems from #{File.basename(@file)}", default_response: true) Process.exit 0 unless res puts "Installing gems from #{@file}" contents = IO.read(@file) gem_list = GemList.new(contents, include_version: @include_version) lines = if @sudo gem_list.sudo elsif @user_install gem_list.user_install else gem_list.normal_install end if @dry_run puts lines.join("\n") Process.exit 0 end `sudo echo -n ''` if @sudo @errors = [] lines.each do |cmd| # rubocop:disable Naming/VariableNumber spinner = TTY::Spinner.new("[:spinner] #{cmd} ...", hide_cursor: true, format: :dots_2) # rubocop:enable Naming/VariableNumber spinner.auto_spin output = `/bin/bash -c '#{cmd}' 2>&1` result = $CHILD_STATUS.success? if result spinner.success spinner.stop else spinner.error spinner.stop @errors << output end end return if @errors.empty? puts 'ERRORS:' puts @errors.join("\n") end |
#local_gems ⇒ Object
6 7 8 |
# File 'lib/binbundle/gem_bins.rb', line 6 def local_gems Gem::Specification.sort_by { |g| [g.name.downcase, g.version] }.group_by(&:name) end |
#write_file(output) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/binbundle/gem_bins.rb', line 126 def write_file(output) if File.exist?(@file) res = Prompt.yn("#{@file} already exists, overwrite", default_response: false) Process.exit 1 unless res end File.open(@file, 'w') do |f| f.puts '#!/bin/bash' f.puts f.puts output end puts "Wrote list to #{@file}" res = Prompt.yn('Make file executable', default_response: true) return unless res FileUtils.chmod 0o777, @file puts 'Made file executable' end |