Skip to content

Commit

Permalink
Update docs-search to the latest version, simplify including it in th…
Browse files Browse the repository at this point in the history
…e build (#775)
  • Loading branch information
f-f authored Apr 19, 2021
1 parent 456be94 commit f5d1fdc
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 69 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ jobs:
shell: bash

- name: Build
run: stack build --pedantic
run: |
make
stack build --pedantic
shell: bash

- name: Install
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ jobs:
if: runner.os != 'Windows'
shell: bash
run: |
make
stack install --dependencies-only
mkdir artifacts
stack build --copy-bins --local-bin-path ./artifacts
Expand All @@ -75,6 +76,7 @@ jobs:
if: runner.os == 'Windows'
shell: bash
run: |
make
stack install --dependencies-only
stack build --copy-bins --local-bin-path ./artifacts
cp artifacts/spago.exe spago.exe
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ test/spago-test
.stack-work/
*~
**/__pycache__
templates/docs-search-app.js
templates/purescript-docs-search
templates/docs-search-app*
templates/purescript-docs-search*
.envrc
curator.log
.DS_Store
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ If you'd like to develop spago locally, the recommended tool to use is [stack][s
To compile the project from source you can do

```bash
$ make
$ stack build --fast
```

Expand Down
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
all: fetch-templates

fetch-templates:
@curl https://github.com/purescript/purescript-docs-search/releases/download/v0.0.10/docs-search-app.js -o templates/docs-search-app-0.0.10.js
@curl https://github.com/purescript/purescript-docs-search/releases/download/v0.0.10/purescript-docs-search -o templates/purescript-docs-search-0.0.10
@chmod +x templates/purescript-docs-search-0.0.10
@curl https://github.com/purescript/purescript-docs-search/releases/download/v0.0.11/docs-search-app.js -o templates/docs-search-app-0.0.11.js
@curl https://github.com/purescript/purescript-docs-search/releases/download/v0.0.11/purescript-docs-search -o templates/purescript-docs-search-0.0.11
@chmod +x templates/purescript-docs-search-0.0.11

2 changes: 2 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ cache:
- '.stack-work -> %STACK_YAML%, appveyor.yml'

install:
- choco install -y make
# http://help.appveyor.com/discussions/problems/6312-curl-command-not-found
- set PATH=C:\Program Files\Git\mingw64\bin;%PATH%
- curl --silent --show-error --output stack.zip --location "https://github.com/commercialhaskell/stack/releases/download/v%STACK_VERSION%/stack-%STACK_VERSION%-windows-x86_64.zip"
Expand All @@ -31,6 +32,7 @@ install:
- chmod a+x purescript

build_script:
- make
- stack build
- stack install --local-bin-path bin

Expand Down
8 changes: 5 additions & 3 deletions spago.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
-- hash: 77b5d785b2feb191ce5732a72494d5d950758a3c9dd77a3c98827bf19c3bafa3
-- hash: 64b3d7194bb34705b51fef4eaafd1841f1900b6826a9cda155ba8d6572b9bd46

name: spago
version: 0.20.0
Expand All @@ -21,10 +21,12 @@ extra-source-files:
README.md
CHANGELOG.md
templates/bower.json
templates/docs-search-app.js
templates/docs-search-app-0.0.10.js
templates/docs-search-app-0.0.11.js
templates/gitignore
templates/packages.dhall
templates/purescript-docs-search
templates/purescript-docs-search-0.0.10
templates/purescript-docs-search-0.0.11
templates/purs-repl
templates/spago.dhall
templates/srcMain.purs
Expand Down
15 changes: 12 additions & 3 deletions src/Spago/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,15 @@ bundleModule maybeModuleName maybeTargetPath noBuild buildOpts usePsa = do
DoBuild -> Run.withBuildEnv usePsa buildOpts $ build (Just bundleAction)
NoBuild -> Run.getEnv >>= (flip runRIO) bundleAction

docsSearchTemplate :: (HasType LogFunc env, HasType PursCmd env) => RIO env Text
docsSearchTemplate = ifM (Purs.hasMinPursVersion "0.14.0")
(pure Templates.docsSearch0011)
(pure Templates.docsSearch0010)

docsSearchAppTemplate :: (HasType LogFunc env, HasType PursCmd env) => RIO env Text
docsSearchAppTemplate = ifM (Purs.hasMinPursVersion "0.14.0")
(pure Templates.docsSearchApp0011)
(pure Templates.docsSearchApp0010)

-- | Generate docs for the `sourcePaths` and run `purescript-docs-search build-index` to patch them.
docs
Expand All @@ -424,8 +433,8 @@ docs format noSearch open = do
when isHTMLFormat $ do
when (noSearch == AddSearch) $ do
logInfo "Making the documentation searchable..."
writeTextFile ".spago/purescript-docs-search" Templates.docsSearch
writeTextFile ".spago/docs-search-app.js" Templates.docsSearchApp
writeTextFile ".spago/purescript-docs-search" =<< docsSearchTemplate
writeTextFile ".spago/docs-search-app.js" =<< docsSearchAppTemplate
let cmd = "node .spago/purescript-docs-search build-index --package-name " <> surroundQuote name
logDebug $ "Running `" <> display cmd <> "`"
shell cmd empty >>= \case
Expand Down Expand Up @@ -463,7 +472,7 @@ search = do
, PursArg "docs"
]

writeTextFile ".spago/purescript-docs-search" Templates.docsSearch
writeTextFile ".spago/purescript-docs-search" =<< docsSearchTemplate
let cmd = "node .spago/purescript-docs-search search --package-name " <> surroundQuote name
logDebug $ "Running `" <> display cmd <> "`"
viewShell $ callCommand $ Text.unpack cmd
10 changes: 4 additions & 6 deletions src/Spago/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Spago.Prelude
, headMay
, lastMay
, empty
, ifM

-- * Logging, errors, printing, etc
, Pretty
Expand Down Expand Up @@ -70,7 +71,6 @@ module Spago.Prelude
, repr
, with
, appendonly
, docsSearchVersion
, githubTokenEnvVar
) where

Expand Down Expand Up @@ -142,6 +142,9 @@ die reasons = do
traverse_ logError reasons
exitFailure

ifM :: Monad m => m Bool -> m a -> m a -> m a
ifM p x y = p >>= \b -> if b then x else y

-- | Suppress the 'Left' value of an 'Either'
hush :: Either a b -> Maybe b
hush = either (const Nothing) Just
Expand Down Expand Up @@ -218,11 +221,6 @@ assertDirectory directory = do
Directory.setPermissions directory private


-- | Release tag for the `purescript-docs-search` app.
docsSearchVersion :: Text
docsSearchVersion = "v0.0.10"


githubTokenEnvVar :: IsString t => t
githubTokenEnvVar = "SPAGO_GITHUB_TOKEN"

Expand Down
8 changes: 8 additions & 0 deletions src/Spago/Purs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Spago.Purs
, bundle
, docs
, pursVersion
, hasMinPursVersion
, parseDocsFormat
, findFlag
, DocsFormat(..)
Expand Down Expand Up @@ -144,6 +145,13 @@ pursVersion = Turtle.Bytes.shellStrictWithErr (purs <> " --version") empty >>= \
where
purs = "purs"

hasMinPursVersion :: (HasLogFunc env, HasPurs env) => Text -> RIO env Bool
hasMinPursVersion maybeMinVersion = do
PursCmd { compilerVersion } <- view (the @PursCmd)
minVersion <- case Version.semver maybeMinVersion of
Left _ -> die [ "Unable to parse min version: " <> displayShow maybeMinVersion ]
Right minVersion -> pure minVersion
pure $ compilerVersion >= minVersion

runWithOutput :: HasLogFunc env => Text -> Text -> Text -> RIO env ()
runWithOutput command success failure = do
Expand Down
8 changes: 2 additions & 6 deletions src/Spago/RunEnv.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Spago.Env
import System.Console.ANSI (hSupportsANSIWithoutEmulation)
import qualified System.Environment as Env
import qualified Distribution.System as OS
import qualified Data.Versions as Version
import qualified RIO
import qualified Turtle

Expand Down Expand Up @@ -208,11 +207,8 @@ getMaybeGraph :: HasPursEnv env => BuildOptions -> Config -> [(PackageName, Pack
getMaybeGraph BuildOptions{ depsOnly, sourcePaths } Config{ configSourcePaths } deps = do
let partitionedGlobs@(Packages.Globs{..}) = Packages.getGlobs deps depsOnly configSourcePaths
globs = Packages.getGlobsSourcePaths partitionedGlobs <> sourcePaths
PursCmd { compilerVersion } <- view (the @PursCmd)
minVersion <- case Version.semver "0.14.0" of
Left _ -> die [ "Unable to parse min version for imports check" ]
Right minVersion -> pure minVersion
if compilerVersion < minVersion
supportsGraph <- Purs.hasMinPursVersion "0.14.0"
if not supportsGraph
then pure Nothing
else do
maybeGraph <- Purs.graph globs
Expand Down
29 changes: 1 addition & 28 deletions src/Spago/TH.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@

module Spago.TH
( embedFileUtf8
, embedURLWithFallback
) where

import Prelude

import Control.Exception (try, SomeException)
import Data.ByteString (ByteString)
import Data.FileEmbed
import Data.Text (unpack)
import Data.Text.Encoding as LT
import Language.Haskell.TH.Syntax (Exp(..), Q, runIO, Lit(StringL))
import Network.HTTP.Client.Conduit (parseUrlThrow)
import Network.HTTP.Simple (httpBS, getResponseBody)
import qualified Data.ByteString as BS
import Language.Haskell.TH.Syntax (Exp(..), Q)

-- | This is here so that we can embed files as Utf8 Text.
-- The reason for that is that since we have unicode Dhall files,
Expand All @@ -28,23 +21,3 @@ import qualified Data.ByteString as BS
embedFileUtf8 :: FilePath -> Q Exp
embedFileUtf8 filePath =
[| LT.decodeUtf8 $(makeRelativeToProject filePath >>= embedFile) |]

-- | Embed a file from a URL or use a local file as a fallback.
-- If fetching the URL was successfull, write the contents to the fallback file.
embedURLWithFallback
:: String
-- ^ A URL.
-> FilePath
-- ^ A fallback file.
-> Q Exp
embedURLWithFallback url filePath = do

eiResponseBody <- runIO $ try $ do
req <- parseUrlThrow url
getResponseBody <$> httpBS req

case eiResponseBody :: Either SomeException ByteString of
Left _ -> embedFileUtf8 filePath
Right responseBody -> do
runIO $ BS.writeFile filePath responseBody
return . LitE . StringL . unpack $ LT.decodeUtf8 responseBody
32 changes: 12 additions & 20 deletions src/Spago/Templates.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ module Spago.Templates where

import qualified Data.Text as T

import Spago.TH (embedFileUtf8, embedURLWithFallback)
import Spago.Prelude

import Spago.TH (embedFileUtf8)

packagesDhall :: T.Text
packagesDhall = $(embedFileUtf8 "templates/packages.dhall")
Expand All @@ -28,20 +26,14 @@ bowerJson = $(embedFileUtf8 "templates/bower.json")
pursRepl :: T.Text
pursRepl = $(embedFileUtf8 "templates/purs-repl")

docsSearchApp :: T.Text
docsSearchApp =
$(embedURLWithFallback
( "https://github.com/spacchetti/purescript-docs-search/releases/download/"
<> T.unpack docsSearchVersion
<> "/docs-search-app.js"
)
"templates/docs-search-app.js")

docsSearch :: T.Text
docsSearch =
$(embedURLWithFallback
( "https://github.com/spacchetti/purescript-docs-search/releases/download/"
<> T.unpack docsSearchVersion
<> "/purescript-docs-search"
)
"templates/purescript-docs-search")
docsSearchApp0010 :: T.Text
docsSearchApp0010 = $(embedFileUtf8 "templates/docs-search-app-0.0.10.js")

docsSearch0010 :: T.Text
docsSearch0010 = $(embedFileUtf8 "templates/purescript-docs-search-0.0.10")

docsSearchApp0011 :: T.Text
docsSearchApp0011 = $(embedFileUtf8 "templates/docs-search-app-0.0.11.js")

docsSearch0011 :: T.Text
docsSearch0011 = $(embedFileUtf8 "templates/purescript-docs-search-0.0.11")

0 comments on commit f5d1fdc

Please sign in to comment.