1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelZero\Framework\Commands\App; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\File; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use LaravelZero\Framework\Commands\Command; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This is the Laravel Zero Framework renamer command class. |
11
|
|
|
* |
12
|
|
|
* @author Nuno Maduro <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Renamer extends Command |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* {@inheritdoc} |
18
|
|
|
*/ |
19
|
|
|
protected $signature = 'app:rename {name? : The new name}'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
protected $description = 'Perform an application rename'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
1 |
|
public function handle(): void |
30
|
|
|
{ |
31
|
1 |
|
$this->alert('Renaming the application...'); |
32
|
|
|
|
33
|
1 |
|
$this->rename(); |
34
|
1 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Perform project modifications in order to apply the |
38
|
|
|
* application name on the composer and on the binary. |
39
|
|
|
* |
40
|
|
|
* @return $this |
41
|
|
|
*/ |
42
|
1 |
|
protected function rename(): Renamer |
43
|
|
|
{ |
44
|
1 |
|
$name = $this->asksForApplicationName(); |
45
|
|
|
|
46
|
1 |
|
if (File::exists(base_path($name))) { |
47
|
|
|
$this->error("Could't rename: Folder or file already exists."); |
48
|
|
|
} else { |
49
|
1 |
|
$this->renameBinary($name)->updateComposer($name); |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Display an welcome message. |
57
|
|
|
* |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
protected function displayWelcomeMessage(): Renamer |
61
|
|
|
{ |
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Asks for the application name. |
67
|
|
|
* |
68
|
|
|
* If there is no interaction, we take the folder basename. |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
1 |
|
protected function asksForApplicationName(): string |
73
|
|
|
{ |
74
|
1 |
|
if (empty($name = $this->input->getArgument('name'))) { |
75
|
|
|
$name = $this->ask('What is your application name?'); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
if (empty($name)) { |
79
|
|
|
$name = trim(basename(BASE_PATH)); |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
return Str::lower($name); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Update composer json with related information. |
87
|
|
|
* |
88
|
|
|
* @param string $name |
89
|
|
|
* |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
1 |
|
protected function updateComposer(string $name): Renamer |
93
|
|
|
{ |
94
|
1 |
|
$this->setComposer( |
95
|
1 |
|
Str::replaceFirst( |
96
|
1 |
|
'"bin": ["'.$this->getCurrentBinaryName().'"]', '"bin": ["'.$name.'"]', $this->getComposer() |
97
|
|
|
) |
98
|
|
|
); |
99
|
|
|
|
100
|
1 |
|
$this->output->writeln('Updating composer: <info>✔</info>'); |
101
|
|
|
|
102
|
1 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Renames the application binary. |
107
|
|
|
* |
108
|
|
|
* @param string $name |
109
|
|
|
* |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
1 |
|
protected function renameBinary(string $name): Renamer |
113
|
|
|
{ |
114
|
1 |
|
File::move(base_path($this->getCurrentBinaryName()), base_path($name)); |
115
|
|
|
|
116
|
1 |
|
$this->output->writeln("Renaming application to: <info>$name</info>"); |
117
|
|
|
|
118
|
1 |
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Set composer file. |
123
|
|
|
* |
124
|
|
|
* @param string $composer |
125
|
|
|
* |
126
|
|
|
* @return $this |
127
|
|
|
*/ |
128
|
1 |
|
protected function setComposer(string $composer): Renamer |
129
|
|
|
{ |
130
|
1 |
|
File::put(base_path('composer.json'), $composer); |
131
|
|
|
|
132
|
1 |
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Returns the current binary name. |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
1 |
|
protected function getCurrentBinaryName(): string |
141
|
|
|
{ |
142
|
1 |
|
$composer = $this->getComposer(); |
143
|
|
|
|
144
|
1 |
|
return current(@json_decode($composer)->bin); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get composer file. |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
1 |
|
protected function getComposer(): string |
153
|
|
|
{ |
154
|
1 |
|
$file = base_path('composer.json'); |
155
|
|
|
|
156
|
1 |
|
if (! File::exists($file)) { |
157
|
|
|
$this->error('composer.json not found.'); |
158
|
|
|
exit(0); |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
return File::get($file); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|