Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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