Class: Rcr::GemSync

Inherits:
Object
  • Object
show all
Defined in:
lib/rcr/gem_sync.rb

Constant Summary collapse

GITHUB =
"http://gems.github.com"
RCR_DEFAULT_GEM_LIST =
File.expand_path(File.join(File.dirname(__FILE__), *%w[.. runcoderun_gems.txt]))
RCR_GITHUB_GEM_LIST =
"http://github.com/runcoderun/gem_sync/raw/master/lib/runcoderun_gems.txt"
RCR_GITHUB_GEM_BLACKLIST =
"http://github.com/runcoderun/gem_sync/raw/master/lib/gem_blacklist.txt"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = ["--github"]) ⇒ GemSync

Returns a new instance of GemSync.



14
15
16
17
18
19
# File 'lib/rcr/gem_sync.rb', line 14

def initialize(args = ["--github"])
  @options = Rcr::OptionParsing.parse(args)
  @platform = @options[:platform]
  @gem_list = @options[:gem_list]
  system("gem env") if verbose?
end

Instance Attribute Details

#gem_listObject (readonly)

Returns the value of attribute gem_list.



12
13
14
# File 'lib/rcr/gem_sync.rb', line 12

def gem_list
  @gem_list
end

#platformObject (readonly)

Returns the value of attribute platform.



12
13
14
# File 'lib/rcr/gem_sync.rb', line 12

def platform
  @platform
end

Instance Method Details

#dry_run?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/rcr/gem_sync.rb', line 35

def dry_run?
  @options[:dry_run]
end

#gem_installed?(name) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/rcr/gem_sync.rb', line 65

def gem_installed?(name)
  installed_gems.any? { |installed| installed.name == name }
end

#gem_version_installed?(version) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
# File 'lib/rcr/gem_sync.rb', line 69

def gem_version_installed?(version)
  return true if version.nil?
  installed_gems.any? { |installed| installed.version == version }
end

#install!(rubygem) ⇒ Object



59
60
61
62
63
# File 'lib/rcr/gem_sync.rb', line 59

def install!(rubygem)
  return unless platform_matches?(rubygem)
  return if installed?(rubygem)
  install_from_rubyforge(rubygem) || install_from_github(rubygem)
end

#install_from_github(rubygem) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/rcr/gem_sync.rb', line 89

def install_from_github(rubygem)
  return unless rubygem.name.include?("-")
  cmd = "gem install #{rubygem.name} --no-ri --no-rdoc"
  cmd << " --version #{rubygem.version}" if rubygem.version
  cmd << " --verbose" if verbose_gem?
  cmd << " --source #{GITHUB}"
  run(cmd)
end

#install_from_rubyforge(rubygem) ⇒ Object



82
83
84
85
86
87
# File 'lib/rcr/gem_sync.rb', line 82

def install_from_rubyforge(rubygem)
  cmd = "gem install #{rubygem.name} --no-ri --no-rdoc"
  cmd << " --version #{rubygem.version}" if rubygem.version
  cmd << " --verbose" if verbose_gem?
  run(cmd)
end

#install_gemsObject



55
56
57
# File 'lib/rcr/gem_sync.rb', line 55

def install_gems
  @gems.each { |rubygem| install!(rubygem) }
end

#installed?(requested) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/rcr/gem_sync.rb', line 74

def installed?(requested)
  gem_installed?(requested.name) && gem_version_installed?(requested.version)
end

#installed_gemsObject



78
79
80
# File 'lib/rcr/gem_sync.rb', line 78

def installed_gems
  @installed_gems ||= Rcr::GemParser.convert_gem_list(`gem list`)
end

#parse_gemsObject



51
52
53
# File 'lib/rcr/gem_sync.rb', line 51

def parse_gems
  @gems = Rcr::GemParser.convert_gem_list(gem_list)
end

#platform_matches?(gem) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
# File 'lib/rcr/gem_sync.rb', line 43

def platform_matches?(gem)
  if gem.platforms && platform
    gem.platforms.include?(platform)
  else
    true
  end
end

#read_gem_listObject



39
40
41
# File 'lib/rcr/gem_sync.rb', line 39

def read_gem_list
  open(@gem_list).read
end

#run(cmd) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/rcr/gem_sync.rb', line 98

def run(cmd)
  if dry_run?
    puts cmd
    true
  else
    puts cmd if verbose?
    system(cmd)
  end
end

#syncObject



21
22
23
24
25
# File 'lib/rcr/gem_sync.rb', line 21

def sync
  @gem_list = read_gem_list
  @gems = parse_gems
  install_gems
end

#verbose?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/rcr/gem_sync.rb', line 27

def verbose?
  @options[:verbose]
end

#verbose_gem?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/rcr/gem_sync.rb', line 31

def verbose_gem?
  @options[:verbose_gem]
end