Skip to content

Commit

Permalink
Update for new starter kits (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseleite authored Aug 24, 2021
1 parent 07e099d commit 8566a7c
Show file tree
Hide file tree
Showing 10 changed files with 766 additions and 278 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ This will present you with a list of supported starter kits to select from. Upo
You may also pass an explicit starter kit repo if you wish to skip the selection prompt:

```
statamic new my-site --starter="statamic/starter-kit-cool-writings"
statamic new my-site statamic/starter-kit-cool-writings
```

### Checking Statamic versions
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"php": ">=7.2.5",
"guzzlehttp/guzzle": "^6.5.5|^7.0.1",
"symfony/console": "^4.0|^5.0",
"symfony/process": "^4.2|^5.0",
"symfony/yaml": "^4.1 || ^5.1"
"symfony/process": "^4.2|^5.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0"
Expand Down
8 changes: 0 additions & 8 deletions resources/starter-kits.yaml

This file was deleted.

181 changes: 70 additions & 111 deletions src/Concerns/InstallsLegacy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,161 +3,131 @@
namespace Statamic\Cli\Concerns;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use RuntimeException;
use Statamic\Cli\Please;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use ZipArchive;

trait InstallsLegacy
{
protected $input;
protected $output;
protected $directory;
protected $version;
protected $progressBar;

/**
* Install Statamic v2.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function installV2(InputInterface $input, OutputInterface $output)
protected function installV2()
{
if (! class_exists('ZipArchive')) {
throw new RuntimeException('The Zip PHP extension is not installed. Please install it and try again.');
}
(new LegacyInstaller($this))->install();

$this->output = $output;
$this->input = $input;
$this->showSuccessMessage();

$dir = $this->input->getArgument('name');
return 0;
}
}

$this->verifyApplicationDoesntExist(
$this->directory = getcwd().'/'.$dir
);
class LegacyInstaller
{
protected $command;
protected $version;
protected $zipFilename;
protected $progressBar;

$this->version = $this->getVersion();
public function __construct($command)
{
$this->command = $command;
}

public function install()
{
$this
->download($zipName = $this->makeFilename())
->extract($zipName)
->cleanup($zipName)
->verifyZipExtension()
->getLatestVersion()
->makeZipFilename()
->download()
->extract()
->cleanup()
->applyPermissions()
->createUser();

$this->output->writeln("<info>[✔] Statamic has been installed into the <comment>{$dir}</comment> directory.</info>");

return 0;
}

/**
* Verify that the application does not already exist.
*
* @param string $directory
* @return void
*/
protected function verifyApplicationDoesntExist($directory)
protected function verifyZipExtension()
{
if (is_dir($directory)) {
throw new RuntimeException('Application already exists!');
if (! class_exists('ZipArchive')) {
throw new RuntimeException('The Zip PHP extension is not installed. Please install it and try again.');
}
}

/**
* Generate a random temporary filename.
*
* @return string
*/
protected function makeFilename()
{
return getcwd().'/statamic_'.md5(time().uniqid());
return $this;
}

protected function getVersion()
protected function getLatestVersion()
{
$this->output->write('Checking for the latest version...');
$this->command->output->write('Checking for the latest version...');

$version = (new Client)
$this->version = (new Client)
->get('https://outpost.statamic.com/v2/check')
->getBody();

$this->output->writeln(" <info>[✔] $version</info>");
$this->command->output->writeln(" <info>[✔] {$this->version}</info>");

return $version;
return $this;
}

protected function makeZipFilename()
{
$this->zipFilename = getcwd().'/statamic_'.md5(time().uniqid());

return $this;
}

/**
* Recursively apply permissions to appropriate directories.
*
* @return void
*/
protected function applyPermissions()
{
$this->output->write('Updating file permissions...');
$this->command->output->write('Updating file permissions...');

foreach (['local', 'site', 'statamic', 'assets'] as $folder) {
$dir = $this->directory . '/' . $folder;
$dir = $this->command->absolutePath . '/' . $folder;
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir));

foreach ($iterator as $item) {
chmod($item, 0777);
}
}

$this->output->writeln(" <info>[✔]</info>");
$this->command->output->writeln(" <info>[✔]</info>");

return $this;
}

protected function createUser()
{
$questionText = 'Create a user? (yes/no) [<comment>no</comment>]: ';
$helper = $this->getHelper('question');
$helper = $this->command->getHelper('question');
$question = new ConfirmationQuestion($questionText, false);

if (! $helper->ask($this->input, $this->output, $question)) {
$this->output->writeln("\x1B[1A\x1B[2K{$questionText}<fg=red>[✘]</>");
$this->output->writeln("<comment>[!]</comment> You may create a user with <comment>php please make:user</comment>");
if (! $helper->ask($this->command->input, $this->command->output, $question)) {
$this->command->output->writeln("\x1B[1A\x1B[2K{$questionText}<fg=red>[✘]</>");
$this->command->output->writeln("<comment>[!]</comment> You may create a user with <comment>php please make:user</comment>");
return $this;
}

(new Please($this->output))
->cwd($this->directory)
(new Please($this->command->output))
->cwd($this->command->absolutePath)
->run('make:user');

$this->output->writeln("\x1B[1A\x1B[2KUser created <info>[✔]</info>");
$this->output->writeln('');
$this->command->output->writeln("\x1B[1A\x1B[2KUser created <info>[✔]</info>");
$this->command->output->writeln('');

return $this;
}

/**
* Download the temporary Zip to the given file.
*
* @param string $zipFile
* @return $this
*/
protected function download($zipFile)
protected function download()
{
$zipContents = $this->getZipFromServer();

file_put_contents($zipFile, $zipContents);
file_put_contents($this->zipFilename, $zipContents);

return $this;
}

protected function getZipFromServer()
{
$this->output->writeln('Downloading...');
$this->output->writeln('Press Ctrl+C to cancel.');
$this->command->output->writeln('Downloading...');
$this->command->output->writeln('Press Ctrl+C to cancel.');

$client = new Client([
'progress' => function ($downloadSize, $downloaded) {
Expand All @@ -177,7 +147,7 @@ protected function getZipFromServer()

$zipContents = $response->getBody();

$this->output->writeln("\n<info>Download complete!</info>");
$this->command->output->writeln("\n<info>Download complete!</info>");

return $zipContents;
}
Expand All @@ -192,7 +162,7 @@ protected function createProgressBar($downloadSize)
return str_pad($this->formatBytes($bar->getProgress()), 11, ' ', STR_PAD_LEFT);
});

$this->progressBar = new ProgressBar($this->output, $downloadSize);
$this->progressBar = new ProgressBar($this->command->output, $downloadSize);
$this->progressBar->setFormat('%current% / %max% %bar% %percent:3s%%');
$this->progressBar->setRedrawFrequency(max(1, floor($downloadSize / 1000)));
$this->progressBar->setBarWidth(60);
Expand All @@ -207,51 +177,40 @@ protected function formatBytes($bytes)
$pow = $bytes ? floor(log($bytes, 1024)) : 0;
$pow = min($pow, count($units) - 1);
$bytes /= pow(1024, $pow);

return number_format($bytes, 2).' '.$units[$pow];
}

/**
* Extract the zip file into the given directory.
*
* @param string $zipFile
* @return $this
*/
protected function extract($zipFile)
protected function extract()
{
$this->output->write('Extracting zip...');
$this->command->output->write('Extracting zip...');

$archive = new ZipArchive;

$archive->open($zipFile);
$archive->open($this->zipFilename);

$archive->extractTo($this->directory.'_tmp');
$archive->extractTo($this->command->absolutePath.'_tmp');

$archive->close();

$this->output->writeln(' <info>[✔]</info>');
$this->command->output->writeln(' <info>[✔]</info>');

return $this;
}

/**
* Clean-up the Zip file.
*
* @param string $zipFile
* @return $this
*/
protected function cleanUp($zipFile)
protected function cleanUp()
{
$this->output->write('Cleaning up...');
$this->command->output->write('Cleaning up...');

rename($this->directory.'_tmp/statamic', $this->directory);
rename($this->command->absolutePath.'_tmp/statamic', $this->command->absolutePath);

@rmdir($this->directory.'_tmp');
@rmdir($this->command->absolutePath.'_tmp');

@chmod($zipFile, 0777);
@chmod($this->zipFilename, 0777);

@unlink($zipFile);
@unlink($this->zipFilename);

$this->output->writeln(' <info>[✔]</info>');
$this->command->output->writeln(' <info>[✔]</info>');

return $this;
}
Expand Down
29 changes: 18 additions & 11 deletions src/Concerns/RunsCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,30 @@

namespace Statamic\Cli\Concerns;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;

trait RunsCommands
{
/**
* Run the given command.
*
* @param string $command
* @return Process
*/
protected function runCommand($command)
{
return $this->runCommands([$command]);
}

/**
* Run the given commands.
*
* @param array $commands
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @param array $commands
* @return Process
*/
protected function runCommands($commands, InputInterface $input, OutputInterface $output)
protected function runCommands($commands)
{
if ($input->getOption('no-ansi')) {
if (! $this->output->isDecorated()) {
$commands = array_map(function ($value) {
if (substr($value, 0, 5) === 'chmod') {
return $value;
Expand All @@ -28,7 +35,7 @@ protected function runCommands($commands, InputInterface $input, OutputInterface
}, $commands);
}

if ($input->getOption('quiet')) {
if ($this->input->getOption('quiet')) {
$commands = array_map(function ($value) {
if (substr($value, 0, 5) === 'chmod') {
return $value;
Expand All @@ -44,12 +51,12 @@ protected function runCommands($commands, InputInterface $input, OutputInterface
try {
$process->setTty(true);
} catch (RuntimeException $e) {
$output->writeln('Warning: '.$e->getMessage());
$this->output->writeln('Warning: '.$e->getMessage());
}
}

$process->run(function ($type, $line) use ($output) {
$output->write(' '.$line);
$process->run(function ($type, $line) {
$this->output->write(' '.$line);
});

return $process;
Expand Down
Loading

0 comments on commit 8566a7c

Please sign in to comment.