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

Add code for scaffold templating. #4

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions .github/workflows/fill-in-scaffold.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { statSync } from 'fs';
import { readdir, readFile } from 'fs/promises';
import { rename as moveFile, writeFile } from 'fs/promises';
import { join as joinPath } from 'path';
import process from 'process';

const repository = JSON.parse( process.argv[2] );

const skip_dirs = [
'.github',
'.git'
];

/**
* @param {string} dirPath
* @param {(filePath: string) => Promise<void>} callback
*/
const traverseDirectory = async ( dirPath, callback ) => {
if ( skip_dirs.includes( dirPath ) ) {
console.log( 'Skipping %s', dirPath );
return;
}
console.log( 'Traversing %s', dirPath );

// Read the contents of the directory
const files = await readdir( dirPath );

// Loop over each file in that directory
for ( const file of files ) {
// Construct the full path to the file
const filePath = joinPath( dirPath, file );

// Check if the current entry is a file
if ( statSync( filePath ).isFile() ) {
await callback( filePath );
} else {
// Recursively traverse directories
await traverseDirectory( filePath, callback );
}
}
};

/**
* Build a template using envs
* @param {string} filePath
*/
const buildTemplate = async ( filePath ) => {
console.log( 'Building %s', filePath );

// Read the template file
const templateFile = await readFile( filePath, 'utf-8' );

// Do all the swaps!
let renderedTemplate = templateFile;

// Some README.md specific strings...
if ( 'README.md' == filePath ) {
renderedTemplate = renderedTemplate.replaceAll(
'EXAMPLE_REPO_NAME',
repository.custom_properties['human-title'] ?? repository.name
);
renderedTemplate = renderedTemplate.replaceAll(
'EXAMPLE_REPO_DESCRIPTION',
repository.description
);
renderedTemplate = renderedTemplate.replaceAll(
'EXAMPLE_REPO_PROD_URL',
repository.custom_properties['site-production-url']
);
renderedTemplate = renderedTemplate.replaceAll(
'EXAMPLE_REPO_DEV_URL',
repository.custom_properties['site-development-url']
);
}

if ( 'project' == repository.custom_properties['repo-type'] ) {
// Some existing string swaps...
renderedTemplate = renderedTemplate.replaceAll(
'A demo project for showcasing standardized build processes for various asset types.',
repository.description
);
renderedTemplate = renderedTemplate.replaceAll(
'build-processes-demo-production.mystagingwebsite.com',
repository.custom_properties['site-production-url']
);
renderedTemplate = renderedTemplate.replaceAll(
'build-processes-demo',
repository.name
);
renderedTemplate = renderedTemplate.replaceAll(
'build_processes_demo',
repository.custom_properties['php-globals-long-prefix']
);
renderedTemplate = renderedTemplate.replaceAll(
'bpd',
repository.custom_properties['php-globals-short-prefix']
);
renderedTemplate = renderedTemplate.replaceAll(
'BPD',
repository.custom_properties['php-globals-short-prefix'].toUpperCase()
);
} else if ( 'plugin' == repository.custom_properties['repo-type'] ) {
renderedTemplate = renderedTemplate.replaceAll(
'Team51 Plugin Scaffold',
repository.custom_properties['human-title'] ?? repository.name
);
renderedTemplate = renderedTemplate.replaceAll(
'A scaffold for WP.com Special Projects plugins.',
repository.description
);
renderedTemplate = renderedTemplate.replaceAll(
'team51-plugin-scaffold',
repository.name
);
renderedTemplate = renderedTemplate.replaceAll(
'wpcomsp-scaffold',
repository.name
);
renderedTemplate = renderedTemplate.replaceAll(
'WPCOMSpecialProjects\\Scaffold',
'WPCOMSpecialProjects\\' + repository.name
);
renderedTemplate = renderedTemplate.replaceAll(
'WPCOMSpecialProjects\\\\Scaffold',
'WPCOMSpecialProjects\\\\' + repository.name
);
renderedTemplate = renderedTemplate.replaceAll(
'wpcomsp_scaffold',
'wpcomsp_' + repository.custom_properties['php-globals-short-prefix']
);
renderedTemplate = renderedTemplate.replaceAll(
'WPCOMSP_SCAFFOLD',
'WPCOMSP_' + repository.custom_properties['php-globals-short-prefix'].toUpperCase
);

}

// If stuff has changed, say so and write it back out. If there were no changes, do nothing.
if ( renderedTemplate !== templateFile ) {
console.log( " Changes were made. Writing file." );
// Write back over the templated file
await writeFile( filePath, renderedTemplate );
}

};

await traverseDirectory( '.', buildTemplate );
68 changes: 68 additions & 0 deletions .github/workflows/fill-in-scaffold.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Fill in the Scaffold Placeholders

on:
repository_dispatch:
types: [fill_scaffold]
workflow_dispatch:

jobs:
fill-scaffold:
# Don't run this in the scaffold repo
if: github.repository != 'a8cteam51/team51-plugin-scaffold'
runs-on: ubuntu-latest
permissions:
# Give the default GITHUB_TOKEN write permission to commit and push the
# added or changed files to the repository.
contents: write
steps:
- uses: actions/checkout@v4
with:
# by default, it uses a depth of 1
# this fetches all history so that we can read each commit
fetch-depth: 0
ref: ${{ github.head_ref }}
- uses: actions/setup-node@v4
with:
node-version: 20

- name: Set github actions[bot] as the committer.
run: |
git config --local user.name 'github-actions[bot]' && git config --local user.email 'github-actions[bot]@users.noreply.github.com'

- name: If it's a project scaffold, rearrange some directories.
if: ${{ 'project' == github.event.repository.custom_properties['repo-type'] }}
run: |
git submodule init
git submodule update
git mv --force README.scaffold.md README.md
git mv themes/build-processes-demo themes/${{ github.event.repository.name }}
git mv mu-plugins/build-processes-demo-blocks mu-plugins/${{ github.event.repository.name }}-blocks
git mv mu-plugins/build-processes-demo-features mu-plugins/${{ github.event.repository.name }}-features

- name: If it's a plugin scaffold, rename the readme and plugin file.
if: ${{ 'plugin' == github.event.repository.custom_properties['repo-type'] }}
run: |
git mv --force README.scaffold.md README.md
git mv team51-plugin-scaffold.php ${{ github.event.repository.name }}.php

- name: If it's an issues repo scaffold, rename the readme file.
if: ${{ 'issues' == github.event.repository.custom_properties['repo-type'] }}
run: |
git mv --force README.scaffold.md README.md

- name: Commit the changed files.
run: |
git commit -am "chore: rename files from scaffolding"

# Run the templating
- name: Do the string replacements within the files.
run: |
node ./.github/workflows/fill-in-scaffold.mjs '${{ toJSON( github.event.repository ) }}'

- name: Commit the changed files.
run: |
git commit -am "chore: fill in scaffolding placeholders"

- name: Push all the changed files.
run: |
git push
58 changes: 58 additions & 0 deletions README.scaffold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# EXAMPLE_REPO_NAME

**Contributors:** wpcomspecialprojects
**Tags:**
**Requires at least:** 6.5
**Tested up to:** 6.5
**Requires PHP:** 8.3
**Stable tag:** 1.0.0
**License:** GPLv3 or later
**License URI:** http://www.gnu.org/licenses/gpl-3.0.html

EXAMPLE_REPO_DESCRIPTION

## Description

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed leo ligula, aliquam et sem luctus, placerat facilisis orci. Cras faucibus, odio ac aliquet scelerisque, nisi ligula dignissim nisi, sed tincidunt magna libero vitae dui. Sed varius lectus turpis, fringilla maximus libero posuere nec. Aenean volutpat pharetra sem, et cursus leo sodales quis.

## Installation

This plugin requires WooCommerce 7.4+ to run. If you're running a lower version, please update first. After you made sure that you're running a supported version of WooCommerce, you may install `Team51 Plugin Scaffold` either manually or through your site's plugins page.

### INSTALL FROM WITHIN WORDPRESS

1. Visit the plugins page withing your dashboard and select `Add New`.
1. Search for `Team51 Plugin Scaffold` and click the `Install Now` button.
1. Activate the plugin from within your `Plugins` page.

### INSTALL MANUALLY

1. Download the plugin from https://wordpress.org/plugins/ and unzip the archive.
1. Upload the `EXAMPLE_REPO_NAME` folder to the `/wp-content/plugins/` directory.
1. Activate the plugin through the `Plugins` menu in WordPress.

### AFTER ACTIVATION

If the minimum required version of WooCommerce is present, you will find a section present in the `Advanced` tab of the WooCommerce `Settings` page. Aliquam dolor sem, convallis malesuada neque sit amet, dictum mattis velit. Vestibulum at pharetra metus. Suspendisse rhoncus libero nisi, sed rhoncus tortor aliquam pretium.

## Frequently Asked Questions

### How can I get help if I'm stuck?

Quisque volutpat tortor id varius pulvinar. Vivamus porttitor, mi non auctor pellentesque, leo purus interdum libero, at aliquam justo lectus sed ligula.

### I have a question that is not listed here

Duis efficitur, sapien ac scelerisque placerat, elit justo tempor nisl, ut feugiat magna orci quis odio.

## Screenshots

### 1. Example screenshot

[missing image]

## Changelog

### 1.0.0 (FIRST RELEASE DATE)

* First official release.
Loading