Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 164 lines (139 sloc) 4.159 kB
4ff25f4 @mislav thor gm:check
authored
1 #!/usr/bin/env ruby
2 require 'net/http'
3 require 'curl'
4 require 'cgi'
99a4a86 @mislav support Haml partials and extract the update form markup
authored
5 require 'haml'
a8db216 @mislav extracted all Endless Tweets styles to an external .sass file
authored
6 require 'sass'
5c094fb @mislav javascript templates can also include templates
authored
7 require 'stringio'
4ff25f4 @mislav thor gm:check
authored
8
9 class Gm < Thor
908fe78 @mislav thor gm:build task for building *.user.js files from source
authored
10 desc 'build', %(Builds the *.user.js files from *.js sources)
11 def build
12 for name in self.class.scripts.keys
13 source = File.open("#{name}/#{name}.js", 'r')
14 target = File.open("#{name}/#{name}.user.js", 'w')
15
5c094fb @mislav javascript templates can also include templates
authored
16 begin
17 render_template(source, target)
18 ensure
19 source.close
20 target.close
908fe78 @mislav thor gm:build task for building *.user.js files from source
authored
21 end
22
23 puts target.path
24 end
25 end
26
09ad56a @mislav add description to gm:check Thor task so it shown up in thor -T
authored
27 desc 'check', %(Checks scriptLength property on both local and remote file)
4ff25f4 @mislav thor gm:check
authored
28 def check
29 Net::HTTP.start('userscripts.org') do |http|
908fe78 @mislav thor gm:build task for building *.user.js files from source
authored
30 for name, id in self.class.scripts
4ff25f4 @mislav thor gm:check
authored
31 req = Net::HTTP::Head.new script_path(id)
32 res = http.request(req)
33 remote_size = res.content_length
34
35 file = script_file(name)
36 local_size = File.stat(file).size
37
38 hardcoded_size = nil
39
40 File.open(file) do |script|
41 script.each do |line|
42 if line =~ /\bscriptLength = (\d+)\b/
43 hardcoded_size = $1.to_i
44 end
45 end
46 end
47
48 puts "#{name} -- remote: #{remote_size}, local: #{local_size}, hardcoded: #{hardcoded_size}"
49 end
50 end
51 end
52
53 def upload(name)
54 id = self.class.scripts[name]
55 raise ArgumentError, "cannot find script '#{name}'" unless id
56
57 print 'UserScripts.org login ("email:pass"): '
58 STDIN.gets
59 auth = $_.chomp.split(':')
60 encoded_auth = auth.map { |i| CGI::escape i }
61
62 # FIXME: this is no work! (prolly because of CSRF protection)
63 c = Curl::Easy.new("http://#{encoded_auth.join(':')}@userscripts.org/scripts/update_src/#{id}")
64 c.multipart_form_post = true
65 c.http_post(
66 Curl::PostField.content('which_source', 'file'),
67 Curl::PostField.file('file[src]', script_file(name))
68 )
69 end
70
8117be2 @mislav change partial that the path is relative to the script directory
authored
71 def self.scripts
72 @@scripts ||= {
73 'endless_tweets' => 24398
74 }
75 end
76
77 private
78
4ff25f4 @mislav thor gm:check
authored
79 def script_path(id)
80 "/scripts/source/#{id}.user.js"
81 end
82
83 def script_file(name)
84 "#{name}/#{name}.user.js"
85 end
86
5c094fb @mislav javascript templates can also include templates
authored
87 def normalize_partial_path(path, dir)
8117be2 @mislav change partial that the path is relative to the script directory
authored
88 if path.index('/')
89 path
90 else
5c094fb @mislav javascript templates can also include templates
authored
91 "#{dir}/#{path}"
8117be2 @mislav change partial that the path is relative to the script directory
authored
92 end
93 end
94
5c094fb @mislav javascript templates can also include templates
authored
95 def render_template(file, target = nil)
96 partial = case file.extension
a8db216 @mislav extracted all Endless Tweets styles to an external .sass file
authored
97 when 'sass'
5c094fb @mislav javascript templates can also include templates
authored
98 css = Sass::Engine.new(file.read, :style => :compact).to_css
99a4a86 @mislav support Haml partials and extract the update form markup
authored
99 %[addCSS(#{javascript_string(css)})\n]
100 when 'haml'
101 html = Haml::Engine.new(file.read).to_html
102 javascript_string(html.rstrip)
5c094fb @mislav javascript templates can also include templates
authored
103 when 'js'
104 target ||= StringIO.new
105 render_js_with_partials(file, target)
106 target.string if StringIO === target
a8db216 @mislav extracted all Endless Tweets styles to an external .sass file
authored
107 else
5c094fb @mislav javascript templates can also include templates
authored
108 raise "don't know how to handle .#{file.extension}"
a8db216 @mislav extracted all Endless Tweets styles to an external .sass file
authored
109 end
5c094fb @mislav javascript templates can also include templates
authored
110 end
111
112 def render_js_with_partials(source, target)
113 dir = File.dirname(source.path)
114
115 for line in source
116 case line
117 when %r{^(\s*)(.*)//= ([\w/.]+)}
118 indentation, code, partial_name = $1, $2, $3
119 partial = normalize_partial_path(partial_name, dir)
120 target << indentation
121 target << code
122
123 if File.exists?(partial)
124 partial_file = File.open(partial, 'r')
125 begin
126 rendered_partial = render_template(partial_file)
127 ensure
128 partial_file.close
129 end
130
131 if code.empty?
132 target.puts "/*** #{partial} ***/"
133 target << indentation
134 end
135
136 unless indentation.empty?
137 rendered_partial.gsub!(/\n([^\n])/, "\n#{indentation}\\1")
138 end
139
99a4a86 @mislav support Haml partials and extract the update form markup
authored
140 target.puts rendered_partial
5c094fb @mislav javascript templates can also include templates
authored
141 else
142 if code.empty?
143 target.puts "/*** NOT FOUND: #{partial} ***/"
144 else
145 target.puts "null // NOT FOUND: #{partial}"
146 end
147 end
148 else
149 target << line
150 end
28744c0 @mislav extract polling, inline reply and friends logic
authored
151 end
4ff25f4 @mislav thor gm:check
authored
152 end
5c094fb @mislav javascript templates can also include templates
authored
153
154 def javascript_string(string)
155 string = string.gsub('\\', '\\\\').gsub(/\n+/, "\\\n").gsub('"', '\"')
156 %["#{string}"]
157 end
158 end
159
160 File.class_eval do
161 def extension
162 @extension ||= path && path =~ /\.(\w{2,5})$/ && $1
163 end
4ff25f4 @mislav thor gm:check
authored
164 end
Something went wrong with that request. Please try again.