From 6140897d088408ed56b3ae661c8d525d0346a685 Mon Sep 17 00:00:00 2001 From: Basil Peace Date: Wed, 29 Apr 2020 00:38:08 +0300 Subject: [PATCH] feat: add archive.name config option --- doc/04-schema.md | 15 +++++++ res/composer-schema.json | 4 ++ src/Composer/Package/AliasPackage.php | 5 +++ .../Package/Archiver/ArchiveManager.php | 39 ++++++++++++------- src/Composer/Package/Dumper/ArrayDumper.php | 3 ++ src/Composer/Package/Loader/ArrayLoader.php | 3 ++ src/Composer/Package/Package.php | 19 +++++++++ src/Composer/Package/PackageInterface.php | 7 ++++ 8 files changed, 80 insertions(+), 15 deletions(-) diff --git a/doc/04-schema.md b/doc/04-schema.md index ecff3d996..57debee9c 100644 --- a/doc/04-schema.md +++ b/doc/04-schema.md @@ -883,6 +883,21 @@ A set of options for creating package archives. The following options are supported: +* **name:** Allows configuring base name for archive. + By default (if not configured, and `--file` is not passed as command-line argument), + `preg_replace('#[^a-z0-9-_]#i', '-', name)` is used. + +Example: + +```json +{ + "name": "org/strangeName", + "archive": { + "name": "Strange_name" + } +} +``` + * **exclude:** Allows configuring a list of patterns for excluded paths. The pattern syntax matches .gitignore files. A leading exclamation mark (!) will result in any matching files to be included even if a previous pattern diff --git a/res/composer-schema.json b/res/composer-schema.json index bcb712882..565454ac8 100644 --- a/res/composer-schema.json +++ b/res/composer-schema.json @@ -354,6 +354,10 @@ "type": ["object"], "description": "Options for creating package archives for distribution.", "properties": { + "name": { + "type": "string", + "description": "A base name for archive." + }, "exclude": { "type": "array", "description": "A list of patterns for paths to exclude or include if prefixed with an exclamation mark." diff --git a/src/Composer/Package/AliasPackage.php b/src/Composer/Package/AliasPackage.php index ee93ec497..34fbd8713 100644 --- a/src/Composer/Package/AliasPackage.php +++ b/src/Composer/Package/AliasPackage.php @@ -387,6 +387,11 @@ class AliasPackage extends BasePackage implements CompletePackageInterface return $this->aliasOf->getNotificationUrl(); } + public function getArchiveName() + { + return $this->aliasOf->getArchiveName(); + } + public function getArchiveExcludes() { return $this->aliasOf->getArchiveExcludes(); diff --git a/src/Composer/Package/Archiver/ArchiveManager.php b/src/Composer/Package/Archiver/ArchiveManager.php index 6f8fa8a01..a523800c7 100644 --- a/src/Composer/Package/Archiver/ArchiveManager.php +++ b/src/Composer/Package/Archiver/ArchiveManager.php @@ -72,7 +72,12 @@ class ArchiveManager */ public function getPackageFilename(PackageInterface $package) { - $nameParts = array(preg_replace('#[^a-z0-9-_]#i', '-', $package->getName())); + if ($package->getArchiveName()) { + $baseName = $package->getArchiveName(); + } else { + $baseName = preg_replace('#[^a-z0-9-_]#i', '-', $package->getName()); + } + $nameParts = array($baseName); if (preg_match('{^[a-f0-9]{40}$}', $package->getDistReference())) { array_push($nameParts, $package->getDistReference(), $package->getDistType()); @@ -125,20 +130,6 @@ class ArchiveManager } $filesystem = new Filesystem(); - if (null === $fileName) { - $packageName = $this->getPackageFilename($package); - } else { - $packageName = $fileName; - } - - // Archive filename - $filesystem->ensureDirectoryExists($targetDir); - $target = realpath($targetDir).'/'.$packageName.'.'.$format; - $filesystem->ensureDirectoryExists(dirname($target)); - - if (!$this->overwriteFiles && file_exists($target)) { - return $target; - } if ($package instanceof RootPackageInterface) { $sourcePath = realpath('.'); @@ -159,12 +150,30 @@ class ArchiveManager if (file_exists($composerJsonPath = $sourcePath.'/composer.json')) { $jsonFile = new JsonFile($composerJsonPath); $jsonData = $jsonFile->read(); + if (!empty($jsonData['archive']['name'])) { + $package->setArchiveName($jsonData['archive']['name']); + } if (!empty($jsonData['archive']['exclude'])) { $package->setArchiveExcludes($jsonData['archive']['exclude']); } } } + if (null === $fileName) { + $packageName = $this->getPackageFilename($package); + } else { + $packageName = $fileName; + } + + // Archive filename + $filesystem->ensureDirectoryExists($targetDir); + $target = realpath($targetDir).'/'.$packageName.'.'.$format; + $filesystem->ensureDirectoryExists(dirname($target)); + + if (!$this->overwriteFiles && file_exists($target)) { + return $target; + } + // Create the archive $tempTarget = sys_get_temp_dir().'/composer_archive'.uniqid().'.'.$format; $filesystem->ensureDirectoryExists(dirname($tempTarget)); diff --git a/src/Composer/Package/Dumper/ArrayDumper.php b/src/Composer/Package/Dumper/ArrayDumper.php index dece598f1..8999666eb 100644 --- a/src/Composer/Package/Dumper/ArrayDumper.php +++ b/src/Composer/Package/Dumper/ArrayDumper.php @@ -70,6 +70,9 @@ class ArrayDumper } } + if ($package->getArchiveName()) { + $data['archive']['name'] = $package->getArchiveName(); + } if ($package->getArchiveExcludes()) { $data['archive']['exclude'] = $package->getArchiveExcludes(); } diff --git a/src/Composer/Package/Loader/ArrayLoader.php b/src/Composer/Package/Loader/ArrayLoader.php index 228632b42..cb9058941 100644 --- a/src/Composer/Package/Loader/ArrayLoader.php +++ b/src/Composer/Package/Loader/ArrayLoader.php @@ -159,6 +159,9 @@ class ArrayLoader implements LoaderInterface $package->setNotificationUrl($config['notification-url']); } + if (!empty($config['archive']['name'])) { + $package->setArchiveName($config['archive']['name']); + } if (!empty($config['archive']['exclude'])) { $package->setArchiveExcludes($config['archive']['exclude']); } diff --git a/src/Composer/Package/Package.php b/src/Composer/Package/Package.php index 6c7b426e7..e5b39b896 100644 --- a/src/Composer/Package/Package.php +++ b/src/Composer/Package/Package.php @@ -57,6 +57,7 @@ class Package extends BasePackage protected $autoload = array(); protected $devAutoload = array(); protected $includePaths = array(); + protected $archiveName; protected $archiveExcludes = array(); /** @@ -551,6 +552,24 @@ class Package extends BasePackage return $this->notificationUrl; } + /** + * Sets default base filename for archive + * + * @param string $name + */ + public function setArchiveName($name) + { + $this->archiveName = $name; + } + + /** + * {@inheritDoc} + */ + public function getArchiveName() + { + return $this->archiveName; + } + /** * Sets a list of patterns to be excluded from archives * diff --git a/src/Composer/Package/PackageInterface.php b/src/Composer/Package/PackageInterface.php index cb16efa7e..adbc3b189 100644 --- a/src/Composer/Package/PackageInterface.php +++ b/src/Composer/Package/PackageInterface.php @@ -345,6 +345,13 @@ interface PackageInterface */ public function getPrettyString(); + /** + * Returns default base filename for archive + * + * @return array + */ + public function getArchiveName(); + /** * Returns a list of patterns to exclude from package archives *