From cb938d869c3f7324f9d703e7e531a7d31daaa57a Mon Sep 17 00:00:00 2001 From: Aviv Eyal Date: Sat, 3 Jun 2023 09:41:30 -0700 Subject: [PATCH] Teach `./bin/celerity` about sprite maps Summary: Add `./bin/celerity sprites`, to replace script `./scripts/celerity/generate_sprites.php`. Also make new workflow run `./bin/celerity map` at the same time. Fixes T15437. Test Plan: Changes a file that goes in the sprites, run new command Reviewers: O1 Blessed Committers, speck Reviewed By: O1 Blessed Committers, speck Subscribers: speck, tobiaswiese, valerio.bozzolan, Matthew, Cigaryno Maniphest Tasks: T15437 Differential Revision: https://we.phorge.it/D25274 --- scripts/celerity/generate_sprites.php | 80 +------------ src/__phutil_library_map__.php | 2 + ...erityManagementGenerateSpritesWorkflow.php | 108 ++++++++++++++++++ 3 files changed, 113 insertions(+), 77 deletions(-) create mode 100644 src/applications/celerity/management/CelerityManagementGenerateSpritesWorkflow.php diff --git a/scripts/celerity/generate_sprites.php b/scripts/celerity/generate_sprites.php index ccdd194b36..a85f9266ae 100755 --- a/scripts/celerity/generate_sprites.php +++ b/scripts/celerity/generate_sprites.php @@ -3,80 +3,6 @@ require_once dirname(dirname(__FILE__)).'/__init_script__.php'; -$args = new PhutilArgumentParser($argv); -$args->setTagline(pht('regenerate CSS sprite sheets')); -$args->setSynopsis(<<parseStandardArguments(); -$args->parse( - array( - array( - 'name' => 'force', - 'help' => pht('Force regeneration even if sources have not changed.'), - ), - )); - -$root = dirname(phutil_get_library_root('phabricator')); -$webroot = $root.'/webroot/rsrc'; -$webroot = Filesystem::readablePath($webroot); - -$generator = new CeleritySpriteGenerator(); - -$sheets = array( - 'tokens' => $generator->buildTokenSheet(), - 'login' => $generator->buildLoginSheet(), -); - -list($err) = exec_manual('optipng'); -if ($err) { - $have_optipng = false; - echo phutil_console_format( - " %s %s\n%s\n", - pht('WARNING'), - pht('`%s` not found in PATH.', 'optipng'), - pht('Sprites will not be optimized! Install `%s`!', 'optipng')); -} else { - $have_optipng = true; -} - -foreach ($sheets as $name => $sheet) { - - $sheet->setBasePath($root); - - $manifest_path = $root.'/resources/sprite/manifest/'.$name.'.json'; - if (!$args->getArg('force')) { - if (Filesystem::pathExists($manifest_path)) { - $data = Filesystem::readFile($manifest_path); - $data = phutil_json_decode($data); - if (!$sheet->needsRegeneration($data)) { - continue; - } - } - } - - $sheet - ->generateCSS($webroot."/css/sprite-{$name}.css") - ->generateManifest($root."/resources/sprite/manifest/{$name}.json"); - - foreach ($sheet->getScales() as $scale) { - if ($scale == 1) { - $sheet_name = "sprite-{$name}.png"; - } else { - $sheet_name = "sprite-{$name}-X{$scale}.png"; - } - - $full_path = "{$webroot}/image/{$sheet_name}"; - $sheet->generateImage($full_path, $scale); - - if ($have_optipng) { - echo pht('Optimizing...')."\n"; - phutil_passthru('optipng -o7 -clobber %s', $full_path); - } - } -} - -echo pht('Done.')."\n"; +echo pht('This script was replaced with `%s`!.', './bin/celerity sprites'); +echo "\n"; +exit(13); diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 3d8570b4f7..17560e11d1 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -318,6 +318,7 @@ phutil_register_library_map(array( 'CelerityDefaultPostprocessor' => 'applications/celerity/postprocessor/CelerityDefaultPostprocessor.php', 'CelerityHighContrastPostprocessor' => 'applications/celerity/postprocessor/CelerityHighContrastPostprocessor.php', 'CelerityLargeFontPostprocessor' => 'applications/celerity/postprocessor/CelerityLargeFontPostprocessor.php', + 'CelerityManagementGenerateSpritesWorkflow' => 'applications/celerity/management/CelerityManagementGenerateSpritesWorkflow.php', 'CelerityManagementMapWorkflow' => 'applications/celerity/management/CelerityManagementMapWorkflow.php', 'CelerityManagementSyntaxWorkflow' => 'applications/celerity/management/CelerityManagementSyntaxWorkflow.php', 'CelerityManagementWorkflow' => 'applications/celerity/management/CelerityManagementWorkflow.php', @@ -6311,6 +6312,7 @@ phutil_register_library_map(array( 'CelerityDefaultPostprocessor' => 'CelerityPostprocessor', 'CelerityHighContrastPostprocessor' => 'CelerityPostprocessor', 'CelerityLargeFontPostprocessor' => 'CelerityPostprocessor', + 'CelerityManagementGenerateSpritesWorkflow' => 'CelerityManagementWorkflow', 'CelerityManagementMapWorkflow' => 'CelerityManagementWorkflow', 'CelerityManagementSyntaxWorkflow' => 'CelerityManagementWorkflow', 'CelerityManagementWorkflow' => 'PhabricatorManagementWorkflow', diff --git a/src/applications/celerity/management/CelerityManagementGenerateSpritesWorkflow.php b/src/applications/celerity/management/CelerityManagementGenerateSpritesWorkflow.php new file mode 100644 index 0000000000..52735b00f7 --- /dev/null +++ b/src/applications/celerity/management/CelerityManagementGenerateSpritesWorkflow.php @@ -0,0 +1,108 @@ +setName('sprites') + ->setExamples('**sprites** [options]') + ->setSynopsis(pht('Rebuild CSS sprite sheets.')) + ->setArguments( + array( + array( + 'name' => 'force', + 'help' => pht('Force regeneration even no sources have changed.'), + ), + array( + 'name' => 'no-map', + 'help' => + pht( + 'Do not invoke `%s` after updating sprites', + 'celerity map'), + ), + )); + } + + public function execute(PhutilArgumentParser $args) { + $resources_map = CelerityPhysicalResources::getAll(); + + $console = PhutilConsole::getConsole(); + + $root = dirname(phutil_get_library_root('phorge')); + $webroot = $root.'/webroot/rsrc'; + $webroot = Filesystem::readablePath($webroot); + + $generator = new CeleritySpriteGenerator(); + + $sheets = array( + 'tokens' => $generator->buildTokenSheet(), + 'login' => $generator->buildLoginSheet(), + ); + + list($err) = exec_manual('optipng'); + if ($err) { + $have_optipng = false; + $console->writeErr( + " %s %s\n%s\n", + pht('WARNING'), + pht('`%s` not found in PATH.', 'optipng'), + pht('Sprites will not be optimized! Install `%s`!', 'optipng')); + } else { + $have_optipng = true; + } + + foreach ($sheets as $name => $sheet) { + + $sheet->setBasePath($root); + + $manifest_path = $root.'/resources/sprite/manifest/'.$name.'.json'; + if (!$args->getArg('force')) { + if (Filesystem::pathExists($manifest_path)) { + $data = Filesystem::readFile($manifest_path); + $data = phutil_json_decode($data); + if (!$sheet->needsRegeneration($data)) { + continue; + } + } + } + + $sheet + ->generateCSS($webroot."/css/sprite-{$name}.css") + ->generateManifest($root."/resources/sprite/manifest/{$name}.json"); + + foreach ($sheet->getScales() as $scale) { + if ($scale == 1) { + $sheet_name = "sprite-{$name}.png"; + } else { + $sheet_name = "sprite-{$name}-X{$scale}.png"; + } + + $full_path = "{$webroot}/image/{$sheet_name}"; + $sheet->generateImage($full_path, $scale); + + if ($have_optipng) { + $console->writeOut("%s\n", pht('Optimizing...')); + phutil_passthru('optipng -o7 -clobber %s', $full_path); + } + } + } + + $run_map = !$args->getArg('no-map'); + + if ($run_map) { + $console->writeOut( + "%s\n", + pht('Done generating sprites - updating map...')); + $map_flow = id($args->getWorkflows())['map']; + // this is very hacky, but it works because `map` has no arguments. + $map_flow->execute($args); + } else { + $console->writeOut("%s\n", pht('Done.')); + return 0; + } + + } + + +}