Skip to content

Commit

Permalink
Merge pull request #1 from creative-commoners/pulls/main/new
Browse files Browse the repository at this point in the history
NEW Create module-standardiser
  • Loading branch information
sabina-talipova authored Jul 24, 2023
2 parents 72c2b91 + cc1c5ef commit 94ace13
Show file tree
Hide file tree
Showing 18 changed files with 1,021 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# For more information about the properties used in
# this file, please see the EditorConfig documentation:
# http://editorconfig.org/

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,js,json,css,scss,eslintrc,feature}]
indent_size = 2
indent_style = space

[composer.json]
indent_size = 4

# Don't perform any clean-up on thirdparty files

[thirdparty/**]
trim_trailing_whitespace = false
insert_final_newline = false

[admin/thirdparty/**]
trim_trailing_whitespace = false
insert_final_newline = false
26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: CI

on:
push:
pull_request:
workflow_dispatch:

jobs:
ci:
name: CI
runs-on: ubuntu-latest
steps:

- name: Checkout code
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2

- name: Install PHP
uses: shivammathur/setup-php@1a18b2267f80291a81ca1d33e7c851fe09e7dfc4 # v2.22.0
with:
php-version: 7.4

- name: Install PHPUnit
run: wget https://phar.phpunit.de/phpunit-9.5.phar

- name: PHPUnit
run: php phpunit-9.5.phar --verbose --colors=always
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
_data
_modules
_tmp
vendor
.phpunit.result.cache
composer.lock
29 changes: 29 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2023, SilverStripe Limited - www.silverstripe.com
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,73 @@
# Module standardiser

This tools standardises some files in Silverstripe modules that's intended to run on a developers laptop and create
a number of pull-requests in GitHub.

**This tool is only intended for use by Silverstripe core committers or the Silverstripe Ltd CMS Squad**

It will run across all modules in [supported-modules](https://github.com/silverstripe/supported-modules) list and the
relevant branch e.g. `5` will be used depending on the command-line `--branch` option that's passed in.

It will run all scripts in the `scripts/any` folder and then run all scripts in the applicable
`scripts/<cms-version>` folder depending on the command-line `--branch` option that's passed in.

## GitHub Token

This tool creates pull-request via the GitHub API. You need to set the `MS_GITHUB_TOKEN` environment variable in order
for this to work.

Create a new GitHub token in [https://github.com/settings/tokens/new](https://github.com/settings/tokens/new)
and only tick the `public_repo` checkbox and set it to expire in 7 days. If you do not set the correct permissions
then you will get a 404 error when attempting to create pull-requests.

Delete this token once you have finished.

## Usage

```bash
git clone [email protected]:silverstripe/module-standardiser.git
cd module-standardiser
composer install
MS_GITHUB_TOKEN=<token> php run.php update <options>
```

**Example usage:**
```bash
MS_GITHUB_TOKEN=abc123 php run.php update --cms-major=5 --branch=next-minor --dry-run --only=silverstripe-config,silverstripe-assets
```

## Command line options:

| Flag | Description |
| ---- | ------------|
| --cms-major=[version] | The major version of CMS to use (default: 5) |
| --branch=[type] | The branch type to use - next-minor\|next-patch (default: next-minor) |
| --only=[modules] | Only include the specified modules (without account prefix) separated by commas e.g. silverstripe-config,silverstripe-assets |
| --exclude=[modules] | Exclude the specified modules (without account prefix) separated by commas e.g. silverstripe-mfa,silverstripe-totp |
| --dry-run | Do not push to github or create pull-requests |
| --account | GitHub account to use for creating pull-requests (default: creative-commoners) |
| --no-delete | Do not delete _data and _modules directories before running |

## GitHub API secondary rate limit

You may hit a secondary GitHub rate limit because this tool may create too many pull-requests in a short space of time.
To help with this the tool will always output the urls of all pull-requests updated and also the repos that were
updated so you can add them to the --exclude flag on subsequent re-runs.

## Adding new scripts

Simply add new scripts to either `scripts/cms-<version>` to run on a specific cms-major or `scripts/cms-any` to run
on any cms-major and they will be automatically picked up and run when the tool is run. Code in the script will be
passed through `eval()` on the module that is currently being processed.

Make use of functions in `funcs_scripts.php` such as `write_file_if_not_exist()` and `read_file()` to access the
correct files in the module that is currently being processed and also to ensure that console output is consistent.

Do not use functions in `funcs_utils.php` as they are not intended to be used in scripts.

Scripts will be automatically wrapped in an anoymous function so you do not need to worry about variables crossing
over into different scripts.

## Updating the tool when a new major version of CMS is updated

Update the `CURRENT_CMS_MAJOR` constant in `run.php`
10 changes: 10 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"require": {
"php": ">=7.4",
"symfony/console": "^6.3",
"symfony/process": "^6.3"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
}
}
143 changes: 143 additions & 0 deletions funcs_scripts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

// These functions in scripts can be used in scripts

/**
* Check that a file exists relative to the root of the module being processed
*
* Example usage:
* check_file_exists('composer.json')
*/
function check_file_exists($filename)
{
global $MODULE_DIR;
$path = "$MODULE_DIR/$filename";
if (!file_exists($path)) {
info("File $path does not exist, though this should be OK");
return false;
}
return true;
}

/**
* Read a file relative to the root of the module being processed
*
* Example usage:
* read_file('composer.json')
*/
function read_file($filename)
{
global $MODULE_DIR;
$path = "$MODULE_DIR/$filename";
if (!file_exists($path)) {
error("File $path does not exist");
}
return file_get_contents($path);
}

/**
* Write a file relative to the root of the module being processed even if it already exists
*
* Example usage:
* write_file_even_if_exists('.github/workflows/ci.yml')
*/
function write_file_even_if_exists($filename, $content)
{
global $MODULE_DIR;
$path = "$MODULE_DIR/$filename";
write_file($path, $content);
}

/**
* Write a file relative to the root of the module being processed only if it doesn't already exist
*
* Example usage:
* write_file_if_not_exist('LICENSE')
*/
function write_file_if_not_exist($filename, $content)
{
global $MODULE_DIR;
$path = "$MODULE_DIR/$filename";
if (!file_exists($path)) {
write_file($path, $content);
}
}

/**
* Delete a file relative to the root of the module being processed if it exists
*
* Example usage:
* delete_file_if_exists('.travis.yml')
*/
function delete_file_if_exists($filename)
{
global $MODULE_DIR;
$path = "$MODULE_DIR/$filename";
if (file_exists($path)) {
unlink($path);
info("Deleted $path");
}
}

/**
* Determine if the module being processed is a recipe, including silverstripe-installer
*
* Example usage:
* module_is_recipe()
*/
function module_is_recipe()
{
global $MODULE_DIR;
if (strpos('/recipe-', $MODULE_DIR) !== false
|| strpos('/silverstripe-installer', $MODULE_DIR) !== false
) {
return true;
}
return false;
}

/**
* Determine if the module being processed is one of the modules in a list
*
* Example usage:
* module_is_one_of(['silverstripe-mfa', 'silverstripe-totp'])
*/
function module_is_one_of($repos)
{
global $MODULE_DIR;
if (!is_array($repos)) {
error('repos is not an array');
}
foreach ($repos as $repo) {
if (!is_string($repo)) {
error('repo is not a string');
}
if (strpos("/$repo", $MODULE_DIR) !== false) {
return true;
}
}
return false;
}

/**
* Output an info message to the console
*
* Example usage:
* info('This is a mildly interesting message')
*/
function info($message)
{
// using writeln with <info> instead of ->info() so that it only takes up one line instead of five
io()->writeln("<info>$message</>");
}

/**
* Output a warning message to the console
*
* Example usage:
* warning('This is something you might want to pay attention to')
*/
function warning($message)
{
io()->warning($message);
}
Loading

0 comments on commit 94ace13

Please sign in to comment.