From 47bb1b7695306fab9f07940420d0e0293d96a2e5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= <glen@delfi.ee>
Date: Wed, 6 Jan 2016 20:26:51 +0200
Subject: [PATCH 1/3] add 'include-filename' to config

this allows using fixed name to include files to avoid polluting
includes/ dir with old versions
---
 src/Composer/Satis/Builder/PackagesBuilder.php | 42 ++++++++++++++------------
 src/Composer/Satis/Command/BuildCommand.php    |  2 ++
 2 files changed, 25 insertions(+), 19 deletions(-)

diff --git a/src/Composer/Satis/Builder/PackagesBuilder.php b/src/Composer/Satis/Builder/PackagesBuilder.php
index 9444bc1..cc566b2 100644
--- a/src/Composer/Satis/Builder/PackagesBuilder.php
+++ b/src/Composer/Satis/Builder/PackagesBuilder.php
@@ -11,7 +11,6 @@
 
 namespace Composer\Satis\Builder;
 
-use Composer\Composer;
 use Composer\Json\JsonFile;
 use Composer\Package\Dumper\ArrayDumper;
 use Symfony\Component\Console\Output\OutputInterface;
@@ -23,12 +22,12 @@
  */
 class PackagesBuilder extends Builder implements BuilderInterface
 {
-    /** @var string prefix of included json files. */
-    private $filenamePrefix;
-
     /** @var string packages.json file name. */
     private $filename;
 
+    /** @var string included json filename template */
+    private $includeFileName;
+
     /**
      * Dedicated Packages Constructor.
      *
@@ -41,8 +40,8 @@ public function __construct(OutputInterface $output, $outputDir, $config, $skipE
     {
         parent::__construct($output, $outputDir, $config, $skipErrors);
 
-        $this->filenamePrefix = $this->outputDir.'/include/all';
         $this->filename = $this->outputDir.'/packages.json';
+        $this->includeFileName = isset($config['include-filename']) ? $config['include-filename'] : 'include/all${sha1}.json';
     }
 
     /**
@@ -52,13 +51,7 @@ public function __construct(OutputInterface $output, $outputDir, $config, $skipE
      */
     public function dump(array $packages)
     {
-        $packageFile = $this->dumpPackageIncludeJson($packages);
-        $packageFileHash = hash_file('sha1', $packageFile);
-
-        $includes = array(
-            'include/all$'.$packageFileHash.'.json' => array('sha1' => $packageFileHash),
-        );
-
+        $includes = $this->dumpPackageIncludeJson($packages);
         $this->dumpPackagesJson($includes);
     }
 
@@ -67,7 +60,7 @@ public function dump(array $packages)
      *
      * @param array $packages List of packages to dump
      *
-     * @return string $filenameWithHash Includes JSON file name
+     * @return array Definition of "includes" block for packages.json
      */
     private function dumpPackageIncludeJson(array $packages)
     {
@@ -76,14 +69,25 @@ private function dumpPackageIncludeJson(array $packages)
         foreach ($packages as $package) {
             $repo['packages'][$package->getName()][$package->getPrettyVersion()] = $dumper->dump($package);
         }
-        $repoJson = new JsonFile($this->filenamePrefix);
+
+        // dump to temporary file
+        $tempFilename = $this->outputDir.'/$include.json';
+        $repoJson = new JsonFile($tempFilename);
         $repoJson->write($repo);
-        $hash = hash_file('sha1', $this->filenamePrefix);
-        $filenameWithHash = $this->filenamePrefix.'$'.$hash.'.json';
-        rename($this->filenamePrefix, $filenameWithHash);
-        $this->output->writeln("<info>wrote packages json $filenameWithHash</info>");
 
-        return $filenameWithHash;
+        // rename file accordingly
+        $includeFileHash = hash_file('sha1', $tempFilename);
+        $includeFileName = str_replace(
+            '{sha1}', $includeFileHash, $this->includeFileName
+        );
+        rename($tempFilename, $this->outputDir.'/'.$includeFileName);
+        $this->output->writeln("<info>Wrote packages json $includeFileName</info>");
+
+        $includes = array(
+            $includeFileName => array('sha1' => $includeFileHash),
+        );
+
+        return $includes;
     }
 
     /**
diff --git a/src/Composer/Satis/Command/BuildCommand.php b/src/Composer/Satis/Command/BuildCommand.php
index 2fc196e..eafa517 100644
--- a/src/Composer/Satis/Command/BuildCommand.php
+++ b/src/Composer/Satis/Command/BuildCommand.php
@@ -88,6 +88,8 @@ protected function configure()
 - <info>"notify-batch"</info>: Allows you to specify a URL that will
   be called every time a user installs a package, see
   https://getcomposer.org/doc/05-repositories.md#notify-batch
+- <info>"include-filename"</info> Specify filename instead of default include/all\${SHA1_HASH}.json
+
 EOT
             );
     }

From 249bffa8d4ab03c234614f4f198700426fb5c609 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= <glen@delfi.ee>
Date: Wed, 6 Jan 2016 20:35:20 +0200
Subject: [PATCH 2/3] add include-filename into satis schema

---
 res/satis-schema.json | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/res/satis-schema.json b/res/satis-schema.json
index e71aff9..7840789 100644
--- a/res/satis-schema.json
+++ b/res/satis-schema.json
@@ -105,6 +105,10 @@
             "type": "boolean",
             "description": "If true, resolve and add all Dev dependencies of each required package."
         },
+        "include-filename": {
+            "type": "string",
+            "description": "Specify filename instead of default include/all${SHA1_HASH}.json"
+        },
         "output-dir": {
             "type": "string",
             "description": "The directory where the static Repository is built."

From d4ffb4356e702d8c69bef5ca19280d75be65eb23 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= <glen@delfi.ee>
Date: Wed, 6 Jan 2016 20:43:30 +0200
Subject: [PATCH 3/3] use Filesystem rename

this ensures that parent dirs are created when renaming the file
---
 src/Composer/Satis/Builder/PackagesBuilder.php | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/src/Composer/Satis/Builder/PackagesBuilder.php b/src/Composer/Satis/Builder/PackagesBuilder.php
index cc566b2..720f8c1 100644
--- a/src/Composer/Satis/Builder/PackagesBuilder.php
+++ b/src/Composer/Satis/Builder/PackagesBuilder.php
@@ -13,6 +13,7 @@
 
 use Composer\Json\JsonFile;
 use Composer\Package\Dumper\ArrayDumper;
+use Composer\Util\Filesystem;
 use Symfony\Component\Console\Output\OutputInterface;
 
 /**
@@ -80,7 +81,9 @@ private function dumpPackageIncludeJson(array $packages)
         $includeFileName = str_replace(
             '{sha1}', $includeFileHash, $this->includeFileName
         );
-        rename($tempFilename, $this->outputDir.'/'.$includeFileName);
+        $fs = new Filesystem();
+        $fs->ensureDirectoryExists(dirname($this->outputDir.'/'.$includeFileName));
+        $fs->rename($tempFilename, $this->outputDir.'/'.$includeFileName);
         $this->output->writeln("<info>Wrote packages json $includeFileName</info>");
 
         $includes = array(
