-
Notifications
You must be signed in to change notification settings - Fork 3
/
heroku_herocutter.rb
149 lines (124 loc) · 3.8 KB
/
heroku_herocutter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
=begin
Copyright (c) 2010 Terence Lee.
This file is part of Heroku Herocutter.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
=end
module Heroku::Command
class Plugins < Base
HEROCUTTER_URL = "http://herocutter.heroku.com"
def install_with_herocutter
name = args.shift
new_name = fetch_git_uri(name)
args.unshift(new_name)
install_without_herocutter
end
alias_method :install_without_herocutter, :install
alias_method :install, :install_with_herocutter
def push
begin
yaml = YAML.load_file(herocutter_file)
rescue Errno::ENOENT
api_key_error
return
end
if yaml['api_key'].nil?
api_key_error
return
end
uri = args[0]
name = args[1]
if uri.nil?
uri = prepare_uri_from_git_origin
end
github_uri_rewrite!(uri)
if uri and not uri.empty?
response = RestClient.post("#{HEROCUTTER_URL}/api/v1/plugins",
{
:api_key => yaml['api_key'],
:plugin => {:uri => uri, :name => name},
:format => 'json'
},
{"Authorization" => yaml['api_key'] }
).to_s
json = JSON.parse(response)
if json and json['error']
push_plugin_error
else
display "pushed plugin with uri: #{uri}"
end
else
push_plugin_error
return
end
uri
end
def list_with_herocutter
if args.first == '-r'
display ""
display "*** REMOTE PLUGINS ***"
display ""
response = RestClient.get("#{HEROCUTTER_URL}/api/v1/plugins", :format => 'json').to_s
json = JSON.parse(response)
json.each do |json_plugin|
display json_plugin['plugin']['name']
end
else
display ""
display "*** LOCAL PLUGINS ***"
display ""
list_without_herocutter
end
end
alias_method :list_without_herocutter, :list
alias_method :list, :list_with_herocutter
private
# determine if they passed in a plugin name or uri
# return the uri if found on herocutter
def fetch_git_uri(name, herocutter_url = HEROCUTTER_URL)
begin
response = RestClient.get("#{herocutter_url}/api/v1/plugins/#{name}.json").to_s
json = JSON.parse(response)
rescue
return name
end
if json['error'].nil? and json['plugin']['uri']
json['plugin']['uri']
else
name
end
end
def herocutter_file
"#{home_directory}/.heroku/herocutter"
end
def api_key_error
error "Could not find file #{herocutter_file}. Please check http://herocutter.heroku.com/profile"
end
def push_plugin_error
error "Could not push plugin, check your API key. See http://herocutter.heroku.com/profile for more info"
end
def git_remote_show_origin
`git remote show origin | grep URL`
end
def github_uri_rewrite!(uri)
if /git@github.com/.match(uri)
uri.sub!("git@github.com:", "git://github.com/")
end
end
def prepare_uri_from_git_origin
uri = git_remote_show_origin
if uri and /URL: /.match(uri)
uri = uri.split("URL: ").last.chomp
end
uri
end
end
end