From 9fdf950c0e1c3bb76ea2dc7237fd17fd7970f89d Mon Sep 17 00:00:00 2001
From: Paul Walker <paul@pwjw.com>
Date: Fri, 12 Nov 2021 22:35:40 -0500
Subject: [PATCH] Rebuild PatchDB from headless if we want

Useful for reproducing #4825
---
 src/surge-testrunner/HeadlessNonTestFunctions.cpp | 12 ++++++++++++
 src/surge-testrunner/HeadlessNonTestFunctions.h   |  1 +
 src/surge-testrunner/main.cpp                     |  4 ++++
 3 files changed, 17 insertions(+)

diff --git a/src/surge-testrunner/HeadlessNonTestFunctions.cpp b/src/surge-testrunner/HeadlessNonTestFunctions.cpp
index 9e3cc102b6b..ec9b5708fc1 100644
--- a/src/surge-testrunner/HeadlessNonTestFunctions.cpp
+++ b/src/surge-testrunner/HeadlessNonTestFunctions.cpp
@@ -13,6 +13,18 @@ namespace Headless
 namespace NonTest
 {
 
+void initializePatchDB()
+{
+    using namespace std::chrono_literals;
+    auto surge = createSurge(44100);
+    surge->storage.initializePatchDb();
+    while (surge->storage.patchDB->numberOfJobsOutstanding() > 0)
+    {
+        std::cout << surge->storage.patchDB->numberOfJobsOutstanding() << std::endl;
+        std::this_thread::sleep_for(100ms);
+    }
+}
+
 void restreamTemplatesWithModifications()
 {
     auto templatesDir = string_to_path("resources/data/patches_factory/Templates");
diff --git a/src/surge-testrunner/HeadlessNonTestFunctions.h b/src/surge-testrunner/HeadlessNonTestFunctions.h
index da703d4d164..7fa6d1bced8 100644
--- a/src/surge-testrunner/HeadlessNonTestFunctions.h
+++ b/src/surge-testrunner/HeadlessNonTestFunctions.h
@@ -7,6 +7,7 @@ namespace Headless
 {
 namespace NonTest
 {
+void initializePatchDB();
 void restreamTemplatesWithModifications();
 void statsFromPlayingEveryPatch();
 void filterAnalyzer(int ft, int fst, std::ostream &os);
diff --git a/src/surge-testrunner/main.cpp b/src/surge-testrunner/main.cpp
index 01641bce4ca..5b160c61c86 100644
--- a/src/surge-testrunner/main.cpp
+++ b/src/surge-testrunner/main.cpp
@@ -21,6 +21,10 @@ int main(int argc, char **argv)
     if (argc > 2 && strcmp(argv[1], "--non-test") == 0)
     {
         std::cout << "# Running in non-test mode : " << argv[2] << std::endl;
+        if (strcmp(argv[2], "--initialize-pdb") == 0)
+        {
+            Surge::Headless::NonTest::initializePatchDB();
+        }
         if (strcmp(argv[2], "--stats-from-every-patch") == 0)
         {
             Surge::Headless::NonTest::statsFromPlayingEveryPatch();