Class: Gem::Commands::MigrateCommand

Inherits:
AbstractCommand
  • Object
show all
Defined in:
lib/commands/migrate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMigrateCommand

Returns a new instance of MigrateCommand.



8
9
10
# File 'lib/commands/migrate.rb', line 8

def initialize
  super 'migrate', description
end

Instance Attribute Details

#rubygemObject (readonly)

Returns the value of attribute rubygem.



2
3
4
# File 'lib/commands/migrate.rb', line 2

def rubygem
  @rubygem
end

Instance Method Details

#check_for_approvedObject



84
85
86
87
88
89
90
91
92
93
# File 'lib/commands/migrate.rb', line 84

def check_for_approved
  say "Asking Gemcutter to verify the upload..."

  response = make_request(:put, "gems/#{rubygem["name"]}/migrate") do |request|
    request.add_field("Content-Length", 0)
    request.add_field("Authorization", api_key)
  end

  say response.body
end

#descriptionObject



4
5
6
# File 'lib/commands/migrate.rb', line 4

def description
  'Migrate a gem your own from Rubyforge to Gemcutter.'
end

#executeObject



12
13
14
15
# File 'lib/commands/migrate.rb', line 12

def execute
  setup
  migrate
end

#find(name) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/commands/migrate.rb', line 24

def find(name)
  require 'json'

  response = make_request(:get, "gems/#{name}.json")

  case response
  when Net::HTTPSuccess
    begin
      @rubygem = JSON.parse(response.body)
    rescue JSON::ParserError => json_error
      say "There was a problem parsing the data: #{json_error}"
      terminate_interaction
    end
  else
    say "This gem is currently not hosted on Gemcutter."
    terminate_interaction
  end
end

#get_tokenObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/commands/migrate.rb', line 43

def get_token
  say "Starting migration of #{rubygem["name"]} from RubyForge..."

  response = make_request(:post, "gems/#{rubygem["name"]}/migrate") do |request|
    request.add_field("Content-Length", 0)
    request.add_field("Authorization", api_key)
  end

  case response
  when Net::HTTPSuccess
    say "A migration token has been created."
    response.body
  else
    say response.body
    terminate_interaction
  end
end

#migrateObject



17
18
19
20
21
22
# File 'lib/commands/migrate.rb', line 17

def migrate
  find(get_one_gem_name)
  token = get_token
  upload_token(token)
  check_for_approved
end

#upload_token(token) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/commands/migrate.rb', line 61

def upload_token(token)
  require 'tempfile'
  require 'net/scp'

  url = "#{self.rubygem['rubyforge_project']}.rubyforge.org"
  say "Uploading the migration token to #{url}. Please enter your RubyForge login:"
   = ask("Login: ")
  password = ask_for_password("Password: ")

  begin
    Net::SCP.start(url, , :password => password) do |scp|
      temp_token = Tempfile.new("token")
      temp_token.chmod 0644
      temp_token.write(token)
      temp_token.close
      scp.upload! temp_token.path, "/var/www/gforge-projects/#{rubygem['rubyforge_project']}/migrate-#{rubygem['name']}.html"
    end
    say "Successfully uploaded your token."
  rescue Exception => e
    say "There was a problem uploading your token: #{e}"
  end
end