7 changed files with 255 additions and 9 deletions
@ -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!'); |
|||
}); |
|||
} |
|||
} |
|||
@ -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 %>" |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,103 @@ |
|||
<?php |
|||
|
|||
// Composer - Autoloader |
|||
require_once __DIR__ . '/vendor/autoload.php'; |
|||
|
|||
use JetBrains\PhpStorm\NoReturn; |
|||
use LightnCandy\Flags; |
|||
use LightnCandy\LightnCandy; |
|||
|
|||
trait Custom_Handlebars { |
|||
public array $custom_handlebars = []; |
|||
|
|||
public function register_default_handlebar_helpers() { |
|||
$this->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, '<?php ' . $content . ' ?>' ); |
|||
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(); |
|||
@ -0,0 +1,11 @@ |
|||
{ |
|||
"config": { |
|||
"platform": { |
|||
"php": "8.0.0" |
|||
} |
|||
}, |
|||
"require": { |
|||
"php": ">=8.0", |
|||
"zordius/lightncandy": "^1.2.6" |
|||
} |
|||
} |
|||
@ -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" |
|||
} |
|||
Loading…
Reference in new issue