Class: VimGet::Installer

Inherits:
Object
  • Object
show all
Defined in:
lib/vimget/installer.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Installer

Manage the installing progress and uninstall/upgrading etc



21
22
23
24
25
# File 'lib/vimget/installer.rb', line 21

def initialize(opts = {})
  @options = opts
  @dist_path = File.expand_path(VimGet.configure.distfiles_dir)
  FileUtils.mkdir_p(@dist_path) if not File.exist? @dist_path
end

Instance Method Details

#extract(filepath) ⇒ Object

Raises:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/vimget/installer.rb', line 87

def extract(filepath)
  abs_file = File.expand_path(filepath)
  raise InstallError, "File #{filepath} is not existed!" if not File.exist? abs_file
  FileUtils.mkdir_p(File.expand_path(VimGet.configure.vim_dir)) if not File.exist? VimGet.configure.vim_dir

  ext = File.extname(abs_file)
  case ext
  when ".zip"
    extract_zip abs_file
  when ".rar"
    extract_rar abs_file
  when ".tgz", ".gz"
    extract_tar(abs_file, "xzof")
  when ".bz", ".bz2",".tbz"
    extract_tar(abs_file, "xjof")
  when ".vim"
    FileUtils.cp abs_file, File.expand_path(File.join(VimGet.configure.vim_dir, "plugin"))
    ["#{File.join("plugin", File.basename(abs_file))}"]
  else
    raise InstallError, "Unknown ext name: #{ext}"
  end
end

#fetch(script) ⇒ Object



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
# File 'lib/vimget/installer.rb', line 61

def fetch(script)
  #raise RuntimeError if script.kind_of? Script

  url = "http://www.vim.org/scripts/#{script.download}"
  print "Downloading...#{url}..." unless @options[:verbose]
  STDOUT.flush
  distfile = ""
  open(url) do |f|
    if f.meta["content-disposition"] =~ /filename=(.+)/
      filename = $1
    else
      raise NetworkError, "Can not find appropiated filename"
    end
    
    puts filename
    
    distfile = File.join(VimGet.configure.distfiles_dir, filename)
    puts "Saving in #{distfile}" if VimGet.configure.verbose
    File.open(File.expand_path(distfile), "w") do |df|
      df.truncate(0)
      df.write(f.read)
    end
  end
  return distfile
end

#install(script) ⇒ Object

Install a script



28
29
30
31
32
33
34
# File 'lib/vimget/installer.rb', line 28

def install(script)
  filepath = fetch(script)
  script.manifest = extract(filepath)
  
  script.installed = script.version
  VimGet.db.update(script) unless @options[:dry_run]
end

#uninstall(script) ⇒ Object

Uninstall one script



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vimget/installer.rb', line 37

def uninstall(script)
  puts "#{script.name} is not installed yet.";return if !script.installed?
  
  puts "Uninstalling #{script.name}..." 
  remove_manifest(script)
  
  if @options[:purge]
    print "Delete manifest file of #{script.name}..."
    VimGet.db.remove(script)
    puts "Done"
  else
    script.installed = ""
    VimGet.db.update(script)
  end
end

#upgrade(script) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/vimget/installer.rb', line 53

def upgrade(script)
  puts "#{script.name} is not installed yet."; return if !script.installed?
  
  puts "Upgrading #{script.name}-#{script.installed}(Relacing by #{script.version})..."
  remove_manifest(script)
  install(script)
end