kachkaev / assets-version-bundle
自动化更新Symfony2 & Symfony3项目中资源版本的过程
Requires
- php: >=5.3.2
- symfony/framework-bundle: ~2.3|~3.0
Requires (Dev)
- satooshi/php-coveralls: ~1.0
- symfony/filesystem: ~2.3|~3.0
- symfony/finder: ~2.3|~3.0
Suggests
- symfony/assetic-bundle: For combining and compressing assets
This package is not auto-updated.
Last update: 2020-11-27 19:18:31 UTC
README
每次部署时手动更新资源版本真的很痛苦。这个Symfony2 & Symfony3包自动化了这个过程,因此让你的生活更加愉快。
该包可以从Symfony控制台读取和写入app/config/parameters.yml
(或任何其他*.yml
文件)中的assets_version
参数。原始文件格式被仔细保留,所以如果存在的话,你不会丢失参数组之间的注释或空行。
想象一下你的项目配置如下
app/config/config.yml
# Symfony >=2.7, >=3.0 framework: # ... assets: version: "%assets_version%" # Symfony <=2.6 framework: # ... templating: { engines: ['twig'], assets_version: "%assets_version%" } # ...
app/config/parameters.yml
parameters: # ... assets_version: v042 # ...
你只需调用bin/console assets-version:increment
,v042
将变为v043
,所有资源都将获得新的URL:my_cosy_homepage.css?v042
→ my_cosy_homepage.css?v043
。更多功能将在下面描述。
更新资源版本后,清除prod
缓存对于更改生效很重要(就像其他任何应用程序参数一样)。
为项目资源进行版本控制是一种常见的良好做法。更多关于assets_version
参数的信息可以在Symfony文档中找到
https://symfony.com.cn/doc/2.6/reference/configuration/framework.html#assets-version
安装
运行composer require kachkaev/assets-version-bundle
在app/AppKernel.php
中注册该包
$bundles = array( // ... new Kachkaev\AssetsVersionBundle\KachkaevAssetsVersionBundle(), // ... );
对安装第三方包不熟悉?Symfony文档会帮助您
https://symfony.com.cn/doc/current/cookbook/bundles/installation.html
配置
以下是包的默认配置
kachkaev_assets_version: # path to the file that contains the assets version parameter file_path: '%kernel.root_dir%/config/parameters.yml' # name of the parameter to work with parameter_name: assets_version # name of the class that reads and writes the assets version parameter manager: Kachkaev\AssetsVersionBundle\AssetsVersionManager
对于这些值生效,您不需要在app/config/config.yml
中添加任何内容。
选项1(简单):资源版本控制在服务器上完成
如果您没有使用AsseticBundle压缩CSS和JS文件,或者您在生产服务器上调用assetic:dump
,您通常不希望assets_version
的变化显示在您的git仓库中。这时,您只需做以下操作
- 修改您的本地副本
parameters.yml.dist
和parameters.yml
parameters: # ... assets_version: v000
-
在
app/config/config.yml
中启用%assets_version%
(请参阅此文件顶部) -
提交并推送本地更改(这还将包括对
app/AppKernel.php
的新条目和对composer.json
和composer.lock
的编辑) -
转到服务器,执行
git pull
和composer install
。由于您在parameters.yml.dist
中有一个新条目,您将需要确认是否要将assets_version: v000
复制到app/config/parameters.yml
。您应该这样做。按回车键。 -
大功告成!现在,每次您想要更新资源版本时,只需在服务器上调用以下命令
bin/console assets-version:increment --env=prod
bin/console cache:clear --env=prod
# bin/console assetic:dump --env=prod # if you are using assetic
注意:如果您尚未切换到Symfony3,请将bin/console
替换为app/console
。
选项 2(推荐):资源版本控制位于源代码管理中
如果你的应用程序运行在多个生产服务器上,或者你有大量的 CSS 和 JS 需要使用 assetic:dump
进行压缩,那么将编译后的资源和它们的版本保存在项目的 git 仓库中将非常有用。虽然部署准备需要更多一些时间,但其他操作几乎可以瞬间完成。你不需要在托管上使用 UglifyCSS、UglifyJS 或其他 assetic 过滤器,并且可以瞬间切换到任何稳定的项目版本。便宜的服务器在编译资源时可能会遇到困难,因为这通常需要大量的处理器时间,因此你还可以避免这种潜在问题。
由于 app/config/parameters.yml
列在 .gitignore
中,所以 assets_version
应该保存在其他位置。
- 创建
app/config/assets_version.yml
并从app/config/config.yml
中链接到它
app/config/assets_version.yml
parameters: assets_version: v000
app/config/config.yml
imports: - { resource: assets_version.yml } # ...
不要 将 app/config/assets_version.yml
添加到 .gitignore
中!
-
在
app/config/config.yml
中启用%assets_version%
(请参阅此文件顶部) -
将以下行添加到
app/config/config.yml
kachkaev_assets_version: file_path: "%kernel.root_dir%/config/assets_version.yml"
- 就是这样,你现在可以提交你的内容了!现在每次你想在服务器上更新资源时,请遵循以下常规操作
在本地机器上
bin/console assets-version:increment
bin/console cache:clear --env=prod
bin/console assetic:dump --env=prod
git commit # if you are doing this from a shell
在生产服务器上
bin/console cache:clear --env=prod git pull
确保编译后的资源不在 .gitignore
中!
提示:通过在 shell 脚本中保存常见的命令序列,可以少打字多做事。示例
bin/refresh_prod
(在本地机器上使用)
#!/bin/sh PROJECT_DIR=$( cd "$( dirname "$0" )" && pwd )/.. if [ "$1" = 'v' ]; then $PROJECT_DIR/bin/console assets-version:increment --env=prod fi $PROJECT_DIR/bin/console cache:clear --env=prod # rm $PROJECT_DIR/web/compiled_assets/* $PROJECT_DIR/bin/console assetic:dump --env=prod cat $PROJECT_DIR/app/config/assets_version.yml
bin/update_from_repo
(在服务器上使用)
#!/bin/sh PROJECT_DIR=$( cd "$( dirname "$0" )" && pwd )/.. cd $PROJECT_DIR & git pull cd $PROJECT_DIR & composer install --prefer-dist --optimize-autoloader rm -rf $PROJECT_DIR/app/cache/prod rm -rf $PROJECT_DIR/app/cache/dev $PROJECT_DIR/bin/console cache:clear --env=prod
控制台命令
该包向 symfony 控制台添加了两个命令:assets-version:increment
和 assets-version:set
。
使用示例
# Increments assets version by 1 (e.g. was v1, became v2; was 0042, became 0043 - leading letters and zeros are kept) bin/console assets-version:increment # Increments assets version by 10 (e.g. was v1, became v11; was 0042, became 0052) bin/console assets-version:increment 10 # Sets version to "1970-01-01_0000" bin/console assets-version:set 1970-01-01_0000 # Sets version to "abcDEF-something_else" (no numeric part, so assets_version:increment will stop working) bin/console assets-version:set abcDEF-something_else # Decrements assets version by 10 (e.g. was 0052, became 0042; was lorem.ipsum.0.15, became lorem.ipsum.0.5) # Note two dashes before the argument that prevent symfony from parsing -1 as an option name bin/console assets-version:increment -- -10 # Decrementing version by a number bigger than current version results 0 (e.g. was v0010, became v0000) bin/console assets-version:increment -- -1000
资源版本的值必须仅由字母、数字以及以下字符组成:.-_
。只有当前参数值为整数或以数字结尾时,递增才有效。
请勿忘记通过调用 bin/console cache:clear --env=prod
清除缓存,以便在生产环境中生效。
Capifony 集成
如果你使用 Capifony,你可以在 deploy.rb
中放置以下代码来自动化部署期间 assets_version
的递增。
before "symfony:cache:warmup", "assets_version:increment", "symfony:cache:clear" namespace :assets_version do task :increment do capifony_pretty_print "--> Increase assets_version" run "#{latest_release}/bin/console assets-version:increment --env=#{symfony_env_prod}" capifony_puts_ok end end