Skip to content

suggestion from #9338: add some command/option to the deploy command to refresh the version #9915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/code/Magento/Deploy/Console/DeployStaticOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ class DeployStaticOptions
*/
const CONTENT_VERSION = 'content-version';

/**
* Key for refresh content version only mode
*/
const REFRESH_CONTENT_VERSION_ONLY = 'refresh-content-version-only';

/**
* Deploy static command options list
*
Expand Down Expand Up @@ -225,6 +230,13 @@ private function getBasicOptions()
'Custom version of static content can be used if running deployment on multiple nodes '
. 'to ensure that static content version is identical and caching works properly.'
),
new InputOption(
self::REFRESH_CONTENT_VERSION_ONLY,
null,
InputOption::VALUE_NONE,
'Refreshing the version of static content only can be used to refresh static content '
. 'in browser cache and CDN cache.'
),
new InputArgument(
self::LANGUAGES_ARGUMENT,
InputArgument::IS_ARRAY,
Expand Down
25 changes: 20 additions & 5 deletions app/code/Magento/Deploy/Service/DeployStaticContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ public function __construct(
*/
public function deploy(array $options)
{
$version = !empty($options[Options::CONTENT_VERSION]) && is_string($options[Options::CONTENT_VERSION])
? $options[Options::CONTENT_VERSION]
: (new \DateTime())->getTimestamp();
$this->versionStorage->save($version);

if ($this->isRefreshContentVersionOnly($options)) {
$this->logger->warning("New content version: " . $version);
return;
}

$queue = $this->queueFactory->create(
[
'logger' => $this->logger,
Expand All @@ -96,11 +106,6 @@ public function deploy(array $options)
]
);

$version = !empty($options[Options::CONTENT_VERSION]) && is_string($options[Options::CONTENT_VERSION])
? $options[Options::CONTENT_VERSION]
: (new \DateTime())->getTimestamp();
$this->versionStorage->save($version);

$packages = $deployStrategy->deploy($options);

if ($options[Options::NO_JAVASCRIPT] !== true) {
Expand Down Expand Up @@ -135,4 +140,14 @@ private function getProcessesAmount(array $options)
{
return isset($options[Options::JOBS_AMOUNT]) ? (int)$options[Options::JOBS_AMOUNT] : 0;
}

/**
* @param array $options
* @return bool
*/
private function isRefreshContentVersionOnly(array $options)
{
return isset($options[Options::REFRESH_CONTENT_VERSION_ONLY])
&& $options[Options::REFRESH_CONTENT_VERSION_ONLY];
}
}
110 changes: 64 additions & 46 deletions app/code/Magento/Deploy/Test/Unit/Service/DeployStaticContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,18 @@ protected function setUp()
public function testDeploy($options, $expectedContentVersion)
{
$package = $this->getMock(Package::class, [], [], '', false);
$package->expects($this->exactly(1))->method('isVirtual')->willReturn(false);
$package->expects($this->exactly(3))->method('getArea')->willReturn('area');
$package->expects($this->exactly(3))->method('getTheme')->willReturn('theme');
$package->expects($this->exactly(2))->method('getLocale')->willReturn('locale');

$packages = [
'package' => $package
];
if ($options['refresh-content-version-only']) {
$package->expects($this->never())->method('isVirtual');
$package->expects($this->never())->method('getArea');
$package->expects($this->never())->method('getTheme');
$package->expects($this->never())->method('getLocale');
} else {
$package->expects($this->exactly(1))->method('isVirtual')->willReturn(false);
$package->expects($this->exactly(3))->method('getArea')->willReturn('area');
$package->expects($this->exactly(3))->method('getTheme')->willReturn('theme');
$package->expects($this->exactly(2))->method('getLocale')->willReturn('locale');
}
$packages = ['package' => $package];

if ($expectedContentVersion) {
$this->versionStorage->expects($this->once())->method('save')->with($expectedContentVersion);
Expand All @@ -132,20 +136,27 @@ public function testDeploy($options, $expectedContentVersion)
$queue = $this->getMockBuilder(Queue::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
$this->queueFactory->expects($this->once())->method('create')->willReturn($queue);
if ($options['refresh-content-version-only']) {
$this->queueFactory->expects($this->never())->method('create');
} else {
$this->queueFactory->expects($this->once())->method('create')->willReturn($queue);
}

$strategy = $this->getMockBuilder(CompactDeploy::class)
->setMethods(['deploy'])
->disableOriginalConstructor()
->getMockForAbstractClass();
$strategy->expects($this->once())->method('deploy')
->with($options)
->willReturn($packages);
$this->deployStrategyFactory->expects($this->once())
->method('create')
->with('compact', ['queue' => $queue])
->willReturn($strategy);

if ($options['refresh-content-version-only']) {
$strategy->expects($this->never())->method('deploy');
} else {
$strategy->expects($this->once())->method('deploy')
->with($options)
->willReturn($packages);
$this->deployStrategyFactory->expects($this->once())
->method('create')
->with('compact', ['queue' => $queue])
->willReturn($strategy);
}
$deployPackageService = $this->getMockBuilder(DeployPackage::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
Expand All @@ -166,34 +177,32 @@ public function testDeploy($options, $expectedContentVersion)
->disableOriginalConstructor()
->getMockForAbstractClass();

$this->objectManager->expects($this->exactly(4))
->method('create')
->withConsecutive(
[DeployPackage::class, ['logger' => $this->logger]],
[DeployRequireJsConfig::class, ['logger' => $this->logger]],
[DeployTranslationsDictionary::class, ['logger' => $this->logger]],
[Bundle::class, ['logger' => $this->logger]]
)
->willReturnOnConsecutiveCalls(
$deployPackageService,
$deployRjsConfig,
$deployI18n,
$deployBundle
);

$this->objectManager->expects($this->exactly(1))
->method('get')
->withConsecutive(
[MinifyTemplates::class]
)
->willReturnOnConsecutiveCalls(
$minifyTemplates
);

$this->assertEquals(
null,
$this->service->deploy($options)
);
if ($options['refresh-content-version-only']) {
$this->objectManager->expects($this->never())->method('create');
$this->objectManager->expects($this->never())->method('get');
} else {
$this->objectManager->expects($this->exactly(4))
->method('create')
->withConsecutive(
[DeployPackage::class, ['logger' => $this->logger]],
[DeployRequireJsConfig::class, ['logger' => $this->logger]],
[DeployTranslationsDictionary::class, ['logger' => $this->logger]],
[Bundle::class, ['logger' => $this->logger]]
)
->willReturnOnConsecutiveCalls(
$deployPackageService,
$deployRjsConfig,
$deployI18n,
$deployBundle
);

$this->objectManager->expects($this->exactly(1))
->method('get')
->withConsecutive([MinifyTemplates::class])
->willReturnOnConsecutiveCalls($minifyTemplates);
}

$this->assertEquals(null, $this->service->deploy($options));
}

public function deployDataProvider()
Expand All @@ -203,7 +212,8 @@ public function deployDataProvider()
[
'strategy' => 'compact',
'no-javascript' => false,
'no-html-minify' => false
'no-html-minify' => false,
'refresh-content-version-only' => false,
],
null // content version value should not be asserted in this case
],
Expand All @@ -212,9 +222,17 @@ public function deployDataProvider()
'strategy' => 'compact',
'no-javascript' => false,
'no-html-minify' => false,
'refresh-content-version-only' => false,
'content-version' => '123456',
],
'123456'
],
[
[
'refresh-content-version-only' => true,
'content-version' => '654321',
],
'654321'
]
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,15 @@ protected function execute(InputInterface $input, OutputInterface $output)

$options = $input->getOptions();
$options[Options::LANGUAGE] = $input->getArgument(Options::LANGUAGES_ARGUMENT) ?: ['all'];
$refreshOnly = isset($options[Options::REFRESH_CONTENT_VERSION_ONLY])
&& $options[Options::REFRESH_CONTENT_VERSION_ONLY];

$verbose = $output->getVerbosity() > 1 ? $output->getVerbosity() : OutputInterface::VERBOSITY_VERBOSE;

$logger = $this->consoleLoggerFactory->getLogger($output, $verbose);
$logger->alert(PHP_EOL . "Deploy using {$options[Options::STRATEGY]} strategy");
if (!$refreshOnly) {
$logger->alert(PHP_EOL . "Deploy using {$options[Options::STRATEGY]} strategy");
}

$this->mockCache();

Expand All @@ -135,7 +139,9 @@ protected function execute(InputInterface $input, OutputInterface $output)

$deployService->deploy($options);

$logger->alert(PHP_EOL . "Execution time: " . (microtime(true) - $time));
if (!$refreshOnly) {
$logger->alert(PHP_EOL . "Execution time: " . (microtime(true) - $time));
}

return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
}
Expand Down