Class: Bundix::CommandLine
- Inherits:
-
Object
- Object
- Bundix::CommandLine
- Defined in:
- lib/bundix/commandline.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ ruby: 'ruby', bundle_pack_path: 'vendor/bundle', gemfile: 'Gemfile', lockfile: 'Gemfile.lock', gemset: 'gemset.nix', project: File.basename(Dir.pwd) }
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #build_gemset ⇒ Object
- #handle_init ⇒ Object
- #handle_lock ⇒ Object
- #handle_magic ⇒ Object
-
#initialize ⇒ CommandLine
constructor
A new instance of CommandLine.
- #object2nix(obj) ⇒ Object
- #parse_options ⇒ Object
- #run ⇒ Object
- #save_gemset(gemset) ⇒ Object
- #shell_nix_context ⇒ Object
- #shell_nix_string ⇒ Object
Constructor Details
#initialize ⇒ CommandLine
25 26 27 |
# File 'lib/bundix/commandline.rb', line 25 def initialize @options = DEFAULT_OPTIONS.clone end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
29 30 31 |
# File 'lib/bundix/commandline.rb', line 29 def @options end |
Class Method Details
.run ⇒ Object
21 22 23 |
# File 'lib/bundix/commandline.rb', line 21 def self.run self.new.run end |
Instance Method Details
#build_gemset ⇒ Object
148 149 150 |
# File 'lib/bundix/commandline.rb', line 148 def build_gemset Bundix.new().convert end |
#handle_init ⇒ Object
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/bundix/commandline.rb', line 123 def handle_init if [:init] if File.file?('shell.nix') warn "won't override existing shell.nix but here is what it'd look like:" puts shell_nix_string else File.write('shell.nix', shell_nix_string) end end end |
#handle_lock ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/bundix/commandline.rb', line 134 def handle_lock if [:lock] lock = !File.file?([:lockfile]) lock ||= File.mtime([:gemfile]) > File.mtime([:lockfile]) if lock ENV.delete('BUNDLE_PATH') ENV.delete('BUNDLE_FROZEN') ENV.delete('BUNDLE_BIN_PATH') system('bundle', 'lock') fail 'bundle lock failed' unless $?.success? end end end |
#handle_magic ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/bundix/commandline.rb', line 99 def handle_magic ENV['BUNDLE_GEMFILE'] = [:gemfile] if [:magic] fail unless system( Bundix::NIX_SHELL, '-p', [:ruby], "bundler.override { ruby = #{[:ruby]}; }", "--command", "bundle lock --lockfile=#{[:lockfile]}") fail unless system( Bundix::NIX_SHELL, '-p', [:ruby], "bundler.override { ruby = #{[:ruby]}; }", "--command", "bundle pack --all --path #{[:bundle_pack_path]}") end end |
#object2nix(obj) ⇒ Object
152 153 154 |
# File 'lib/bundix/commandline.rb', line 152 def object2nix(obj) Nixer.serialize(obj) end |
#parse_options ⇒ 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 96 97 |
# File 'lib/bundix/commandline.rb', line 40 def op = OptionParser.new do |o| o.on '-m', '--magic', 'lock, pack, and write dependencies' do [:magic] = true end o.on "--ruby=#{[:ruby]}", 'ruby version to use for magic and init, defaults to latest' do |value| [:ruby] = value end o.on "--bundle-pack-path=#{[:bundle_pack_path]}", "path to pack the magic" do |value| [:bundle_pack_path] = value end o.on '-i', '--init', "initialize a new shell.nix for nix-shell (won't overwrite old ones)" do [:init] = true end o.on "--gemset=#{[:gemset]}", 'path to the gemset.nix' do |value| [:gemset] = File.(value) end o.on "--lockfile=#{[:lockfile]}", 'path to the Gemfile.lock' do |value| [:lockfile] = File.(value) end o.on "--gemfile=#{[:gemfile]}", 'path to the Gemfile' do |value| [:gemfile] = File.(value) end o.on '-d', '--dependencies', 'include gem dependencies (deprecated)' do warn '--dependencies/-d is deprecated because' warn 'dependencies will always be fetched' end o.on '-q', '--quiet', 'only output errors' do [:quiet] = true end o.on '-l', '--lock', 'generate Gemfile.lock first' do [:lock] = true end o.on '-v', '--version', 'show the version of bundix' do puts Bundix::VERSION exit end o.on '--env', 'show the environment in bundix' do system('env') exit end end op.parse! $VERBOSE = ![:quiet] end |
#run ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/bundix/commandline.rb', line 31 def run handle_magic handle_lock handle_init gemset = build_gemset save_gemset(gemset) end |
#save_gemset(gemset) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/bundix/commandline.rb', line 156 def save_gemset(gemset) tempfile = Tempfile.new('gemset.nix', encoding: 'UTF-8') begin tempfile.write(object2nix(gemset)) tempfile.flush FileUtils.cp(tempfile.path, [:gemset]) FileUtils.chmod(0644, [:gemset]) ensure tempfile.close! tempfile.unlink end end |
#shell_nix_context ⇒ Object
114 115 116 |
# File 'lib/bundix/commandline.rb', line 114 def shell_nix_context ShellNixContext.from_hash() end |
#shell_nix_string ⇒ Object
118 119 120 121 |
# File 'lib/bundix/commandline.rb', line 118 def shell_nix_string tmpl = ERB.new(File.read(File.('../../template/shell-nix.erb', __dir__))) tmpl.result(shell_nix_context.bind) end |