ghostwriter / wip

Template for PHP projects
https://github.com/ghostwriter/wip
BSD 3-Clause "New" or "Revised" License
2 stars 0 forks source link

Workspace #4

Open ghostwriter opened 3 years ago

ghostwriter commented 3 years ago

I would like to build a tool that would allow me to use PHP code to create/update/delete any files and folders and execute commands in a specific directory.

<?php

    $vendor = 'ghostwriter';
    $package = 'container';
    $packageName = $vendor . '/' . $package;
    $vendorNamespace = \ucfirst($vendor);
    $packageClass = \ucfirst($package);
    $packageNamespace = $vendorNamespace . '\\' . $packageClass;
    $packageDescription = 'A simple DI container for PHP';

    $packageAuthor = 'Nathanael Esayeas'; // $workspace->run('git config --get user.name');
    $packageAuthorGithub = $vendor;
    $packageAuthorEmail = 'nathanael.esayeas@protonmail.com'; // $workspace->run('git config --get user.email');

    $directory = Directory::new(__DIR__);
    $workspace = Workspace::new($directory);

    $textDocument = $workspace->open('README.md');

    $textDocument->write('# ' . $packageClass . \PHP_EOL);
    $textDocument->write($packageDescription . \PHP_EOL);

    $textDocument->write('## Installation' . \PHP_EOL . \PHP_EOL);

    $textDocument->write('```bash' . \PHP_EOL);
    $textDocument->write('composer require ' . $packageName . \PHP_EOL);
    $textDocument->write('```' . \PHP_EOL . \PHP_EOL);

    $textDocument->write('## Usage' . \PHP_EOL);

    $textDocument->write('```php' . \PHP_EOL);
    $textDocument->write('use ' . $packageNamespace . ';' . \PHP_EOL . \PHP_EOL);
    $textDocument->write('$' . $package . ' = ' . $packageClass . '::new();' . \PHP_EOL . \PHP_EOL);
    $textDocument->write('```' . \PHP_EOL . \PHP_EOL);

    $textDocument->save();

    $textDocument = $workspace->open('composer.json');

    $composerJson = [];
    $composerJson['name'] = $packageName;
    $composerJson['description'] = $packageDescription;
    $composerJson['type'] = 'library';
    $composerJson['keywords'] = [$vendor, $package];
    $composerJson['license'] = 'BSD-3-Clause';
    $composerJson['authors']['name'] = $packageAuthor;
    $composerJson['authors']['email'] = $packageAuthorEmail;
    $composerJson['authors']['homepage'] = 'https://github.com/' . $vendor;
    $composerJson['authors']['role'] = 'Developer';
    $composerJson['homepage'] = 'https://github.com/' . $packageName;
    $composerJson['funding']['type'] = 'github';
    $composerJson['funding']['url'] = 'https://github.com/sponsors/' . $vendor;
    $composerJson['require']['php'] = '>=8.2';
    $composerJson['require-dev']['phpunit/phpunit'] = '^10.5';
    $composerJson['require-dev']['vimeo/psalm'] = '^5.19';
    $composerJson['autoload']['psr-4'][$packageNamespace . '\\'] = 'src';
    $composerJson['autoload-dev']['psr-4'][$packageNamespace . 'Tests\\'] = 'tests';
    $composerJson['scripts']['test'] = 'phpunit';
    $composerJson['scripts']['psalm'] = 'psalm';

    $textDocument->write(\json_encode($composerJson, \JSON_PRETTY_PRINT));
    $textDocument->save();

    $workspace->run('composer install');

    $textDocument = $workspace->open('phpunit.xml.dist');
    $phpunitXml = '...';
    $textDocument->write($phpunitXml);
    $textDocument->save();

    $textDocument = $workspace->open('src/' . $packageClass . '.php');
    $packageClassPhp = '...';
    $textDocument->write($packageClassPhp);
    $textDocument->save();

    $textDocument = $workspace->open('tests/' . $packageClass . 'Test.php');
    $packageClassTestPhp = '...';
    $textDocument->write($packageClassTestPhp);
    $textDocument->save();

    $workspace->run('composer test');
    $workspace->run('composer psalm');

Then add custom

ghostwriter commented 8 months ago

public function open(string $file): ?Document
    {
        $extension = pathinfo($file, PATHINFO_EXTENSION);

        switch ($extension) {
            case 'md':
                return new MarkdownDocument($file);
            case 'php':
                return new PhpDocument($file);
            case 'xml':
                return new XmlDocument($file);
            default:
                return new TextDocument($file);
        }
    }
ghostwriter commented 8 months ago

create functionality (LSP-like) protocol.

ghostwriter commented 8 months ago
ghostwriter commented 8 months ago

Directory is separated out because i want to use it to build an api to use both real directories that exist, and virtual directories that only exist in-memory.

for virtual storage, we can use array,sqlite(:memory:) or sqlite:file with a custom table)