Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workspace #4

Open
ghostwriter opened this issue Jan 16, 2021 · 4 comments
Open

Workspace #4

ghostwriter opened this issue Jan 16, 2021 · 4 comments

Comments

@ghostwriter
Copy link
Owner

ghostwriter commented Jan 16, 2021

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

  • middleware
  • events & listeners to hook into all the things
  • tools ($workspace->psalm($level) )
@ghostwriter
Copy link
Owner Author

ghostwriter commented Jan 12, 2024

  • based on the filetype passed to $workspace->open('README.md');, $workspace->open('test.php');, $workspace->open('phpunit.xml'); return a MarkdownDocument, PhpDocument, XmlDocument and define additional methods in the respective document classes.
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);
        }
    }
  • the document is a stream resource handler (internally)

@ghostwriter
Copy link
Owner Author

create functionality (LSP-like) protocol.

@ghostwriter
Copy link
Owner Author

  • PhpDocument (will include AST via nikic/php-parser or microsoft/tolerant-php-parser)

@ghostwriter
Copy link
Owner Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant