Skip to content

Commit

Permalink
ENH Update logic for license
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Aug 9, 2023
1 parent 94ace13 commit 98fe0ab
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ _tmp
vendor
.phpunit.result.cache
composer.lock
test.php
55 changes: 53 additions & 2 deletions funcs_scripts.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Panlatent\CronExpressionDescriptor\ExpressionDescriptor;

// These functions in scripts can be used in scripts

/**
Expand Down Expand Up @@ -79,6 +81,24 @@ function delete_file_if_exists($filename)
}
}

/**
* Rename a file to the root of the module being processed if it exists
*
* Example usage:
* rename_file_if_exists('oldfilename.md', 'newfilename.md')
*/
function rename_file_if_exists($oldFilename, $newFilename)
{
global $MODULE_DIR;
$oldPath = "$MODULE_DIR/$oldFilename";
$newPath = "$MODULE_DIR/$newFilename";
if (file_exists($oldPath)) {
$contents = read_file($oldFilename);
write_file($newPath, $contents);
delete_file_if_exists($oldFilename);
}
}

/**
* Determine if the module being processed is a recipe, including silverstripe-installer
*
Expand Down Expand Up @@ -119,9 +139,24 @@ function module_is_one_of($repos)
return false;
}

/**
* Return the github account of the module being processed
*
* Example usage:
* module_account()
*/
function module_account()
{
$s = read_file('.git/config');
if (!preg_match('#github.com:([^/]+)/#', $s, $matches)) {
error('Could not determine github account');
}
return $matches[1];
}

/**
* Output an info message to the console
*
*
* Example usage:
* info('This is a mildly interesting message')
*/
Expand All @@ -133,11 +168,27 @@ function 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);
}

/**
* Converts a cron expression to a human readable string
* Says UTC because that's what GitHub Actions uses
*
* Example usage:
* human_cron('5 4 * * 0')
* => 'At 4:05 AM UTC, only on Sunday'
*/
function human_cron(string $cron): string
{
$str = (new ExpressionDescriptor($cron))->getDescription();
$str = preg_replace('#0([1-9]):#', '$1:', $str);
$str = preg_replace('# (AM|PM),#', ' $1 UTC,', $str);
return $str;
}
23 changes: 21 additions & 2 deletions scripts/cms-any/license.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
$contents = <<<EOT
BSD 3-Clause License
Copyright (c) $year, SilverStripe Limited - www.silverstripe.com
Copyright (c) $year, Silverstripe Limited - www.silverstripe.com
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -34,4 +34,23 @@
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
EOT;

write_file_if_not_exist('LICENSE', $contents);
// standard filename used on framework, admin, cms, etc
$licenseFilename = 'LICENSE';

// can rename the licence filename on any account
foreach (['LICENSE.md', 'license.md', 'license'] as $filename) {
rename_file_if_exists($filename, $licenseFilename);
}

// only update licence contents if module is on silverstripe account
if (module_account() === 'silverstripe') {
if (file_exists('LICENSE')) {
$oldContents = read_file($licenseFilename);
$newContents = str_replace('SilverStripe', 'Silverstripe', $oldContents);
if ($newContents !== $oldContents) {
write_file_even_if_exists($licenseFilename, $newContents);
}
} else {
write_file_if_not_exist($licenseFilename, $contents);
}
}

0 comments on commit 98fe0ab

Please sign in to comment.