-
Notifications
You must be signed in to change notification settings - Fork 0
/
pack.php
executable file
·354 lines (268 loc) · 8.88 KB
/
pack.php
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
$projDir = dirname( $_SERVER['argv'][0] );
$configPath = "{$projDir}/config.json";
$projectPath = "{$projDir}/project.json";
if ( !is_readable( "{$projDir}/custom.php" ) )
{
IO::STDERR( "Missing required custom.php library!\n" );
exit( 1 );
}
if ( !is_readable( $configPath ) )
{
IO::STDERR( "Required configuration file \"config.json\" is not found!\n" );
exit( 1 );
}
require_once "{$projDir}/custom.php";
$config = PBTXJSON::FromJSON( $configPath, TRUE );
$project = @json_decode( @file_get_contents($projectPath), TRUE );
if ( $project !== NULL )
{
// Overwriting template information with project content
if ( ($value = trim(@"{$project[ 'project_id' ]}")) != "" )
{
IO::STDOUT( "App Id... done!\n" );
$config->attr( 'id', $value );
$config->addChild( $_obj = new PBTXJSON( 'preference' ) );
$_obj->attr( 'name', 'App-Identifier' );
$_obj->attr( 'value', $value );
}
if ( ($value = trim(@"{$project[ 'version' ]}")) != "" )
{
IO::STDOUT( "App Version... " );
if ( preg_match( '/^(\d).(\d).(\d)(-(\d+)){0,1}$/', $value, $matches ) )
{
$matches[4] = ( count($matches) > 4 ) ? $matches[5] : 0;
foreach( $matches as &$value ) $value = @intval($value);
$config->attr( 'version', "{$matches[1]}.{$matches[2]}.{$matches[3]}" );
$config->attr( 'android-versionCode', ($matches[1] * 10000 + $matches[2] * 100 + $matches[3]) * 100 + $matches[4] );
$config->attr( 'ios-CFBundleVersion', "{$matches[1]}.{$matches[2]}." . ($matches[3] * 100 + $matches[4]) );
$config->addChild( $_obj = new PBTXJSON( 'preference' ) );
$_obj->attr( 'name', 'AppVersion' );
$_obj->attr( 'value', "{$matches[1]}.{$matches[2]}.{$matches[3]}-{$matches[4]}" );
IO::STDOUT( "done!\n" );
}
else
IO::STDOUT( "Version is malformed! Skipping...\n" );
}
if ( ($value = trim(@"{$project[ 'name' ]}")) != "" )
{
IO::STDOUT( "App Name... done\n" );
$_locked = NULL;
foreach( $config->children as &$_child )
{
if ( $_child->name == "name" ) $_locked = $_child;
}
if ( !$_locked )
$config->addChild( $_locked = new PBTXJSON( 'name' ) );
$_locked->children = $value;
}
if ( ($value = trim(@"{$project[ 'description' ]}")) != "" )
{
IO::STDOUT( "App Description... done\n" );
$_locked = NULL;
foreach( $config->children as &$_child )
{
if ( $_child->name == "description" ) $_locked = $_child;
}
if ( !$_locked )
$config->addChild( $_locked = new PBTXJSON( 'description' ) );
$_locked->children = $value;
}
// Adding project specified configurations
if ( is_array( $project[ 'expansion' ] ) )
{
IO::STDOUT( "Copy additional configurations\n" );
foreach( $project[ 'expansion' ] as $exp ) $config->addChild( PBTXJSON::FromJSON( $exp ) );
}
// Generate Image Resources
$hasIcon = is_readable( $iconPath = "{$projDir}/{$project['icon']}" );
$hasSplash = is_readable( $splashPath = "{$projDir}/{$project['splash']}" );
if ( $hasIcon || $hasSplash )
{
IO::STDOUT( "Processing image resources...\n" );
foreach( $config->children as $_confElement )
{
if ( $_confElement->name !== "platform" ) continue;
foreach( $_confElement->children as $_config )
{
@mkdir( dirname($_config->attr( 'src' )), 0755, TRUE );
if ( $hasIcon && ($_config->name == "icon") )
ConvertImage( $iconPath, $_config->attr( 'src' ), $_config->attr( 'width' ), $_config->attr( 'height' ) );
else
if ( $hasSplash && ($_config->name == "splash") )
ConvertImage( $splashPath, $_config->attr( 'src' ), $_config->attr( 'width' ), $_config->attr( 'height' ) );
}
}
}
// Copy project specified resources
if ( is_array( $project[ 'files' ] ) )
{
IO::STDOUT( "Processing project dependent files...\n" );
foreach ( $project[ 'files' ] as $file )
{
if ( $file[ 'op' ] == "copy" )
{
$sourcePath = "{$projDir}/{$file[ 'src' ]}";
$destPath = "{$projDir}/{$file[ 'dest' ]}";
$destDir = dirname( $destPath );
if ( !is_file( $sourcePath ) ) continue;
@mkdir( $destDir, 0755, TRUE );
copy( $sourcePath, $destPath );
}
}
}
}
$config->xml( "{$projDir}/config.xml" );
TIDY( "{$projDir}/config.xml" );
class PBTXJSON
{
const DEFAULT_NODE_NAME = "PBTXJSON";
public static function FromJSON( $json, $isFile = FALSE ) {
// Load json from file
if ( $isFile ) {
$json = @json_decode( @file_get_contents($json), TRUE );
if ( $json === NULL ) return NULL;
}
// Check input data
if ( is_a($json, stdClass::class) ) $json = (array)$json;
if ( !is_array( $json ) ) return NULL;
// Create new root element
$rootObject = new PBTXJSON( @$json[ '@@node' ] );
// Add attribute
$attributes = &$rootObject->_attributes;
foreach( $json as $attr => $value )
{
if ( $attr == "@@children" ) continue;
$attributes[ $attr ] = $value;
}
// Add children
$children = @$json[ '@@children' ];
if ( is_array($children) )
{
foreach( $json[ '@@children' ] as $child )
{
if ( !is_array($child) ) continue;
$rootObject->_children[] = self::FromJSON( $child );
}
}
else
if ( $children !== NULL )
{
$rootObject->_children = @$json[ '@@children' ];
}
return $rootObject;
}
private $_nodeName = self::DEFAULT_NODE_NAME;
private $_children = NULL;
private $_attributes = [];
public function __construct( $tagName = self::DEFAULT_NODE_NAME ) {
$this->_nodeName = @"{$tagName}";
if (empty( $this->_nodeName )) $this->_nodeName = self::DEFAULT_NODE_NAME;
}
public function __set( $name, $value ) {
switch( $name )
{
case "children":
$this->_children = $value;
break;
case "attributes":
if ( is_array($value) ) $this->_attributes = $value;
break;
case "name":
$this->_nodeName = "{$value}";
break;
}
}
public function& __get( $name ) {
switch( $name )
{
case "children":
return $this->_children;
case "attributes":
return $this->_attributes;
case "name";
$name = $this->_nodeName;
return $name;
}
return NULL;
}
public function attr( $name, $value = NULL ) {
if ( func_num_args() < 2 ) return @$this->_attributes[ $name ];
if ( $value === NULL )
unset( $this->_attributes[ $name ] );
else
$this->_attributes[ $name ] = $value;
return TRUE;
}
public function addChild( PBTXJSON $child ) {
if ( !is_array($this->_children) )
$this->_children = [];
$this->_children[] = $child;
}
public function xml( $fileName = NULL ) {
$content = "<?xml version='1.0' encoding='utf-8'?>" . self::__renderXMLNode( $this );
return empty($fileName) ? $content : file_put_contents( $fileName, $content );
}
public function json( $fileName = NULL ) {
$content = self::__renderJSONNode( $this );
if ( !empty($fileName) ) $content = json_encode( $content );
return (!is_string($fileName)) ? $content : file_put_contents( $fileName, $content );
}
private static function& __renderJSONNode( PBTXJSON &$node ) {
$object = [];
if ( is_array(@$node->_children) )
{
$object[ '@@children' ] = [];
foreach( $node->_children as $child )
{
// Ignore none PBTXJSON children
if ( !is_a($child, self::class) ) continue;
$object[ '@@children' ][] = self::__renderJSONNode( $child );
}
}
else
if ( $node->_children !== NULL )
{
$object[ '@@children' ] = $node->_children;
}
foreach( $node->_attributes as $attr => $value )
{
if ( in_array( $attr, [ '@@node', '@@children' ] ) )
continue;
$object[ $attr ] = $value;
}
return $object;
}
private static function __renderXMLNode( PBTXJSON &$node ) {
$childContent = "";
if ( !is_array(@$node->_children) )
$childContent .= @"{$node->_children}";
else
{
foreach( $node->_children as $child )
{
// Ignore none PBTXJSON children
if ( !is_a($child, self::class) ) continue;
$childContent .= self::__renderXMLNode( $child );
}
}
$attr = self::__renderXMLAttribute( $node );
if ( !empty($attr) ) $attr = " {$attr}";
$childContent = trim($childContent);
return empty($childContent) ? "<{$node->_nodeName}{$attr} />" : "<{$node->_nodeName}{$attr}>{$childContent}</{$node->_nodeName}>";
}
private static function __renderXMLAttribute( PBTXJSON &$node ) {
$attributes = [];
foreach( $node->_attributes as $attr => $value )
{
if ( substr( $attr, 0, 2 ) == "@@" ) continue;
if ( $value === NULL ) continue;
$attributes[] = "{$attr}=\"" . htmlentities($value) . "\"";
}
return implode( ' ', $attributes );
}
}
final class IO{
public static function STDOUT( $msg ){ fwrite( STDOUT, $msg ); }
public static function STDERR( $msg ){ fwrite( STDERR, $msg ); }
}