Class: Binbundle::GemBins
- Inherits:
-
Object
- Object
- Binbundle::GemBins
- Defined in:
- lib/binbundle/gem_bins.rb
Overview
Main class
Instance Attribute Summary collapse
-
#bin_for ⇒ Object
writeonly
Get bins for gem :bin_for.
-
#dry_run ⇒ Object
writeonly
Set options (for testing).
-
#file ⇒ Object
writeonly
Set options (for testing).
-
#gem_for ⇒ Object
writeonly
Get gem for bin :gem_for.
Instance Method Summary collapse
-
#bins_to_s ⇒ String
Output all gems as Binfile format.
-
#generate ⇒ Object
Output or write Binfile.
-
#info(options) ⇒ String
Retrieve info (bin_for or gem_for).
-
#initialize(options = {}) ⇒ GemBins
constructor
Create a new GemBins object.
-
#install ⇒ Object
Install all gems in Binfile.
-
#write_file(output) ⇒ Object
Writes to Binfile.
Constructor Details
#initialize(options = {}) ⇒ GemBins
Create a new GemBins object
26 27 28 29 30 31 32 |
# File 'lib/binbundle/gem_bins.rb', line 26 def initialize( = {}) @include_version = [:include_version] || false @user_install = [:user_install] @sudo = [:sudo] @dry_run = [:dry_run] @file = File.([:file]) end |
Instance Attribute Details
#bin_for=(value) ⇒ Object (writeonly)
Get bins for gem :bin_for
10 11 12 |
# File 'lib/binbundle/gem_bins.rb', line 10 def bin_for=(value) @bin_for = value end |
#dry_run=(value) ⇒ Object (writeonly)
Set options (for testing)
13 14 15 |
# File 'lib/binbundle/gem_bins.rb', line 13 def dry_run=(value) @dry_run = value end |
#file=(value) ⇒ Object (writeonly)
Set options (for testing)
13 14 15 |
# File 'lib/binbundle/gem_bins.rb', line 13 def file=(value) @file = value end |
#gem_for=(value) ⇒ Object (writeonly)
Get gem for bin :gem_for
7 8 9 |
# File 'lib/binbundle/gem_bins.rb', line 7 def gem_for=(value) @gem_for = value end |
Instance Method Details
#bins_to_s ⇒ String
Output all gems as Binfile format
132 133 134 |
# File 'lib/binbundle/gem_bins.rb', line 132 def bins_to_s local_gems.map(&:gem_command).join("\n\n") end |
#generate ⇒ Object
Output or write Binfile
139 140 141 142 143 144 145 146 147 |
# File 'lib/binbundle/gem_bins.rb', line 139 def generate output = bins_to_s if @dry_run puts output else write_file(output) end end |
#info(options) ⇒ String
Retrieve info (bin_for or gem_for)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/binbundle/gem_bins.rb', line 43 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 = JewelryBox.new(contents: contents, include_version: @include_version, sudo: @sudo, user_install: @user_install) if [:gem_for] gem_list.gem_for_bin([:gem_for]) elsif [:bin_for] gem_list.bins_for_gem([:bin_for]) end end |
#install ⇒ Object
Install all gems in Binfile
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/binbundle/gem_bins.rb', line 69 def install unless File.exist?(@file) puts "File #{@file} not found" Process.exit 1 end contents = IO.read(@file) lines = JewelryBox.new(contents: contents, include_version: @include_version, sudo: @sudo, user_install: @user_install) total = lines.count successes = 0 failures = 0 res = Prompt.yn("Install #{total} gems from #{File.basename(@file)}", default_response: true) Process.exit 0 unless res puts "Installing gems from #{@file}" if @dry_run puts lines 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 successes += 1 spinner.success spinner.stop else failures += 1 spinner.error spinner.stop @errors << output end end puts "Total #{total}, installed: #{successes}, #{failures} errors." return if @errors.empty? puts 'ERRORS:' puts @errors.join("\n") Process.exit 1 end |
#write_file(output) ⇒ Object
Writes to Binfile
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/binbundle/gem_bins.rb', line 152 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 |