Skip to content

Commit

Permalink
Merge pull request #194 from wclr/tidy-format
Browse files Browse the repository at this point in the history
Formatting sources with purs-tidy
  • Loading branch information
nwolverson authored May 22, 2023
2 parents babd5f7 + 2a0e5c6 commit fddcc2c
Show file tree
Hide file tree
Showing 60 changed files with 2,570 additions and 1,382 deletions.
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
d5aeaaef2a2abac8ab8bd529cc71e6cb81202738 # tidy formatting Feb 28, 2023
2e584a0baeb2837b856a35245e7a34687fc7d165 # pose formatting Oct 16, 2021
10 changes: 10 additions & 0 deletions .tidyrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"importSort": "ide",
"importWrap": "source",
"indent": 2,
"operatorsFile": null,
"ribbon": 0.9,
"typeArrowPlacement": "last",
"unicode": "never",
"width": 100
}
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"purescript.formatter": "pose"
"purescript.formatter": "purs-tidy",
"purescript.addNpmPath": true,
"[purescript]": {
"editor.defaultFormatter": "nwolverson.ide-purescript"
}
}
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"build:bundle-deps": "npm run build:bundle -- --outfile=bundle.js ",
"watch:bundle": "npm run build:bundle-nodeps -- --watch",
"watch:tsc": "npm run build:tsc -- --watch",
"format:js": "prettier --write ./{src,test}/**/*.{ts,js} && npm run build:tsc"
"format:js": "prettier --write ./{src,test}/**/*.{ts,js} && npm run build:tsc",
"format:purs": "purs-tidy format-in-place src/**/*.purs"
},
"files": [
"server.js"
Expand All @@ -46,11 +47,11 @@
"which": "^2.0.2"
},
"devDependencies": {
"@rowtype-yoga/prettier-plugin-purescript": "^1.11.2",
"@types/node": "^16.9.6",
"esbuild": "^0.13.15",
"npm-run-all": "^4.1.5",
"prettier": "^2.4.1",
"purs-tidy": "^0.9.2",
"typescript": "^4.8.0-dev.20220601"
}
}
1 change: 1 addition & 0 deletions spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ You can edit this file as you like.
, "contravariant"
, "control"
, "datetime"
, "debug"
, "effect"
, "either"
, "enums"
Expand Down
111 changes: 71 additions & 40 deletions src/IdePurescript/Build.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Control.Monad.Error.Class (throwError)
import Data.Array (intercalate, uncons, (:))
import Data.Array as Array
import Data.Bifunctor (bimap)
import Data.Either (either, Either(..))
import Data.Either (Either(..), either)
import Data.Maybe (Maybe(..), fromMaybe, maybe)
import Data.String (Pattern(Pattern), indexOf, joinWith, split)
import Data.String as String
Expand All @@ -32,19 +32,18 @@ import PscIde as P
import PscIde.Command (CodegenTarget, RebuildResult(..))
import PscIde.Server (Executable(Executable))

type BuildOptions
= { command :: Command
, directory :: String
, useNpmDir :: Boolean
}
type BuildOptions =
{ command :: Command
, directory :: String
, useNpmDir :: Boolean
}

data Command
= Command String (Array String)
data Command = Command String (Array String)

type BuildResult
= { errors :: PscResult
, success :: Boolean
}
type BuildResult =
{ errors :: PscResult
, success :: Boolean
}

-- check if retrieved (copied) env object has "PATH" property, then use it,
-- otherwise use "Path" (for windows)
Expand All @@ -58,15 +57,24 @@ spawn { command: Command cmd args, directory, useNpmDir } = do
if useNpmDir then do
pathVar <- getPathVar useNpmDir directory
env <- getEnv
pure { env: Just $ Object.insert (getPathProp env) (either identity identity pathVar) env, path: either (const Nothing) Just pathVar }
pure
{ env: Just $ Object.insert (getPathProp env)
(either identity identity pathVar)
env
, path: either (const Nothing) Just pathVar
}
else
pure { env: Nothing, path: Nothing }

cmd' <- (fromMaybe cmd <<< Array.head) <$> whichSync { path, pathExt: Nothing } cmd
CP.spawn cmd' args (CP.defaultSpawnOptions { cwd = Just directory, env = env })
cmd' <- (fromMaybe cmd <<< Array.head) <$> whichSync
{ path, pathExt: Nothing }
cmd
CP.spawn cmd' args
(CP.defaultSpawnOptions { cwd = Just directory, env = env })

-- Spawn with npm path, "which" call (windows support) and version info gathering
spawnWithVersion :: BuildOptions -> Aff { cmdBins :: Array Executable, cp :: Maybe ChildProcess }
spawnWithVersion ::
BuildOptions -> Aff { cmdBins :: Array Executable, cp :: Maybe ChildProcess }
spawnWithVersion { command: Command cmd args, directory, useNpmDir } = do
pathVar <- liftEffect $ getPathVar useNpmDir directory
cmdBins <- findBins pathVar cmd
Expand All @@ -75,8 +83,14 @@ spawnWithVersion { command: Command cmd args, directory, useNpmDir } = do
$ case uncons cmdBins of
Just { head: Executable cmdBin _ } -> do
env <- liftEffect getEnv
let childEnv = Object.insert (getPathProp env) (either identity identity pathVar) env
Just <$> CP.spawn cmdBin args (CP.defaultSpawnOptions { cwd = Just directory, env = Just childEnv })
let
childEnv = Object.insert (getPathProp env)
(either identity identity pathVar)
env
Just <$> CP.spawn cmdBin args
( CP.defaultSpawnOptions
{ cwd = Just directory, env = Just childEnv }
)
_ -> pure Nothing
pure { cmdBins, cp }

Expand All @@ -97,7 +111,8 @@ build logCb buildOptions@{ command: Command cmd args } = do
case cp' of
Nothing -> succ $ Left $ "Didn't find command in PATH: " <> cmd
Just cp -> do
logCb Info $ "Running build command: " <> intercalate " " (cmd : args)
logCb Info $ "Running build command: " <> intercalate " "
(cmd : args)
CP.onError cp (cb <<< Left <<< CP.toStandardError)
errOutput <- Ref.new []
outOutput <- Ref.new []
Expand All @@ -110,29 +125,42 @@ build logCb buildOptions@{ command: Command cmd args } = do
( \exit -> case exit of
CP.Normally n
| n == 0 || n == 1 -> do
pursError <- Ref.read errOutput >>= Buffer.concat >>= Buffer.toString Encoding.UTF8
pursOutput <- Ref.read outOutput >>= Buffer.concat >>= Buffer.toString Encoding.UTF8
let
lines = split (Pattern "\n") $ pursError <> pursOutput
{ yes: json, no: toLog } = Array.partition (\s -> indexOf (Pattern "{\"") s == Just 0) lines
logCb Info $ joinWith "\n" toLog
case parsePscOutput <$> json of
[ Left e ] -> succ $ Left $ "Couldn't parse build output: " <> e
[ Right r ] -> succ $ Right { errors: r, success: n == 0 }
[] ->
succ
$ Left
$ "Problem running build: "
<> if String.length pursError > 0 then
String.take 500 pursError
else
"didn't find JSON output"
_ -> succ $ Left "Found multiple lines of JSON output, don't know what to do"
pursError <- Ref.read errOutput >>= Buffer.concat >>=
Buffer.toString Encoding.UTF8
pursOutput <- Ref.read outOutput >>= Buffer.concat >>=
Buffer.toString Encoding.UTF8
let
lines = split (Pattern "\n") $ pursError <> pursOutput
{ yes: json, no: toLog } = Array.partition
(\s -> indexOf (Pattern "{\"") s == Just 0)
lines
logCb Info $ joinWith "\n" toLog
case parsePscOutput <$> json of
[ Left e ] -> succ $ Left $
"Couldn't parse build output: " <> e
[ Right r ] -> succ $ Right
{ errors: r, success: n == 0 }
[] ->
succ
$ Left
$ "Problem running build: "
<>
if String.length pursError > 0 then
String.take 500 pursError
else
"didn't find JSON output"
_ -> succ $ Left
"Found multiple lines of JSON output, don't know what to do"
_ -> succ $ Left "Build process exited abnormally"
)
pure mempty

rebuild :: Int -> String -> Maybe String -> Maybe (Array CodegenTarget) -> Aff BuildResult
rebuild ::
Int ->
String ->
Maybe String ->
Maybe (Array CodegenTarget) ->
Aff BuildResult
rebuild port file actualFile targets = do
res <- P.rebuild port file actualFile targets
either
Expand All @@ -143,8 +171,11 @@ rebuild port file actualFile targets = do

onResult :: Either RebuildResult RebuildResult -> BuildResult
onResult =
either (\errors -> { errors: PscResult { errors, warnings: [] }, success: true })
(\warnings -> { errors: PscResult { errors: [], warnings }, success: true })
either
(\errors -> { errors: PscResult { errors, warnings: [] }, success: true })
( \warnings ->
{ errors: PscResult { errors: [], warnings }, success: true }
)
<<< bimap unwrap unwrap
where
unwrap (RebuildResult r) = r
Loading

0 comments on commit fddcc2c

Please sign in to comment.