From 11716a6d9c65f572e7b3965e301d95c5cb61ac8c Mon Sep 17 00:00:00 2001
From: Anton Dessiatov <anton.dessiatov@gmail.com>
Date: Tue, 22 Sep 2015 17:21:09 +0600
Subject: [PATCH] Replace full platform name and cabal version in build
 artifact path on Windows with first 8 symbols of its SHA1 hash. This is done
 to decrease chances of hitting 260 symbol path limit.

---
 src/Stack/Constants.hs | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/src/Stack/Constants.hs b/src/Stack/Constants.hs
index df277ca733..30117b71fb 100644
--- a/src/Stack/Constants.hs
+++ b/src/Stack/Constants.hs
@@ -1,3 +1,4 @@
+{-# LANGUAGE CPP #-}
 {-# LANGUAGE TemplateHaskell #-}
 {-# LANGUAGE OverloadedStrings #-}
 
@@ -47,6 +48,13 @@ import           Prelude
 import           Stack.Types.Config
 import           Stack.Types.PackageIdentifier
 import           Stack.Types.PackageName
+#ifdef mingw32_HOST_OS
+import qualified Crypto.Hash.SHA1 as SHA1
+import qualified Data.ByteString as B
+import qualified Data.ByteString.Base16 as Base16
+import qualified Data.ByteString.Char8 as B8
+import qualified Data.Text.Encoding as T
+#endif
 
 -- | Extensions for anything that can be a Haskell module.
 haskellModuleExts :: [Text]
@@ -205,11 +213,22 @@ distRelativeDir = do
         parseRelDir $
         packageIdentifierString
             (PackageIdentifier cabalPackageName cabalPkgVer)
+
+#ifdef mingw32_HOST_OS
+    -- This is an attempt to shorten path to stack build artifacts dir on Windows to
+    -- decrease our chances of hitting 260 symbol path limit.
+    -- The idea is to calculate SHA1 hash from concatenated platform and cabal strings,
+    -- encode with base 16 and take first 8 symbols of it.
+    let concatenatedText = T.pack . toFilePath $ platform </> cabal
+        sha1 = SHA1.hash $ T.encodeUtf8 concatenatedText
+    platformAndCabal <- parseRelDir . B8.unpack . B.take 8 $ Base16.encode sha1
+#else
+    let platformAndCabal = platform </> cabal
+#endif
     return $
         workDirRel </>
         $(mkRelDir "dist") </>
-        platform </>
-        cabal
+        platformAndCabal
 
 -- | Get a URL for a raw file on Github
 rawGithubUrl :: Text -- ^ user/org name