This repository has been archived by the owner on Mar 18, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
PresetCommand.php
132 lines (117 loc) · 4.55 KB
/
PresetCommand.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
129
130
131
132
<?php
namespace Roots\Sage\Installer\Console\Commands;
use Illuminate\Filesystem\Filesystem;
use InvalidArgumentException;
use Roots\Sage\Installer\Presets\Bootstrap5;
use Roots\Sage\Installer\Presets\Bootstrap4;
use Roots\Sage\Installer\Presets\Bulma;
use Roots\Sage\Installer\Presets\Foundation;
use Roots\Sage\Installer\Presets\None;
use Roots\Sage\Installer\Presets\Preset;
use Roots\Sage\Installer\Presets\Tachyons;
use Roots\Sage\Installer\Presets\Tailwind;
use Roots\Sage\Installer\Transformations\Presets;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Ramsey\Uuid\Uuid;
class PresetCommand extends Command
{
/** {@inheritdoc} */
protected $description = 'Swap the front-end scaffolding for the theme';
/** @var Presets */
public $presets;
/** {@inheritdoc} */
public function __construct($presets = null)
{
$this->presets = new Presets($presets ?: $this->defaultPresets());
$slugs = implode(', ', $this->presets->slugs());
$this->signature = "preset { framework : The front-end framework ({$slugs}) }";
parent::__construct();
}
protected function configure()
{
parent::configure();
$this->addOption(
'overwrite',
'Y',
InputOption::VALUE_NONE,
'Confirm overwriting files',
null
);
}
/** {@inheritdoc} */
protected function interact(InputInterface $input, OutputInterface $output)
{
$this->framework();
}
/** {@inheritdoc} */
protected function execute(InputInterface $input, OutputInterface $output)
{
if (!in_array($this->argument('framework'), $this->presets->slugs())) {
throw new InvalidArgumentException('Invalid preset.');
}
$preset = $this->presets[$this->argument('framework')];
if (!$this->confirmOverwrite($preset->filesToOverwrite())) {
$this->info('No actions were taken.');
return;
}
$preset->handle();
$this->info(' Done.');
// phpcs:disable
if ($this->confirm("Send anonymous usage data? \n<comment> We are only sending your framework selection and OS</comment>\n\n")) {
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://stats.roots.io/i?device_id='.Uuid::uuid4().'&app_key=fd022e092a7ff07e996ca3cec86847bf1baf0879&begin_session=1&events=[{"key":"framework_selected","count":1,"segmentation":{"framework":"'.$this->argument('framework').'"}}]&metrics={"_os":"'.PHP_OS.'","_browser":"null","_browser_version":"0","_device":"null"}&end_session=1&session_duration=30',
CURLOPT_USERAGENT => 'sage-installer-composer'
]);
$resp = curl_exec($curl);
curl_close($curl);
}
// phpcs:enable
$this->comment('Please run `yarn && yarn build` to compile your fresh scaffolding.');
$this->comment('');
$this->comment('Help support our open-source development efforts by contributing to Sage via Patreon:');
$this->comment('https://www.patreon.com/rootsdev');
$this->comment('Join us on the Roots Slack when you become a supporter!');
}
protected function framework()
{
if ($this->argument('framework')) {
return;
}
$default = array_search('bootstrap', $this->presets->slugs());
$framework = $this->choice('Which framework would you like to load?', $this->presets->names(), $default);
$framework = array_search($framework, $this->presets->names());
$this->input->setArgument('framework', $this->presets->slugs()[$framework]);
}
/**
* Confirm overwriting files
*
* @return bool
*/
protected function confirmOverwrite(array $files = [])
{
if (!$files || $this->option('overwrite')) {
return true;
}
$files = implode("\n - ", $files);
return $this->confirm(
"Are you sure you want to overwrite the following files?\n<comment> - {$files}</comment>\n\n"
);
}
/** @return Preset[] */
protected function defaultPresets()
{
return [
new None($this->root),
new Bootstrap4($this->root),
new Bootstrap5($this->root),
new Bulma($this->root),
new Foundation($this->root),
new Tachyons($this->root),
new Tailwind($this->root),
];
}
}