From 2917cb73324f359130a22ed505a9fc0d5eb73cec Mon Sep 17 00:00:00 2001 From: Roman Axelrod Date: Thu, 18 Aug 2022 05:46:55 +0300 Subject: [PATCH] Added WordPress adaptation. --- build.cjs | 43 ++++++++++ generators/block/index.cjs | 6 ++ generators/block/templates/package.json | 17 ++-- package.json | 4 +- platforms/php/build.php | 103 ++++++++++++++++++++++++ platforms/php/composer.json | 11 +++ platforms/php/composer.lock | 80 ++++++++++++++++++ 7 files changed, 255 insertions(+), 9 deletions(-) create mode 100755 build.cjs create mode 100644 platforms/php/build.php create mode 100644 platforms/php/composer.json create mode 100644 platforms/php/composer.lock diff --git a/build.cjs b/build.cjs new file mode 100755 index 0000000..7d67ea9 --- /dev/null +++ b/build.cjs @@ -0,0 +1,43 @@ +#!/usr/bin/env node + +const {exec} = require('child_process'); +const config = require('config'); +const Generator = require("yeoman-generator"); + +const isDev = process.env.NODE_ENV === 'development'; // Check README file in case you get "missing files" error. +const modulePath = isDev ? '' : 'node_modules/create-block-dev-tool/'; + +const blockName = config.has('blockName') ? config.get('blockName') : 'development'; + +module.exports = class extends Generator { + async prompting() { + this.data = await this.prompt([ + { + type: "list", + name: "platfrom", + message: "Choose Platform", + choices: ['WordPress', 'Hubspot', 'JavaScript', 'PHP'], + default: 'WordPress' + } + ]) + } + + writing() { + new Promise((resolve => { + if (['WordPress', 'PHP'].includes(this.data.platfrom)) { + exec(`cd adapters/php && composer install && php build.php '${blockName}' '${modulePath}'`, function (error, stdout, stderr) { + console.log(stdout); + resolve(); + }); + } else if (this.data.platform === 'Hubspot') { + console.log('Coming soon...'); + resolve(); + } else { + resolve(); + } + })) + .then(() => { + console.log('--------------------\nDone!'); + }); + } +} diff --git a/generators/block/index.cjs b/generators/block/index.cjs index b4d4d5a..ae4d95c 100644 --- a/generators/block/index.cjs +++ b/generators/block/index.cjs @@ -34,6 +34,12 @@ module.exports = class extends Generator { message: "Provide declaration of 1rem:", default: 16 }, + { + type: 'input', + name: 'devToolSource', + message: 'DevTool Version/Source (master)', + default: 'git+https://roman-axe-web@bitbucket.org/axeweb/create-block-dev-tool.git#master' + } ]); } diff --git a/generators/block/templates/package.json b/generators/block/templates/package.json index e1f5a64..e95c2ed 100644 --- a/generators/block/templates/package.json +++ b/generators/block/templates/package.json @@ -1,10 +1,11 @@ { - "name": "<%= blockFilename %>", - "version": "1.0.0", - "scripts": { - "start": "component-dev" - }, - "devDependencies": { - "create-block-dev-tool": "git+https://roman-axe-web@bitbucket.org/axeweb/create-block-dev-tool.git#master" - } + "name": "<%= blockFilename %>", + "version": "1.0.0", + "scripts": { + "start": "component-dev", + "build": "component-build" + }, + "devDependencies": { + "create-block-dev-tool": "<%= devToolSource %>" + } } diff --git a/package.json b/package.json index 040da4c..b56eab3 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "dev": "NODE_ENV=development node server.js", "generate-block": "yo ./generators/block/index.cjs", "build": "rollup --config rollup.config.js", + "build-platform": "NODE_ENV=development yo ./build.cjs", "dev-js": "rollup --config rollup.config.js --watch" }, "license": "ISC", @@ -48,6 +49,7 @@ "styled-components": "^5.3.5" }, "bin": { - "component-dev": "./server.js" + "component-dev": "./server.js", + "component-build": "yo ./build.cjs" } } diff --git a/platforms/php/build.php b/platforms/php/build.php new file mode 100644 index 0000000..10b9f45 --- /dev/null +++ b/platforms/php/build.php @@ -0,0 +1,103 @@ +add_handlebar( 'esc_url', function ( $context ) { + if ( function_exists( 'esc_url' ) ) { + return esc_url( $context ); + } + + return $context; + } ); + + $this->add_handlebar( 'esc_attr', function ( $context ) { + if ( function_exists( 'esc_attr' ) ) { + return esc_attr( $context ); + } + + return $context; + } ); + + $this->add_handlebar( 'esc_html', function ( $context ) { + if ( function_exists( 'esc_html' ) ) { + return esc_html( $context ); + } + + return $context; + } ); + + $this->add_handlebar( 'wp_kses_post', function ( $context ) { + if ( function_exists( 'wp_kses_post' ) ) { + return wp_kses_post( $context ); + } + + return $context; + } ); + } + + public function add_handlebar( $key, $func ) { + $this->custom_handlebars[ $key ] = $func; + } +} + +class Component_Builder { + use Custom_Handlebars; + + public string $component_name = ''; + private string $module_path = ''; + + function __construct( $component_name, $module_path ) { + $this->module_path = $module_path; + $this->component_name = $component_name; + $this->register_default_handlebar_helpers(); + } + + #[NoReturn] + function build() { + $file_name = $this->get_handlebars_template( __DIR__ . '/' . $this->module_path . '/../../src/' . $this->component_name . '.template.hbs' ); + echo "Generated '$file_name'."; + } + + private function get_handlebars_template( $path = '' ): string { + $template = file_get_contents( $path ); + $phpStr = LightnCandy::compile( $template, + [ + 'flags' => Flags::FLAG_NOESCAPE | Flags::FLAG_PARENT | Flags::FLAG_SPVARS | Flags::FLAG_ELSE | Flags::FLAG_JSLENGTH | Flags::FLAG_JSTRUE, + 'helpers' => $this->custom_handlebars ?? [], + ] + ); + + /** + * NOTE: + * PHP 8.0.0 has problems with the LightCandy lib. + * If you're running the exact php8.0.0 version, try to downgrade to LightCandy@1.2.5. + */ + + return self::create_cache_file( $path, $phpStr ); + } + + private static function create_cache_file( string $file_path, string $content ): string { + $file_path_parts = explode( ".", $file_path ); + array_pop( $file_path_parts ); // remove ".hbs" format. + $file_path_parts[] = 'php'; + $file_path = join( '.', $file_path_parts ); + + $t = file_put_contents( $file_path, '' ); + if ( $t === false ) { + die( "Error: Can't generate HBS template to PHP file. Cache folder is not accessible." ); + } + + return $file_path; + } +} + +( new Component_Builder( $argv[1], $argv[2] ) )->build(); diff --git a/platforms/php/composer.json b/platforms/php/composer.json new file mode 100644 index 0000000..08ce477 --- /dev/null +++ b/platforms/php/composer.json @@ -0,0 +1,11 @@ +{ + "config": { + "platform": { + "php": "8.0.0" + } + }, + "require": { + "php": ">=8.0", + "zordius/lightncandy": "^1.2.6" + } +} diff --git a/platforms/php/composer.lock b/platforms/php/composer.lock new file mode 100644 index 0000000..3c0f663 --- /dev/null +++ b/platforms/php/composer.lock @@ -0,0 +1,80 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "1d9b4e7a02be366b48383c185193727f", + "packages": [ + { + "name": "zordius/lightncandy", + "version": "v1.2.6", + "source": { + "type": "git", + "url": "https://github.com/zordius/lightncandy.git", + "reference": "b451f73e8b5c73e62e365997ba3c993a0376b72a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/zordius/lightncandy/zipball/b451f73e8b5c73e62e365997ba3c993a0376b72a", + "reference": "b451f73e8b5c73e62e365997ba3c993a0376b72a", + "shasum": "" + }, + "require": { + "php": ">=7.1.0" + }, + "require-dev": { + "phpunit/phpunit": ">=7" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.2.5-dev" + } + }, + "autoload": { + "psr-4": { + "LightnCandy\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Zordius Chen", + "email": "zordius@gmail.com" + } + ], + "description": "An extremely fast PHP implementation of handlebars ( http://handlebarsjs.com/ ) and mustache ( http://mustache.github.io/ ).", + "homepage": "https://github.com/zordius/lightncandy", + "keywords": [ + "handlebars", + "logicless", + "mustache", + "php", + "template" + ], + "support": { + "issues": "https://github.com/zordius/lightncandy/issues", + "source": "https://github.com/zordius/lightncandy/tree/v1.2.6" + }, + "time": "2021-07-11T04:52:41+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=8.0" + }, + "platform-dev": [], + "platform-overrides": { + "php": "8.0.0" + }, + "plugin-api-version": "2.1.0" +}