Generate Template_Defaults file.

This commit is contained in:
2022-11-12 23:32:01 +02:00
parent 05660eb4e4
commit ac70e7eb9a
9 changed files with 1224 additions and 65 deletions
@@ -5,26 +5,31 @@ use Core\Global_Functions;
class <%= blockClassModel %>_Component {
public function __construct() {
add_action( 'wp_enqueue_scripts', [ $this, 'register_assets' ] );
// add_action( 'wp_enqueue_scripts', [ $this, 'register_assets' ] );
add_action( 'after_setup_theme', [ $this, 'register_assets' ] );
}
function register_assets(): void {
wp_enqueue_style( '<%= blockFilename %>',
wp_register_style( '<%= blockFilename %>',
get_template_directory_uri() . '/components/partials/<%= blockFilename %>/templates/styles/<%= blockFilename %>.min.css',
[ 'style-wp' ],
Global_Functions::get_current_version_number()
);
wp_enqueue_style( '<%= blockFilename %>' )
wp_enqueue_script( '<%= blockFilename %>',
wp_register_script( 'script-<%= blockFilename %>',
get_template_directory_uri() . '/components/partials/<%= blockFilename %>/templates/scripts/<%= blockFilename %>.min.js',
[ 'jquery', 'swiper' ],
Global_Functions::get_current_version_number(),
true
);
wp_enqueue_script( 'script-<%= blockFilename %>' )
}
public function render( $args = [] ): void {
$output = ( include( __DIR__ . '/templates/<%= blockFilename %>.template.php' ) )( array_merge( [], $args ), self::class );
$args = array_merge( [], Helpers\<%= blockClassModel %>_Defaults::default_args( $args ), $args);
$output = ( include( __DIR__ . '/templates/<%= blockFilename %>.template.php' ) )( $args, self::class );
echo apply_filters( 'the_content', wpautop( $output ) );
}
@@ -8,8 +8,10 @@ namespace AXEWEB_Blocks\Blocks\<%= ownerClass %>\<%= blockClassModel %>\Helpers;
class <%= blockClassModel %>_Defaults {
public static function default_args(): array {
return [
'title' => '<%= title %>',
];
$args = <%- defaultData %>;
// $args['base_url'] = \Core\Global_Functions::get_file_url( __DIR__ . '/../templates/' );
return $args;
}
}
+86 -16
View File
@@ -1,24 +1,28 @@
import path from "path";
import {mkdir, copyFile} from "fs/promises";
import {mkdir, copyFile, readFile} from "fs/promises";
import {capitalize, createFiles, getBlockName, getConfigs, readJSONFile} from "../../helpers.js";
import {fileURLToPath} from 'url';
import {copy} from "fs-extra";
import fsExtra from "fs-extra";
import {exec} from 'child_process';
import execPhp from "exec-php";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const {projectPath} = getConfigs();
const {modulesPath, projectPath} = getConfigs();
export async function buildWordPress(isBlock = false) {
export async function buildWordPress(blockName, isBlock = false) {
const distPath = path.join(projectPath, 'exports', 'wordpress');
await mkdir(distPath, {recursive: true})
// await mkdir(distPath, {recursive: true})
await mkdir(path.join(distPath, 'templates'), {recursive: true})
const jsonFilePath = path.join(projectPath, 'block.json');
await copyFile(jsonFilePath, `${distPath}/block.json`)
const blockFilePath = path.join(projectPath, 'block.json');
await copyFile(blockFilePath, path.join(distPath, 'block.json'));
let data = await readJSONFile(jsonFilePath);
let data = await readJSONFile(blockFilePath);
Object.assign(data, getBlockName(data.name));
// let data = await readJSONFile(path.join(projectPath, `block.json`));
const title = capitalize(data.name);
const owner = capitalize(data.project);
@@ -37,6 +41,39 @@ export async function buildWordPress(isBlock = false) {
include_elementor_widget: false,
});
const backPath = modulesPath ? modulesPath.split('/').map(() => '..').join('/') : '';
const phpGeneratorPath = path.join(modulesPath, 'platforms', 'php');
await execCommand(`cd ${phpGeneratorPath} && composer install`);
await execPHPFile(path.join(phpGeneratorPath, 'build.php'), 'build', {blockName, backPath, projectPath});
await copyStaticFile(
path.join(projectPath, 'src', 'styles', `${data.blockFilename}.min.css`),
path.join(distPath, 'templates', 'styles', `${data.blockFilename}.min.css`),
);
await copyStaticFile(
path.join(projectPath, 'src', 'scripts', `${data.blockFilename}.min.js`),
path.join(distPath, 'templates', 'scripts', `${data.blockFilename}.min.js`),
);
await copy(
path.join(projectPath, 'src', 'images'),
path.join(distPath, 'templates', 'images'),
);
const phpDataObject = await execPHPFile(path.join(phpGeneratorPath, 'build.php'), 'jsonToPhp', {
json: await readJSONFile(path.join(projectPath, 'data', 'default.json'), "utf8"),
});
await createFiles(Object.assign({}, data, {defaultData: phpDataObject}), [{
from: `templates/helpers/Template_Defaults.php`,
to: `helpers/${data.blockClassModel}_Defaults.php`,
}], {
pathDist: distPath,
generatorsPath: path.join(__dirname),
});
if (isBlock) {
await createFiles(data, [{
from: `templates/Template_Component.php`,
@@ -53,14 +90,6 @@ export async function buildWordPress(isBlock = false) {
pathDist: distPath,
generatorsPath: path.join(__dirname)
});
await createFiles(data, [{
from: `templates/helpers/Template_Defaults.php`,
to: `helpers/${data.blockClassModel}_Defaults.php`,
}], {
pathDist: distPath,
generatorsPath: path.join(__dirname)
});
} else {
await createFiles(data, [{
from: `templates/Template_Basic_Component.php`,
@@ -71,3 +100,44 @@ export async function buildWordPress(isBlock = false) {
});
}
}
export function execCommand(cmd = '') {
return new Promise((resolve, reject) => {
exec(cmd, function (error, stdout) {
if (error) {
console.log('Error:', error)
reject(error);
}
// console.log(stdout);
resolve();
});
});
}
function execPHPFile(file = '', functionName = '', args = {}) {
return new Promise((resolve, reject) => {
execPhp(file, 'php', (err, php, out) => {
if (err) {
console.error(out);
return reject(err);
}
php[functionName.toLowerCase()](args, (err, res, out, print) => {
if (err) {
console.error(out);
return reject(err);
}
return resolve(res);
})
})
});
}
async function copyStaticFile(from = '', to = '') {
await mkdir(path.dirname(to), {recursive: true})
await copyFile(from, to);
}