Skip to content

Commit fb491e0

Browse files
committed
WIP offline lamdera check mode when working without internet
1 parent ddb26e1 commit fb491e0

File tree

4 files changed

+107
-56
lines changed

4 files changed

+107
-56
lines changed

extra/Lamdera/CLI/Check.hs

Lines changed: 96 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import qualified Lamdera.Evergreen.Snapshot
4444
import qualified Lamdera.TypeHash
4545
import qualified Lamdera.Types
4646
import qualified Lamdera.Legacy
47+
import qualified Network.Status
4748

4849

4950
progressPointer t = do
@@ -89,6 +90,67 @@ run () () = do
8990
debug $ "app name:" ++ show appName
9091

9192
(localTypes, externalTypeWarnings) <- getLocalInfo
93+
94+
ips <- Network.Status.ips
95+
96+
if ips == []
97+
then offlineCheck root
98+
else onlineCheck root appName inDebug localTypes externalTypeWarnings isHoistRebuild forceVersion forceNotProd inProduction
99+
100+
101+
offlineCheck root = do
102+
progressDoc $ D.stack
103+
[ D.red $ D.reflow $ "--- OFFLINE MODE ---"
104+
, D.red $ D.reflow $ "It appears you are offline, continuing with limited checks."
105+
, D.red $ D.reflow $ "Note: being offline and continuing can cause serious confusion when returning offline, in particular:"
106+
, D.red $ D.reflow $ "- I have no idea what's in production, so I can only guess no types have changed."
107+
, D.red $ D.reflow $ "- This means I won't make any new snapshots"
108+
, D.red $ D.reflow $ "- I will assume the latest current migration on disk is the real latest migration"
109+
, D.red $ D.reflow $ "- I will type check that migration under Evergreen"
110+
]
111+
112+
shouldContinue <- Reporting.ask $
113+
D.stack [ D.reflow $ "Do you want me to continue despite this? [Y/n]: " ]
114+
115+
if shouldContinue
116+
then do
117+
migrationFilePaths <- Lamdera.Evergreen.findMigrationFilePaths root
118+
119+
let
120+
nextVersionInfo =
121+
case last_ (debugNote "migrationVersions" $ Lamdera.Evergreen.migrationVersions migrationFilePaths) 1 of
122+
1 -> WithoutMigrations 1
123+
x -> WithMigrations x
124+
125+
nextVersion = vinfoVersion nextVersionInfo
126+
127+
nextMigrationPathBare = ("src/Evergreen/Migrate/V") <> (show nextVersion) <> ".elm"
128+
nextMigrationPath = root </> nextMigrationPathBare
129+
130+
debug_ $ "nextVersionInfo" ++ show nextVersionInfo
131+
132+
debug $ "Reading migration source..."
133+
migrationSource <- T.decodeUtf8 <$> IO.readUtf8 nextMigrationPath
134+
135+
if textContains "Unimplemented" migrationSource
136+
then do
137+
Progress.throw $
138+
Help.report "UNIMPLEMENTED MIGRATION" (Just nextMigrationPathBare)
139+
("The latest migration still has migration placeholders that need implementing:")
140+
[ D.reflow $ nextMigrationPathBare
141+
, D.fillSep ["See",D.cyan ("<https://dashboard.lamdera.app/docs/evergreen>"),"for more info."]
142+
]
143+
144+
else do
145+
migrationCheck root nextVersionInfo
146+
else do
147+
pure ()
148+
149+
pure ()
150+
151+
152+
onlineCheck root appName inDebug localTypes externalTypeWarnings isHoistRebuild forceVersion forceNotProd inProduction = do
153+
92154
(prodVersion, productionTypes, nextVersion) <- getProdInfo appName inProduction forceNotProd forceVersion isHoistRebuild localTypes
93155

94156
let
@@ -278,39 +340,39 @@ forceVersion_ = do
278340
Nothing -> Nothing
279341

280342

281-
getNextVersionInfo :: FilePath -> IO VersionInfo
282-
getNextVersionInfo root = do
283-
284-
hoistRebuild <- Env.lookupEnv "HOIST_REBUILD"
285-
forceVersionM <- Env.lookupEnv "VERSION"
286-
forceNotProd <- Env.lookupEnv "NOTPROD"
287-
inProduction <- Lamdera.inProduction
288-
289-
let isHoistRebuild = (hoistRebuild /= Nothing)
290-
forceVersion = fromMaybe (-1) $
291-
case forceVersionM of
292-
Just fv -> Text.Read.readMaybe fv
293-
Nothing -> Nothing
294-
295-
-- Lamdera.Legacy.temporaryCheckOldTypesNeedingMigration inProduction root
296-
297-
-- progressPointer_ "Checking project compiles..."
298-
-- checkUserProjectCompiles root
299-
300-
appName <- Lamdera.Project.appNameOrThrow
301-
302-
(localTypes, externalTypeWarnings) <- getLocalInfo
303-
(prodVersion, productionTypes, nextVersion) <- getProdInfo appName inProduction forceNotProd forceVersion isHoistRebuild localTypes
304-
305-
let
306-
localTypesChangedFromProduction = productionTypes /= localTypes
307-
308-
nextVersionInfo <- getNextVersionInfo_ nextVersion prodVersion isHoistRebuild localTypesChangedFromProduction
309-
310-
pure nextVersionInfo
311-
312-
313-
343+
-- getNextVersionInfo :: FilePath -> IO VersionInfo
344+
-- getNextVersionInfo root = do
345+
--
346+
-- hoistRebuild <- Env.lookupEnv "HOIST_REBUILD"
347+
-- forceVersionM <- Env.lookupEnv "VERSION"
348+
-- forceNotProd <- Env.lookupEnv "NOTPROD"
349+
-- inProduction <- Lamdera.inProduction
350+
--
351+
-- let isHoistRebuild = (hoistRebuild /= Nothing)
352+
-- forceVersion = fromMaybe (-1) $
353+
-- case forceVersionM of
354+
-- Just fv -> Text.Read.readMaybe fv
355+
-- Nothing -> Nothing
356+
--
357+
-- -- Lamdera.Legacy.temporaryCheckOldTypesNeedingMigration inProduction root
358+
--
359+
-- -- progressPointer_ "Checking project compiles..."
360+
-- -- checkUserProjectCompiles root
361+
--
362+
-- appName <- Lamdera.Project.appNameOrThrow
363+
--
364+
-- (localTypes, externalTypeWarnings) <- getLocalInfo
365+
-- (prodVersion, productionTypes, nextVersion) <- getProdInfo appName inProduction forceNotProd forceVersion isHoistRebuild localTypes
366+
--
367+
-- let
368+
-- localTypesChangedFromProduction = productionTypes /= localTypes
369+
--
370+
-- nextVersionInfo <- getNextVersionInfo_ nextVersion prodVersion isHoistRebuild localTypesChangedFromProduction
371+
--
372+
-- pure nextVersionInfo
373+
374+
375+
getLocalInfo :: IO (([Text], [(Text, [Text], Lamdera.Types.DiffableType)]))
314376
getLocalInfo = do
315377
hashesResult <- Lamdera.TypeHash.calculateAndWrite
316378
case hashesResult of
@@ -320,6 +382,7 @@ getLocalInfo = do
320382
pure (localTypes, externalTypeWarnings)
321383

322384

385+
getProdInfo :: Text -> Bool -> Maybe String -> Int -> Bool -> [Text] -> IO (Int, [Text], Int)
323386
getProdInfo appName inProduction forceNotProd forceVersion isHoistRebuild localTypes = do
324387
(prodVersion, productionTypes) <-
325388
if isHoistRebuild

extra/Lamdera/CLI/Live.hs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import qualified Network.WebSockets.Snap as WS
4848
import SocketServer
4949
import Data.Word (Word8)
5050
import System.Process
51-
import Network.Info
51+
5252
import System.Entropy
5353
import Snap.Util.FileServe
5454
import Control.Monad (guard, void)
@@ -598,17 +598,4 @@ passOnIndex pwd =
598598
pure ()
599599

600600

601-
network = do
602-
ns <- getNetworkInterfaces
603-
604-
ns
605-
& fmap ipv4
606-
& filter (\v -> show v /= "0.0.0.0")
607-
& filter (\v -> show v /= "127.0.0.1")
608-
& mapM_ (putStr . show)
609-
610-
611-
showInterface n = name n ++ "\n"
612-
++ " IPv4: " ++ show (ipv4 n) ++ "\n"
613-
++ " IPv6: " ++ show (ipv6 n) ++ "\n"
614-
++ " MAC: " ++ show (mac n) ++ "\n"
601+
x = 1

extra/Lamdera/Evergreen.hs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@ import qualified Ext.ElmFormat
2222

2323
createLamderaGenerated :: FilePath -> VersionInfo -> IO Text
2424
createLamderaGenerated root nextVersion = do
25+
migrationFilepaths <- findMigrationFilePaths root
26+
lamderaGenerated nextVersion migrationFilepaths
2527

26-
debug_ $ "Reading src/Evergreen/Migrate to determine migrationSequence"
2728

29+
findMigrationFilePaths :: FilePath -> IO [FilePath]
30+
findMigrationFilePaths root = do
31+
debug_ $ "Reading src/Evergreen/Migrate to determine migrationSequence"
2832
paths <- safeListDirectory $ root </> "src/Evergreen/Migrate"
29-
30-
let migrationFilepaths = paths & filter (\p -> not $ ".bk" `isInfixOf` p)
31-
32-
debug_ $ "migrationFilepaths:" <> show migrationFilepaths
33-
34-
lamderaGenerated nextVersion migrationFilepaths
33+
let migrationFilePaths = paths & filter (\p -> not $ ".bk" `isInfixOf` p)
34+
debug_ $ "migrationFilePaths:" <> show migrationFilePaths
35+
pure migrationFilePaths
3536

3637

3738
lamderaGenerated :: VersionInfo -> [FilePath] -> IO Text

extra/Lamdera/Injection.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ injections isBackend isLocalDev =
206206
var dead = false;
207207
var upgradeMode = false;
208208

209-
function mtime() {
209+
function mtime() { // microseconds
210210
if (!isBackend) { return 0; }
211211
const hrTime = process.hrtime();
212212
return Math.floor(hrTime[0] * 1000000 + hrTime[1] / 1000);

0 commit comments

Comments
 (0)