/
github.rb
109 lines (92 loc) · 2.68 KB
/
github.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
module Jail
# This is a wrapper around ::Github
class Github
attr_reader :github, :name, :repo, :spec
attr_accessor :path
cattr_accessor :githublist
def self.add_githublist(yaml_path)
hash = YAML.load_file(yaml_path).to_hash
self.githublist= self.githublist.merge(hash)
end
self.githublist={}
add_githublist( Jail::Engine.root.join("config", "jail.jqueryplugins.yml") )
# TODO : raise if missing descr:
def self.all
githublist.group_by {|k, v| v[:descr].match(/\w+:/).to_s if v[:descr]}
end
# Uhh useless ? Or AR Sexy ?
def self.find(name, repo, path = nil)
new(name, repo).tap do |yo|
yo.path= path
end
end
def initialize(name, repo)
@name= name
@repo= repo
@spec= self.class.githublist["#{@name}/#{@repo}"]
@github = defined?(Jail::LOGIN) ?
::Github.new({ :basic_auth => Jail::LOGIN }) :
::Github.new
end
# another AR alike for chaining
def where(path)
self.path= path
self
end
# do I need this method ?
def repos
@repos ||= github.repos.get(name, repo)
end
def contents
self.path ||= "/"
github.repos.contents.get(name, repo, path)
end
# TODO : split in 2 methods for consistency
def readme
text = Base64.decode64 github.repos.contents.readme(name, repo).content
github.markdown.render :text => text
end
def read
#raise "blah" unless contents.type == "file"
text = Base64.decode64 contents.content
end
def download(type = nil)
t = target(type) and t.parent.mkpath
t.open('w') {|f| f.write( read ) }
end
def delete(type = nil)
t = target(type) and t.exist? and t.delete
end
def target(type=nil)
filename= Pathname(path).basename
type = type || extract_type(filename)
case type
when :js, :coffee
Rails.root.join("vendor/assets/javascripts/#{filename}")
when :css, :scss, :less
Rails.root.join("vendor/assets/stylesheets/#{filename}")
when :img, :png, :gif, :jpeg
Rails.root.join("vendor/assets/images/#{filename}")
end
end
def extract_type(filename)
type = Pathname(filename).extname
type = type.gsub(/\./, "").to_sym
end
#
# Depends on yaml files: dont use elsewhere
#
def write_all
[:js, :css, :img].each do |type|
where(spec[type]).download(type) if spec[type]
end
end
alias install write_all
def delete_all
[:js, :css, :img].each do |type|
where(spec[type]).delete(type) if spec[type]
end
end
alias remove delete_all
end
end