Skip to content

Commit ba662fc

Browse files
committed
Merge pull request #54 from loadsys/f/cake3-mark-deux
Various Cake 3 improvements from field use
2 parents 02d288e + 5847f6f commit ba662fc

File tree

14 files changed

+255
-178
lines changed

14 files changed

+255
-178
lines changed

cache-clear

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ if [ "$1" = '-h' ]; then
2626
fi
2727

2828

29-
DIR="$( cd -P "$( dirname "$0" )"/.. >/dev/null 2>&1 && pwd )"
30-
3129
echo "## Clearing cache folders.";
3230

3331
cat <<-'EOPHP' | bin/cake console

codesniffer-run

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ Usage:
1818
n - Suppress warnings during the sniff run.
1919
x - Always exit zero regardless of sniff results.
2020
21+
Environment:
22+
CODESNIFFER_RUN_STANDARD - If set, overrides the name of the Coding
23+
Standard to use. The standard is assumed
24+
to be "installed" already.
25+
(Default: Loadsys)
26+
2127
EOT
2228

2329
exit ${1:-0} # Exit with code 0 unless an arg is passed to the method.
@@ -27,13 +33,13 @@ EOT
2733
# Set up local variables.
2834
umask a+rw
2935

30-
DIR="$( cd -P "$( dirname "$0" )"/.. >/dev/null 2>&1 && pwd )"
31-
TMP_DIR="${DIR}/tmp"
36+
TMP_DIR="tmp"
3237
REPORT_DIR="${TMP_DIR}/code-sniffs"
3338
FULL_REPORT_FILE="${REPORT_DIR}/report-full.txt"
3439
SUMMARY_REPORT_FILE="${REPORT_DIR}/report-summary.txt"
3540

36-
CODE_STANDARD="vendor/cakephp/cakephp-codesniffer/CakePHP,vendor/loadsys/loadsys_codesniffer/Loadsys"
41+
CODE_STANDARD=${CODESNIFFER_RUN_STANDARD:-Loadsys}
42+
CODE_STANDARDS=("vendor/cakephp/cakephp-codesniffer" "vendor/loadsys/loadsys_codesniffer")
3743
SNIFF_FOLDERS=("./src" "./plugins" "./tests" "./config" "./webroot")
3844
SNIFF_FAIL_CAUSES_SCRIPT_FAIL=0 # 0 = true. Script will exit with phpcs's return code.
3945

@@ -67,7 +73,27 @@ while getopts ":fhnx" opt; do
6773
done
6874

6975

76+
# Make sure phpcs has the sniffs configured.
77+
INSTALLED_ALREADY=$( bin/phpcs --config-show | grep installed_paths )
78+
INSTALLED_ALREADY=${INSTALLED_ALREADY#installed_paths: }
79+
#echo "old = $INSTALLED_ALREADY"
80+
81+
for STANDARD in "${CODE_STANDARDS[@]}"; do
82+
if [[ ! $INSTALLED_ALREADY == *"$STANDARD"* ]]; then
83+
TO_INSTALL="${TO_INSTALL-},${STANDARD}"
84+
fi
85+
done
86+
87+
TO_INSTALL=${TO_INSTALL#,}
88+
#echo "new = $TO_INSTALL"
89+
if [ -n "$TO_INSTALL" ] && [ "$TO_INSTALL" != "$INSTALLED_ALREADY" ]; then
90+
echo "## Adding required coding standards installed paths."
91+
bin/phpcs --config-set installed_paths $TO_INSTALL > /dev/null
92+
fi
93+
94+
7095
# Run the sniffs.
96+
echo "## Executing code sniffer:"
7197
bin/phpcs -ps $SUPPRESS_WARNINGS \
7298
--extensions=php \
7399
--standard="$CODE_STANDARD" \

composer-install

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ fi
3434

3535

3636
# Set up working vars.
37-
DIR="$( cd -P "$( dirname "$0" )"/.. >/dev/null 2>&1 && pwd )"
38-
BIN_DIR="${DIR}/bin"
39-
COMPOSER_CONFIG_FILE="$DIR/composer.json"
40-
GUESSES=( "$( which composer )" "$BIN_DIR/composer" "$( which composer.phar )" "$BIN_DIR/composer.phar" )
37+
COMPOSER_CONFIG_FILE="composer.json"
38+
GUESSES=( "$( which composer )" "bin/composer" "$( which composer.phar )" "bin/composer.phar" )
4139

4240

4341
# Bail out if there's no config file present.

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"require": {
1212
"phpunit/phpunit": "~4.1",
1313
"phpdocumentor/phpdocumentor": "~2.0",
14-
"squizlabs/php_codesniffer": "~2.0",
14+
"psy/psysh": "@stable",
1515
"loadsys/loadsys_codesniffer": "~3.0"
1616
},
1717
"config": {
@@ -22,10 +22,12 @@
2222
"codesniffer-run",
2323
"composer-install",
2424
"coverage-ensure",
25+
"coverage-report",
2526
"db-backup",
2627
"db-credentials",
2728
"db-login",
2829
"db-sample-data",
30+
"deploy",
2931
"docs-generate",
3032
"email",
3133
"folder-delete-items-older-than-days",
@@ -39,7 +41,6 @@
3941
"owner-set",
4042
"rename-empty",
4143
"tests-run",
42-
"update",
4344
"vagrant-exec"
4445
]
4546
}

coverage-report

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env bash
2+
3+
#---------------------------------------------------------------------
4+
usage ()
5+
{
6+
cat <<EOT
7+
8+
${0##*/}
9+
Convenience wrapper to execute phpunit test suite, generate
10+
html coverage and open it in your browser (if you're on a Mac.)
11+
12+
Should be run from the project root folder.
13+
14+
Usage:
15+
bin/${0##*/}
16+
17+
18+
EOT
19+
20+
exit ${1:-0} # Exit with code 0 unless an arg is passed to the method.
21+
}
22+
if [ "$1" = '-h' ]; then
23+
usage
24+
fi
25+
26+
27+
COVERAGE_PATH="tmp/coverage/html"
28+
29+
30+
# Launch the coverage in a browser if we're on the host.
31+
if command -v 'open' >/dev/null 2>&1 && command -v 'vagrant' >/dev/null 2>&1; then
32+
bin/vagrant-exec "bin/phpunit --coverage-html="$COVERAGE_PATH""
33+
open "${COVERAGE_PATH}/index.html"
34+
else
35+
bin/phpunit --coverage-html="$COVERAGE_PATH"
36+
echo ""
37+
echo "## Run 'open ${COVERAGE_PATH}/index.html'"
38+
echo "## from your host to view coverage reports in a browser."
39+
echo ""
40+
fi

db-backup

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ EOD;
4242
* @return string The converted value with the "closest" unit used.
4343
*/
4444
function human_filesize($bytes, $decimals = 2) {
45-
$sz = 'BKMGTP';
45+
$sz = 'BKMGTPEZY';
4646
$factor = floor((strlen(intval($bytes)) - 1) / 3);
4747
return (sprintf("%.{$decimals}f", intval($bytes) / pow(1024, $factor)) + 0) . @$sz[$factor];
4848
}
@@ -56,16 +56,15 @@ if (isset($argv[1]) && $argv[1] == '-h') {
5656
}
5757

5858
// Set up variables.
59-
$dir = getcwd();
60-
$backupDir = $dir . '/backups';
59+
$backupDir = 'backups';
6160
$date = date('Ymd-His');
6261
try {
63-
$credentialsScript = "{$dir}/bin/db-credentials";
62+
$credentialsScript = 'bin/db-credentials';
6463
if (!is_readable($credentialsScript)) {
6564
echo "!! DB credentials are not available. Aborting.";
6665
exit(3);
6766
}
68-
67+
6968
ob_start(); // Capture the shebang line from db-credentials.
7069
$db = include($credentialsScript);
7170
ob_get_clean(); // Flush it.

db-credentials

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,9 @@ class DbConfig {
8282
* @return void
8383
*/
8484
public function __construct() {
85-
$dir = getcwd();
86-
$injectedCode = 'use Cake\Datasource\ConnectionManager; echo serialize(ConnectionManager::config("default")) . PHP_EOL; exit;'; // This must be on a single line to work with the REPL.
85+
$injectedCode = 'echo serialize(\Cake\Datasource\ConnectionManager::config("default")) . PHP_EOL; exit;'; // This must be on a single line to work with the REPL.
8786
$cmd = "echo '{$injectedCode}' | "
88-
. escapeshellcmd("{$dir}/bin/cake Console -q");
87+
. escapeshellcmd("bin/cake Console -q");
8988
$response = [];
9089
$code = 0;
9190
exec($cmd, $response, $code);

db-login

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ ${0##*/}
99
Uses the db-credentials script to start a command line mysql
1010
session with the correct host, database, user and password.
1111
Should be run from the project root folder. Passes all command
12-
line arguments (except for \`-h\`) to mysql.
12+
line arguments (except for \`--help\`) and I/O redirection to
13+
mysql. This script can be used as a complete replacement for
14+
the normal mysql command, without having to worry about
15+
connection credentials.
16+
17+
Called without arguments, the command will log you into the
18+
default database used by the CakePHP app. Using I/O redirection,
19+
.sql files can be imported into that database as well:
20+
\`bin/db-login < my-file.sql\`
1321
1422
Usage:
1523
bin/${0##*/}
@@ -19,32 +27,27 @@ EOT
1927

2028
exit ${1:-0} # Exit with code 0 unless an arg is passed to the method.
2129
}
22-
if [ "$1" = '-h' ]; then
30+
if [ "$1" = '--help' ]; then
2331
usage
2432
fi
2533

2634

27-
DIR="$( cd -P "$( dirname "$0" )"/.. >/dev/null 2>&1 && pwd )"
28-
BIN_DIR="$DIR/bin"
29-
3035
# Holy yuck, but nothing else works!
31-
eval $( ${BIN_DIR}/db-credentials )
36+
eval $( bin/db-credentials )
3237

3338
# Set a custom port, if provided to us.
3439
if [ -n "${DB_PORT}" ]; then
3540
PORT_CLAUSE="--port=${DB_PORT}"
3641
else
37-
PORT_CLAUSE=""
38-
fi
3942

4043
# Define the primary command line.
41-
CMD="mysql --host=${DB_HOST} ${PORT_CLAUSE} --database=${DB_DATABASE} --user=${DB_USERNAME}"
44+
CMD="mysql --host=${DB_HOST} ${PORT_CLAUSE-} --database=${DB_DATABASE} --user=${DB_USERNAME}"
4245

4346
# Handle all the different ways to provide (or not provide) a password.
4447
if [[ $DB_PASSWORD =~ " |'" ]]; then
45-
$CMD -p"${DB_PASSWORD}" "$@"
48+
$CMD -p"${DB_PASSWORD}" "$@" <&0
4649
elif [ -n "${DB_PASSWORD}" ]; then
47-
$CMD -p$DB_PASSWORD "$@"
50+
$CMD -p$DB_PASSWORD "$@" <&0
4851
else
49-
$CMD "$@"
52+
$CMD "$@" <&0
5053
fi

db-sample-data

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ ${0##*/}
1616
Usage:
1717
bin/${0##*/} [table] [table2] [table3]
1818
19+
Environment:
20+
DB_SAMPLE_DATA_URL - If this environment variable is set, it will
21+
be used when displaying the download URL for
22+
the temporary zip file.
1923
2024
EOT
2125

@@ -30,28 +34,44 @@ if [ -z "$1" ]; then
3034
else
3135
TABLES="--tables "$*""
3236
fi
33-
DIR="$( cd -P "$( dirname "$0" )"/.. >/dev/null 2>&1 && pwd )"
34-
APP_DIR="$DIR"
35-
BIN_DIR="${DIR}/bin"
3637

3738

3839
# Configuration vars. Set these appropriately for your app and environment.
39-
PUBLIC_URL=""
40-
WEBROOT_PATH="$APP_DIR/webroot/"
40+
PUBLIC_URL=${DB_SAMPLE_DATA_URL:-}
41+
WEBROOT_PATH="webroot/"
4142

4243

4344
# Internal var setup.
44-
eval $( ${BIN_DIR}/db-credentials ) # Yuck, but nothing else works!
45-
DATE=$(date +%Y-%m-%d)
45+
eval $( bin/db-credentials ) # Yuck, but nothing else works!
4646
OPTIONS="--skip-add-drop-table --no-create-info"
47-
TMP_PATH="$APP_DIR/tmp/"
48-
DESTINATION_NAME="${DB_NAME}_sample_data_${DATE}"
49-
50-
# Generate the dump, compress it and move it into place.
51-
cd "${TMP_PATH}"
52-
mysqldump --host="${DB_HOST}" --user="${DB_USER}" -p${DB_PASS} ${OPTIONS} ${DB_NAME} $TABLES > "${DESTINATION_NAME}.sql"
53-
zip -rq9 "${WEBROOT_PATH}${DESTINATION_NAME}.zip" "${DESTINATION_NAME}.sql"
54-
rm -f "${DESTINATION_NAME}.sql"
47+
TMP_PATH="tmp"
48+
DESTINATION_NAME="${DB_DATABASE}_sample_data_$(date +%Y-%m-%d)"
49+
50+
51+
# Generate the dump command.
52+
if [ -n "$DB_PORT" ]; then
53+
PORT_CLAUSE="--port=$DB_PORT"
54+
fi
55+
CMD="mysqldump --host=${DB_HOST} ${PORT_CLAUSE} --user=${DB_USERNAME} ${OPTIONS} ${DB_DATABASE} $TABLES"
56+
57+
58+
# Run the dump command.
59+
# Handle all the different ways to provide (or not provide) a password.
60+
pushd "${TMP_PATH}" >/dev/null
61+
if [[ $DB_PASSWORD =~ " |'" ]]; then
62+
$CMD -p"${DB_PASSWORD}" > "${DESTINATION_NAME}.sql"
63+
elif [ -n "${DB_PASSWORD}" ]; then
64+
$CMD -p$DB_PASSWORD > "${DESTINATION_NAME}.sql"
65+
else
66+
$CMD > "${DESTINATION_NAME}.sql"
67+
fi
68+
popd >/dev/null
69+
70+
71+
# Zip the file and remove the original .sql.
72+
zip -rq9 "${WEBROOT_PATH}${DESTINATION_NAME}.zip" "${TMP_PATH}/${DESTINATION_NAME}.sql"
73+
rm -f "${TMP_PATH}/${DESTINATION_NAME}.sql"
74+
5575

5676
# Prompt to download the file, then delete it.
5777
echo "## Download URL: ${PUBLIC_URL}/${DESTINATION_NAME}.zip"

0 commit comments

Comments
 (0)