This repository has been archived by the owner on May 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
RunCommand.php
93 lines (79 loc) · 3.45 KB
/
RunCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/*
* This file is part of JoliCi.
*
* (c) Joel Wurtz <jwurtz@jolicode.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Joli\JoliCi\Command;
use Docker\Docker;
use Docker\Http\Client;
use Joli\JoliCi\Builder;
use Joli\JoliCi\Executor;
use Joli\JoliCi\BuildStrategy\JoliCiBuildStrategy;
use Joli\JoliCi\BuildStrategy\TravisCiBuildStrategy;
use Joli\JoliCi\Log\SimpleFormatter;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Filesystem\Filesystem;
class RunCommand extends Command
{
/**
* @var string Base path for resources
*/
private $resourcesPath;
public function __construct($resourcesPath)
{
parent::__construct();
$this->resourcesPath = $resourcesPath;
}
/**
* Configures the current command.
*/
protected function configure()
{
$this->setName('run');
$this->setDescription('Run tests on your project');
$this->addOption('project-path', 'p', InputOption::VALUE_OPTIONAL, "Path where you project is", ".");
$this->addOption('no-cache', null, InputOption::VALUE_NONE, "Do not use cache of docker");
$this->addOption('docker-host', null, InputOption::VALUE_OPTIONAL, "Docker server location", "unix:///var/run/docker.sock");
$this->addArgument('cmd', InputArgument::OPTIONAL, "Override test command");
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$quietBuild = !(OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity());
$tmpDir = sys_get_temp_dir().DIRECTORY_SEPARATOR."jolici-builds";
$logger = new Logger("standalone-logger");
$joliciStrategy = new JoliCiBuildStrategy($tmpDir);
$travisCiStrategy = new TravisCiBuildStrategy($tmpDir, $this->resourcesPath.DIRECTORY_SEPARATOR."travisci");
$docker = new Docker(new Client($input->getOption('docker-host')));
$executor = new Executor($logger, $docker, !$input->getOption('no-cache'), $quietBuild);
$filesystem = new Filesystem();
$handler = new StreamHandler("php://stdout", $quietBuild ? Logger::INFO : Logger::DEBUG);
$builder = new Builder();
$handler->setFormatter(new SimpleFormatter());
$builder->pushStrategy($joliciStrategy);
$builder->pushStrategy($travisCiStrategy);
$logger->pushHandler($handler);
$output->writeln("<info>Creating builds...</info>");
$builds = $builder->createBuilds($input->getOption("project-path"));
$output->writeln(sprintf("<info>%s builds created</info>", count($builds)));
foreach ($builds as $build) {
$output->writeln(sprintf("\n<info>Running build %s</info>\n", $build->getName()));
if ($executor->runBuild($build->getDirectory(), $build->getDockerName())) {
$executor->runTest($build->getDockerName(), $input->getArgument('cmd'));
}
$filesystem->remove($build->getDirectory());
}
if (count($builds) > 0) {
rmdir(dirname($build->getDirectory()));
}
}
}