Skip to content
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
11 changes: 11 additions & 0 deletions app/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use PhpSchool\PhpWorkshop\Command\PrintCommand;
use PhpSchool\PhpWorkshop\Command\RunCommand;
use PhpSchool\PhpWorkshop\Command\VerifyCommand;
use PhpSchool\PhpWorkshop\Command\WarmCacheCommand;
use PhpSchool\PhpWorkshop\CommandDefinition;
use PhpSchool\PhpWorkshop\CommandRouter;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
Expand All @@ -51,6 +52,7 @@
use PhpSchool\PhpWorkshop\Listener\InitialCodeListener;
use PhpSchool\PhpWorkshop\Listener\OutputRunInfoListener;
use PhpSchool\PhpWorkshop\Listener\PrepareSolutionListener;
use PhpSchool\PhpWorkshop\ReferencePackages;
use PhpSchool\PhpWorkshop\Listener\RealPathListener;
use PhpSchool\PhpWorkshop\Listener\RemoveStudentSymlinksListener;
use PhpSchool\PhpWorkshop\Listener\SelfCheckListener;
Expand Down Expand Up @@ -160,6 +162,7 @@
new CommandDefinition('verify', [], VerifyCommand::class),
new CommandDefinition('run', [], RunCommand::class),
new CommandDefinition('credits', [], CreditsCommand::class),
new CommandDefinition('warm-cache', [], WarmCacheCommand::class),
],
'menu',
$c->get(EventDispatcher::class),
Expand Down Expand Up @@ -221,6 +224,10 @@
},

//commands
WarmCacheCommand::class => function (ContainerInterface $c) {
return new WarmCacheCommand($c->get(ReferencePackages::class));
},

MenuCommand::class => function (ContainerInterface $c) {
return new MenuCommand($c->get('menu'));
},
Expand Down Expand Up @@ -272,6 +279,10 @@
);
},

ReferencePackages::class => function (ContainerInterface $c) {
return new ReferencePackages($c->get(ExerciseRepository::class), $c->get(Sandbox::class));
},

//Listeners
InitialCodeListener::class => function (ContainerInterface $c) {
return new InitialCodeListener($c->get('currentWorkingDirectory'), $c->get(LoggerInterface::class));
Expand Down
22 changes: 22 additions & 0 deletions src/Command/WarmCacheCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace PhpSchool\PhpWorkshop\Command;

use PhpSchool\PhpWorkshop\ReferencePackages;

/**
* Run by the workshop's composer post-install-cmd, so verifies can install reference packages offline.
*/
class WarmCacheCommand
{
public function __construct(private ReferencePackages $referencePackages) {}

public function __invoke(): int
{
$this->referencePackages->warm();

return 0;
}
}
40 changes: 29 additions & 11 deletions src/Listener/PrepareSolutionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
use PhpSchool\PhpWorkshop\Exception\RuntimeException;
use PhpSchool\PhpWorkshop\Process\Sandbox;
use PhpSchool\PhpWorkshop\Process\ProcessInput;
use PhpSchool\PhpWorkshop\Process\SandboxExecution;

/**
* Listener to install composer deps for an exercise solution
* Listener to install composer deps for an exercise solution, offline when ReferencePackages has warmed the cache
*/
class PrepareSolutionListener
{
Expand Down Expand Up @@ -39,16 +40,33 @@ public function __invoke(ExerciseRunnerEvent $event): void

//prepare composer deps
//only install if vendor folder not available
if (!file_exists(sprintf('%s/vendor', $event->getContext()->getReferenceExecutionDirectory()))) {
$execution = $this->sandbox->create(
new ProcessInput('composer', ['install', '--no-interaction'], $event->getContext()->getReferenceExecutionDirectory(), [], []),
);

try {
$execution->mustRun();
} catch (\Symfony\Component\Process\Exception\RuntimeException $e) {
throw new RuntimeException('Composer dependencies could not be installed', 0, $e);
}
if (file_exists(sprintf('%s/vendor', $event->getContext()->getReferenceExecutionDirectory()))) {
return;
}

//offline first, from the cache ReferencePackages warms. Without it, eg. a workshop run from a clone, download
$offline = $this->install($event, ['COMPOSER_DISABLE_NETWORK' => '1']);
$offline->start();
$offline->wait();

if ($offline->isSuccessful()) {
return;
}

try {
$this->install($event, [])->mustRun();
} catch (\Symfony\Component\Process\Exception\RuntimeException $e) {
throw new RuntimeException('Composer dependencies could not be installed', 0, $e);
}
}

/**
* @param array<string, string> $env
*/
private function install(ExerciseRunnerEvent $event, array $env): SandboxExecution
{
return $this->sandbox->create(
new ProcessInput('composer', ['install', '--no-interaction'], $event->getContext()->getReferenceExecutionDirectory(), $env, []),
);
}
}
53 changes: 53 additions & 0 deletions src/ReferencePackages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace PhpSchool\PhpWorkshop;

use PhpSchool\PhpWorkshop\Exception\RuntimeException;
use PhpSchool\PhpWorkshop\Exercise\ProvidesSolution;
use PhpSchool\PhpWorkshop\Process\ProcessInput;
use PhpSchool\PhpWorkshop\Process\Sandbox;
use Symfony\Component\Filesystem\Filesystem;

/**
* Locks and caches the packages of each reference solution, so a verify can install them with no network.
* Run it on install/deploy, like the runtime image build.
*/
class ReferencePackages
{
private Filesystem $filesystem;

public function __construct(private ExerciseRepository $exerciseRepository, private Sandbox $sandbox)
{
$this->filesystem = new Filesystem();
}

public function warm(): void
{
foreach ($this->exerciseRepository->findAll() as $exercise) {
if (!$exercise instanceof ProvidesSolution || !$exercise->getSolution()->hasComposerFile()) {
continue;
}

$directory = $exercise->getSolution()->getBaseDirectory();

//an old vendor folder would mean nothing is downloaded, so nothing is cached
$this->filesystem->remove($directory . '/vendor');

//keeps a committed composer.lock, or writes one next to the solution. It is copied with it on each verify
$execution = $this->sandbox->create(
new ProcessInput('composer', ['install', '--no-interaction'], $directory, [], []),
);

try {
$execution->mustRun();
} catch (\Symfony\Component\Process\Exception\RuntimeException $e) {
throw new RuntimeException(sprintf('Composer packages for "%s" could not be installed', $exercise->getName()), 0, $e);
}

//verifies install from the cache, this copy is never used
$this->filesystem->remove($directory . '/vendor');
}
}
}
2 changes: 1 addition & 1 deletion src/Solution/DirectorySolution.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function __construct(string $directory, string $entryPoint, array $exclus
*/
public static function fromDirectory(string $directory, array $exclusions = [], $entryPoint = 'solution.php'): self
{
return new self($directory, $entryPoint, array_merge($exclusions, ['composer.lock', 'vendor']));
return new self($directory, $entryPoint, array_merge($exclusions, ['vendor']));
}

/**
Expand Down
20 changes: 20 additions & 0 deletions test/Command/WarmCacheCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace PhpSchool\PhpWorkshopTest\Command;

use PhpSchool\PhpWorkshop\Command\WarmCacheCommand;
use PhpSchool\PhpWorkshop\ReferencePackages;
use PHPUnit\Framework\TestCase;

class WarmCacheCommandTest extends TestCase
{
public function testWarmsTheReferencePackages(): void
{
$referencePackages = $this->createMock(ReferencePackages::class);
$referencePackages->expects($this->once())->method('warm');

$this->assertSame(0, (new WarmCacheCommand($referencePackages))->__invoke());
}
}
39 changes: 39 additions & 0 deletions test/ReferencePackagesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace PhpSchool\PhpWorkshopTest;

use PhpSchool\PhpWorkshop\ExerciseRepository;
use PhpSchool\PhpWorkshop\Process\HostSandbox;
use PhpSchool\PhpWorkshop\ReferencePackages;
use PhpSchool\PhpWorkshop\Solution\DirectorySolution;
use PhpSchool\PhpWorkshop\Utils\System;
use PhpSchool\PhpWorkshopTest\Asset\CliExerciseImpl;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;

class ReferencePackagesTest extends TestCase
{
public function testWarmLocksEachSolutionWithPackages(): void
{
$directory = System::randomTempDir();
mkdir($directory, 0777, true);
file_put_contents($directory . '/solution.php', '<?php');
file_put_contents($directory . '/composer.json', json_encode(['require' => ['psr/log' => '^3.0']]));

$withPackages = new CliExerciseImpl();
$withPackages->setSolution(DirectorySolution::fromDirectory($directory));

(new ReferencePackages(new ExerciseRepository([$withPackages]), new HostSandbox()))->warm();

$this->assertFileExists($directory . '/composer.lock');
$this->assertDirectoryDoesNotExist($directory . '/vendor');
$this->assertContains('composer.lock', array_map(
fn($file) => $file->getRelativePath(),
DirectorySolution::fromDirectory($directory)->getFiles(),
));

(new Filesystem())->remove($directory);
}
}
Loading