class CrxMake

Constants

EXT_VERSION

this is chromium extension version

KEY

CERT_PUBLIC_KEY_INFO struct

KEY_SIZE
MAGIC

thx masover

VERSION

Public Class Methods

new(opt) click to toggle source
   # File lib/crxmake.rb
32 def initialize opt
33   @opt = opt
34 end

Private Class Methods

make(opt) click to toggle source
    # File lib/crxmake.rb
240 def make opt
241   new(opt).make
242 end
zip(opt) click to toggle source
    # File lib/crxmake.rb
244 def zip opt
245   new(opt).zip
246 end

Public Instance Methods

make() click to toggle source
   # File lib/crxmake.rb
36 def make
37   check_valid_option @opt
38   if @pkey
39     read_key
40   else
41     generate_key
42   end
43   zip_buffer = create_zip
44   sign_zip(zip_buffer)
45   write_crx(zip_buffer)
46 ensure
47   #remove_zip
48 end
zip() click to toggle source
   # File lib/crxmake.rb
50 def zip
51   check_valid_option_zip @opt
52   unless @pkey
53     generate_key
54     @pkey = @pkey_o
55   end
56   #remove_zip
57   zip_buffer = create_zip do |zip|
58     puts "include pem key: \"#{@pkey}\"" if @verbose
59     zip.add('key.pem', @pkey)
60   end
61   File.open(@zip,'wb'){|f|f<<zip_buffer}
62 end

Private Instance Methods

check_valid_option(o) click to toggle source
    # File lib/crxmake.rb
 65   def check_valid_option o
 66     @exdir, @pkey, @pkey_o, @crx, @verbose, @ignorefile, @ignoredir = o[:ex_dir], o[:pkey], o[:pkey_output], o[:crx_output], o[:verbose], o[:ignorefile], o[:ignoredir]
 67     @exdir = File.expand_path(@exdir) if @exdir
 68     raise "extension dir not exist" if !@exdir || !File.exist?(@exdir) || !File.directory?(@exdir)
 69     @pkey = File.expand_path(@pkey) if @pkey
 70     raise "private key not exist" if @pkey && (!File.exist?(@pkey) || !File.file?(@pkey))
 71     if @pkey_o
 72       @pkey_o = File.expand_path(@pkey_o)
 73       raise "private key output path is directory" if File.directory?(@pkey_o)
 74     else
 75       count = 0
 76       loop do
 77         if count.zero?
 78           @pkey_o = File.expand_path("./#{File.basename(@exdir)}.pem")
 79         else
 80           @pkey_o = File.expand_path("./#{File.basename(@exdir)}-#{count+=1}.pem")
 81         end
 82         break unless File.directory?(@pkey_o)
 83       end
 84     end
 85     if @crx
 86       @crx = File.expand_path(@crx)
 87       raise "crx path is directory" if File.directory?(@crx)
 88     else
 89       count = 0
 90       loop do
 91         if count.zero?
 92           @crx = File.expand_path("./#{File.basename(@exdir)}.crx")
 93         else
 94           @crx = File.expand_path("./#{File.basename(@exdir)}-#{count+=1}.crx")
 95         end
 96         break unless File.directory?(@crx)
 97       end
 98     end
 99     puts <<-EOS if @verbose
100 crx output dir: \"#{@crx}\"
101 ext dir: \"#{@exdir}\"
102     EOS
103     @zip = File.join(File.dirname(@crx), 'extension.zip')
104   end
check_valid_option_zip(o) click to toggle source
    # File lib/crxmake.rb
106   def check_valid_option_zip o
107     @exdir, @pkey, @pkey_o, @zip, @verbose, @ignorefile, @ignoredir = o[:ex_dir], o[:pkey], o[:pkey_output], o[:zip_output], o[:verbose], o[:ignorefile], o[:ignoredir]
108     @exdir = File.expand_path(@exdir) if @exdir
109     raise "extension dir not exist" if !@exdir || !File.exist?(@exdir) || !File.directory?(@exdir)
110     @pkey = File.expand_path(@pkey) if @pkey
111     raise "private key not exist" if @pkey && (!File.exist?(@pkey) || !File.file?(@pkey))
112     if @pkey_o
113       @pkey_o = File.expand_path(@pkey_o)
114       raise "private key output path is directory" if File.directory?(@pkey_o)
115     else
116       count = 0
117       loop do
118         if count.zero?
119           @pkey_o = File.expand_path("./#{File.basename(@exdir)}.pem")
120         else
121           @pkey_o = File.expand_path("./#{File.basename(@exdir)}-#{count+=1}.pem")
122         end
123         break unless File.directory?(@pkey_o)
124       end
125     end
126     if @zip
127       @zip = File.expand_path(@zip)
128       raise "crx path is directory" if File.directory?(@zip)
129     else
130       count = 0
131       loop do
132         if count.zero?
133           @zip = File.expand_path("./#{File.basename(@exdir)}.zip")
134         else
135           @zip = File.expand_path("./#{File.basename(@exdir)}-#{count+=1}.zip")
136         end
137         break unless File.directory?(@zip)
138       end
139     end
140     puts <<-EOS if @verbose
141 zip output dir: \"#{@zip}\"
142 ext dir: \"#{@exdir}\"
143     EOS
144   end
create_zip() { |zip| ... } click to toggle source
    # File lib/crxmake.rb
166   def create_zip
167     puts "create zip" if @verbose
168     buffer = Zip::File.add_buffer do |zip|
169       Find.find(@exdir) do |path|
170         unless path == @exdir
171           if File.directory?(path)
172             if @ignoredir && File.basename(path) =~ @ignoredir
173               puts "ignore dir: \"#{path}\"" if @verbose
174               Find.prune
175             else
176               puts "include dir: \"#{path}\"" if @verbose
177               zip.mkdir(get_relative(@exdir, path))
178             end
179           else
180             if @ignorefile && File.basename(path) =~ @ignorefile
181               puts "ignore file: \"#{path}\"" if @verbose
182             else
183               puts "include file: \"#{path}\"" if @verbose
184               zip.add(get_relative(@exdir, path), path)
185             end
186           end
187         end
188       end
189       yield zip if block_given?
190     end
191     puts <<-EOS if @verbose
192 create zip...done
193 zip file at \"#{@zip}\"
194     EOS
195     return buffer.string
196   end
generate_key() click to toggle source
    # File lib/crxmake.rb
153 def generate_key
154   if defined?($pkcs8_warning)&&@verbose
155     $stderr.puts 'Warn: generated pem must be converted into PKCS8 in order to upload to Chrome WebStore.'
156     $stderr.puts 'To suppress this message, do: gem install openssl_pkcs8'
157   end
158   puts "generate pemkey to  \"#{@pkey_o}\"" if @verbose
159   @key = OpenSSL::PKey::RSA.generate(KEY_SIZE)
160   # save key
161   File.open(@pkey_o, 'wb') do |file|
162     file << @key.to_pem
163   end
164 end
get_relative(base, target) click to toggle source
    # File lib/crxmake.rb
198 def get_relative base, target
199   Pathname.new(target.to_s).relative_path_from(Pathname.new(base.to_s)).to_s
200 end
read_key() click to toggle source
    # File lib/crxmake.rb
146 def read_key
147   puts "read pemkey: \"#{@pkey}\"" if @verbose
148   File.open(@pkey, 'rb') do |io|
149     @key = OpenSSL::PKey::RSA.new(io.read)
150   end
151 end
sign_zip(zip_buffer) click to toggle source
    # File lib/crxmake.rb
202 def sign_zip(zip_buffer)
203   puts "sign zip" if @verbose
204   plain = nil
205   #File.open(@zip, 'rb') do |file|
206   #  plain = file.read
207   #end
208   plain = zip_buffer
209   @sig = @key.sign(OpenSSL::Digest::SHA1.new, plain)
210 end
to_sizet(num) click to toggle source
    # File lib/crxmake.rb
231 def to_sizet num
232   return [num].pack('V')
233 end
write_crx(zip_buffer) click to toggle source
    # File lib/crxmake.rb
212 def write_crx(zip_buffer)
213   print "write crx..." if @verbose
214   key = @key.public_key.to_der
215   key.index(KEY) != 0 and key = KEY + key
216   File.open(@crx, 'wb') do |file|
217     file << MAGIC
218     file << EXT_VERSION
219     file << to_sizet(key.size)
220     file << to_sizet(@sig.size)
221     file << key
222     file << @sig
223     #File.open(@zip, 'rb') do |zip|
224     #  file << zip.read
225     #end
226     file << zip_buffer
227   end
228   puts "done at \"#{@crx}\"" if @verbose
229 end