forked from tomahawk-player/tomahawk-resolvers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeaxe.rb
executable file
·172 lines (148 loc) · 5.3 KB
/
makeaxe.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env ruby
# === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
#
# Copyright 2013, Teo Mrnjavac <[email protected]>
#
# Tomahawk is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Tomahawk is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
#
# This script reads a json metadata/manifest file and creates a Tomahawk
# resolver bundle (axe).
# The script should be executed with the top-level resolver directory path as
# parameter, and expects the following structure:
# Mandatory:
# content/
# + metadata.json
# Suggested:
# content/
# + metadata.json
# + contents/
# + code/
# + <resolver script>.js
# + config.ui
# + <everything else>
# + images/
# + icon.png
#
require 'rubygems'
require 'json'
require 'zipruby'
require 'digest/md5'
BUNDLEVERSION = 1 #might never be used but best to plan ahead
def usage
puts "This script creates a Tomahawk resolver bundle."
puts "\nMake sure you have the zipruby gem."
puts "\nUsage: ruby makeaxe.rb path_to_resolver_directory [options]"
puts " --release\tskip trying to add the git revision hash to the bundle"
puts " --help\t\tthis help message"
end
if ARGV.length < 1 or not ARGV.delete( "--help" ).nil?
usage
exit
end
if not ARGV.delete( "--release" ).nil?
release = true
else
release = false
end
inputPath = File.absolute_path( ARGV[0] )
if not Dir.exists?( inputPath )
puts "Bad input directory path."
exit
end
metadataRelPath = "content/metadata.json"
metadataPath = File.join( inputPath, metadataRelPath )
if not File.exists?( metadataPath ) or not File.readable?( metadataPath )
puts "Cannot find metadata file."
puts "Make sure #{metadataRelPath} exists and is readable."
exit
end
metadataFile = File.open( metadataPath, 'r' )
metadataString = metadataFile.read
metadata = JSON.parse( metadataString )
metadataFile.close unless metadataFile == nil
if not metadata["pluginName"].nil? and
not metadata["name"].nil? and
not metadata["version"].nil? and
not metadata["description"].nil? and
not metadata["type"].nil? and
not metadata["manifest"].nil? and
not metadata["manifest"]["main"].nil? and
not metadata["manifest"]["icon"].nil?
outputPath = File.join( inputPath, metadata["pluginName"] + "-" + metadata["version"] + ".axe" )
puts "Bundle metadata looks ok."
else
puts "Bad metadata file."
exit
end
# Let's add some stuff to the metadata file, this is information that's much
# easier to fill in automatically now than manually whenever.
# * Timestamp of right now i.e. packaging time.
# * Git revision because it makes sense, especially during development.
# * Bundle format version, which might never be used but we add it just in
# case we ever need to distinguish one bundle format from another.
# We save it all as _metadata.json, which then gets added to the archive as
# metadata.json instead of the original one.
_metadataPath = File.join( inputPath, "content/_metadata.json" )
if not File.exists?( _metadataPath ) or File.writable?( _metadataPath )
File.open( _metadataPath, 'w' ) do |f|
metadata["timestamp"] = Time.now.utc.to_i
unless release
gitCmd = "git rev-parse --short HEAD 2>&1"
inGit = system( gitCmd + "&>/dev/null" ) #will return true only if we're in a repo
if inGit
revision = %x[ #{gitCmd} ].sub( "\n", "" )
metadata["revision"] = revision
end
end
metadata["bundleVersion"] = BUNDLEVERSION
f.write( JSON.pretty_generate( metadata ) )
end
end
# Let's do some zipping according to the manifest.
filesToZip = []
begin
m = metadata["manifest"]
filesToZip << File.join( "content", m["main"] )
m["scripts"].each do |s|
filesToZip << File.join( "content", s )
end
filesToZip << File.join( "content", m["icon"] )
if not m["iconBackground"].nil?
filesToZip << File.join( "content", m["iconBackground"] )
end
if not m["iconWhite"].nil?
filesToZip << File.join( "content", m["iconWhite"] )
end
m["resources"].each do |s|
filesToZip << File.join( "content", s )
end
end
puts "Creating package for #{metadata["name"]}: '#{File.basename( outputPath )}'."
if File.exists?( outputPath )
File.delete( outputPath )
end
Zip::Archive.open( outputPath, Zip::CREATE | Zip::TRUNC ) do |z|
filesToZip.each do |relPath|
z.add_file( relPath, File.join( inputPath, relPath ) )
end
z.add_file( metadataRelPath, _metadataPath )
end
puts "Cleaning up."
File.delete( _metadataPath )
File.open( outputPath, 'r' ) do |f|
File.open( outputPath.sub( "axe", "md5" ), 'w' ) do |g|
g.write( Digest::MD5.hexdigest( f.read ).to_s + "\t" + File.basename( outputPath ) )
end
end
puts "All done. Have a nice day."