Skip to content

Commit 76a241d

Browse files
committed
Merge pull request #49 from loadsys/f/cakephp3-improvements
F/cakephp3 improvements
2 parents a8c3806 + 8a52a9c commit 76a241d

33 files changed

Lines changed: 424 additions & 1299 deletions

README.md

Lines changed: 40 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
# Loadsys Cake Command Line Scripts #
1+
# Loadsys Cake Command Line Scripts
22

33
**DISCLAIMER: We use these tools ourselves, but that doesn't mean they will necessarily work for you in your situation. This repo is public in case it can be of use to anyone else (and because it's convenient for us), but it _is not supported_ and _may change without notice_. Issues and contributions may be flat out ignored if they don't impact us. You have been warned!**
44

55
This collection of scripts is intended to provide consistency and shortcuts for common project tasks. Key tools are highlighted and described below.
66

7-
* The 1.x branch is tuned for projects that primarily use git submodules for dependency management.
8-
* The 2.x branch is meant for projects that use composer (although submodules are still supported).
7+
* The master branch is meant for use with Cake 3 projects exclusively.
8+
* The 2.x branch is meant for Cake 2.x projects that use composer (git submodules are still supported).
9+
* The 1.x branch is tuned for Cake 1.x projects that use git submodules for dependency management.
910
* Most scripts listed below can take a `-h` option as their first argument to output usage information.
1011
* Most are designed to run with few or no arguments; they try to guess sensible defaults whenever possible.
11-
* Most are designed to fail gracefully. For example, if `pear` is available and your project defines a pear config file, then those dependencies will be installed by `deps-install`, otherwise pear will be silently ignored. This provides maximum flexibility without having to customize the scripts per-project.
12+
* Most are designed to fail gracefully with a non-zero exit code.
1213

1314

14-
## Requirements ##
15+
## Requirements
1516

16-
The following dependencies are assumed to be available on the target system and available in the default `PATH`.
17+
The following dependencies are assumed to be available on the target system and available via the default `$PATH`.
1718

1819
* `bash`
1920
* `composer`
2021
* `git`
2122
* `mail`
2223
* `mysqldump`
23-
* `pear`
2424
* `php`
2525
* `realpath` (not available by default on OS X)
2626
* `readlink`
@@ -36,107 +36,44 @@ Additionally, some scripts expect additional tools that should be automatically
3636
If these items are not available, some scripts may not function as expected.
3737

3838

39-
## Installation ##
39+
## Installation
4040

41-
**WARNING!** These scripts are currently only compatible with Cake 2.x. Don't try to use them on a 1.x project.
41+
**WARNING!** These scripts are currently only compatible with Cake 3.x. Don't try to use them on anything else.
4242

43-
### composer ###
44-
45-
Your project's own `composer.json` file should look something like this:
43+
Your project's `composer.json` file should include something like this:
4644

4745
```json
4846
{
49-
"require": {
50-
"loadsys/cakephp-shell-scripts": "2.0.*"
51-
},
52-
"config": {
47+
"require": {
48+
"loadsys/cakephp-shell-scripts": "~3.0"
49+
},
50+
"config": {
5351
"bin-dir": "bin"
54-
}
52+
}
5553
}
5654
```
5755
Then run `composer install` to pull this repo into your project. A `bin/` folder should be created in your project root with symlinks to all of the scripts from this package.
5856

5957
**Note**: The `bin-dir` is non-standard and may cause conflicts with other composer packages that install "binaries". These scripts are all expect to live in `PROJECT_ROOT/bin` though so don't expect anything to work if you forgo the `bin-dir` setting in your project.
6058

61-
### git submodule ###
62-
63-
(This is the old method and will eventually be retired.)
64-
65-
The scripts all expect to live together in a subfolder of your project root named `bin/`. To install them, navigate to your project root and run:
66-
67-
```bash
68-
git submodule add https://github.com/loadsys/CakePHP-Shell-Scripts.git bin
69-
```
70-
As mentioned above, the `bin` at the end is critical. The scripts expect to be able to call each other in a folder directly inside the project root named `bin/`.
71-
72-
73-
### Getting Submodule Updates ###
74-
75-
To pull any recent changes to the script library into your project, use the `bin/bin-selfupdate` script to update the submodule to the latest release and add the changed commit to the parent repo for you to commit.
76-
77-
```bash
78-
bin/bin-selfupdate
79-
```
80-
81-
If your bin/ dir does not yet have the bin-selfupdate script, you can run the following commands manually the first time:
82-
83-
```bash
84-
cd bin
85-
git pull origin master
86-
cd ..
87-
git add bin
88-
git commit -m "Updated bin submodule to latest release."
89-
```
90-
91-
The above changes the active commit for the submoduled bin repo, which will then be applied to other copies of the project when `git submodule update` is executed (either by `bin/update`, `bin/init-repo` or manually.)
9259

60+
## Contributing Improvements
9361

94-
## Contributing Improvements ##
95-
96-
**WARNING: Outside contributions are appreciated, but may be ignored if they do not impact our usage of these tools. Please consider this before sending a pull request.**
62+
**WARNING: Outside contributions are appreciated, but may be rejected if they do not impact our usage of these tools. Please consider this before sending a pull request.**
9763

9864
There currently is not a convenient way to set up a test harness around this repo.
9965

10066
1. The best thing to do is to clone this repo (or a fork) into the `bin/` folder of an existing and established project so you have something to test against.
101-
1. Make your edits to the scripts as necessary and commit to a feature branch.
102-
1. If you have **added** new scripts, make sure they are executable and run `./composer-binaries` to update the composer.json file automatically (you should be using PHP 5.4 to make proper use of the `JSON_PRETTY_PRINT` flag).
67+
1. Make your edits to the scripts as necessary and commit to a topic branch.
68+
1. If you have **added** new scripts, make sure they are executable and run `./composer-binaries` to update the composer.json file automatically (you should be using PHP 5.4+ to make proper use of the `JSON_PRETTY_PRINT` flag).
10369
1. Run `composer validate` to run a syntax check on the json file.
10470
1. Submit a pull request for your branch.
10571

10672

107-
## Issues ##
108-
109-
@TODO: Explanations below are off the cuff and need refinement.
110-
111-
Need to talk about the consequences and delays in how changes to this repo affect script runs in projects that include it.
112-
113-
For example, take a project that has already included this repo as a submodule but hasn't been updated for a while (and we've made changes to this bin repo in the meantime). When that project runs `bin/update` to update the staging copy of the app, it will be the "stale" version of bin/update that executes. That version of bin/update will eventually trigger a `git submodule update`, which might pull in changes to _itself_-- but too late for the current runtime.
73+
## Notable Scripts
11474

115-
The bigger issue is that if a change to a bin/ script happens DURING the run of the stale `update` script as a result of the included `git submodules update` call, and the changes to that dependency script are incompatible with the stale version of `update`, the run will fail in unpredictable ways.
11675

117-
The script really needs a way of updating JUST the CakePHP-Shell-Scripts submodule **first** (and no other submodules that probably expect the codebase to be changing along with it), and if it detects any changes have been applied, exits and re-calls the newer version of itself. Seems like there is a potential for an infinite loop here if the change detection is done wrong though.
118-
119-
This may all be far less of an issue when we switch to composer-based installations, since it will handle the "bootstrapping" for us.
120-
121-
122-
## Key Scripts ##
123-
124-
### bin/init-repo ###
125-
126-
Designed to perform all of the steps necessary to initialize a project for a developer who has just cloned the repo for the first time. (It should never be needed after that.) It performs tasks like:
127-
128-
* Sym-linking a Cake core into the project, if necessary.
129-
* Copying default config files into place for the given environment.
130-
* Writing database connection details into the `database.php` config file.
131-
* Setting project folder ownership and write permissions.
132-
* Initializing and updating any included git submodules.
133-
* Applying database migrations, if present.
134-
* Offering to load Seed data, if present.
135-
136-
Given an existing web-hosted folder and a database ready for use, it should fully prep a newly cloned copy of the project for use, or development.
137-
138-
139-
### bin/update ###
76+
### bin/update
14077

14178
Automates all of the steps for a read-only copy of the app (such as staging or production) to pull new code from the repo and update the local running copy. It performs tasks like:
14279

@@ -152,11 +89,26 @@ Automates all of the steps for a read-only copy of the app (such as staging or p
15289
* Showing the user the new active commit's log.
15390

15491

155-
### bin/db-login ###
92+
### bin/db-login
93+
94+
A simple shortcut script that uses the `[Datasources][default]` key defined in `config/app.php` to start a command line `mysql` session for you. Incredibly convenient in a production environment to run manual data queries during troubleshooting.
95+
96+
97+
### bin/db-backup
98+
99+
Uses the default database credentials in `config/app.php` to create a ZIPed `mysqldump` of that database in a local `backups/` folder. Helpful because you don't have to specify DB credentials. It also reports the ZIP size to help you keep a mental tally over time.
100+
101+
102+
### @TODO:
103+
104+
Add cache-clear, tests-run, docs-generate, codesniffer-run here as notable scripts.
105+
106+
107+
## License
156108

157-
A simple shortcut script that uses the contents of `Config/database.php` to start a command line `mysql` session for you. Incredibly convenient in a production environment to run manual data queries during troubleshooting.
109+
MIT
158110

159111

160-
### bin/db-backup ###
112+
## Copyright
161113

162-
Uses the default credentials in `Config/database.php` to create a ZIPed `mysqldump` of that database into a local `backups/` folder. Helpful because you don't have to specify DB credentials in multiple places.
114+
Copyright 2015 Loadsys Web Strategies

cache-clear

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env bash
2+
3+
#---------------------------------------------------------------------
4+
usage ()
5+
{
6+
cat <<EOT
7+
8+
${0##*/}
9+
Clears Cake caches by engaging the Cake ConsoleShell and
10+
instructing each configured Cache object to clear itself. This
11+
makes it portable across caching providers, and works just as
12+
well with Memcached and Redis as it does with File caching.
13+
14+
Should be run from the project root folder.
15+
16+
Usage:
17+
bin/${0##*/}
18+
19+
20+
EOT
21+
22+
exit 0
23+
}
24+
if [ "$1" = '-h' ]; then
25+
usage
26+
fi
27+
28+
29+
DIR="$( cd -P "$( dirname "$0" )"/.. >/dev/null 2>&1 && pwd )"
30+
31+
echo "## Clearing cache folders.";
32+
33+
cat <<-'EOPHP' | bin/cake console
34+
use Cake\Cache\Cache;
35+
foreach (Cache::configured() as $key) {
36+
Cache::clear(false, $key);
37+
echo "Cleared: $key\n";
38+
}
39+
return "Done";
40+
41+
EOPHP
42+
43+

clear-cache

Lines changed: 0 additions & 35 deletions
This file was deleted.

run-codesniffer renamed to codesniffer-run

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ umask a+rw
3030
DIR="$( cd -P "$( dirname "$0" )"/.. >/dev/null 2>&1 && pwd )"
3131
BIN_DIR="${DIR}/bin"
3232
TMP_DIR="${DIR}/tmp"
33+
SRC_DIR="${DIR}/src"
3334
REPORT_DIR="${TMP_DIR}/code-sniffs"
3435
FULL_REPORT_FILE="${REPORT_DIR}/report-full.txt"
3536
SUMMARY_REPORT_FILE="${REPORT_DIR}/report-summary.txt"
3637

37-
CODE_STANDARD="Vendor/loadsys/loadsys_codesniffer/Loadsys"
38-
SNIFF_FOLDERS=("${DIR}/Controller" "${DIR}/Model" "${DIR}/View" "${DIR}/Lib" "${DIR}/Console/Command")
38+
CODE_STANDARD="vendor/loadsys/loadsys_codesniffer/Loadsys"
39+
SNIFF_FOLDERS=("${SRC_DIR}/Controller" "${SRC_DIR}/Model" "${SRC_DIR}/View" "${SRC_DIR}/Lib" "${SRC_DIR}/Console/Shell")
3940
SNIFF_FAIL_CAUSES_SCRIPT_FAIL=1 # 1 = false. This script will therefore always exit 0.
4041

4142
# Bail out if phpcs isn't available to us.
@@ -75,4 +76,4 @@ if [ $SNIFF_FAIL_CAUSES_SCRIPT_FAIL -eq 0 ]; then
7576
exit $SNIFF_RESULT
7677
else
7778
exit 0
78-
fi
79+
fi

composer-binaries

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
/**
1010
* Prints usage information and exits.
1111
*
12-
* @param string $scriptName Typically set to basename($argv[0]) when the method is called.
12+
* @param string $script Typically set to basename($argv[0]) when the method is called.
13+
* @param int $exitCode The numeric exit code to return. 0 = success. >0 = failure.
1314
* @return void
1415
*/
15-
function usage ($scriptName, $exitCode = 0) {
16+
function usage($script = null, $exitCode = 0) {
17+
$script = $script ?: basename(__FILE__);
1618
$usage = <<<EOD
1719
18-
{$scriptName}
20+
{$script}
21+
A meta script for working with the CakePHP-Shell-Scripts repo itself.
1922
2023
Searches the directory this script lives in for other executable files.
2124
Generates a list of all _other_ executable files (exculding itself) in
@@ -28,7 +31,7 @@ function usage ($scriptName, $exitCode = 0) {
2831
projects that require this package.
2932
3033
Usage:
31-
./{$scriptName}
34+
./{$script}
3235
3336
3437
EOD;
@@ -42,10 +45,11 @@ EOD;
4245
* any non-executables from the given directory.
4346
*/
4447
class ExeIterator extends FilterIterator {
48+
4549
/**
46-
* Stores the name of the current script to strip ot from output.
50+
* Stores the name of the current script to strip out from output.
4751
*
48-
* @var string $selfName
52+
* @var string
4953
*/
5054
private $selfName = null;
5155

@@ -66,7 +70,7 @@ class ExeIterator extends FilterIterator {
6670
* false for dot directories (. and ..), the currently executing script,
6771
* and anything that is not set as executable.
6872
*
69-
* @return boolean True if current file is executable and not ourself, false otherwise.
73+
* @return bool True if current file is executable and not ourself, false otherwise.
7074
*/
7175
public function accept() {
7276
$f = parent::current();
@@ -96,7 +100,7 @@ class ExeIterator extends FilterIterator {
96100
* OR of all available options, suitable for passing as the second argument
97101
* to `json_encode()`.
98102
*
99-
* @return integer The bitwise OR-ing of all valid and available JSON_* options.
103+
* @return int The bitwise OR-ing of all valid and available JSON_* options.
100104
*/
101105
function json_opts() {
102106
$bitwise = 0;
@@ -112,7 +116,7 @@ function json_opts() {
112116
* main() ===================================================================
113117
*/
114118

115-
$options = getopt('h', array());
119+
$options = getopt('h', []);
116120
$dir = dirname(__FILE__);
117121
$composerFile = $dir . '/composer.json';
118122
$selfName = basename($argv[0]);
@@ -124,6 +128,12 @@ if (isset($options['h'])) {
124128
$executables = array_values(iterator_to_array(new ExeIterator($dir, $selfName)));
125129
$composerJson = json_decode(file_get_contents($composerFile), $assoc = false);
126130
$composerJson->bin = $executables;
127-
file_put_contents($composerFile, json_encode($composerJson, json_opts('JSON_UNESCAPED_SLASHES', 'JSON_PRETTY_PRINT')));
131+
file_put_contents(
132+
$composerFile,
133+
json_encode(
134+
$composerJson,
135+
json_opts('JSON_UNESCAPED_SLASHES', 'JSON_PRETTY_PRINT')
136+
)
137+
);
128138

129139
echo "{$composerFile} updated." . PHP_EOL . PHP_EOL;

0 commit comments

Comments
 (0)