Skip to content

Commit 2f80eea

Browse files
author
Oleksii Korshenko
authored
MAGETWO-69832: suggestion from #9338: add some command/option to the deploy command to refresh the version #9915
2 parents 26a7c33 + 0130bb1 commit 2f80eea

File tree

4 files changed

+104
-53
lines changed

4 files changed

+104
-53
lines changed

app/code/Magento/Deploy/Console/DeployStaticOptions.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ class DeployStaticOptions
131131
*/
132132
const CONTENT_VERSION = 'content-version';
133133

134+
/**
135+
* Key for refresh content version only mode
136+
*/
137+
const REFRESH_CONTENT_VERSION_ONLY = 'refresh-content-version-only';
138+
134139
/**
135140
* Deploy static command options list
136141
*
@@ -225,6 +230,13 @@ private function getBasicOptions()
225230
'Custom version of static content can be used if running deployment on multiple nodes '
226231
. 'to ensure that static content version is identical and caching works properly.'
227232
),
233+
new InputOption(
234+
self::REFRESH_CONTENT_VERSION_ONLY,
235+
null,
236+
InputOption::VALUE_NONE,
237+
'Refreshing the version of static content only can be used to refresh static content '
238+
. 'in browser cache and CDN cache.'
239+
),
228240
new InputArgument(
229241
self::LANGUAGES_ARGUMENT,
230242
InputArgument::IS_ARRAY,

app/code/Magento/Deploy/Service/DeployStaticContent.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ public function __construct(
7575
*/
7676
public function deploy(array $options)
7777
{
78+
$version = !empty($options[Options::CONTENT_VERSION]) && is_string($options[Options::CONTENT_VERSION])
79+
? $options[Options::CONTENT_VERSION]
80+
: (new \DateTime())->getTimestamp();
81+
$this->versionStorage->save($version);
82+
83+
if ($this->isRefreshContentVersionOnly($options)) {
84+
$this->logger->warning("New content version: " . $version);
85+
return;
86+
}
87+
7888
$queue = $this->queueFactory->create(
7989
[
8090
'logger' => $this->logger,
@@ -96,11 +106,6 @@ public function deploy(array $options)
96106
]
97107
);
98108

99-
$version = !empty($options[Options::CONTENT_VERSION]) && is_string($options[Options::CONTENT_VERSION])
100-
? $options[Options::CONTENT_VERSION]
101-
: (new \DateTime())->getTimestamp();
102-
$this->versionStorage->save($version);
103-
104109
$packages = $deployStrategy->deploy($options);
105110

106111
if ($options[Options::NO_JAVASCRIPT] !== true) {
@@ -135,4 +140,14 @@ private function getProcessesAmount(array $options)
135140
{
136141
return isset($options[Options::JOBS_AMOUNT]) ? (int)$options[Options::JOBS_AMOUNT] : 0;
137142
}
143+
144+
/**
145+
* @param array $options
146+
* @return bool
147+
*/
148+
private function isRefreshContentVersionOnly(array $options)
149+
{
150+
return isset($options[Options::REFRESH_CONTENT_VERSION_ONLY])
151+
&& $options[Options::REFRESH_CONTENT_VERSION_ONLY];
152+
}
138153
}

app/code/Magento/Deploy/Test/Unit/Service/DeployStaticContentTest.php

Lines changed: 64 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,18 @@ protected function setUp()
114114
public function testDeploy($options, $expectedContentVersion)
115115
{
116116
$package = $this->getMock(Package::class, [], [], '', false);
117-
$package->expects($this->exactly(1))->method('isVirtual')->willReturn(false);
118-
$package->expects($this->exactly(3))->method('getArea')->willReturn('area');
119-
$package->expects($this->exactly(3))->method('getTheme')->willReturn('theme');
120-
$package->expects($this->exactly(2))->method('getLocale')->willReturn('locale');
121-
122-
$packages = [
123-
'package' => $package
124-
];
117+
if ($options['refresh-content-version-only']) {
118+
$package->expects($this->never())->method('isVirtual');
119+
$package->expects($this->never())->method('getArea');
120+
$package->expects($this->never())->method('getTheme');
121+
$package->expects($this->never())->method('getLocale');
122+
} else {
123+
$package->expects($this->exactly(1))->method('isVirtual')->willReturn(false);
124+
$package->expects($this->exactly(3))->method('getArea')->willReturn('area');
125+
$package->expects($this->exactly(3))->method('getTheme')->willReturn('theme');
126+
$package->expects($this->exactly(2))->method('getLocale')->willReturn('locale');
127+
}
128+
$packages = ['package' => $package];
125129

126130
if ($expectedContentVersion) {
127131
$this->versionStorage->expects($this->once())->method('save')->with($expectedContentVersion);
@@ -132,20 +136,27 @@ public function testDeploy($options, $expectedContentVersion)
132136
$queue = $this->getMockBuilder(Queue::class)
133137
->disableOriginalConstructor()
134138
->getMockForAbstractClass();
135-
$this->queueFactory->expects($this->once())->method('create')->willReturn($queue);
139+
if ($options['refresh-content-version-only']) {
140+
$this->queueFactory->expects($this->never())->method('create');
141+
} else {
142+
$this->queueFactory->expects($this->once())->method('create')->willReturn($queue);
143+
}
136144

137145
$strategy = $this->getMockBuilder(CompactDeploy::class)
138146
->setMethods(['deploy'])
139147
->disableOriginalConstructor()
140148
->getMockForAbstractClass();
141-
$strategy->expects($this->once())->method('deploy')
142-
->with($options)
143-
->willReturn($packages);
144-
$this->deployStrategyFactory->expects($this->once())
145-
->method('create')
146-
->with('compact', ['queue' => $queue])
147-
->willReturn($strategy);
148-
149+
if ($options['refresh-content-version-only']) {
150+
$strategy->expects($this->never())->method('deploy');
151+
} else {
152+
$strategy->expects($this->once())->method('deploy')
153+
->with($options)
154+
->willReturn($packages);
155+
$this->deployStrategyFactory->expects($this->once())
156+
->method('create')
157+
->with('compact', ['queue' => $queue])
158+
->willReturn($strategy);
159+
}
149160
$deployPackageService = $this->getMockBuilder(DeployPackage::class)
150161
->disableOriginalConstructor()
151162
->getMockForAbstractClass();
@@ -166,34 +177,32 @@ public function testDeploy($options, $expectedContentVersion)
166177
->disableOriginalConstructor()
167178
->getMockForAbstractClass();
168179

169-
$this->objectManager->expects($this->exactly(4))
170-
->method('create')
171-
->withConsecutive(
172-
[DeployPackage::class, ['logger' => $this->logger]],
173-
[DeployRequireJsConfig::class, ['logger' => $this->logger]],
174-
[DeployTranslationsDictionary::class, ['logger' => $this->logger]],
175-
[Bundle::class, ['logger' => $this->logger]]
176-
)
177-
->willReturnOnConsecutiveCalls(
178-
$deployPackageService,
179-
$deployRjsConfig,
180-
$deployI18n,
181-
$deployBundle
182-
);
183-
184-
$this->objectManager->expects($this->exactly(1))
185-
->method('get')
186-
->withConsecutive(
187-
[MinifyTemplates::class]
188-
)
189-
->willReturnOnConsecutiveCalls(
190-
$minifyTemplates
191-
);
192-
193-
$this->assertEquals(
194-
null,
195-
$this->service->deploy($options)
196-
);
180+
if ($options['refresh-content-version-only']) {
181+
$this->objectManager->expects($this->never())->method('create');
182+
$this->objectManager->expects($this->never())->method('get');
183+
} else {
184+
$this->objectManager->expects($this->exactly(4))
185+
->method('create')
186+
->withConsecutive(
187+
[DeployPackage::class, ['logger' => $this->logger]],
188+
[DeployRequireJsConfig::class, ['logger' => $this->logger]],
189+
[DeployTranslationsDictionary::class, ['logger' => $this->logger]],
190+
[Bundle::class, ['logger' => $this->logger]]
191+
)
192+
->willReturnOnConsecutiveCalls(
193+
$deployPackageService,
194+
$deployRjsConfig,
195+
$deployI18n,
196+
$deployBundle
197+
);
198+
199+
$this->objectManager->expects($this->exactly(1))
200+
->method('get')
201+
->withConsecutive([MinifyTemplates::class])
202+
->willReturnOnConsecutiveCalls($minifyTemplates);
203+
}
204+
205+
$this->assertEquals(null, $this->service->deploy($options));
197206
}
198207

199208
public function deployDataProvider()
@@ -203,7 +212,8 @@ public function deployDataProvider()
203212
[
204213
'strategy' => 'compact',
205214
'no-javascript' => false,
206-
'no-html-minify' => false
215+
'no-html-minify' => false,
216+
'refresh-content-version-only' => false,
207217
],
208218
null // content version value should not be asserted in this case
209219
],
@@ -212,9 +222,17 @@ public function deployDataProvider()
212222
'strategy' => 'compact',
213223
'no-javascript' => false,
214224
'no-html-minify' => false,
225+
'refresh-content-version-only' => false,
215226
'content-version' => '123456',
216227
],
217228
'123456'
229+
],
230+
[
231+
[
232+
'refresh-content-version-only' => true,
233+
'content-version' => '654321',
234+
],
235+
'654321'
218236
]
219237
];
220238
}

setup/src/Magento/Setup/Console/Command/DeployStaticContentCommand.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
120120

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

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

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

129133
$this->mockCache();
130134

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

136140
$deployService->deploy($options);
137141

138-
$logger->alert(PHP_EOL . "Execution time: " . (microtime(true) - $time));
142+
if (!$refreshOnly) {
143+
$logger->alert(PHP_EOL . "Execution time: " . (microtime(true) - $time));
144+
}
139145

140146
return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
141147
}

0 commit comments

Comments
 (0)