-
Notifications
You must be signed in to change notification settings - Fork 3
/
CreateProjectCommand.php
128 lines (111 loc) · 4.32 KB
/
CreateProjectCommand.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
namespace Skeletor\Console;
use Skeletor\Exceptions\FailedFilesystem;
use Skeletor\Frameworks\Framework;
use Skeletor\Packages\ConfigurablePackageInterface;
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\Process\Process;
class CreateProjectCommand extends SkeletorCommand
{
protected function configure()
{
$this->setName('project:create')
->setDescription('Create a new Laravel/Lumen project skeleton')
->addArgument('name', InputArgument::REQUIRED, 'Project name')
->addOption('dry-run', null, InputOption::VALUE_NONE, 'Dryrun the install', null);
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return bool
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$dryRun = $input->getOption('dry-run');
$name = strtolower($input->getArgument('name'));
if (!$dryRun) {
$this->setupFolder($name);
}
$this->setupCommand($dryRun);
//Start process in the background
$process = new Process('skeletor package:show --no-ansi');
$process->setTimeout(7200);
$process->start();
$this->cli->br()->yellow(sprintf('Skeletor - %s project creator', implode(" / ", $this->frameworkManager->getFrameworkNames())))->br();
$activeFramework = $this->frameworkManager->getFrameworkOption();
$activePackages = $this->packageManager->getPackageOptions();
if ($this->confirmOptions("\nSpecify package versions?")) {
$process->wait(function ($type, $buffer) {
if (Process::ERR === $type) {
$this->cli->red('ERR > ');
} else {
$this->cli->green('Fetching versions...');
}
});
$this->packageManager->specifyPackagesVersions($activePackages);
}
$this->showEnteredOptions($activeFramework, $activePackages);
if (!$this->confirmOptions()) {
return false;
}
$activePackages = $this->packageManager->mergePackagesWithDefault($activePackages);
$this->buildProject($activeFramework, $activePackages);
$this->cli->br()->green('Yhea, success')->br();
}
/**
* @param string $name
*/
private function setupFolder(string $name)
{
if ((is_dir($name) || is_file($name)) && $name != getcwd()) {
throw new FailedFilesystem(sprintf('Failed to make directory %s already exists', $name));
}
if (!mkdir($name)) {
throw new FailedFilesystem('Failed make directory for: ' . $name);
};
if (!chdir($name)) {
throw new FailedFilesystem('Failed change directory to: ' . $name);
}
}
/**
* @param Framework $activeFramework
* @param array $activePackages
*/
private function showEnteredOptions(Framework $activeFramework, array $activePackages)
{
$padding = $this->cli->padding(20);
$this->cli->br()->yellow('Project setup:');
$padding->label('Framework')->result($activeFramework->getName());
$padding->label('Version')->result($activeFramework->getVersion());
$this->cli->br()->yellow('Packages:');
$this->cli->table($this->packageManager->showPackagesTable($activePackages));
}
/**
* @param string $text
* @return bool
*/
private function confirmOptions($text = "Continue?")
{
$input = $this->cli->confirm($text);
return $input->confirmed();
}
/**
* @param Framework $activeFramework
* @param array $activePackages
*/
private function buildProject(Framework $activeFramework, array $activePackages)
{
$this->cli->br()->green('Building..');
$this->frameworkManager->install($activeFramework);
$this->frameworkManager->configure($activeFramework);
foreach ($activePackages as $key => $package) {
$this->packageManager->install($package);
if ($package instanceof ConfigurablePackageInterface) {
$this->packageManager->configure($package, $activeFramework);
}
}
}
}