From 449a117f1139dfb2108ebc03cbceb9df80d553af Mon Sep 17 00:00:00 2001
From: Michael Peyton Jones <me@michaelpj.com>
Date: Fri, 17 Dec 2021 17:23:24 +0000
Subject: [PATCH] Automatically read in the doc version from the cabal file

It was out of date. This way it will always be read in from the file, so
we can forget about it.
---
 docs/conf.py | 33 +++++++++++++++++++++++++++++----
 flake.nix    |  4 ++--
 2 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/docs/conf.py b/docs/conf.py
index c30f7ae1c8..c029a8f21a 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -14,16 +14,41 @@
 # import sys
 # sys.path.insert(0, os.path.abspath('.'))
 
+import re
+import sys
 
 # -- Project information -----------------------------------------------------
 
 project = 'haskell-language-server'
-copyright = '2021, The Haskell IDE Team'
-author = 'The Haskell IDE Team'
 
-# The full version, including alpha/beta/rc tags
-release = '1.3.0'
+# We want to take some of the metadata from the Cabal file, especially the version.
+# (otherwise it's very easy to forget to update it!)
+release = None
+copyright = None
+author = None
+versionPattern = re.compile("^version:\s*([\d.]+)")
+copyrightPattern = re.compile("^copyright:\s*(.+)")
+authorPattern = re.compile("^author:\s*(.+)")
+for i, line in enumerate(open('../haskell-language-server.cabal')):
+    versionMatch = re.search(versionPattern, line)
+    if versionMatch:
+        release = versionMatch.group(1)
+    copyrightMatch = re.search(copyrightPattern, line)
+    if copyrightMatch:
+        copyright = copyrightMatch.group(1)
+    authorMatch = re.search(authorPattern, line)
+    if authorMatch:
+        author = authorMatch.group(1)
 
+if not release:
+    print("Couldn't find version")
+    sys.exit()
+if not copyright:
+    print("Couldn't find copyright")
+    sys.exit()
+if not author:
+    print("Couldn't find author")
+    sys.exit()
 
 # -- General configuration ---------------------------------------------------
 
diff --git a/flake.nix b/flake.nix
index 33bd0d65eb..be65c1b420 100644
--- a/flake.nix
+++ b/flake.nix
@@ -179,10 +179,10 @@
 
         docs = pkgs.stdenv.mkDerivation {
           name = "hls-docs";
-          src = pkgs.lib.sourceFilesBySuffices ./docs [ ".py" ".rst" ".md" ".png" ".gif" ".svg" ];
+          src = pkgs.lib.sourceFilesBySuffices ./. [ ".py" ".rst" ".md" ".png" ".gif" ".svg" ".cabal" ];
           buildInputs = [ pythonWithPackages ];
           # -n gives warnings on missing link targets, -W makes warnings into errors
-          buildPhase = ''sphinx-build -n -W . $out'';
+          buildPhase = ''cd docs; sphinx-build -n -W . $out'';
           dontInstall = true;
         };