Class: Binbundle::GemBins

Inherits:
Object
  • Object
show all
Defined in:
lib/binbundle/gem_bins.rb

Overview

Main class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GemBins

Create a new GemBins object

Parameters:

  • options (defaults to: {})

    The options

Options Hash (options):

  • :include_version (Boolean)

    Include version number in output

  • :sudo (Boolean)

    Include sudo in output

  • :user_install (Boolean)

    Include –user-install in output

  • :dry_run (Boolean)

    Output to STDOUT

  • :file (String)

    File to parse

  • :local (Boolean)

    Work from local gems instead of Binfile



26
27
28
29
30
31
32
# File 'lib/binbundle/gem_bins.rb', line 26

def initialize(options = {})
  @include_version = options[:include_version] || false
  @user_install = options[:user_install]
  @sudo = options[:sudo]
  @dry_run = options[:dry_run]
  @file = File.expand_path(options[: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_sString

Output all gems as Binfile format

Returns:

  • (String)

    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

#generateObject

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)

Parameters:

  • options

    The options

Options Hash (options):

  • :local (Boolean)

    Work from local gems instead of Binfile

  • :gem_for (String)

    Find gem for this binary

  • :bin_for (String)

    Find bins for this gem

Returns:

  • (String)

    resulting gem or bins



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(options)
  unless File.exist?(@file) || options[:local]
    puts "File #{@file} not found"
    Process.exit 1
  end

  contents = if options[: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 options[:gem_for]
    gem_list.gem_for_bin(options[:gem_for])
  elsif options[:bin_for]
    gem_list.bins_for_gem(options[:bin_for])
  end
end

#installObject

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