Class: MVCLI::BundleProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/mvcli/plugins/providers/bundle_provider.rb

Instance Method Summary collapse

Instance Method Details

#activate!Object



8
9
10
# File 'lib/mvcli/plugins/providers/bundle_provider.rb', line 8

def activate!
  require activatefile if activatefile.exist?
end

#activatefileObject



55
56
57
# File 'lib/mvcli/plugins/providers/bundle_provider.rb', line 55

def activatefile
  dir.join 'activate.rb'
end

#builderObject



63
64
65
66
67
# File 'lib/mvcli/plugins/providers/bundle_provider.rb', line 63

def builder
  @builder ||= Bundler::Dsl.new.tap do |builder|
    builder.eval_gemfile gemfile
  end
end

#dirObject



59
60
61
# File 'lib/mvcli/plugins/providers/bundle_provider.rb', line 59

def dir
  config.directory('plugins')
end

#gemfileObject



37
38
39
40
41
42
43
44
45
# File 'lib/mvcli/plugins/providers/bundle_provider.rb', line 37

def gemfile
  dir.join('Gemfile').tap do |path|
    unless path.exist?
      path.open "wb" do |file|
        file.puts "source 'https://rubygems.org'"
      end
    end
  end
end

#lockObject



69
70
71
72
# File 'lib/mvcli/plugins/providers/bundle_provider.rb', line 69

def lock
  require 'bundler'
  Bundler::LockfileParser.new lockfile.read
end

#lockfileObject



47
48
49
# File 'lib/mvcli/plugins/providers/bundle_provider.rb', line 47

def lockfile
  dir.join 'Gemfile.lock'
end

#pluginsObject



31
32
33
34
35
# File 'lib/mvcli/plugins/providers/bundle_provider.rb', line 31

def plugins
  lock.specs.select do |spec|
    builder.dependencies.detect { |dep| dep.name == spec.name }
  end
end

#remove(gem_name) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/mvcli/plugins/providers/bundle_provider.rb', line 21

def remove(gem_name)
  require 'bundler'
  dep = builder.dependencies.find { |d| d.name == gem_name }
  fail "#{gem_name} is not an installed plugin" unless dep
  builder.dependencies.reject! { |d| d == dep }
  update! gem_name
  write!
  return dep
end

#replace(name, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/mvcli/plugins/providers/bundle_provider.rb', line 12

def replace(name, options = {})
  require 'bundler'
  builder.dependencies.reject! { |dep| dep.name == name }
  dep, *rest = builder.gem name, options
  update! name
  write!
  return dep
end

#setupfileObject



51
52
53
# File 'lib/mvcli/plugins/providers/bundle_provider.rb', line 51

def setupfile
  dir.join 'bundle/bundler/setup.rb'
end

#update!(name) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mvcli/plugins/providers/bundle_provider.rb', line 100

def update!(name)
  path = Bundler.settings[:path]
  original_definition_method = Bundler.method(:definition)
  Bundler.with_clean_env do
    ENV['BUNDLE_GEMFILE'] = gemfile.to_s
    definition = builder.to_definition(lockfile, name => true)
    Bundler.settings[:path] = dir.join('bundle').to_s
    Dir.chdir dir do
      Bundler.define_singleton_method(:definition) { definition }
      Bundler::Installer.install dir, definition, standalone: [], "update" => true
    end
  end
ensure
  Bundler.define_singleton_method(:definition, &original_definition_method)
  Bundler.settings[:path] = path
end

#valueObject



4
5
6
# File 'lib/mvcli/plugins/providers/bundle_provider.rb', line 4

def value
  self
end

#write!Object



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
# File 'lib/mvcli/plugins/providers/bundle_provider.rb', line 74

def write!
  gemfile.open "w" do |file|
    file.puts "source 'https://rubygems.org'"
    builder.dependencies.each do |dep|
      file << %|gem "#{dep.name}"|
      if req = dep.requirements_list.first
        file << %|, "#{req}"|
      end
      options = dep.source ? dep.source.options || {} : {}
      options = Hash[options.map { |k, v| [k,v.to_s]}]
      options.merge! "require" =>  dep.autorequire if dep.autorequire
      file << %|, #{options.inspect}|
      file << "\n"
    end
  end
  activatefile.open "w" do |file|
    file.puts "require #{setupfile.to_s.inspect}"
    builder.dependencies.each do |dep|
      require_names = dep.autorequire || [dep.name]
      require_names.each do |name|
        file.puts "require #{name.inspect}"
      end
    end
  end
end