From a867e1fc40f11ac9a1149ec716f0dc3047fc40fc Mon Sep 17 00:00:00 2001
From: chylex <contact@chylex.com>
Date: Tue, 8 May 2018 20:20:02 +0200
Subject: [PATCH] Optimize speed and memory usage of build process for app &
 installers

---
 README.md               |  2 +-
 Resources/PostBuild.ps1 | 44 +++++++++++++++++++++++------------------
 bld/gen_full.iss        |  1 +
 bld/gen_port.iss        |  1 +
 bld/gen_upd.iss         |  1 +
 5 files changed, 29 insertions(+), 20 deletions(-)

diff --git a/README.md b/README.md
index 78ba6cb6..c4ba17b4 100644
--- a/README.md
+++ b/README.md
@@ -65,4 +65,4 @@ After the window closes, three installers will be generated inside the `bld/Outp
 
 > There is a small chance running `RUN BUILD.bat` immediately shows a resource error. If that happens, close the console window (which terminates all Inno Setup processes and leaves corrupted installers in the output folder), and run it again.
 
-> Running `RUN BUILD.bat` uses about 784 MB of RAM due to very high compression. You can lower this to about 232 MB by opening `gen_full.iss` and `gen_port.iss`, and changing `Compression=lzma2/ultra` to `Compression=lzma2/max`.
+> Running `RUN BUILD.bat` uses about 400 MB of RAM due to high compression. You can lower this to about 140 MB by opening `gen_full.iss` and `gen_port.iss`, and changing `LZMADictionarySize=15360` to `LZMADictionarySize=4096`.
diff --git a/Resources/PostBuild.ps1 b/Resources/PostBuild.ps1
index 7ccf7a43..06a28a0f 100644
--- a/Resources/PostBuild.ps1
+++ b/Resources/PostBuild.ps1
@@ -44,42 +44,48 @@ try{
   
   # Helper functions
   
+  function Remove-Empty-Lines{
+    Param([Parameter(Mandatory = $True, Position = 1)] $lines)
+    
+    ForEach($line in $lines){
+      if ($line -ne ''){
+        $line
+      }
+    }
+  }
+  
   function Check-Carriage-Return{
-    Param(
-      [Parameter(Mandatory = $True, Position = 1)] $fname
-    )
+    Param([Parameter(Mandatory = $True, Position = 1)] $file)
     
-    $file = @(Get-ChildItem -Path $targetDir -Include $fname -Recurse)[0]
-    
-    if ((Get-Content -Path $file.FullName -Raw).Contains("`r")){
-      Throw "$fname cannot contain carriage return"
+    if (!(Test-Path $file)){
+      Throw "$file does not exist"
     }
     
-    Write-Host "Verified" $file.FullName.Substring($targetDir.Length)
+    if ((Get-Content -Path $file -Raw).Contains("`r")){
+      Throw "$file cannot contain carriage return"
+    }
+    
+    Write-Host "Verified" $file.Substring($targetDir.Length)
   }
   
   function Rewrite-File{
-    [CmdletBinding()]
-    Param(
-      [Parameter(Mandatory = $True, Position = 1)] $file,
-      [Parameter(Mandatory = $True, Position = 2)] $lines
-    )
+    Param([Parameter(Mandatory = $True, Position = 1)] $file,
+          [Parameter(Mandatory = $True, Position = 2)] $lines)
     
+    $lines = Remove-Empty-Lines($lines)
     $relativePath = $file.FullName.Substring($targetDir.Length)
     
     if ($relativePath.StartsWith("scripts\")){
       $lines = (,("#" + $version) + $lines)
     }
     
-    $lines = $lines | Where { $_ -ne '' }
-    
     [IO.File]::WriteAllLines($file.FullName, $lines)
     Write-Host "Processed" $relativePath
   }
   
   # Post processing
   
-  Check-Carriage-Return("emoji-ordering.txt")
+  Check-Carriage-Return(Join-Path $targetDir "plugins\official\emoji-keyboard\emoji-ordering.txt")
   
   ForEach($file in Get-ChildItem -Path $targetDir -Filter "*.js" -Exclude "configuration.default.js" -Recurse){
     $lines = [IO.File]::ReadLines($file.FullName)
@@ -92,9 +98,9 @@ try{
   ForEach($file in Get-ChildItem -Path $targetDir -Filter "*.css" -Recurse){
     $lines = [IO.File]::ReadLines($file.FullName)
     $lines = $lines -Replace '\s*/\*.*?\*/', ''
-    $lines = $lines -Replace '^\s+(.+):\s?(.+?)(?:\s?(!important))?;$', '$1:$2$3;'
-    $lines = $lines -Replace '^(\S.*?) {$', '$1{'
-    $lines = @(($lines | Where { $_ -ne '' }) -Join ' ')
+    $lines = $lines -Replace '^(\S.*) {$', '$1{'
+    $lines = $lines -Replace '^\s+(.+?):\s*(.+?)(?:\s*(!important))?;$', '$1:$2$3;'
+    $lines = @((Remove-Empty-Lines($lines)) -Join ' ')
     Rewrite-File $file $lines
   }
   
diff --git a/bld/gen_full.iss b/bld/gen_full.iss
index a66f466e..9c61f4a6 100644
--- a/bld/gen_full.iss
+++ b/bld/gen_full.iss
@@ -30,6 +30,7 @@ Uninstallable=TDIsUninstallable
 UninstallDisplayName={#MyAppName}
 UninstallDisplayIcon={app}\{#MyAppExeName}
 Compression=lzma2/ultra
+LZMADictionarySize=15360
 SolidCompression=yes
 InternalCompressLevel=normal
 MinVersion=0,6.1
diff --git a/bld/gen_port.iss b/bld/gen_port.iss
index d1693984..06b1fafa 100644
--- a/bld/gen_port.iss
+++ b/bld/gen_port.iss
@@ -30,6 +30,7 @@ Uninstallable=no
 UsePreviousAppDir=no
 PrivilegesRequired=lowest
 Compression=lzma2/ultra
+LZMADictionarySize=15360
 SolidCompression=yes
 InternalCompressLevel=normal
 MinVersion=0,6.1
diff --git a/bld/gen_upd.iss b/bld/gen_upd.iss
index 740c650f..970e901a 100644
--- a/bld/gen_upd.iss
+++ b/bld/gen_upd.iss
@@ -32,6 +32,7 @@ UninstallDisplayName={#MyAppName}
 UninstallDisplayIcon={app}\{#MyAppExeName}
 PrivilegesRequired=lowest
 Compression=lzma/normal
+LZMADictionarySize=512
 SolidCompression=True
 InternalCompressLevel=normal
 MinVersion=0,6.1