on 5b5230c help sign in
elk/src/CommandLineRouter.hs
84 lines 2.7 kB history raw
1module Main where
3import qualified System.Directory as Dir
4import qualified System.Environment as Env
5import System.Exit (exitWith)
6import System.IO (hPutStrLn, stderr, stdin, stdout)
7import qualified System.Process as Proc
8import GHC.IO.Encoding (setLocaleEncoding, utf8)
10import qualified Elm.Compiler.Version as Compiler
11import qualified Elm.Package as Pkg
15main :: IO ()
16main =
17 do setLocaleEncoding utf8
19 allArgs <- Env.getArgs
20 case allArgs of
21 [] ->
22 putStrLn usageMessage
24 ["--version"] ->
25 putStrLn $ Pkg.versionToString Compiler.version
27 ["-v"] ->
28 putStrLn $ Pkg.versionToString Compiler.version
30 command : args ->
31 attemptToRun command args
34attemptToRun :: String -> [String] -> IO ()
35attemptToRun command args =
36 do found <- Dir.findExecutable ("elm-" ++ command)
37 case found of
38 Nothing ->
39 hPutStrLn stderr $
40 "Could not find command `" ++ command ++ "`. Maybe there is a typo?\n\n\
41 \Default commands include:\n\n"
42 ++ availableCommands ++
43 "\nWhen you try to run the command `" ++ command ++ "` we actually search for an\n\
44 \ executable named elm-" ++ command ++ ". Are you able to run elm-" ++ command ++ "?\n\
45 \ Is it on your PATH?"
47 Just path ->
48 let createProc =
49 (Proc.proc path args)
50 { Proc.std_in = Proc.UseHandle stdin
51 , Proc.std_out = Proc.UseHandle stdout
52 , Proc.std_err = Proc.UseHandle stderr
53 }
54 in
55 do (_, _, _, handle) <- Proc.createProcess createProc
56 exitWith =<< Proc.waitForProcess handle
59usageMessage :: String
60usageMessage =
61 "Elm Platform " ++ Pkg.versionToString Compiler.version ++ " - a way to run all Elm tools\n\
62 \\n\
63 \Usage: elm <command> [<args>]\n\
64 \\n\
65 \Available commands include:\n\n"
66 ++ availableCommands ++
67 "\nYou can learn more about a specific command by running things like:\n\
68 \\n\
69 \ elm make --help\n\
70 \ elm package --help\n\
71 \ elm <command> --help\n\
72 \\n\
73 \In all these cases we are simply running 'elm-<command>' so if you create an\n\
74 \executable named 'elm-foobar' you will be able to run it as 'elm foobar' as\n\
75 \long as it appears on your PATH."
78availableCommands :: String
79availableCommands =
80 " make Compile an Elm file or project into JS or HTML\n\
81 \ package Manage packages from <http://package.elm-lang.org>\n\
82 \ reactor Develop with compile-on-refresh and time-travel debugging\n\
83 \ repl A REPL for running individual expressions\n"