| 
									
										
										
										
											2012-08-14 14:17:19 -07:00
										 |  |  | #!/usr/bin/env php
 | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require_once dirname(dirname(__FILE__)).'/__init_script__.php'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $args = new PhutilArgumentParser($argv); | 
					
						
							| 
									
										
										
										
											2015-05-22 17:27:56 +10:00
										 |  |  | $args->setTagline(pht('regenerate CSS sprite sheets')); | 
					
						
							| 
									
										
										
										
											2012-08-14 14:17:19 -07:00
										 |  |  | $args->setSynopsis(<<<EOHELP | 
					
						
							|  |  |  | **sprites** | 
					
						
							|  |  |  |     Rebuild CSS sprite sheets. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | EOHELP | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | $args->parseStandardArguments(); | 
					
						
							|  |  |  | $args->parse( | 
					
						
							|  |  |  |   array( | 
					
						
							| 
									
										
										
										
											2012-11-27 14:03:25 -08:00
										 |  |  |     array( | 
					
						
							|  |  |  |       'name'  => 'force', | 
					
						
							| 
									
										
										
										
											2015-05-22 17:27:56 +10:00
										 |  |  |       'help'  => pht('Force regeneration even if sources have not changed.'), | 
					
						
							| 
									
										
										
										
											2012-11-27 14:03:25 -08:00
										 |  |  |     ), | 
					
						
							| 
									
										
										
										
											2012-08-14 14:17:19 -07:00
										 |  |  |   )); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-23 16:35:39 -08:00
										 |  |  | $root = dirname(phutil_get_library_root('phabricator')); | 
					
						
							|  |  |  | $webroot = $root.'/webroot/rsrc'; | 
					
						
							| 
									
										
										
										
											2012-08-14 14:17:19 -07:00
										 |  |  | $webroot = Filesystem::readablePath($webroot); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-23 16:19:06 -08:00
										 |  |  | $generator = new CeleritySpriteGenerator(); | 
					
						
							| 
									
										
										
										
											2012-10-06 16:21:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-27 14:03:25 -08:00
										 |  |  | $sheets = array( | 
					
						
							|  |  |  |   'menu' => $generator->buildMenuSheet(), | 
					
						
							| 
									
										
										
										
											2013-02-15 07:47:14 -08:00
										 |  |  |   'tokens' => $generator->buildTokenSheet(), | 
					
						
							| 
									
										
										
										
											2013-06-11 10:22:09 -07:00
										 |  |  |   'login' => $generator->buildLoginSheet(), | 
					
						
							| 
									
										
										
										
											2012-11-27 14:03:25 -08:00
										 |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-07 13:36:35 -08:00
										 |  |  | list($err) = exec_manual('optipng'); | 
					
						
							|  |  |  | if ($err) { | 
					
						
							|  |  |  |   $have_optipng = false; | 
					
						
							|  |  |  |   echo phutil_console_format( | 
					
						
							| 
									
										
										
										
											2015-05-22 17:27:56 +10:00
										 |  |  |     "<bg:red> %s </bg> %s\n%s\n", | 
					
						
							|  |  |  |     pht('WARNING'), | 
					
						
							|  |  |  |     pht('`%s` not found in PATH.', 'optipng'), | 
					
						
							|  |  |  |     pht('Sprites will not be optimized! Install `%s`!', 'optipng')); | 
					
						
							| 
									
										
										
										
											2012-12-07 13:36:35 -08:00
										 |  |  | } else { | 
					
						
							|  |  |  |   $have_optipng = true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-27 14:03:25 -08:00
										 |  |  | foreach ($sheets as $name => $sheet) { | 
					
						
							| 
									
										
										
										
											2013-05-18 10:34:10 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   $sheet->setBasePath($root); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-27 14:03:25 -08:00
										 |  |  |   $manifest_path = $root.'/resources/sprite/manifest/'.$name.'.json'; | 
					
						
							|  |  |  |   if (!$args->getArg('force')) { | 
					
						
							|  |  |  |     if (Filesystem::pathExists($manifest_path)) { | 
					
						
							|  |  |  |       $data = Filesystem::readFile($manifest_path); | 
					
						
							| 
									
										
										
										
											2015-05-05 20:20:11 +10:00
										 |  |  |       $data = phutil_json_decode($data); | 
					
						
							| 
									
										
										
										
											2012-11-27 14:03:25 -08:00
										 |  |  |       if (!$sheet->needsRegeneration($data)) { | 
					
						
							|  |  |  |         continue; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   $sheet | 
					
						
							|  |  |  |     ->generateCSS($webroot."/css/sprite-{$name}.css") | 
					
						
							|  |  |  |     ->generateManifest($root."/resources/sprite/manifest/{$name}.json"); | 
					
						
							| 
									
										
										
										
											2012-12-07 13:35:49 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   foreach ($sheet->getScales() as $scale) { | 
					
						
							|  |  |  |     if ($scale == 1) { | 
					
						
							|  |  |  |       $sheet_name = "sprite-{$name}.png"; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       $sheet_name = "sprite-{$name}-X{$scale}.png"; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-12-07 13:36:35 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $full_path = "{$webroot}/image/{$sheet_name}"; | 
					
						
							|  |  |  |     $sheet->generateImage($full_path, $scale); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ($have_optipng) { | 
					
						
							| 
									
										
										
										
											2015-05-22 17:27:56 +10:00
										 |  |  |       echo pht('Optimizing...')."\n"; | 
					
						
							| 
									
										
										
										
											2012-12-07 13:36:35 -08:00
										 |  |  |       phutil_passthru('optipng -o7 -clobber %s', $full_path); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-12-07 13:35:49 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-11-27 14:03:25 -08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2012-08-14 14:17:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-22 17:27:56 +10:00
										 |  |  | echo pht('Done.')."\n"; |