Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ before_script:

script:
- vendor/bin/phpunit
- vendor/bin/psalm

after_success:
- if [ "$DEPLOY_WEBSITE" = "true" ]; then ./travis-prepare-release.sh && bin/couscous travis-auto-deploy; fi;
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@
},
"require-dev": {
"phpunit/phpunit": "~7.5",
"squizlabs/php_codesniffer": "^3.3"
"squizlabs/php_codesniffer": "^3.3",
"vimeo/psalm": "^3.16"
},
"config": {
"platform": {
"php": "7.1"
"php": "7.1.3"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to specify this bugfix version as it is the minimum required by vimeo/psalm

}
}
}
15 changes: 15 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
6 changes: 4 additions & 2 deletions src/Application/Cli/ClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ public function __construct(Filesystem $filesystem)
parent::__construct();
}

protected function configure()
protected function configure(): void
{
$this
->setName('clear')
->setDescription('Clear all files generated by Couscous');
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$dir = getcwd().'/.couscous';

Expand All @@ -45,5 +45,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
} else {
$output->writeln('<comment>Nothing to clear</comment>');
}

return 0;
}
}
11 changes: 9 additions & 2 deletions src/Application/Cli/DeployCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct(Generator $generator, Deployer $deployer, Filesystem
/**
* {@inheritdoc}
*/
protected function configure()
protected function configure(): void
{
try {
$remoteUrl = $this->git->getRemoteUrl();
Expand Down Expand Up @@ -95,11 +95,15 @@ protected function configure()
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
/** @var string */
$sourceDirectory = $input->getArgument('source');
/** @var string */
$repositoryUrl = $input->getOption('repository');
/** @var ?string */
$targetBranch = $input->getOption('branch');
/** @var array */
$cliConfig = $input->getOption('config');

$project = new Project($sourceDirectory, getcwd().'/.couscous/generated');
Expand All @@ -111,12 +115,15 @@ protected function execute(InputInterface $input, OutputInterface $output)

// If no branch was provided, use the configured one or the default
if (!$targetBranch) {
/** @var string */
$targetBranch = isset($project->metadata['branch']) ? $project->metadata['branch'] : 'gh-pages';
}

$output->writeln('');

// Deploy it
$this->deployer->deploy($project, $output, $repositoryUrl, $targetBranch);

return 0;
}
}
13 changes: 10 additions & 3 deletions src/Application/Cli/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(Generator $generator)
parent::__construct();
}

protected function configure()
protected function configure(): void
{
$this
->setName('generate')
Expand All @@ -56,14 +56,21 @@ protected function configure()
);
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
/** @var string */
$source = $input->getArgument('source');
/** @var string */
$target = $input->getOption('target');
/** @var array */
$cliConfig = $input->getOption('config');

$project = new Project($input->getArgument('source'), $input->getOption('target'));
$project = new Project($source, $target);

$project->metadata['cliConfig'] = $cliConfig;

$this->generator->generate($project, $output);

return 0;
}
}
21 changes: 12 additions & 9 deletions src/Application/Cli/InitTemplateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
class InitTemplateCommand extends Command
{
protected function configure()
protected function configure(): void
{
$this
->setName('init:template')
Expand All @@ -33,13 +33,16 @@ protected function configure()
);
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$fileExtension = '.twig';

/** @var string */
$dirName = $input->getArgument('directory');
$directory = getcwd().'/'.$dirName.'/';
$templateName = $input->getArgument('template_name').$fileExtension;
/** @var string */
$templateName = $input->getArgument('template_name');
$templateName = $templateName.$fileExtension;

$fileLocation = $directory.$templateName;
$fileExists = file_exists($fileLocation);
Expand All @@ -53,12 +56,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln('<error>That template exists at '.$fileLocation.', so nothing has been changed.</error>');
$output->writeln('<error>Try another name!</error>');

return;
return 1;
}

if (!$fileExists) {
$output->writeln('<comment>Initialising template.</comment>');
$template = <<<'HTML'
$output->writeln('<comment>Initialising template.</comment>');
$template = <<<'HTML'
<!DOCTYPE html>
<html>
<head>
Expand All @@ -85,7 +87,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
</body>
</html>
HTML;
file_put_contents($fileLocation, $template);
}
file_put_contents($fileLocation, $template);

return 0;
}
}
65 changes: 39 additions & 26 deletions src/Application/Cli/PreviewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Couscous\Generator;
use Couscous\Model\Project;
use Couscous\Model\WatchList\WatchList;
use RuntimeException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -34,7 +35,7 @@ public function __construct(Generator $generator)
/**
* {@inheritdoc}
*/
protected function configure()
protected function configure(): void
{
$this
->setName('preview')
Expand Down Expand Up @@ -76,25 +77,31 @@ protected function configure()
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
/** @var array */
$cliConfig = $input->getOption('config');

if (!$this->isSupported()) {
$output->writeln('<error>PHP 5.4 or above is required to run the internal webserver</error>');
$output->writeln('<error>PHP 7.1 or above is required to run the internal webserver</error>');

return 1;
}

/** @var string */
$sourceDirectory = $input->getArgument('source');
/** @var string */
$targetDirectory = $input->getOption('target');

if ($input->hasParameterOption('--livereload')) {
if ($input->getOption('livereload') === null) {
$input->setOption('livereload', 'livereload');
/** @var ?string */
$livereload = $input->getOption('livereload');

if ($livereload === null) {
$livereload = 'livereload';
}

if (!$this->isFound($input->getOption('livereload'))) {
if (!$this->isFound($livereload)) {
$output->writeln(
'<error>Impossible to launch Livereload, '
.'did you forgot to install it with "pip install livereload" (sudo maybe required)?</error>'
Expand All @@ -103,7 +110,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 1;
}

$this->startLivereload($input->getOption('livereload'), $output, $sourceDirectory, $targetDirectory);
$this->startLivereload($livereload, $output, $sourceDirectory, $targetDirectory);
}

$watchlist = $this->generateWebsite($output, $sourceDirectory, $targetDirectory, $cliConfig);
Expand All @@ -114,7 +121,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
if (function_exists('pcntl_signal')) {
declare(ticks=1);

$handler = function ($signal) use ($serverProcess, $output, &$throwOnServerStop) {
$handler = function (int $signal) use ($serverProcess, $output, &$throwOnServerStop): void {
$throwOnServerStop = !$this->stopWebServer($serverProcess, $output, $signal);
};

Expand Down Expand Up @@ -146,11 +153,11 @@ protected function execute(InputInterface $input, OutputInterface $output)

private function generateWebsite(
OutputInterface $output,
$sourceDirectory,
$targetDirectory,
$cliConfig,
$regenerate = false
) {
string $sourceDirectory,
string $targetDirectory,
array $cliConfig,
bool $regenerate = false
): WatchList {
$project = new Project($sourceDirectory, $targetDirectory);

$project->metadata['cliConfig'] = $cliConfig;
Expand All @@ -163,21 +170,23 @@ private function generateWebsite(
return $project->watchlist;
}

private function startWebServer(InputInterface $input, OutputInterface $output, $targetDirectory)
private function startWebServer(InputInterface $input, OutputInterface $output, string $targetDirectory): Process
{
$processArguments = [PHP_BINARY, '-S', $input->getArgument('address')];
/** @var string */
$address = $input->getArgument('address');
$processArguments = [PHP_BINARY, '-S', $address];

$process = new Process($processArguments);
$process->setWorkingDirectory($targetDirectory);
$process->setTimeout(null);
$process->start();

$output->writeln(sprintf('Server running on <comment>http://%s</comment>', $input->getArgument('address')));
$output->writeln(sprintf('Server running on <comment>http://%s</comment>', $address));

return $process;
}

private function stopWebServer(Process $serverProcess, OutputInterface $output, $signal = null)
private function stopWebServer(Process $serverProcess, OutputInterface $output, int $signal = null): bool
{
$signal = $signal ?: SIGTERM;

Expand All @@ -198,8 +207,12 @@ private function stopWebServer(Process $serverProcess, OutputInterface $output,
return false;
}

private function startLivereload($executablePath, OutputInterface $output, $sourceDirectory, $targetDirectory)
{
private function startLivereload(
string $executablePath,
OutputInterface $output,
string $sourceDirectory,
string $targetDirectory
): void {
$processArguments = [$executablePath, $targetDirectory, '-w', '3'];

$process = new Process($processArguments);
Expand All @@ -210,21 +223,21 @@ private function startLivereload($executablePath, OutputInterface $output, $sour
$output->writeln('<info>Livereload launched!</info>');
}

private function isSupported()
private function isSupported(): bool
{
if (version_compare(phpversion(), '5.4.0', '<')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That isSupported() is for checking that PHP >= 5.4 because that's the version they added the built-in webserver.

We could completely remove that check now instead of upgrading the error message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if (version_compare(phpversion(), '7.1.0', '<')) {
return false;
}

return true;
}

private function isFound($executablePath)
private function isFound(string $executablePath): bool
{
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$folders = explode(';', getenv('PATH'));
$folders = explode(';', (string) getenv('PATH'));
} else {
$folders = explode(':', getenv('PATH'));
$folders = explode(':', (string) getenv('PATH'));
}

foreach ($folders as $folder) {
Expand All @@ -236,9 +249,9 @@ private function isFound($executablePath)
return false;
}

private function fileListToDisplay(array $files, $sourceDirectory)
private function fileListToDisplay(array $files, string $sourceDirectory): string
{
$files = array_map(function ($file) use ($sourceDirectory) {
$files = array_map(function (string $file) use ($sourceDirectory): string {
return substr($file, strlen($sourceDirectory) + 1);
}, $files);

Expand Down
Loading