Debian Codegraph @

lib/bundler/cli/init.rb

raw
1 # frozen_string_literal: true
2
3 module Bundler
4 class CLI::Init
5 attr_reader :options
6 def initialize(options)
7 @options = options
8 end
9
10 def run
11 if File.exist?(gemfile)
12 Bundler.ui.error "#{gemfile} already exists at #{File.expand_path(gemfile)}"
13 exit 1
14 end
15
16 unless File.writable?(Dir.pwd)
17 Bundler.ui.error "Can not create #{gemfile} as the current directory is not writable."
18 exit 1
19 end
20
21 if options[:gemspec]
22 gemspec = File.expand_path(options[:gemspec])
23 unless File.exist?(gemspec)
24 Bundler.ui.error "Gem specification #{gemspec} doesn't exist"
25 exit 1
26 end
27
28 spec = Bundler.load_gemspec_uncached(gemspec)
29
30 File.open(gemfile, "wb") do |file|
31 file << "# Generated from #{gemspec}\n"
32 file << spec.to_gemfile
33 end
34 else
35 File.open(File.expand_path("../templates/Gemfile", __dir__), "r") do |template|
36 File.open(gemfile, "wb") do |destination|
37 IO.copy_stream(template, destination)
38 end
39 end
40 end
41
42 puts "Writing new #{gemfile} to #{SharedHelpers.pwd}/#{gemfile}"
43 end
44
45 private
46
47 def gemfile
48 @gemfile ||= options[:gemfile] || Bundler.preferred_gemfile_name
49 end
50 end
51 end