Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 112 lines (94 sloc) 2.854 kb
4ff25f4 @mislav thor gm:check
authored
1 #!/usr/bin/env ruby
2 require 'net/http'
3 require 'curl'
4 require 'cgi'
a8db216 @mislav extracted all Endless Tweets styles to an external .sass file
authored
5 require 'sass'
4ff25f4 @mislav thor gm:check
authored
6
7 class Gm < Thor
908fe78 @mislav thor gm:build task for building *.user.js files from source
authored
8 desc 'build', %(Builds the *.user.js files from *.js sources)
9 def build
10 for name in self.class.scripts.keys
11 source = File.open("#{name}/#{name}.js", 'r')
12 target = File.open("#{name}/#{name}.user.js", 'w')
13
14 for line in source
15 case line
8117be2 @mislav change partial that the path is relative to the script directory
authored
16 when %r{(\s*)//= ([\w/.]+)}
17 indentation, partial_name = $1, $2
18 partial = normalize_partial_path(partial_name, name)
19 target.puts "/*** #{partial} ***/"
20 target.write read_partial(partial, indentation)
908fe78 @mislav thor gm:build task for building *.user.js files from source
authored
21 else
22 target.write line
23 end
24 end
25
26 source.close
27 target.close
28 puts target.path
29 end
30 end
31
09ad56a @mislav add description to gm:check Thor task so it shown up in thor -T
authored
32 desc 'check', %(Checks scriptLength property on both local and remote file)
4ff25f4 @mislav thor gm:check
authored
33 def check
34 Net::HTTP.start('userscripts.org') do |http|
908fe78 @mislav thor gm:build task for building *.user.js files from source
authored
35 for name, id in self.class.scripts
4ff25f4 @mislav thor gm:check
authored
36 req = Net::HTTP::Head.new script_path(id)
37 res = http.request(req)
38 remote_size = res.content_length
39
40 file = script_file(name)
41 local_size = File.stat(file).size
42
43 hardcoded_size = nil
44
45 File.open(file) do |script|
46 script.each do |line|
47 if line =~ /\bscriptLength = (\d+)\b/
48 hardcoded_size = $1.to_i
49 end
50 end
51 end
52
53 puts "#{name} -- remote: #{remote_size}, local: #{local_size}, hardcoded: #{hardcoded_size}"
54 end
55 end
56 end
57
58 def upload(name)
59 id = self.class.scripts[name]
60 raise ArgumentError, "cannot find script '#{name}'" unless id
61
62 print 'UserScripts.org login ("email:pass"): '
63 STDIN.gets
64 auth = $_.chomp.split(':')
65 encoded_auth = auth.map { |i| CGI::escape i }
66
67 # FIXME: this is no work! (prolly because of CSRF protection)
68 c = Curl::Easy.new("http://#{encoded_auth.join(':')}@userscripts.org/scripts/update_src/#{id}")
69 c.multipart_form_post = true
70 c.http_post(
71 Curl::PostField.content('which_source', 'file'),
72 Curl::PostField.file('file[src]', script_file(name))
73 )
74 end
75
8117be2 @mislav change partial that the path is relative to the script directory
authored
76 def self.scripts
77 @@scripts ||= {
78 'endless_tweets' => 24398
79 }
80 end
81
82 private
83
4ff25f4 @mislav thor gm:check
authored
84 def script_path(id)
85 "/scripts/source/#{id}.user.js"
86 end
87
88 def script_file(name)
89 "#{name}/#{name}.user.js"
90 end
91
8117be2 @mislav change partial that the path is relative to the script directory
authored
92 def normalize_partial_path(path, script_name)
93 if path.index('/')
94 path
95 else
96 "#{script_name}/#{path}"
97 end
98 end
99
100 def read_partial(path, indentation = nil)
101 source = File.read(path)
a8db216 @mislav extracted all Endless Tweets styles to an external .sass file
authored
102 extension = path =~ /\.(\w{2,5})$/ && $1
103
104 case extension
105 when 'sass'
106 css = Sass::Engine.new(source, :style => :compact).to_css
107 %[addCSS("#{css.gsub(/\n+/, "\\\n").gsub('"', '\"')}")\n]
108 else
109 source
110 end
4ff25f4 @mislav thor gm:check
authored
111 end
112 end
Something went wrong with that request. Please try again.