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

Runtime adapters #1

Open
adamziel opened this issue Feb 13, 2024 · 0 comments
Open

Runtime adapters #1

adamziel opened this issue Feb 13, 2024 · 0 comments

Comments

@adamziel
Copy link
Collaborator

adamziel commented Feb 13, 2024

Blueprints should support at least the following runtimes:

  • wp-cli
  • Docker
  • wp-now
  • Playground
  • Native PHP CLI
  • VS Code extension

Runtime-specific features

Caching the downloads

Blueprints shouldn't download the same 10MB WordPress.zip twice. Let's make sure we cache the downloads and invalidate the entries as new versions of Core and Plugins are released.

  • In native PHP CLI, we can cache in the filesystem, e.g. ~/.wp/. Ideally, this would take HTTP headers into consideration.
  • In Playground, we can rely on HTTP cache built into fetch(). In addition, we could cache at the service worker level.
  • In VS Code, there's a caching API AFAIR.
  • In Docker, we could use the docker build layers for caching.

Network calls

  • In native PHP CLI, we can use libcurl or fopen("https://")
  • In Playground, we can only rely on fetch(). We can't open raw TCP sockets so fopen("https://") and libcurl cannot be easily used.
  • In VS Code, we can open raw TCP sockets so fopen("https://"), but libcurl isn't since Playground doesn't support it yet.

Spawning child processes

Blueprint steps, wp-cli, phpunit, and other tools will often spawn child processes.

Reporting progress

  • In any CLI, we should display an ASCII progress bar.
  • With Docker as a backend, we should report the build progress somehow.
  • In Playground, progress bar is a <div> that needs to change its widths and innerText

Filesystem interactions

I'd love to avoid abstract the filesystem and just use the regular PHP functions like copy() and fread()

Local mounts

It may useful to support mounting directories

  • In the browser, we would either bale out (with a warning?) or mount an OPFS directory, whether that's page-specific or a local directory handle
  • In wp-now and Docker, we can mount directories in the runtime without changing anything on the disk
  • In Native PHP we could use symlinks

Technical implementation

Dependency injection

Let's explore the dependency injection pattern. Pimple is a tiny service container we can use to plug-in runtime-specific implementations:

$container = new Container();
switch ( $runtime ) {
	case self::RUNTIME_NATIVE:
		$container['downloads_cache']   = function ( $c ) {
			return new FileCache();
		};
		$container['http_client']       = function ( $c ) {
			return HttpClient::create();
		};
		$container['progress_reporter'] = function ( $c ) {
			return function ( ProgressEvent $event ) {
				echo $event->url . ' ' . $event->downloadedBytes . '/' . $event->totalBytes . "                         \r";
			};
		};
		break;
	case self::RUNTIME_PLAYGROUND:
		$container['downloads_cache']   = function ( $c ) {
			// @TODO
		};
		$container['http_client']       = function ( $c ) {
			// @TODO
		};
		$container['progress_reporter'] = function ( $c ) {
			// @TODO
			// post_message_to_js();
		};
		break;

Other ideas

What are other viable solutions to this problem?

This was referenced Feb 13, 2024
@adamziel adamziel added this to the Shape PHP Blueprint milestone Feb 29, 2024
@adamziel adamziel mentioned this issue Mar 19, 2024
14 tasks
@adamziel adamziel moved this to Future work in Playground Board Jun 30, 2024
brandonpayton added a commit to brandonpayton/blueprints-library that referenced this issue Oct 25, 2024
Runtime:       PHP 8.3.12
Configuration: /Users/brandon/src/blueprints-library/phpunit.xml.dist

...................................................               51 / 51 (100%)

Time: 00:00.021, Memory: 10.00 MB

Blueprint Mapper (unit\blueprint\BlueprintMapper)
 ✔ Maps empty blueprint
 ✔ Maps word press version
 ✔ Maps multiple plugins
 ✔ Maps plugins with different data types
 ✔ Fails when plugins with invalid data types
 ✔ Maps when specific step appears twice
 ✔ Maps wp config constants
 ✔ Maps site options

Json Mapper (unit\json_mapper\JsonMapper)
 ✔ Custom factory
 ✔ Maps to array object
 ✔ Sets public properties
 ✔ Sets private properties with setter
 ✔ Sets protected properties with setter
 ✔ Fails setting private property with no setter
 ✔ Fails setting protected property with no setter
 ✔ Maps to deep scalar array
 ✔ Maps to deep mixed array
 ✔ Fails when array wrong scalar type
 ✔ Fails when wrong scalar type
 ✔ Maps to array of arrays
 ✔ Maps to array

Mkdir Step Runner (unit\steps\MkdirStepRunner)
 ✔ Create directory when using relative path
 ✔ Create directory when using absolute path
 ✔ Create directory recursively
 ✔ Create readable and writable directory
 ✔ Throw exception when creating directory and it already exists

Property Parser (unit\json_mapper\PropertyParser)
 ✔ Parses properties with scalar types
 ✔ Parses properties with arrays of scalar types
 ✔ Parses properties with arrays
 ✔ Parses properties with no doc blocks
 ✔ Parses properties with public visibility
 ✔ Parses properties with protected visibility
 ✔ Parses properties with private visibility
 ✔ Parses properties with union types
 ✔ Parses properties with global namespace prefixed type
 ✔ Parses properties with null type

Rm Step Runner (unit\steps\RmStepRunner)
 ✔ Remove directory when using absolute path
 ✔ Remove directory when using relative path
 ✔ Remove directory with subdirectory
 ✔ Remove directory with file
 ✔ Remove file
 ✔ Throw exception when removing nonexistent directory and using relative path
 ✔ Throw exception when removing nonexistent directory and using absolute path
 ✔ Throw exception when removing nonexistent file and using absolute path
 ✔ Throw exception when removing nonexistent file and using relative path

Unzip Step Runner (unit\steps\UnzipStepRunner)
 ✔ Unzip file when using absolute path
 ✔ Unzip file when using relative path

Zip Stream Writer
 ✔ Write file from path with data set #0
 ✔ Write file from path with data set WordPress#1
 ✔ Write file from string with data set #0
 ✔ Write file from string with data set WordPress#1

OK (51 tests, 80 assertions) for running tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant