Class: Drupal::Install

Inherits:
Object
  • Object
show all
Includes:
REXML
Defined in:
lib/drupal/install.rb

Instance Method Summary collapse

Instance Method Details

#check_coreObject

Allow users to type ‘core’ instead of ‘drupal install drupal’



46
47
48
# File 'lib/drupal/install.rb', line 46

def check_core
  @project = 'drupal' if @project =~ /^core|drupal$/
end

#debug(message) ⇒ Object



20
21
22
# File 'lib/drupal/install.rb', line 20

def debug(message)
  puts '... ' + message
end

#destination_empty?Boolean

Check if the destination is empty.

Returns:

  • (Boolean)


41
42
43
# File 'lib/drupal/install.rb', line 41

def destination_empty?
  Dir['*'].length == 0
end

#get_release(xmldoc, which) ⇒ Object

returns the release with releasenumber ‘which’ from xmldoc. use ‘latest’ for which, if you want the latest stable release



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/drupal/install.rb', line 105

def get_release(xmldoc, which)
  releases = get_releases(xmldoc)

  ordered = releases.keys.sort_by {|k| k.to_s.split(/\.|-/).map {|v| v.to_i} }

  if ( which == 'latest' )
    release = releases[ordered.last]
  elsif ordered.include? which
    release = releases[which]
  else
    raise "Failed to find requested release #{which}"
  end

  return release
end

#get_releases(xmldoc) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/drupal/install.rb', line 93

def get_releases(xmldoc)
  releases = Hash.new

  xmldoc.root.each_element('//release') do |release|
    releases[release.text('version')] = release
  end

  return releases
end

#get_tarball(release) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/drupal/install.rb', line 121

def get_tarball(release)
  tarball = release.elements['//download_link'].text
  tarpath = File.basename(tarball)

  abort "Failed to find Drupal 6 tar of #{@project}" if tarball.nil?
  debug "Found tarball #{tarball}"

  # Fetch tarball
  begin
    response = Net::HTTP.get_response(URI.parse(tarball))
    puts tarpath
    File.open(tarpath, 'w') do |f|
      f.write response.body
    end
    debug "Copied tarball to #{tarpath}"
  rescue
    abort "Failed to copy remote tarball #{tarball} to #{tarpath}"
  end

  return tarpath
end

#get_xml(project, version = '6.x') ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/drupal/install.rb', line 74

def get_xml project, version='6.x'
  debug "Locating #{project} page for version #{version}"
  # Locate tarball from project page
  begin
    response = Net::HTTP.get_response(URI.parse("http://updates.drupal.org/release-history/#{project}/#{version}"))
    # TODO: unhardcode the dependency on Drupal 6, make this an environment or static var.
    # TODO: check 404, 403 etc.
    xmldoc = Document.new response.body
    if xmldoc.root.name == 'error' #TODO: better error handling here.
      message = xmldoc.root.text
      raise message
    end
  rescue
    abort "Could not fetch #{project}. Error: #{message}"
  end

  return xmldoc
end

#install_projectObject

Install project.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/drupal/install.rb', line 57

def install_project
  xmldoc = get_xml(@project, @version)
  release = get_release(xmldoc, 'latest')
  @tarpath = get_tarball(release)

  # Extract tarball
  tgz = Zlib::GzipReader.new(File.open(@tarpath, 'rb'))
  Archive::Tar::Minitar.unpack(tgz, @dest) rescue abort "Failed to extract #{@tarpath} to #{@dest}"
  
  # Remove tarball
  File.delete(@tarpath) rescue abort "Failed to remove #{@tarpath}"

  # Installation complete
  debug "Project #{@project} installed to #{File.dirname(@tarpath)}" unless @dest == '.'
  debug 'Installation complete'
end

#install_projectsObject

Install single project or iterate lists.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/drupal/install.rb', line 25

def install_projects
  if @project.include? ','
    projects = @project.split ','
    projects.each do |p|
      @project = p
      check_core
      install_project
      puts
    end
  else
    check_core
    install_project
  end
end

#run(arguments) ⇒ Object

Attempt to download core installation or module.



11
12
13
14
15
16
17
18
# File 'lib/drupal/install.rb', line 11

def run(arguments)
  @project = arguments[0]
  @dest = arguments[1] || '.'
  @version = arguments[2] || '6.x'
  abort "Destination #{@dest} is not a directory." unless File.directory?(@dest)
  abort 'Project name required (core | <project>).' if arguments.empty?
  install_projects
end

#uri_available?(uri) ⇒ Boolean

Check if a uri is available.

Returns:

  • (Boolean)


51
52
53
# File 'lib/drupal/install.rb', line 51

def uri_available?(uri)
  open(uri) rescue false
end