Skip to content

Feature/lamdera format #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: lamdera-next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions elm.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ Executable lamdera
-- CLI Experimental
Lamdera.CLI.Annotate
Lamdera.CLI.Interpreter
Lamdera.CLI.Format

Lamdera.AppConfig
Lamdera.Checks
Expand Down
33 changes: 19 additions & 14 deletions ext-common/Ext/ElmFormat.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Ext.ElmFormat where
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text as Text
import System.IO (FilePath)

import System.IO.Unsafe (unsafePerformIO)
import qualified System.Process
Expand All @@ -20,31 +21,35 @@ import qualified ElmFormat.Cli
-- import qualified ElmFormat.Render.Text as Render
import ElmVersion
import ElmFormat.Messages
import Reporting.Annotation (Located(..), Region(..), Position(..))
import CommandLine.InfoFormatter (ToConsole(..))


formatWithEmbedded :: Text -> Either ElmFormat.Messages.InfoMessage Text
formatWithEmbedded inputText = do
ElmFormat.Cli.format ElmVersion.Elm_0_19 ("stdin:nofilepath", inputText)
formatWithEmbedded :: FilePath -> Text -> Either ElmFormat.Messages.InfoMessage Text
formatWithEmbedded filePath inputText = do
ElmFormat.Cli.format ElmVersion.Elm_0_19 (filePath, inputText)


format :: Text -> (Either Text Text)
format text = do
case formatWithEmbedded text of
Left err ->
Left $ Lamdera.show_ err
Right formatted ->
Right formatted
format :: FilePath -> Text -> (Either Text Text)
format filePath text = do
case formatWithEmbedded filePath text of
Left err -> Left $ toConsole err
Right formatted -> Right formatted


formatOrPassthrough :: Text -> Text
formatOrPassthrough text = do
case format text of
case format "stdin:nofilepath" text of
Right formatted -> formatted
Left err -> do
-- let !_ = Lamdera.debug $ "🔥💅 warning: " <> show err
text
Left _ -> text


formatOrPassthroughFile :: FilePath -> Text -> Text
formatOrPassthroughFile filePath text = do
case format filePath text of
Right formatted -> formatted
Left _ -> text


-- Old versions that rely on local elm-format binary

Expand Down
23 changes: 21 additions & 2 deletions extra/Lamdera/CLI.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{-# LANGUAGE OverloadedStrings #-}

module Lamdera.CLI (live, login, check, deploy, reset, update, annotate, eval) where
module Lamdera.CLI (live, login, check, deploy, reset, update, annotate, eval, format) where

import Text.Read (readMaybe)
import qualified Text.PrettyPrint.ANSI.Leijen as P
Expand Down Expand Up @@ -193,6 +193,25 @@ eval =
Terminal.Command "eval" (Common summary) details example args noFlags Lamdera.CLI.Interpreter.run


-- FORMAT
-- @LAMDERA Stub - intercepted in Terminal.hs


format :: Terminal.Command
format =
let
summary =
"Format Elm source files."

details =
"The `format` command is handled directly by elm-format for perfect compatibility."

example =
reflow "See elm-format documentation at <https://github.com/avh4/elm-format>"
in
Terminal.Command "format" (Common summary) details example noArgs noFlags (\_ _ -> return ())


-- HELPERS


Expand All @@ -203,4 +222,4 @@ stack docs =

reflow :: String -> P.Doc
reflow string =
P.fillSep $ map P.text $ words string
P.fillSep $ map P.text $ words string
9 changes: 9 additions & 0 deletions extra/Lamdera/CLI/Format.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Lamdera.CLI.Format
( run
) where

import qualified ElmFormat.Cli

-- | Delegate to elm-format
run :: [String] -> IO ()
run args = ElmFormat.Cli.mainIO args
29 changes: 17 additions & 12 deletions terminal/impl/Terminal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import qualified Terminal.Error as Error


import qualified Lamdera.Version
import qualified Lamdera.CLI.Format
import qualified Sanity

-- COMMAND
Expand Down Expand Up @@ -86,21 +87,25 @@ app intro outro commands =
Exit.exitSuccess

command : chunks ->
do case List.find (\cmd -> toName cmd == command) commands of
Nothing ->
Error.exitWithUnknown command (map toName commands)
-- @LAMDERA format intercept
if command == "format" then
do Lamdera.CLI.Format.run chunks
else
do case List.find (\cmd -> toName cmd == command) commands of
Nothing ->
Error.exitWithUnknown command (map toName commands)

Just (Command _ _ details example args_ flags_ callback) ->
if elem "--help" chunks then
Error.exitWithHelp (Just command) details example args_ flags_
Just (Command _ _ details example args_ flags_ callback) ->
if elem "--help" chunks then
Error.exitWithHelp (Just command) details example args_ flags_

else
case snd $ Chomp.chomp Nothing chunks args_ flags_ of
Right (argsValue, flagsValue) ->
callback argsValue flagsValue
else
case snd $ Chomp.chomp Nothing chunks args_ flags_ of
Right (argsValue, flagsValue) ->
callback argsValue flagsValue

Left err ->
Error.exitWithError err
Left err ->
Error.exitWithError err



Expand Down
1 change: 1 addition & 0 deletions terminal/src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ main =
, Lamdera.CLI.update
, Lamdera.CLI.annotate
, Lamdera.CLI.eval
, Lamdera.CLI.format
-- , reactor
-- , bump
-- , diff
Expand Down