Skip to content

Commit

Permalink
bin/install installer
Browse files Browse the repository at this point in the history
  • Loading branch information
sixty-nine committed Aug 19, 2022
1 parent f4f2072 commit c37e6cd
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 73 deletions.
10 changes: 2 additions & 8 deletions .github/workflows/behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,9 @@ jobs:
run: |
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
- name: Create .env file
- name: Install timesheep
run: |
cp .env.example .env
- name: Create database file
run: |
mkdir database
touch database/database.db
vendor/bin/doctrine orm:schema-tool:create -q
bin/install
mv database/database.db database/database.test.db
- name: Execute lint
Expand Down
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ Simple timesheet manager.
## Install

```bash
composer install
cp .env.example .env
mkdir database
touch database/database.db
vendor/bin/doctrine orm:schema-tool:create -q
bin/install
```

## Usage
Expand Down
22 changes: 22 additions & 0 deletions bin/doctrine
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env php
<?php

require_once dirname(__DIR__).'/vendor/autoload.php';

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;
use SixtyNine\Timesheep\Bootstrap;

$container = Bootstrap::boostrap();
$commands = [
// If you want to add your own custom console commands,
// you can do so here.
];

/** @var EntityManager $em */
$em = $container->get('em');
ConsoleRunner::run(
new SingleManagerProvider($em),
$commands
);
9 changes: 9 additions & 0 deletions bin/install
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env php
<?php

require_once dirname(__DIR__).'/vendor/autoload.php';

use SixtyNine\Timesheep\Setup;

$setup = new Setup(dirname(__DIR__));
$setup->check();
13 changes: 0 additions & 13 deletions cli-config.php

This file was deleted.

6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
"myclabs/deep-copy": "^1.9",
"league/flysystem": "^1.0",
"symfony/cache": "^5.4",
"doctrine/annotations": "^1.13"
"doctrine/annotations": "^1.13",
"symfony/process": "^4.3"
},
"require-dev": {
"roave/security-advisories": "dev-master",
"phpunit/phpunit": "^8.2",
"squizlabs/php_codesniffer": "~3.4",
"phpstan/phpstan": "^0.12",
"behat/behat": "^3.5",
"symfony/process": "^4.3"
"behat/behat": "^3.5"
},
"autoload": {
"psr-4": {
Expand Down
87 changes: 43 additions & 44 deletions src/SixtyNine/Timesheep/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,88 @@

namespace SixtyNine\Timesheep;

use InvalidArgumentException;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
use Webmozart\Assert\Assert;

class Setup
{
public const TIMESHEEP_DIR = '.timesheep';

/** @var string */
private $homeDir;
private $directory;
/** @var Filesystem */
private $fs;
/** @var ConsoleOutput */
private $output;


public function __construct()
public function __construct(string $directory)
{
$homeDir = posix_getpwuid(posix_getuid());

if (!$homeDir) {
throw new \InvalidArgumentException('Cannot find home directory');
if (!file_exists($directory)) {
throw new InvalidArgumentException('Cannot find directory '.$directory);
}

$this->homeDir = $homeDir['dir'];
$this->fs = new Filesystem(new Local($this->homeDir));
$this->directory = $directory;
$this->fs = new Filesystem(new Local($directory));
$this->output = new ConsoleOutput();
}

public function getInstallDir(): string
{
return $this->homeDir.'/'.self::TIMESHEEP_DIR;
}

public function isInstalled(): bool
{
return $this->fs->has(self::TIMESHEEP_DIR)
&& $this->fs->has(self::TIMESHEEP_DIR.'/config')
&& $this->fs->has(self::TIMESHEEP_DIR.'/database.db')
;
return $this->fs->has('/.env') && $this->fs->has('/database/database.db');
}

public function check(): void
{
if (!$this->isInstalled()) {
$this->output->writeln('Timesheep is not installed, installing...');
Assert::true(
is_writable($this->homeDir),
sprintf('The directory "%s" is not writable', $this->homeDir)
);

$this->install();
if ($this->isInstalled()) {
$this->output->writeln('<question>Timesheep is already installed</question>');
die;
}

$this->output->writeln('Timesheep is not installed, installing...');
Assert::true(
is_writable($this->directory),
sprintf('<error>The directory "%s" is not writable</error>', $this->directory)
);

$this->install();
}

protected function install(): void
{
$tsDir = $this->homeDir.'/'.self::TIMESHEEP_DIR;

if (!$this->fs->has($tsDir)) {
$this->output->writeln(sprintf('Creating home dir <info>%s</info>', $tsDir));
$this->fs->createDir($tsDir);
}

if (!$this->fs->has(self::TIMESHEEP_DIR.'/config')) {
if (!$this->fs->has('/.env')) {
$this->output->writeln('Creating <info>config</info>');
$config = "TIMESHEEP_DB_URL=sqlite://$tsDir/database.db\n";
$config = "TIMESHEEP_DB_URL=sqlite://./database/database.db\n";
$config .= "BOX_STYLE=box\n";
$this->fs->write(self::TIMESHEEP_DIR.'/config', $config);
$this->fs->write('/.env', $config);
}

if (!$this->fs->has(self::TIMESHEEP_DIR.'/database.db')) {
if (!$this->fs->has('/database/database.db')) {
$this->output->writeln('Creating <info>database.db</info>');
$localFs = new Filesystem(new Local(dirname(__DIR__, 3)));
$stream = $localFs->readStream('/database/database.empty.db'); // TODO: add this to phar creation

if (!$stream) {
die('cannot create the database');
try {
if (!$this->fs->has('database')) {
$this->fs->createDir('database');
}
$dbFile = "{$this->directory}/database/database.db";
$process = Process::fromShellCommandline("touch $dbFile");
$process->mustRun();
} catch (ProcessFailedException $ex) {
$this->output->writeln(sprintf('<error>Cannot create the database file: %s</error>', $dbFile));
$this->output->writeln($ex->getMessage());
die;
}

$this->fs->writeStream(self::TIMESHEEP_DIR.'/database.db', $stream);
is_resource($stream) && fclose($stream);
try {
$process = Process::fromShellCommandline('bin/doctrine orm:schema-tool:create -q');
$process->mustRun();
} catch (ProcessFailedException $ex) {
$this->output->writeln('Doctrine error: Cannot install the database');
die;
}
}
}
}

0 comments on commit c37e6cd

Please sign in to comment.