Skip to content

Commit 9127b7d

Browse files
authored
Git support (#185)
* Set up a new Git Repository * GitHub support * Jetstream separately * Apply fixes from StyleCI (#184) * Fix initial branch name
1 parent 95885a0 commit 9127b7d

File tree

1 file changed

+91
-1
lines changed

1 file changed

+91
-1
lines changed

src/NewCommand.php

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ protected function configure()
2626
->setDescription('Create a new Laravel application')
2727
->addArgument('name', InputArgument::REQUIRED)
2828
->addOption('dev', null, InputOption::VALUE_NONE, 'Installs the latest "development" release')
29+
->addOption('git', null, InputOption::VALUE_NONE, 'Initialize a Git repository')
30+
->addOption('github', null, InputOption::VALUE_OPTIONAL, 'Create a new repository on GitHub')
2931
->addOption('jet', null, InputOption::VALUE_NONE, 'Installs the Laravel Jetstream scaffolding')
3032
->addOption('stack', null, InputOption::VALUE_OPTIONAL, 'The Jetstream stack that should be installed')
3133
->addOption('teams', null, InputOption::VALUE_NONE, 'Indicates whether Jetstream should be scaffolded with team support')
@@ -121,10 +123,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
121123
);
122124
}
123125

126+
if ($input->hasOption('git') || $input->hasOption('github')) {
127+
$this->createRepository($directory, $input, $output);
128+
}
129+
124130
if ($installJetstream) {
125131
$this->installJetstream($directory, $stack, $teams, $input, $output);
126132
}
127133

134+
if ($input->hasOption('github')) {
135+
$this->pushToGitHub($name, $directory, $input, $output);
136+
}
137+
128138
$output->writeln(PHP_EOL.'<comment>Application ready! Build something amazing.</comment>');
129139
}
130140

@@ -153,6 +163,8 @@ protected function installJetstream(string $directory, string $stack, bool $team
153163
]);
154164

155165
$this->runCommands($commands, $input, $output);
166+
167+
$this->commitChanges('Install Jetstream', $directory, $input, $output);
156168
}
157169

158170
/**
@@ -182,6 +194,84 @@ protected function jetstreamStack(InputInterface $input, OutputInterface $output
182194
return $helper->ask($input, new SymfonyStyle($input, $output), $question);
183195
}
184196

197+
/**
198+
* Create a Git repository and commit the base Laravel skeleton.
199+
*
200+
* @param string $directory
201+
* @param \Symfony\Component\Console\Input\InputInterface $input
202+
* @param \Symfony\Component\Console\Output\OutputInterface $output
203+
* @return void
204+
*/
205+
protected function createRepository(string $directory, InputInterface $input, OutputInterface $output)
206+
{
207+
chdir($directory);
208+
209+
$commands = [
210+
'git init -q -b main .',
211+
'git add .',
212+
'git commit -q -m "Set up a fresh Laravel app"',
213+
];
214+
215+
$this->runCommands($commands, $input, $output);
216+
}
217+
218+
/**
219+
* Commit any changes in the current working directory.
220+
*
221+
* @param string $message
222+
* @param string $directory
223+
* @param \Symfony\Component\Console\Input\InputInterface $input
224+
* @param \Symfony\Component\Console\Output\OutputInterface $output
225+
* @return void
226+
*/
227+
protected function commitChanges(string $message, string $directory, InputInterface $input, OutputInterface $output)
228+
{
229+
if (! $input->hasOption('git') && ! $input->hasOption('github')) {
230+
return;
231+
}
232+
233+
chdir($directory);
234+
235+
$commands = [
236+
'git add .',
237+
"git commit -q -m \"$message\"",
238+
];
239+
240+
$this->runCommands($commands, $input, $output);
241+
}
242+
243+
/**
244+
* Create a GitHub repository and push the git log to it.
245+
*
246+
* @param string $name
247+
* @param string $directory
248+
* @param \Symfony\Component\Console\Input\InputInterface $input
249+
* @param \Symfony\Component\Console\Output\OutputInterface $output
250+
* @return void
251+
*/
252+
protected function pushToGitHub(string $name, string $directory, InputInterface $input, OutputInterface $output)
253+
{
254+
$process = new Process(['gh', 'auth', 'status']);
255+
$process->run();
256+
257+
if (! $process->isSuccessful()) {
258+
$output->writeln('Warning: make sure the "gh" CLI tool is installed and that you\'re authenticated to GitHub. Skipping...');
259+
260+
return;
261+
}
262+
263+
chdir($directory);
264+
265+
$flags = $input->getOption('github') ?: '--private';
266+
267+
$commands = [
268+
"gh repo create $name -y $flags",
269+
'git push -q -u origin main',
270+
];
271+
272+
$this->runCommands($commands, $input, $output);
273+
}
274+
185275
/**
186276
* Verify that the application does not already exist.
187277
*
@@ -232,7 +322,7 @@ protected function findComposer()
232322
* @param array $commands
233323
* @param \Symfony\Component\Console\Input\InputInterface $input
234324
* @param \Symfony\Component\Console\Output\OutputInterface $output
235-
* @return Process
325+
* @return \Symfony\Component\Process\Process
236326
*/
237327
protected function runCommands($commands, InputInterface $input, OutputInterface $output)
238328
{

0 commit comments

Comments
 (0)