Skip to content

Commit

Permalink
Merge branch 'master' into no-interaction
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Concerns/RunsCommands.php
  • Loading branch information
jasonvarga committed Oct 24, 2024
2 parents e92fd32 + 2abc0f8 commit 272215c
Show file tree
Hide file tree
Showing 9 changed files with 694 additions and 145 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/pint-fix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Fix PHP code style issues

on:
push:
paths:
- '**.php'

permissions:
contents: write

jobs:
fix-php-code-styling:
runs-on: ubuntu-latest
if: github.repository_owner == 'statamic'

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
token: ${{ secrets.PINT }}

- name: Fix PHP code style issues
uses: aglipanci/laravel-pint-action@v2
with:
pintVersion: 1.16.0

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Fix styling
21 changes: 21 additions & 0 deletions .github/workflows/pint-link.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Lint PHP code style issues

on:
pull_request:
paths:
- '**.php'

jobs:
lint-php-code-styling:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Check PHP code style issues
uses: aglipanci/laravel-pint-action@v2
with:
testMode: true
verboseMode: true
pintVersion: 1.16.0
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Statamic CLI Tool

![Statamic 3.0+](https://img.shields.io/badge/Statamic-3.0+-FF269E?style=for-the-badge&link=https://statamic.com)

🌴 Install and manage your **Statamic** projects from the command line.

- [Installing the CLI tool](#installing-the-cli-tool)
Expand Down
166 changes: 166 additions & 0 deletions src/Concerns/ConfiguresDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php

namespace Statamic\Cli\Concerns;

use function Laravel\Prompts\select;

/**
* The code in this trait is a slightly modified version of the code in Laravel's Installer CLI.
* See: https://github.com/laravel/installer/blob/master/src/NewCommand.php
*/
trait ConfiguresDatabase
{
/**
* Determine the default database connection.
*/
protected function promptForDatabaseOptions(): string
{
$defaultDatabase = collect(
$databaseOptions = $this->databaseOptions()
)->keys()->first();

if ($this->input->isInteractive()) {
$database = select(
label: 'Which database will your application use?',
options: $databaseOptions,
default: $defaultDatabase,
);
}

return $database ?? $defaultDatabase;
}

/**
* Get the available database options.
*/
protected function databaseOptions(): array
{
return collect([
'sqlite' => ['SQLite', extension_loaded('pdo_sqlite')],
'mysql' => ['MySQL', extension_loaded('pdo_mysql')],
'mariadb' => ['MariaDB', extension_loaded('pdo_mysql')],
'pgsql' => ['PostgreSQL', extension_loaded('pdo_pgsql')],
'sqlsrv' => ['SQL Server', extension_loaded('pdo_sqlsrv')],
])
->sortBy(fn ($database) => $database[1] ? 0 : 1)
->map(fn ($database) => $database[0].($database[1] ? '' : ' (Missing PDO extension)'))
->all();
}

/**
* Configure the default database connection.
*/
protected function configureDefaultDatabaseConnection(string $database, string $name): void
{
$this->pregReplaceInFile(
'/DB_CONNECTION=.*/',
'DB_CONNECTION='.$database,
$this->absolutePath.'/.env'
);

$this->pregReplaceInFile(
'/DB_CONNECTION=.*/',
'DB_CONNECTION='.$database,
$this->absolutePath.'/.env.example'
);

if ($database === 'sqlite') {
$environment = file_get_contents($this->absolutePath.'/.env');

// If database options aren't commented, comment them for SQLite...
if (! str_contains($environment, '# DB_HOST=127.0.0.1')) {
$this->commentDatabaseConfigurationForSqlite($this->absolutePath);

return;
}

return;
}

// Any commented database configuration options should be uncommented when not on SQLite...
$this->uncommentDatabaseConfiguration($this->absolutePath);

$defaultPorts = [
'pgsql' => '5432',
'sqlsrv' => '1433',
];

if (isset($defaultPorts[$database])) {
$this->replaceInFile(
'DB_PORT=3306',
'DB_PORT='.$defaultPorts[$database],
$this->absolutePath.'/.env'
);

$this->replaceInFile(
'DB_PORT=3306',
'DB_PORT='.$defaultPorts[$database],
$this->absolutePath.'/.env.example'
);
}

$this->replaceInFile(
'DB_DATABASE=laravel',
'DB_DATABASE='.str_replace('-', '_', strtolower($name)),
$this->absolutePath.'/.env'
);

$this->replaceInFile(
'DB_DATABASE=laravel',
'DB_DATABASE='.str_replace('-', '_', strtolower($name)),
$this->absolutePath.'/.env.example'
);
}

/**
* Comment the irrelevant database configuration entries for SQLite applications.
*/
protected function commentDatabaseConfigurationForSqlite(string $directory): void
{
$defaults = [
'DB_HOST=127.0.0.1',
'DB_PORT=3306',
'DB_DATABASE=laravel',
'DB_USERNAME=root',
'DB_PASSWORD=',
];

$this->replaceInFile(
$defaults,
collect($defaults)->map(fn ($default) => "# {$default}")->all(),
$directory.'/.env'
);

$this->replaceInFile(
$defaults,
collect($defaults)->map(fn ($default) => "# {$default}")->all(),
$directory.'/.env.example'
);
}

/**
* Uncomment the relevant database configuration entries for non SQLite applications.
*/
protected function uncommentDatabaseConfiguration(string $directory): void
{
$defaults = [
'# DB_HOST=127.0.0.1',
'# DB_PORT=3306',
'# DB_DATABASE=laravel',
'# DB_USERNAME=root',
'# DB_PASSWORD=',
];

$this->replaceInFile(
$defaults,
collect($defaults)->map(fn ($default) => substr($default, 2))->all(),
$directory.'/.env'
);

$this->replaceInFile(
$defaults,
collect($defaults)->map(fn ($default) => substr($default, 2))->all(),
$directory.'/.env.example'
);
}
}
28 changes: 16 additions & 12 deletions src/Concerns/RunsCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ trait RunsCommands
/**
* Run the given command.
*
* @param string $command
* @param bool $disableOutput
* @return Process
*/
protected function runCommand($command, $disableOutput = false)
protected function runCommand(string $command, ?string $workingPath = null, bool $disableOutput = false)
{
return $this->runCommands([$command], $disableOutput);
return $this->runCommands([$command], $workingPath, $disableOutput);
}

/**
* Run the given commands.
*
* @param array $commands
* @param bool $disableOutput
* @return Process
*/
protected function runCommands($commands, $disableOutput = false)
protected function runCommands(array $commands, ?string $workingPath = null, bool $disableOutput = false)
{
if (! $this->output->isDecorated()) {
$commands = array_map(function ($value) {
if (substr($value, 0, 5) === 'chmod' || substr($value, 0, 3) === 'rm ') {
if (str_starts_with($value, 'chmod') || str_starts_with($value, 'rm ')) {
return $value;
}

if (str_starts_with($value, 'git')) {
return $value;
}

Expand All @@ -39,15 +39,19 @@ protected function runCommands($commands, $disableOutput = false)

if ($this->input->getOption('quiet')) {
$commands = array_map(function ($value) {
if (substr($value, 0, 5) === 'chmod' || substr($value, 0, 3) === 'rm ') {
if (str_starts_with($value, 'chmod') || str_starts_with($value, 'rm ')) {
return $value;
}

if (str_starts_with($value, 'git')) {
return $value;
}

return $value.' --quiet';
}, $commands);
}

$process = Process::fromShellCommandline(implode(' && ', $commands), null, null, null, null);
$process = Process::fromShellCommandline(implode(' && ', $commands), $workingPath, timeout: null);

if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) {
try {
Expand All @@ -57,15 +61,15 @@ protected function runCommands($commands, $disableOutput = false)
$process->setTty(true);
}
} catch (RuntimeException $e) {
$this->output->writeln('Warning: '.$e->getMessage());
$this->output->writeln(' <bg=yellow;fg=black> WARN </> '.$e->getMessage().PHP_EOL);
}
}

if ($disableOutput) {
$process->disableOutput()->run();
} else {
$process->run(function ($type, $line) {
$this->output->write($line);
$this->output->write(' '.$line);
});
}

Expand Down
Loading

0 comments on commit 272215c

Please sign in to comment.