From 44283307267037029b67ea8c3e2f5c998d5790b6 Mon Sep 17 00:00:00 2001 From: Paul Mitchum Date: Mon, 13 Jun 2022 14:44:22 -0700 Subject: [PATCH 1/4] first pass --- bin/app.php | 12 +++++- bin/dktl | 64 +++++++++++++++++--------------- composer.json | 4 ++ src/Command/FrontendCommands.php | 2 + src/Command/InitCommands.php | 11 +++--- src/Util/Util.php | 18 ++++----- 6 files changed, 66 insertions(+), 45 deletions(-) diff --git a/bin/app.php b/bin/app.php index a9acede4..55c24ed9 100755 --- a/bin/app.php +++ b/bin/app.php @@ -4,7 +4,17 @@ use Consolidation\AnnotatedCommand\CommandFileDiscovery; use Symfony\Component\Console\Output\ConsoleOutput; -require_once __DIR__ . '/../vendor/autoload.php'; +foreach ([ + // Legacy-style autoload. + '/../vendor/autoload.php', + // Under vendor. + '/../../vendor/autoload.php', + ] as $path) { + $pathy = __DIR__ . $path; + if (file_exists($pathy)) { + require_once $pathy; + } +} putenv("COMPOSER_MEMORY_LIMIT=-1"); diff --git a/bin/dktl b/bin/dktl index 7846af7a..93d44769 100755 --- a/bin/dktl +++ b/bin/dktl @@ -13,7 +13,7 @@ main() { set_project_directory "$@" set_slug - + if [ "$DKTL_MODE" = "DOCKER" ]; then dktl_docker_run "$@" elif [ "$DKTL_MODE" = "HOST" ]; then @@ -52,20 +52,20 @@ dktl_docker_run() { # Proxy-pass to docker and save exit status. The command will re- # run in host mode inside the cli container, so dktl_run will still - # ultimately be run whichever mode dktl is set to. + # ultimately be run whichever mode dktl is set to. dc_base exec $EXEC_OPTS cli dktl "$1" "${@:2}" exit_status=$? dktl_docker_cleanup "$@" - # if we encountered a non-zero exit status during the docker exec, + # if we encountered a non-zero exit status during the docker exec, # pass it on. if [ $exit_status -ne 0 ]; then exit $exit_status fi } -# Set DKTL_PLATFORM to which platform we're running on (linux and mac +# Set DKTL_PLATFORM to which platform we're running on (linux and mac # supported) set_platform() { if [ -z $PLATFORM ]; then @@ -73,7 +73,7 @@ set_platform() { fi } -# Set DKTL_MODE. Determine whether we want to run inside the docker container +# Set DKTL_MODE. Determine whether we want to run inside the docker container # or in the host machine. set_mode() { if [ -z $DKTL_MODE ] || [ "$DKTL_MODE" = "DOCKER" ]; then @@ -103,22 +103,25 @@ check_dependencies() { # Set DKTL_PROJECT_DIRECTORY. Needs "$@" because dktl init hsa different logic. set_project_directory() { - # Find project root so we can run from anywhere - find_up () { - path=$(pwd) - while [[ "$path" != "" && ! -e "$path/$1" ]]; do - path=${path%/*} - done - echo "$path" - } - - if [ "$1" = "init" ] || [ "$1" = "down" ]; then + if [ -z "$DKTL_PROJECT_DIRECTORY" ]; then DKTL_PROJECT_DIRECTORY=$(pwd) - else - DKTL_PROJECT_DIRECTORY=$(find_up dktl.yml) - if [ -z "$DKTL_PROJECT_DIRECTORY" ]; then - echo "DKTL is running outside of a DKTL project. Run dktl init in the project directory first." - exit 1 + # Find project root so we can run from anywhere + find_up () { + path=$(pwd) + while [[ "$path" != "" && ! -e "$path/$1" ]]; do + path=${path%/*} + done + echo "$path" + } + + if [ "$1" = "init" ] || [ "$1" = "down" ]; then + DKTL_PROJECT_DIRECTORY=$(pwd) + else + DKTL_PROJECT_DIRECTORY=$(find_up dktl.yml) + if [ -z "$DKTL_PROJECT_DIRECTORY" ]; then + echo "DKTL is running outside of a DKTL project. Run dktl init in the project directory first." + exit 1 + fi fi fi export DKTL_PROJECT_DIRECTORY @@ -126,17 +129,20 @@ set_project_directory() { # Set DKTL_DIRECTORY set_directory() { - DKTL_DIRECTORY=$(which dktl) + if [ -z "$DKTL_DIRECTORY" ]; then + DKTL_DIRECTORY=$(which dktl) - if [[ -L $(which dktl) ]]; then - # readlink command needs -f to work properly in linux - if [ "$PLATFORM" = "Linux" ]; then RL_OPT='-f'; fi; - DKTL_DIRECTORY=$(readlink $RL_OPT "$DKTL_DIRECTORY") - fi + if [[ -L $(which dktl) ]]; then + # readlink command needs -f to work properly in linux + if [ "$PLATFORM" = "Linux" ]; then RL_OPT='-f'; fi; + DKTL_DIRECTORY=$(readlink $RL_OPT "$DKTL_DIRECTORY") + fi - # Currently DKTL_DIRECTORY contains /bin/dktl, strip the - # /bin/dktl to get the root dktl directory. - DKTL_DIRECTORY="${DKTL_DIRECTORY%/bin/dktl}" + # TODO: Figure out a way to modify the path without making assumptions. + # Currently DKTL_DIRECTORY contains /bin/dktl, strip the + # /bin/dktl to get the root dktl directory. + DKTL_DIRECTORY="${DKTL_DIRECTORY%/bin/dktl}" + fi export DKTL_DIRECTORY } diff --git a/composer.json b/composer.json index c7f060b5..7446b86c 100644 --- a/composer.json +++ b/composer.json @@ -5,6 +5,10 @@ "pear/archive_tar": "^1.4", "aws/aws-sdk-php": "^3.67" }, + "bin": [ + "bin/dktl", + "bin/app.php" + ], "autoload": { "psr-4": { "DkanTools\\": "src", diff --git a/src/Command/FrontendCommands.php b/src/Command/FrontendCommands.php index 9b5bfb05..a40bf5c9 100644 --- a/src/Command/FrontendCommands.php +++ b/src/Command/FrontendCommands.php @@ -207,6 +207,8 @@ private function npmInstall() /** * Symlink in global cypress installation. + * + * @todo: make this go away. @danielgading */ private function cypressLink() { diff --git a/src/Command/InitCommands.php b/src/Command/InitCommands.php index b5440830..3b0885a8 100644 --- a/src/Command/InitCommands.php +++ b/src/Command/InitCommands.php @@ -13,8 +13,6 @@ class InitCommands extends \Robo\Tasks * and directories for development, including a composer.json (but NOT * including any Composer dependencies.) * - * @option str drupal - * Drupal composer version (expressed as composer constraint). * @option str dkan * DKAN version (expressed as composer constraint). Use 2.x-dev for current * bleeding edge. @@ -25,9 +23,10 @@ class InitCommands extends \Robo\Tasks */ public function init($opts = ['dkan' => null, 'dkan-local' => false]) { - $this->initConfig(); - $this->initSrc(); - $this->initDrupal(); + // Handled by Composer. + // $this->initConfig(); + // $this->initSrc(); + // $this->initDrupal(); if ($opts['dkan-local']) { $this->initLocalDkan(); $version = $this->localDkanVersion(); @@ -231,7 +230,7 @@ public function initDkan(string $version = null) */ private function initLocalDkan() { - $this->io()->section('Adding local DKAN repository in /dkan.'); + $this->io()->section('Adding local DKAN repository for /dkan.'); $this->taskComposerConfig() ->repository('getdkan', 'dkan', 'path') ->run(); diff --git a/src/Util/Util.php b/src/Util/Util.php index 774ed8b7..2735849f 100644 --- a/src/Util/Util.php +++ b/src/Util/Util.php @@ -25,14 +25,14 @@ public static function getDktlProxyDomain() } } - public static function getProjectDirectory() - { - if (isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] == "init") { - $directory = exec("pwd"); - return $directory; - } - - return getenv("DKTL_PROJECT_DIRECTORY"); + public static function getProjectDirectory() { + if ($proj_dir = getenv("DKTL_PROJECT_DIRECTORY")) { + return $proj_dir; + } + if (isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] == "init") { + $directory = exec("pwd"); + return $directory; + } } public static function getUri() @@ -80,7 +80,7 @@ public static function urlExists($url) $headers = @get_headers($url); return (count(preg_grep('/^HTTP.*404/', $headers)) > 0) ? false : true; } - + public static function directoryAndFileCreationCheck(\Robo\Result $result, $df, $io) { if ($result->getExitCode() == 0 && file_exists($df)) { From b3c0eea93896e56d019e4787959483c696b27278 Mon Sep 17 00:00:00 2001 From: Paul Mitchum Date: Mon, 13 Jun 2022 14:50:39 -0700 Subject: [PATCH 2/4] revert some changes --- src/Command/FrontendCommands.php | 2 -- src/Command/InitCommands.php | 11 ++++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Command/FrontendCommands.php b/src/Command/FrontendCommands.php index a40bf5c9..9b5bfb05 100644 --- a/src/Command/FrontendCommands.php +++ b/src/Command/FrontendCommands.php @@ -207,8 +207,6 @@ private function npmInstall() /** * Symlink in global cypress installation. - * - * @todo: make this go away. @danielgading */ private function cypressLink() { diff --git a/src/Command/InitCommands.php b/src/Command/InitCommands.php index 3b0885a8..b5440830 100644 --- a/src/Command/InitCommands.php +++ b/src/Command/InitCommands.php @@ -13,6 +13,8 @@ class InitCommands extends \Robo\Tasks * and directories for development, including a composer.json (but NOT * including any Composer dependencies.) * + * @option str drupal + * Drupal composer version (expressed as composer constraint). * @option str dkan * DKAN version (expressed as composer constraint). Use 2.x-dev for current * bleeding edge. @@ -23,10 +25,9 @@ class InitCommands extends \Robo\Tasks */ public function init($opts = ['dkan' => null, 'dkan-local' => false]) { - // Handled by Composer. - // $this->initConfig(); - // $this->initSrc(); - // $this->initDrupal(); + $this->initConfig(); + $this->initSrc(); + $this->initDrupal(); if ($opts['dkan-local']) { $this->initLocalDkan(); $version = $this->localDkanVersion(); @@ -230,7 +231,7 @@ public function initDkan(string $version = null) */ private function initLocalDkan() { - $this->io()->section('Adding local DKAN repository for /dkan.'); + $this->io()->section('Adding local DKAN repository in /dkan.'); $this->taskComposerConfig() ->repository('getdkan', 'dkan', 'path') ->run(); From aa5f40cae11bfd1b1e988317e6bcd7284247f3e3 Mon Sep 17 00:00:00 2001 From: Paul Mitchum Date: Mon, 13 Jun 2022 15:04:15 -0700 Subject: [PATCH 3/4] PSR-2 coding standards per CircleCi --- .editorconfig | 27 ++++++++++++++++++ bin/app.php | 16 +++++------ src/Util/Util.php | 71 +++++++++++++++++++++++------------------------ 3 files changed, 69 insertions(+), 45 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..c72cfa30 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,27 @@ +# EditorConfig helps keep your project formatting consistent. +# See https://EditorConfig.org +# +# PHP PSR-2 Coding Standards +# http://www.php-fig.org/psr/psr-2/ +# +# Author: Sal Ferrarello (@salcode) +# +# You can download this file directly +# to your project from the command-line with +# curl -O https://gist.githubusercontent.com/salcode/18eaa8cfa3b097da2f58b582197906d7/raw/.editorconfig + +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +indent_style = space +indent_size = 2 + +[*.php] +indent_size = 4 + +[*.md] +trim_trailing_whitespace = false diff --git a/bin/app.php b/bin/app.php index 55c24ed9..3098c07b 100755 --- a/bin/app.php +++ b/bin/app.php @@ -5,15 +5,15 @@ use Symfony\Component\Console\Output\ConsoleOutput; foreach ([ - // Legacy-style autoload. - '/../vendor/autoload.php', - // Under vendor. - '/../../vendor/autoload.php', + // Legacy-style autoload. + '/../vendor/autoload.php', + // Under vendor. + '/../../vendor/autoload.php', ] as $path) { - $pathy = __DIR__ . $path; - if (file_exists($pathy)) { - require_once $pathy; - } + $pathy = __DIR__ . $path; + if (file_exists($pathy)) { + require_once $pathy; + } } putenv("COMPOSER_MEMORY_LIMIT=-1"); diff --git a/src/Util/Util.php b/src/Util/Util.php index 2735849f..95823d45 100644 --- a/src/Util/Util.php +++ b/src/Util/Util.php @@ -7,36 +7,34 @@ * * @todo Refactor this to follow Robo standards (tasks? base command class?) */ -class Util -{ +class Util { + const TMP_DIR = "/tmp/dktl"; - public static function getDktlDirectory() - { + public static function getDktlDirectory() { return getenv("DKTL_DIRECTORY"); } - public static function getDktlProxyDomain() - { + public static function getDktlProxyDomain() { if ($proxy = getenv("DKTL_PROXY_DOMAIN")) { return $proxy; - } else { + } + else { return ''; } } public static function getProjectDirectory() { - if ($proj_dir = getenv("DKTL_PROJECT_DIRECTORY")) { - return $proj_dir; - } - if (isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] == "init") { - $directory = exec("pwd"); - return $directory; - } + if ($proj_dir = getenv("DKTL_PROJECT_DIRECTORY")) { + return $proj_dir; + } + if (isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] == "init") { + $directory = exec("pwd"); + return $directory; + } } - public static function getUri() - { + public static function getUri() { $domain = getenv("DKTL_PROXY_DOMAIN"); if ($domain) { $domain = "http://" . $domain; @@ -44,48 +42,44 @@ public static function getUri() return $domain; } - public static function getProjectDocroot() - { + public static function getProjectDocroot() { return self::getProjectDirectory() . "/docroot"; } - public static function drushConcurrency() - { + public static function drushConcurrency() { if (`uname` == "Darwin") { $concurrency = trim(`sysctl -n hw.ncpu`); - } else { + } + else { $concurrency = trim(`grep -c ^processor /proc/cpuinfo`); } return is_numeric($concurrency) ? $concurrency : ''; } - public static function prepareTmp() - { + public static function prepareTmp() { $tmp_dir = self::TMP_DIR; if (!file_exists($tmp_dir)) { mkdir($tmp_dir); } } - public static function cleanupTmp() - { + public static function cleanupTmp() { $tmp_dir = self::TMP_DIR; if (file_exists($tmp_dir)) { exec("rm -rf {$tmp_dir}"); } } - public static function urlExists($url) - { + public static function urlExists($url) { $headers = @get_headers($url); - return (count(preg_grep('/^HTTP.*404/', $headers)) > 0) ? false : true; + return (count(preg_grep('/^HTTP.*404/', $headers)) > 0) ? FALSE : TRUE; } - public static function directoryAndFileCreationCheck(\Robo\Result $result, $df, $io) - { + public static function directoryAndFileCreationCheck(\Robo\Result $result, $df, $io) { if ($result->getExitCode() == 0 && file_exists($df)) { $io->success("{$df} was created."); - } else { + } + else { throw new \Exception("{$df} was not created."); } } @@ -93,9 +87,12 @@ public static function directoryAndFileCreationCheck(\Robo\Result $result, $df, /** * Copy of \Drupal\Component\Utility\Crypt::randomBytesBase64() */ - public static function generateHashSalt($count = 32) - { - return str_replace(['+', '/', '='], ['-', '_', ''], base64_encode(random_bytes($count))); + public static function generateHashSalt($count = 32) { + return str_replace(['+', '/', '='], [ + '-', + '_', + '', + ], base64_encode(random_bytes($count))); } /** @@ -103,12 +100,12 @@ public static function generateHashSalt($count = 32) * Exception. */ - public static function ensureFilesExist(array $paths, $message) - { + public static function ensureFilesExist(array $paths, $message) { foreach ($paths as $path) { - if (! file_exists($path)) { + if (!file_exists($path)) { throw new \Exception("{$path} is missing."); } } } + } From 2193d5e6cc4310dd6e07b6e780cd7f320c3cbafd Mon Sep 17 00:00:00 2001 From: Paul Mitchum Date: Mon, 13 Jun 2022 15:20:54 -0700 Subject: [PATCH 4/4] PSR-2 config for phpcs. --- composer.json | 3 ++ composer.lock | 63 +++++++++++++++++++++++++++++++-- phpcs.xml | 21 +++++++++++ src/Command/ProjectCommands.php | 3 +- src/Util/Util.php | 51 +++++++++++++++----------- 5 files changed, 115 insertions(+), 26 deletions(-) create mode 100644 phpcs.xml diff --git a/composer.json b/composer.json index 7446b86c..43bd27f0 100644 --- a/composer.json +++ b/composer.json @@ -19,5 +19,8 @@ "platform": { "php": "7.4" } + }, + "require-dev": { + "squizlabs/php_codesniffer": "^3.7" } } diff --git a/composer.lock b/composer.lock index c7a4abbf..734aba25 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "fe253ea4beefd6b14efcc890d092a240", + "content-hash": "b5feae8d371314005ccfdd48444c15dc", "packages": [ { "name": "aws/aws-crt-php", @@ -3032,7 +3032,64 @@ "time": "2022-01-26T16:32:32+00:00" } ], - "packages-dev": [], + "packages-dev": [ + { + "name": "squizlabs/php_codesniffer", + "version": "3.7.0", + "source": { + "type": "git", + "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", + "reference": "a2cd51b45bcaef9c1f2a4bda48f2dd2fa2b95563" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/a2cd51b45bcaef9c1f2a4bda48f2dd2fa2b95563", + "reference": "a2cd51b45bcaef9c1f2a4bda48f2dd2fa2b95563", + "shasum": "" + }, + "require": { + "ext-simplexml": "*", + "ext-tokenizer": "*", + "ext-xmlwriter": "*", + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" + }, + "bin": [ + "bin/phpcs", + "bin/phpcbf" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Greg Sherwood", + "role": "lead" + } + ], + "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", + "homepage": "https://github.com/squizlabs/PHP_CodeSniffer", + "keywords": [ + "phpcs", + "standards" + ], + "support": { + "issues": "https://github.com/squizlabs/PHP_CodeSniffer/issues", + "source": "https://github.com/squizlabs/PHP_CodeSniffer", + "wiki": "https://github.com/squizlabs/PHP_CodeSniffer/wiki" + }, + "time": "2022-06-13T06:31:38+00:00" + } + ], "aliases": [], "minimum-stability": "stable", "stability-flags": [], @@ -3043,5 +3100,5 @@ "platform-overrides": { "php": "7.4" }, - "plugin-api-version": "2.2.0" + "plugin-api-version": "2.3.0" } diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 00000000..9e349c31 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,21 @@ + + + The coding standard for DKAN Tools. + + */bin/dktl + */assets + */vendor + + + + + + + + + + + + + diff --git a/src/Command/ProjectCommands.php b/src/Command/ProjectCommands.php index c8118630..61e4618b 100644 --- a/src/Command/ProjectCommands.php +++ b/src/Command/ProjectCommands.php @@ -71,8 +71,7 @@ protected function symlinkOrInstallCypress(): void if ($result->getExitCode() !== 0) { throw new \RuntimeException('Failed to symlink cypress package folder'); } - } - else { + } else { $this->io()->warning('Cypress installation not found in standard location; Attempting to install cypress locally...'); $result = $this->taskExec('npm install cypress') ->dir(self::TESTS_DIR) diff --git a/src/Util/Util.php b/src/Util/Util.php index 95823d45..7b0cbba2 100644 --- a/src/Util/Util.php +++ b/src/Util/Util.php @@ -7,24 +7,27 @@ * * @todo Refactor this to follow Robo standards (tasks? base command class?) */ -class Util { +class Util +{ const TMP_DIR = "/tmp/dktl"; - public static function getDktlDirectory() { + public static function getDktlDirectory() + { return getenv("DKTL_DIRECTORY"); } - public static function getDktlProxyDomain() { + public static function getDktlProxyDomain() + { if ($proxy = getenv("DKTL_PROXY_DOMAIN")) { return $proxy; - } - else { + } else { return ''; } } - public static function getProjectDirectory() { + public static function getProjectDirectory() + { if ($proj_dir = getenv("DKTL_PROJECT_DIRECTORY")) { return $proj_dir; } @@ -34,7 +37,8 @@ public static function getProjectDirectory() { } } - public static function getUri() { + public static function getUri() + { $domain = getenv("DKTL_PROXY_DOMAIN"); if ($domain) { $domain = "http://" . $domain; @@ -42,44 +46,48 @@ public static function getUri() { return $domain; } - public static function getProjectDocroot() { + public static function getProjectDocroot() + { return self::getProjectDirectory() . "/docroot"; } - public static function drushConcurrency() { + public static function drushConcurrency() + { if (`uname` == "Darwin") { $concurrency = trim(`sysctl -n hw.ncpu`); - } - else { + } else { $concurrency = trim(`grep -c ^processor /proc/cpuinfo`); } return is_numeric($concurrency) ? $concurrency : ''; } - public static function prepareTmp() { + public static function prepareTmp() + { $tmp_dir = self::TMP_DIR; if (!file_exists($tmp_dir)) { mkdir($tmp_dir); } } - public static function cleanupTmp() { + public static function cleanupTmp() + { $tmp_dir = self::TMP_DIR; if (file_exists($tmp_dir)) { exec("rm -rf {$tmp_dir}"); } } - public static function urlExists($url) { + public static function urlExists($url) + { $headers = @get_headers($url); - return (count(preg_grep('/^HTTP.*404/', $headers)) > 0) ? FALSE : TRUE; + return (count(preg_grep('/^HTTP.*404/', $headers)) > 0) ? false : true; } - public static function directoryAndFileCreationCheck(\Robo\Result $result, $df, $io) { + public static function directoryAndFileCreationCheck(\Robo\Result $result, $df, $io) + { if ($result->getExitCode() == 0 && file_exists($df)) { $io->success("{$df} was created."); - } - else { + } else { throw new \Exception("{$df} was not created."); } } @@ -87,7 +95,8 @@ public static function directoryAndFileCreationCheck(\Robo\Result $result, $df, /** * Copy of \Drupal\Component\Utility\Crypt::randomBytesBase64() */ - public static function generateHashSalt($count = 32) { + public static function generateHashSalt($count = 32) + { return str_replace(['+', '/', '='], [ '-', '_', @@ -100,12 +109,12 @@ public static function generateHashSalt($count = 32) { * Exception. */ - public static function ensureFilesExist(array $paths, $message) { + public static function ensureFilesExist(array $paths, $message) + { foreach ($paths as $path) { if (!file_exists($path)) { throw new \Exception("{$path} is missing."); } } } - }