-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
use Apiato\Installer\Console\GreetCommand; | ||
use Symfony\Component\Console\Application; | ||
|
||
require __DIR__ . './vendor/autoload.php'; | ||
|
||
$application = new Application('Apiato Installer', '0.1.0-alpha.1'); | ||
|
||
# add our commands | ||
$application->add(new GreetCommand()); | ||
|
||
$application->run(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
<?php | ||
|
||
namespace Apiato\Installer\Console; | ||
|
||
use RuntimeException; | ||
use Symfony\Component\Console\Command\Command; | ||
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 GreetCommand extends Command | ||
{ | ||
|
||
protected static $defaultName = 'new'; | ||
|
||
protected function configure() | ||
{ | ||
$this | ||
->setDescription('Create a new Apiato application') | ||
->addArgument('name', InputArgument::REQUIRED) | ||
->addOption('dev', null, InputOption::VALUE_NONE, 'Installs the latest "development" release') | ||
->addOption('force', 'f', InputOption::VALUE_NONE, 'Forces install even if the directory already exists') | ||
->setHelp('This command only shows a message to welcome user and nothing more.'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
|
||
$output->writeln([ | ||
'<fg=red>' . PHP_EOL . PHP_EOL . PHP_EOL . PHP_EOL . PHP_EOL, | ||
" ___ .______ __ ___ .___________. ______ ", | ||
" / \ | _ \ | | / \ | | / __ \ ", | ||
" / ^ \ | |_) | | | / ^ \ `---| |----`| | | | ", | ||
" / /_\ \ | ___/ | | / /_\ \ | | | | | | ", | ||
" / _____ \ | | | | / _____ \ | | | `--' | ", | ||
"/__/ \__\ | _| |__| /__/ \__\ |__| \______/ ", | ||
'</>' . PHP_EOL . PHP_EOL . PHP_EOL . PHP_EOL . PHP_EOL, | ||
]); | ||
|
||
sleep(1); | ||
|
||
$name = $input->getArgument('name'); | ||
|
||
$directory = $name !== '.' ? getcwd() . '/' . $name : '.'; | ||
|
||
$version = $this->getVersion($input); | ||
|
||
if (!$input->getOption('force')) { | ||
$this->verifyApplicationDoesntExist($directory); | ||
} | ||
|
||
if ($input->getOption('force') && $directory === '.') { | ||
throw new RuntimeException('Cannot use --force option when using current directory for installation!'); | ||
} | ||
|
||
$composer = $this->findComposer(); | ||
|
||
$commands = [ | ||
$composer . " create-project apiato/apiato \"$directory\" $version --remove-vcs --prefer-dist", | ||
]; | ||
|
||
if ($directory != '.' && $input->getOption('force')) { | ||
if (PHP_OS_FAMILY == 'Windows') { | ||
array_unshift($commands, "rd /s /q \"$directory\""); | ||
} else { | ||
array_unshift($commands, "rm -rf \"$directory\""); | ||
} | ||
} | ||
|
||
if (PHP_OS_FAMILY != 'Windows') { | ||
$commands[] = "chmod 755 \"$directory/artisan\""; | ||
} | ||
|
||
if (($process = $this->runCommands($commands, $input, $output))->isSuccessful()) { | ||
if ($name !== '.') { | ||
$this->replaceInFile( | ||
'APP_URL=http://localhost', | ||
'APP_URL=http://' . $name . '.test', | ||
$directory . '/.env' | ||
); | ||
|
||
$this->replaceInFile( | ||
'DB_DATABASE=laravel', | ||
'DB_DATABASE=' . str_replace('-', '_', strtolower($name)), | ||
$directory . '/.env' | ||
); | ||
|
||
$this->replaceInFile( | ||
'DB_DATABASE=laravel', | ||
'DB_DATABASE=' . str_replace('-', '_', strtolower($name)), | ||
$directory . '/.env.example' | ||
); | ||
} | ||
|
||
$output->writeln(PHP_EOL . '<comment>Apiato ready! Build something amazing.</comment>'); | ||
} | ||
|
||
return $process->getExitCode(); | ||
} | ||
|
||
/** | ||
* Get the version that should be downloaded. | ||
* | ||
* @param \Symfony\Component\Console\Input\InputInterface $input | ||
* @return string | ||
*/ | ||
protected function getVersion(InputInterface $input) | ||
{ | ||
if ($input->getOption('dev')) { | ||
return 'dev-master'; | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
|
||
/** | ||
* Verify that the application does not already exist. | ||
* | ||
* @param string $directory | ||
* @return void | ||
*/ | ||
protected function verifyApplicationDoesntExist($directory) | ||
{ | ||
if ((is_dir($directory) || is_file($directory)) && $directory != getcwd()) { | ||
throw new RuntimeException('Application already exists!'); | ||
} | ||
} | ||
|
||
/** | ||
* Get the composer command for the environment. | ||
* | ||
* @return string | ||
*/ | ||
protected function findComposer() | ||
{ | ||
$composerPath = getcwd() . '/composer.phar'; | ||
|
||
if (file_exists($composerPath)) { | ||
return '"' . PHP_BINARY . '" ' . $composerPath; | ||
} | ||
|
||
return 'composer'; | ||
} | ||
|
||
/** | ||
* Run the given commands. | ||
* | ||
* @param array $commands | ||
* @param \Symfony\Component\Console\Input\InputInterface $input | ||
* @param \Symfony\Component\Console\Output\OutputInterface $output | ||
* @param array $env | ||
* @return \Symfony\Component\Process\Process | ||
*/ | ||
protected function runCommands($commands, InputInterface $input, OutputInterface $output, array $env = []) | ||
{ | ||
if ($input->getOption('no-ansi')) { | ||
$commands = array_map(function ($value) { | ||
if (substr($value, 0, 5) === 'chmod') { | ||
return $value; | ||
} | ||
|
||
return $value . ' --no-ansi'; | ||
}, $commands); | ||
} | ||
|
||
if ($input->getOption('quiet')) { | ||
$commands = array_map(function ($value) { | ||
if (substr($value, 0, 5) === 'chmod') { | ||
return $value; | ||
} | ||
|
||
return $value . ' --quiet'; | ||
}, $commands); | ||
} | ||
|
||
$process = Process::fromShellCommandline(implode(' && ', $commands), null, $env, null, null); | ||
|
||
if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) { | ||
try { | ||
$process->setTty(true); | ||
} catch (RuntimeException $e) { | ||
$output->writeln('Warning: ' . $e->getMessage()); | ||
} | ||
} | ||
|
||
$process->run(function ($type, $line) use ($output) { | ||
$output->write(' ' . $line); | ||
}); | ||
|
||
return $process; | ||
} | ||
|
||
/** | ||
* Replace the given string in the given file. | ||
* | ||
* @param string $search | ||
* @param string $replace | ||
* @param string $file | ||
* @return void | ||
*/ | ||
protected function replaceInFile(string $search, string $replace, string $file) | ||
{ | ||
file_put_contents( | ||
$file, | ||
str_replace($search, $replace, file_get_contents($file)) | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "moslem/installer", | ||
"description": "Apiato application installer.", | ||
|
||
"require": { | ||
"php": "^7.4|^8.0", | ||
"symfony/console": "5.2.6", | ||
"symfony/process": "5.2.4" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Apiato\\Installer\\Console\\": "commands/" | ||
} | ||
} | ||
} |