Codebase server default ui path is relative to the executable now

To test, I added a script that downloads the latest UI release and puts it next to the executable.
This commit is contained in:
Paul Chiusano 2021-05-28 14:45:10 -05:00
parent 43a22072dd
commit 26f8dd37f2
3 changed files with 30 additions and 14 deletions

10
dev-ui-install.sh Executable file
View File

@ -0,0 +1,10 @@
echo "This script downloads the latest codebase UI release"
echo "and puts it in the correct spot next to the unison"
echo "executable built by stack."
echo ""
stack build
curl -L https://github.com/unisonweb/codebase-ui/releases/download/latest/ucm.zip --output ucm.zip
parent_dir="$(dirname -- $(stack exec which unison))"
mkdir -p "$parent_dir/ui"
unzip -o ucm.zip -d "$parent_dir/ui"

View File

@ -8,9 +8,13 @@ _Disclaimer_ If you have trouble getting started, please get in touch via [Slack
To get cracking with Unison: To get cracking with Unison:
* [Install `stack`](https://docs.haskellstack.org/en/stable/README/#how-to-install). 1. [Install `stack`](https://docs.haskellstack.org/en/stable/README/#how-to-install).
* Build the project with `stack build`. This builds all executables. 2. Build the project with `stack build`. This builds all executables.
* After building, `stack exec unison` will fire up the codebase editor, create a codebase in the current directory, and watch for `.u` file changes. If you want to run it in a different directory, just add `unison` to your `PATH`, after finding it with `find .stack-work -name unison -type f`. (For me, this finds two, they both work, but have different contents. ¯\\\_(ツ)\_/¯ ) 3. (Optional) Run `./dev-ui-install.hs` to fetch the latest release of the codebase UI. If you don't care about running the codebase UI locally you can ignore this step.
4. After building do `stack exec unison -- init` will initialize a codebase in your home directory (in `~/.unison`). This only needs to be done once.
5. `stack exec unison` starts Unison and watches for `.u` file changes in the current directory. If you want to run it in a different directory, just add `unison` to your `PATH`, after finding it with `stack exec which unison`.
On startup, Unison prints a url for the codebase UI. If you did step 3 above, then visiting that URL in a browser will give you a nice interface to your codebase.
## Running Tests ## Running Tests

View File

@ -87,9 +87,10 @@ import Servant.Server
err404, err404,
) )
import Servant.Server.StaticFiles (serveDirectoryWebApp) import Servant.Server.StaticFiles (serveDirectoryWebApp)
import System.Directory (doesFileExist) import System.Directory (doesFileExist, canonicalizePath)
import System.Environment (getArgs, lookupEnv) import System.Environment (getArgs, lookupEnv, getExecutablePath)
import System.FilePath.Posix ((</>)) import System.FilePath ((</>))
import qualified System.FilePath as FilePath
import System.Random.Stateful (getStdGen, newAtomicGenM, uniformByteStringM) import System.Random.Stateful (getStdGen, newAtomicGenM, uniformByteStringM)
import Text.Read (readMaybe) import Text.Read (readMaybe)
import Unison.Codebase (Codebase) import Unison.Codebase (Codebase)
@ -165,7 +166,7 @@ serverAPI = Proxy
app app
:: Var v :: Var v
=> Codebase IO v Ann => Codebase IO v Ann
-> Maybe FilePath -> FilePath
-> Strict.ByteString -> Strict.ByteString
-> Application -> Application
app codebase uiPath expectedToken = app codebase uiPath expectedToken =
@ -263,7 +264,10 @@ startServer
-> Maybe Port -> Maybe Port
-> Maybe String -> Maybe String
-> IO () -> IO ()
startServer codebase k envToken envHost envPort envUI = do startServer codebase k envToken envHost envPort envUI0 = do
-- the `canonicalizePath` resolves symlinks
exePath <- canonicalizePath =<< getExecutablePath
envUI <- canonicalizePath $ fromMaybe (FilePath.takeDirectory exePath </> "ui") envUI0
token <- case envToken of token <- case envToken of
Just t -> return $ C8.pack t Just t -> return $ C8.pack t
_ -> genToken _ -> genToken
@ -301,19 +305,17 @@ serveIndex path = do
<> " environment variable to the directory where the UI is installed." <> " environment variable to the directory where the UI is installed."
} }
serveUI :: Handler () -> Maybe FilePath -> Server WebUI serveUI :: Handler () -> FilePath -> Server WebUI
serveUI tryAuth p _ = serveUI tryAuth path _ = tryAuth *> serveIndex path
let path = fromMaybe "ui" p
in tryAuth *> serveIndex path
server server
:: Var v :: Var v
=> Codebase IO v Ann => Codebase IO v Ann
-> Maybe FilePath -> FilePath
-> Strict.ByteString -> Strict.ByteString
-> Server AuthedServerAPI -> Server AuthedServerAPI
server codebase uiPath token = server codebase uiPath token =
serveDirectoryWebApp (fromMaybe "ui" uiPath </> "static") serveDirectoryWebApp (uiPath </> "static")
:<|> ((\t -> :<|> ((\t ->
serveUI (tryAuth t) uiPath serveUI (tryAuth t) uiPath
:<|> ( ( (serveNamespace (tryAuth t) codebase) :<|> ( ( (serveNamespace (tryAuth t) codebase)