codesvault/wp-bundler 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

codesvault/wp-bundler

Composer 安装命令:

composer require codesvault/wp-bundler

包简介

WP Bundler is a CI/CD tool. It can configure and run build process, and bundle zip(s) for WordPress plugins.

README 文档

README

Build dependencies & Bundle production ready plugin.
WP Bundler is a CI/CD tool. It can configure and run build process, and bundle zip(s) for WordPress plugins.


Requirements

  • Environment: Mac, Linux.
  • PHP CLI 7.4 >=
  • Composer


Installation

It is required to use composer to install WP Bundler.

composer require codesvault/wp-bundler --dev

Setup

Create a bundler file in the root folder of your plugin. E.g. wp-content/plugins/kathamo/bundler. Add the below code in the file.

#!/usr/bin/env php
<?php

use CodesVault\Bundle\Bundler;
use CodesVault\Bundle\Setup;

require __DIR__ . "/vendor/autoload.php";

$bundler = new Bundler(__DIR__);

Make a .distignore file in the root folder of your plugin. Add all those files, folders which you want to exclude from the production zip like below.

bundler

node_modules
package.json
package-lock.json

composer.json
composer.lock

assets/dev


Uses

// basic uses

$bundler
    ->createProductionRepo('kathamo')
    ->command("composer install --no-dev")
    ->command("npm install")
    ->command("npm run build")
    ->cleanUp()
    ->zip('kathamo')
    ->executionTime();

Now Let's add build pipeline in the above bundler file using this codes. Then from terminal cd into plugin's folder and run the below command to create a production zip.

php bundler

It's creating a repo in the /pluginName/prod folder then running build command then 'cleaning' up the repo based on .distignore and finally making a zip.



Envirnoment variables

Get env file data using WP Bundler.

// .env file
DEV_MODE='true'
TIERS_PRODUCTIDS="basic:1722795,plus:1722797,elite:1722799"


// bundler file
$env = CodesVault\Bundle\Setup::loadEnv(__DIR__, '.env');

if ('true' === $env->getenv('DEV_MODE')) {
  $bundler
    ->command("composer install")
    ->command("npm install")
    ->command("npm run build");
}

$tiers_pids = $setup->kv($setup->getEnv('TIERS_PID'));
// array (
//   [
//     'key'   => 'basic',
//     'value' => '1722795',
//   ],
//   [
//     'key'   => 'plus',
//     'value' => '1722797',
//   ],
//   [
//     'key'   => 'elite',
//     'value' => '1722799',
//   ],
// );


Update File content

You can also update specific file data dynamically before making the zip using updateFileContent api.

Configuration

Create a bundler-schema.json file in the root folder of your plugin. bundler-schema.json file data structure will be like below.

{
  "kathamo": {
    "path": "",
    "extension": "php",
    "schema": [
      {
        "target": "Plugin Name: Kathamo",
        "template": "Plugin Name: Kathamo {{tier_name}}"
      },
      {
        "target": "Version: 1.5.2",
        "template": "Version: {{release_version}}"
      },
      {
          "target": "define('CV_VERSION', '1.5.2');",
          "template": "define('CV_VERSION', '{{release_version}}');"
      }
    ]
  },
  "README": {
    "path": "",
    "extension": "txt",
    "schema": [
      {
        "target": "Version: 1.5.2",
        "template": "Version: {{release_version}}"
      }
    ]
  }
}

Here Kathamo, README these keys are the file names. path is the file's relative path. extension is the file extension. schema is the array of objects where target is the data which you want to update and template is the data which you want to replace with.


Usage

// bundler file

// bundler-schema.json file's `{{placeholder}}` name should be same as the key names in the below array.
$intended_data = [
  "tier_name"       => "Pro",
  "release_version" => $setup->getEnv('RELEASE_VERSION'),
];

$bundler
  ->createProductionRepo('kathamo')
  ->command("composer install --no-dev")
  ->cleanUp()
  ->updateFileContent($intended_data)
  ->zip('kathamo');


Find and Replace

Update entire plugin file's data using findAndReplace api.

$bundler
  ->createProductionRepo('kathamo')
  ->findAndReplace([
    [
      'find'          => "use Kathamo\\Bundle\\Bundler;",
      'updated_data'  => "use CodesVault\\Kathamo\\Bundle\\Bundler;",
    ]
  ])
  ->cleanUp()
  ->zip($zip_name);


Multiple Zips

When you want to create multiple zip, use buildIterator api.

// .env file
TIERS_PID="basic:123,plus:231,pro:3240"


// bundler file
$setup = Setup::loadEnv(__DIR__, '.env');
$tiers_pids = $setup->kv($setup->getEnv('TIERS_PID'));

$bundler
  ->createProductionRepo('kathamo')
  ->command("composer install --no-dev")
  ->command("npm install")
  ->command("npm run build")
  ->cleanUp()
  ->buildIterator($tiers_pids, function($meta, $builder) {
    $zip_name = "kathamo-" . $meta['key'] . "-" . $meta['value'];

    $builder
      ->zip($zip_name);
  })
  ->executionTime();


Callable Function as command

You can also use callable or callback function as command. It will be executed in the prod repo folder.

$bundler
  ->createProductionRepo('kathamo')
  ->command('foo', $env) // foo is a callable function with $env parameter
  ->command(function() {
    echo "Hello world!\n";
  })
  ->cleanUp()
  ->buildIterator($tiers_pids, function($meta, $builder) {
    $zip_name = "kathamo-" . $meta['key'] . "-" . $meta['value'];

    $builder
      ->zip($zip_name);
  })
  ->executionTime();


function foo() {
  // Do something here
}


Example

Here is an example of a bundler file.

#!/usr/bin/env php
<?php

require __DIR__ . "/vendor/autoload.php";

// data loaded from .env file
$setup = Setup::loadEnv(__DIR__, '.env');
$tiers_pids = $setup->kv($setup->getEnv('TIERS_PID'));

$bundler
  ->createProductionRepo('kathamo')
  ->command("composer install --no-dev")
  ->command("npm install")
  ->command("npm run build")
  ->cleanUp()
  ->copy('/schema.json', '/schema.json')
  ->renameProdFile('kathamo.php', 'kathamo-pro.php')
  ->buildIterator($tiers_pids, function($meta, $builder) {
    $zip_name = "kathamo-" . $meta['key'] . "-" . $meta['value'];
    $intended_data = [
      "tier_name"       => $meta['tier'],
      "release_version" => $setup->getEnv('RELEASE_VERSION'),
    ];

    $builder
      ->updateFileContent($intended_data)
      ->findAndReplace([
        [
          'find'          => "use Kathamo\\Bundle\\Bundler;",
          'updated_data'  => "use CodesVault\\Kathamo\\Bundle\\Bundler;",
        ]
      ])
      ->zip($zip_name);
  })
  ->executionTime();

codesvault/wp-bundler 适用场景与选型建议

codesvault/wp-bundler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 654 次下载、GitHub Stars 达 10, 最近一次更新时间为 2024 年 05 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 codesvault/wp-bundler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 codesvault/wp-bundler 我们能提供哪些服务?
定制开发 / 二次开发

基于 codesvault/wp-bundler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 654
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 10
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-22