Skip to content

Commit

Permalink
Faster gem install call by grouping gems.
Browse files Browse the repository at this point in the history
See cyx#6.
  • Loading branch information
djanowski committed Apr 16, 2014
1 parent 597f02e commit bab2341
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gems
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
cutest -v 1.2.0
override -v 0.0.10
56 changes: 27 additions & 29 deletions bin/dep
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

require "fileutils"

def die
abort("error: dep --help for more info")
end

module Dep
class List
attr :path
Expand Down Expand Up @@ -52,20 +56,18 @@ module Dep
end

def to_s
"#{name} -v #{version}"
"#{name}:#{version}"
end

def ==(other)
to_s == other.to_s
end
end

module CLI
class << self
attr_accessor :prerelease, :list, :file
end
class CLI
attr_accessor :prerelease, :list, :file

def self.add(name)
def add(name)
dependency = Gem::Dependency.new(name)
fetcher = Gem::SpecFetcher.fetcher

Expand All @@ -87,14 +89,14 @@ module Dep
puts "dep: added #{lib}"
end

def self.rm(name)
def rm(name)
@list.remove(Dep::Lib.new(name))
@list.save

puts "dep: removed #{name}"
end

def self.check
def check
if @list.missing_libraries.empty?
puts "dep: all cool"
else
Expand All @@ -108,25 +110,19 @@ module Dep
end
end

def self.install
def install
if @list.missing_libraries.empty?
puts "dep: nothing to install"
exit
end

@list.missing_libraries.each do |lib|
run "gem install #{lib}"
end
run "gem install #{@list.missing_libraries.join(" ")}"
end

def self.run(cmd)
def run(cmd)
puts " #{cmd}"
`#{cmd}`
end

def self.abort
abort("error: dep --help for more info")
end
end
end

Expand All @@ -140,7 +136,7 @@ private
when 1 then block.call(ARGV.delete_at(index))
when 0 then block.call
else
Dep::CLI.abort
die
end
end
end
Expand All @@ -151,15 +147,17 @@ end
# script, we have to instead rely on a different condition.
if File.basename($0) == "dep"

Dep::CLI.file = File.join(Dir.pwd, ".gems")
Dep::CLI.prerelease = false
cli = Dep::CLI.new

cli.file = File.join(Dir.pwd, ".gems")
cli.prerelease = false

on("-f") do |file|
Dep::CLI.file = file
cli.file = file
end

on("--pre") do
Dep::CLI.prerelease = true
cli.prerelease = true
end

on("--help") do
Expand All @@ -171,21 +169,21 @@ if File.basename($0) == "dep"
exit
end

Dep::CLI.list = Dep::List.new(Dep::CLI.file)
cli.list = Dep::List.new(cli.file)

FileUtils.touch(Dep::CLI.list.path) unless File.exist?(Dep::CLI.list.path)
FileUtils.touch(cli.list.path) unless File.exist?(cli.list.path)

case ARGV[0]
when "add"
Dep::CLI.add(ARGV[1])
cli.add(ARGV[1])
when "rm"
Dep::CLI.rm(ARGV[1])
cli.rm(ARGV[1])
when "install", "i"
Dep::CLI.install
cli.install
when nil
Dep::CLI.check
cli.check
else
Dep::CLI.abort
die
end

end
Expand Down
32 changes: 31 additions & 1 deletion test/dep.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
require "cutest"
require "override"

load File.expand_path("../../bin/dep", __FILE__)

class Cutest::Scope
include Override
end

# Dep::Lib
scope do
test "parsing" do
Expand All @@ -23,7 +28,7 @@

test "to_s" do
lib = Dep::Lib.new("cutest", "1.1.3")
assert_equal "cutest -v 1.1.3", lib.to_s
assert_equal "cutest:1.1.3", lib.to_s
end
end

Expand Down Expand Up @@ -51,3 +56,28 @@
assert list.libraries.include?(Dep::Lib.new("cutest", "2.0"))
end
end

# Dep::CLI
scope do
setup do
list = Dep::List.new("/dev/null")

list.add(Dep::Lib.new("foo", "2.0"))
list.add(Dep::Lib.new("bar", "1.1"))

commands = []

cli = Dep::CLI.new
cli.list = list

override(cli, run: -> args { commands << args })

[cli, commands]
end

test "install" do |cli, commands|
cli.install

assert_equal ["gem install foo:2.0 bar:1.1"], commands
end
end

0 comments on commit bab2341

Please sign in to comment.