From b0b124006752746ed7ac74004b24d05ab8c3fe3c Mon Sep 17 00:00:00 2001 From: Jamie McClelland Date: Mon, 23 Jan 2023 11:10:17 -0500 Subject: [PATCH 01/10] add support for git repos --- src/DownloadsParser.php | 2 + src/Handler/GitHandler.php | 77 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/Handler/GitHandler.php diff --git a/src/DownloadsParser.php b/src/DownloadsParser.php index 614882e..12ad738 100644 --- a/src/DownloadsParser.php +++ b/src/DownloadsParser.php @@ -16,6 +16,7 @@ use LastCall\DownloadsPlugin\Handler\BaseHandler; use LastCall\DownloadsPlugin\Handler\FileHandler; use LastCall\DownloadsPlugin\Handler\PharHandler; +use LastCall\DownloadsPlugin\Handler\GitHandler; class DownloadsParser { @@ -62,6 +63,7 @@ public function pickClass($extraFile) 'archive' => ArchiveHandler::CLASS, 'file' => FileHandler::CLASS, 'phar' => PharHandler::CLASS, + 'git' => GitHandler::CLASS, ]; if (isset($extraFile['type'], $types[$extraFile['type']])) { return $types[$extraFile['type']]; diff --git a/src/Handler/GitHandler.php b/src/Handler/GitHandler.php new file mode 100644 index 0000000..f1532d7 --- /dev/null +++ b/src/Handler/GitHandler.php @@ -0,0 +1,77 @@ +setDistType('git'); + return $pkg; + } + + public function getTrackingFile() + { + $file = basename($this->extraFile['id']) . '-' . md5($this->extraFile['id']) . '.json'; + return + dirname($this->getTargetPath()) . + DIRECTORY_SEPARATOR . self::DOT_DIR . + DIRECTORY_SEPARATOR . $file; + } + + /** + * @param Composer $composer + * @param IOInterface $io + */ + public function download(Composer $composer, IOInterface $io) { + $urlAndVersion = $this->createTrackingData()['url']; + $config = $composer->getConfig(); + $wd = $this->getTargetPath(); + $process = new ProcessExecutor($io); + $cfs = new Filesystem(); + $git = new Git($io, $config, $process, $cfs); + if (file_exists($wd)) { + $gitCallable = static function ($urlAndVersion): string { + $parts = explode('@', $urlAndVersion); + $url = $parts[0]; + if (count($parts) > 1) { + $version = $parts[1]; + } + else { + $version = 'master'; + } + return sprintf('git remote update --prune origin && git checkout %s', ProcessExecutor::escape($version)); + }; + } + else { + if (!file_exists($wd)) { + mkdir($wd); + } + $gitCallable = static function ($urlAndVersion): string { + print_r($urlAndVersion); + $parts = explode('@', $urlAndVersion); + $url = $parts[0]; + if (count($parts) > 1) { + $version = $parts[1]; + } + else { + $version = 'master'; + } + return sprintf('git init && git remote add origin %s && git remote update --prune origin && git checkout %s', ProcessExecutor::escape($url), ProcessExecutor::escape($version)); + }; + } + $git->runCommand($gitCallable, $urlAndVersion, $wd); + } + +} From ed47064cbd4604ad22c7e1f1d2208b380574e299 Mon Sep 17 00:00:00 2001 From: Jamie McClelland Date: Mon, 23 Jan 2023 11:19:31 -0500 Subject: [PATCH 02/10] more sensible way to get the URL. --- src/Handler/GitHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Handler/GitHandler.php b/src/Handler/GitHandler.php index f1532d7..9b3da63 100644 --- a/src/Handler/GitHandler.php +++ b/src/Handler/GitHandler.php @@ -35,7 +35,7 @@ public function getTrackingFile() * @param IOInterface $io */ public function download(Composer $composer, IOInterface $io) { - $urlAndVersion = $this->createTrackingData()['url']; + $urlAndVersion = $this->getSubpackage()->getDistUrl(); $config = $composer->getConfig(); $wd = $this->getTargetPath(); $process = new ProcessExecutor($io); From c339ff87ec0037b6cc3b6db49ee1c5d97b8f0f5a Mon Sep 17 00:00:00 2001 From: Jamie McClelland Date: Tue, 24 Jan 2023 10:46:23 -0500 Subject: [PATCH 03/10] fix deprecation warnings. --- tests/SniffTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/SniffTest.php b/tests/SniffTest.php index f78c7f2..1dacb61 100644 --- a/tests/SniffTest.php +++ b/tests/SniffTest.php @@ -92,7 +92,7 @@ public function testDownloadAndRedownload($path, $file, $sha256) { else { unlink($path); } - $this->assertFileNotExists($file); + $this->assertFileDoesNotExist($file); $composer_path = self::getComposerPath(); PH::runOk("$composer_path install -v"); @@ -102,7 +102,7 @@ public function testDownloadAndRedownload($path, $file, $sha256) { public function assertFileChecksum($file, $sha256, $message = NULL) { if ($sha256 === NULL) { - $this->assertFileNotExists($file, "($message) File should not exist"); + $this->assertFileDoesNotExist($file, "($message) File should not exist"); } else { $this->assertFileExists($file, "($message) File should exist"); From 12abc9f2de0b418378efa1b2b5c36ea2506af518 Mon Sep 17 00:00:00 2001 From: Jamie McClelland Date: Tue, 24 Jan 2023 12:25:01 -0500 Subject: [PATCH 04/10] always purge download directory. --- src/Handler/GitHandler.php | 46 +++++++++++++++----------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/src/Handler/GitHandler.php b/src/Handler/GitHandler.php index 9b3da63..77a36cd 100644 --- a/src/Handler/GitHandler.php +++ b/src/Handler/GitHandler.php @@ -42,36 +42,26 @@ public function download(Composer $composer, IOInterface $io) { $cfs = new Filesystem(); $git = new Git($io, $config, $process, $cfs); if (file_exists($wd)) { - $gitCallable = static function ($urlAndVersion): string { - $parts = explode('@', $urlAndVersion); - $url = $parts[0]; - if (count($parts) > 1) { - $version = $parts[1]; - } - else { - $version = 'master'; - } - return sprintf('git remote update --prune origin && git checkout %s', ProcessExecutor::escape($version)); - }; + $cfs->removeDirectory($wd); } - else { - if (!file_exists($wd)) { - mkdir($wd); + // Make the directory recursively. + mkdir($wd, 0755, TRUE); + $gitCallable = static function ($urlAndVersion): string { + $parts = explode('@', $urlAndVersion); + $url = $parts[0]; + if (count($parts) > 1) { + $version = $parts[1]; } - $gitCallable = static function ($urlAndVersion): string { - print_r($urlAndVersion); - $parts = explode('@', $urlAndVersion); - $url = $parts[0]; - if (count($parts) > 1) { - $version = $parts[1]; - } - else { - $version = 'master'; - } - return sprintf('git init && git remote add origin %s && git remote update --prune origin && git checkout %s', ProcessExecutor::escape($url), ProcessExecutor::escape($version)); - }; - } - $git->runCommand($gitCallable, $urlAndVersion, $wd); + else { + $version = 'master'; + } + return sprintf('git init && git fetch --depth 1 %s %s && git checkout %s', + ProcessExecutor::escape($url), + ProcessExecutor::escape($version), + ProcessExecutor::escape($version) + ); + }; + $git->runCommand($gitCallable, $urlAndVersion, $wd); } } From e8aab4f4299e1f02e0833d060ec98f88d673e8f6 Mon Sep 17 00:00:00 2001 From: Jamie McClelland Date: Tue, 24 Jan 2023 12:25:12 -0500 Subject: [PATCH 05/10] update documentation. --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7ca12bf..157d588 100644 --- a/README.md +++ b/README.md @@ -60,8 +60,9 @@ The `extra.downloads` section contains a list of files to download. Each extra-f * `archive`: The `url` references a zip or tarball which should be extracted at the given `path`. (Default for URLs involving `*.zip`, `*.tar.gz`, or `*.tgz`.) * `file`: The `url` should be downloaded to the given `path`. (Default for all other URLs.) * `phar`: The `url` references a PHP executable which should be installed at the given `path`. + * `git`: The `url` references a git repository address and commit, tag or branch (separated by an @ sign) which should be checked out at the given `path`. (E.g. `https://git.me.org/foo/bar@123abc`.) -* `ignore`: (*Optional*) A list of a files that should be omited from the extracted folder. (This supports a subset of `.gitignore` notation.) +* `ignore`: (*Optional*) A list of a files that should be omited from the extracted folder. (This supports a subset of `.gitignore` notation.) The `ignore` property is ignored when using the `git` type. * `version`: (*Optional*) A version number for the downloaded artifact. This has no functional impact on the lifecycle of the artifact, but it can affect the console output, and it can be optionally used as a variable when setting `url` or `path`. From 3cfb161f19d04e6af6755a3a21b17fc403886ae3 Mon Sep 17 00:00:00 2001 From: Jamie McClelland Date: Tue, 24 Jan 2023 12:25:19 -0500 Subject: [PATCH 06/10] add test for git type. --- tests/SniffTest.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/SniffTest.php b/tests/SniffTest.php index 1dacb61..d123ba0 100644 --- a/tests/SniffTest.php +++ b/tests/SniffTest.php @@ -45,6 +45,11 @@ public static function getComposerJson() { 'url' => 'https://download.civicrm.org/cv/cv.phar-2019-08-20-14fe9da8', 'path' => 'bin/cv', ], + 'org.civicrm.sms.twilio' => [ + 'type' => 'git', + 'url' => 'https://github.com/civicrm/org.civicrm.sms.twilio@494728cf13bb65c228429665c158d899cd7e6dc1', + 'path' => 'web/sites/default/files/civicrm/ext/org.civicrm.sms.twilio', + ], ], ], 'config' => [ @@ -69,7 +74,8 @@ public function getExampleChecksums() { ['extern/jquery-full', 'extern/jquery-full/Gruntfile.js', '3508ff74f8ef106a80f25f28f44a20c47a2b67d84396bb141928ff978ba4012e'], ['extern/jquery-lesser', 'extern/jquery-lesser/dist/jquery.js', '5f2caf09052782caf67e1772c0abce31747ffbc7a1c50690e331b99c7d9ea8dc'], ['extern/jquery-lesser', 'extern/jquery-lesser/Gruntfile.js', NULL], - ['bin/cv', 'bin/cv', 'bf162d5d7dd0bef087d7dd07f474039b2e25c4bcca328a2b2097958ac6294476'] + ['bin/cv', 'bin/cv', 'bf162d5d7dd0bef087d7dd07f474039b2e25c4bcca328a2b2097958ac6294476'], + ['web/sites/default/files/civicrm/ext/org.civicrm.sms.twilio', 'web/sites/default/files/civicrm/ext/org.civicrm.sms.twilio/info.xml', 'fce6bbd3af2315b4357393b3fc76b52ebc6ab3c27290c82ce7545f0bf2928109'], ]; } From 6f94fa2b744ce1b9017f35ba31c772c922c4557d Mon Sep 17 00:00:00 2001 From: Jamie McClelland Date: Tue, 24 Jan 2023 12:30:57 -0500 Subject: [PATCH 07/10] use composer functions when available. --- src/Handler/GitHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Handler/GitHandler.php b/src/Handler/GitHandler.php index 77a36cd..ffe226e 100644 --- a/src/Handler/GitHandler.php +++ b/src/Handler/GitHandler.php @@ -45,7 +45,7 @@ public function download(Composer $composer, IOInterface $io) { $cfs->removeDirectory($wd); } // Make the directory recursively. - mkdir($wd, 0755, TRUE); + $cfs->ensureDirectoryExists($wd); $gitCallable = static function ($urlAndVersion): string { $parts = explode('@', $urlAndVersion); $url = $parts[0]; From 8ae9f607fe5c7999268a6aaef21e39c2368765eb Mon Sep 17 00:00:00 2001 From: Jamie McClelland Date: Tue, 24 Jan 2023 13:36:29 -0500 Subject: [PATCH 08/10] specifying depth limits the commits we can checkout We could make it another parametr? --- src/Handler/GitHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Handler/GitHandler.php b/src/Handler/GitHandler.php index ffe226e..dc9dc56 100644 --- a/src/Handler/GitHandler.php +++ b/src/Handler/GitHandler.php @@ -55,7 +55,7 @@ public function download(Composer $composer, IOInterface $io) { else { $version = 'master'; } - return sprintf('git init && git fetch --depth 1 %s %s && git checkout %s', + return sprintf('git init && git fetch %s && git checkout %s', ProcessExecutor::escape($url), ProcessExecutor::escape($version), ProcessExecutor::escape($version) From a943cd027f5832e6f09a6a8ff74742a33fe9692c Mon Sep 17 00:00:00 2001 From: Jamie McClelland Date: Tue, 24 Jan 2023 13:41:25 -0500 Subject: [PATCH 09/10] remove extra argument to sprintf --- src/Handler/GitHandler.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Handler/GitHandler.php b/src/Handler/GitHandler.php index dc9dc56..fbf4eab 100644 --- a/src/Handler/GitHandler.php +++ b/src/Handler/GitHandler.php @@ -57,7 +57,6 @@ public function download(Composer $composer, IOInterface $io) { } return sprintf('git init && git fetch %s && git checkout %s', ProcessExecutor::escape($url), - ProcessExecutor::escape($version), ProcessExecutor::escape($version) ); }; From 1a2da1b9b0c26c35982fc1adfaa7c0de9b201757 Mon Sep 17 00:00:00 2001 From: Jamie McClelland Date: Tue, 24 Jan 2023 14:58:33 -0500 Subject: [PATCH 10/10] we have to get all the branches we can't necessarily count on the commit being in the main branch. Alternatively, we could alter our syntax to allow us to specify a branch in the composer.json file? --- src/Handler/GitHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Handler/GitHandler.php b/src/Handler/GitHandler.php index fbf4eab..af502a0 100644 --- a/src/Handler/GitHandler.php +++ b/src/Handler/GitHandler.php @@ -55,7 +55,7 @@ public function download(Composer $composer, IOInterface $io) { else { $version = 'master'; } - return sprintf('git init && git fetch %s && git checkout %s', + return sprintf('git init && git fetch %s "+refs/heads/*:refs/remotes/origin/*" && git checkout %s', ProcessExecutor::escape($url), ProcessExecutor::escape($version) );