on 6a96e21 help sign in
193 lines 6.9 kB history raw
1{-# LANGUAGE OverloadedStrings #-}
2module Reporting.Exit
3 ( Exit(..)
4 , toString
5 , toStderr
6 , toJson
7 )
8 where
11import qualified Data.ByteString.Builder as B
12import Data.Monoid ((<>))
13import System.IO (stderr)
14import qualified Text.PrettyPrint.ANSI.Leijen as P
16import qualified Elm.Compiler as Compiler
17import qualified Elm.Compiler.Module as Module
18import qualified Elm.Package as Pkg
19import qualified Elm.Utils as Utils
20import qualified Json.Encode as Encode
21import qualified Reporting.Exit.Assets as Asset
22import qualified Reporting.Exit.Bump as Bump
23import qualified Reporting.Exit.Compile as Compile
24import qualified Reporting.Exit.Crawl as Crawl
25import qualified Reporting.Exit.Deps as Deps
26import qualified Reporting.Exit.Diff as Diff
27import qualified Reporting.Exit.Help as Help
28import qualified Reporting.Exit.Http as Http
29import qualified Reporting.Exit.Publish as Publish
33-- ALL POSSIBLE ERRORS
36data Exit
37 = NoElmJson
38 | Assets Asset.Exit
39 | Bump Bump.Exit
40 | Compile Compile.Exit [Compile.Exit]
41 | Crawl Crawl.Exit
42 | Cycle [Module.Raw] -- TODO write docs to help with this scenario
43 | Deps Deps.Exit
44 | Diff Diff.Exit
45 | Publish Publish.Exit
46 | BadHttp String Http.Exit
48 -- misc
49 | NoSolution [Pkg.Name]
50 | CannotMakeNothing
51 | CannotOptimizeDebug Module.Raw [Module.Raw]
55-- RENDERERS
58toString :: Exit -> String
59toString exit =
60 Help.toString (Help.reportToDoc (toReport exit))
63toStderr :: Exit -> IO ()
64toStderr exit =
65 Help.toStderr (Help.reportToDoc (toReport exit))
68toJson :: Exit -> IO ()
69toJson exit =
70 B.hPutBuilder stderr $ Encode.encodeUgly $ Help.reportToJson (toReport exit)
73toReport :: Exit -> Help.Report
74toReport exit =
75 case exit of
76 NoElmJson ->
77 Help.report "WELCOME" Nothing
78 "It looks like you are trying to start a new Elm project. Very exciting! :D"
79 [ P.fillSep
80 ["I","very","highly","recommend","working","through"
81 ,P.green "<https://guide.elm-lang.org>","which","will","teach","you","the"
82 ,"basics","of","Elm,","including","how","to","start","new","projects."
83 ]
84 , P.fillSep
85 ["For","folks","who","have","already","built","stuff","with","Elm,","the"
86 ,"problem","is","just","that","there","is","no",P.dullyellow "elm.json","yet."
87 ,"If","you","want","to","work","from","an","example,","check","out","the"
88 ,"one","at","<https://github.com/evancz/elm-todomvc/blob/master/elm.json>"
89 ]
90 , Help.reflow
91 "Whatever your scenario, I hope you have a lovely time using Elm!"
92 ]
94 Assets assetError ->
95 Asset.toReport assetError
97 Bump bumpError ->
98 Bump.toReport bumpError
100 Compile e es ->
101 Help.compilerReport e es
103 Crawl crawlError ->
104 Crawl.toReport crawlError
106 Cycle names ->
107 Help.report "IMPORT CYCLE" Nothing
108 "Your module imports form a cycle:"
109 [ P.indent 4 (Utils.drawCycle names)
110 , Help.reflow $
111 "Learn more about why this is disallowed and how to break cycles here:"
112 ++ Help.hintLink "import-cycles"
113 ]
115 Deps depsError ->
116 Deps.toReport depsError
118 Diff commandsError ->
119 Diff.toReport commandsError
121 Publish publishError ->
122 Publish.toReport publishError
124 BadHttp url httpError ->
125 Http.toReport url httpError
127 NoSolution badPackages ->
128 case badPackages of
129 [] ->
130 Help.report "UNSOLVABLE DEPENDENCIES" (Just "elm.json")
131 "This usually happens if you try to modify dependency constraints by\
132 \ hand. I recommend deleting any dependency you added recently (or all\
133 \ of them if things are bad) and then adding them again with:"
134 [ P.indent 4 $ P.green "elm install"
135 , Help.reflow $
136 "And do not be afaid to ask for help on Slack if you get stuck!"
137 ]
139 _:_ ->
140 Help.report "OLD DEPENDENCIES" (Just "elm.json")
141 ( "The following packages do not work with Elm " ++ Pkg.versionToString Compiler.version ++ " right now:"
142 )
143 [ P.indent 4 $ P.vcat $ map (P.red . P.text . Pkg.toString) badPackages
144 , Help.reflow $
145 "This may be because it is not upgraded yet. It may be because a\
146 \ better solution came along, so there was no need to upgrade it.\
147 \ Etc. Try asking around on Slack to learn more about the topic."
148 , Help.note
149 "Whatever the case, please be kind to the relevant package authors! Having\
150 \ friendly interactions with users is great motivation, and conversely, getting\
151 \ berated by strangers on the internet sucks your soul dry. Furthermore, package\
152 \ authors are humans with families, friends, jobs, vacations, responsibilities,\
153 \ goals, etc. They face obstacles outside of their technical work you will never\
154 \ know about, so please assume the best and try to be patient and supportive!"
155 ]
157 CannotMakeNothing ->
158 Help.report "NO INPUT" Nothing
159 "What should I make though? I need more information, like:"
160 [ P.vcat
161 [ P.indent 4 $ P.green "elm make MyThing.elm"
162 , P.indent 4 $ P.green "elm make This.elm That.elm"
163 ]
164 , Help.reflow
165 "However many files you give, I will create one JS file out of them."
166 ]
168 CannotOptimizeDebug m ms ->
169 Help.report "DEBUG REMNANTS" Nothing
170 "There are uses of the `Debug` module in the following modules:"
171 [ P.indent 4 $ P.red $ P.vcat $ map (P.text . Module.nameToString) (m:ms)
172 , Help.reflow "But the --optimize flag only works if all `Debug` functions are removed!"
173 , toSimpleNote $
174 "The issue is that --optimize strips out info needed by `Debug` functions.\
175 \ Here are two examples:"
176 , P.indent 4 $ Help.reflow $
177 "(1) It shortens record field names. This makes the generated JavaScript is\
178 \ smaller, but `Debug.toString` cannot know the real field names anymore."
179 , P.indent 4 $ Help.reflow $
180 "(2) Values like `type Height = Height Float` are unboxed. This reduces\
181 \ allocation, but it also means that `Debug.toString` cannot tell if it is\
182 \ looking at a `Height` or `Float` value."
183 , Help.reflow $
184 "There are a few other cases like that, and it will be much worse once we start\
185 \ inlining code. That optimization could move `Debug.log` and `Debug.todo` calls,\
186 \ resulting in unpredictable behavior. I hope that clarifies why this restriction\
187 \ exists!"
188 ]
191toSimpleNote :: String -> P.Doc
192toSimpleNote message =
193 P.fillSep ((P.underline "Note" <> ":") : map P.text (words message))