Skip to content
This repository was archived by the owner on Mar 22, 2021. It is now read-only.

Commit 2a56275

Browse files
committed
Sort module functions alphabetically
1 parent 8a96e0a commit 2a56275

1 file changed

Lines changed: 41 additions & 37 deletions

File tree

src/Foreign/Lua/Module/System.hs

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -55,43 +55,9 @@ pushModule = do
5555
preloadModule :: String -> Lua ()
5656
preloadModule = flip addPackagePreloader pushModule
5757

58-
-- | Run an action, then restore the old environment variable values.
59-
with_env :: Map.Map String String -> Callback -> Lua NumResults
60-
with_env environment callback =
61-
bracket (Lua.liftIO Env.getEnvironment)
62-
setEnvironment
63-
(\_ -> setEnvironment (Map.toList environment) >> invoke callback)
64-
where
65-
setEnvironment newEnv = Lua.liftIO $ do
66-
-- Crude, but fast enough: delete all entries in new environment,
67-
-- then restore old environment one-by-one.
68-
curEnv <- Env.getEnvironment
69-
forM_ curEnv (Env.unsetEnv . fst)
70-
forM_ newEnv (uncurry Env.setEnv)
71-
72-
with_tmpdir :: String -- ^ parent dir or template
73-
-> AnyValue -- ^ template or callback
74-
-> Optional Callback -- ^ callback or nil
75-
-> Lua NumResults
76-
with_tmpdir parentDir tmpl callback =
77-
case fromOptional callback of
78-
Nothing -> do
79-
-- At most two args. The first arg (parent dir) has probably been
80-
-- omitted, so we shift arguments and use the system's canonical
81-
-- temporary directory.
82-
let tmpl' = parentDir
83-
callback' <- Lua.peek (fromAnyValue tmpl)
84-
Temp.withSystemTempDirectory tmpl' (invokeWithFilePath callback')
85-
Just callback' -> do
86-
-- all args given. Second value must be converted to a string.
87-
tmpl' <- Lua.peek (fromAnyValue tmpl)
88-
Temp.withTempDirectory parentDir tmpl' (invokeWithFilePath callback')
89-
90-
-- | List the contents of a directory.
91-
ls :: Optional FilePath -> Lua [FilePath]
92-
ls fp = do
93-
let fp' = fromMaybe "." (fromOptional fp)
94-
ioToLua (Directory.listDirectory fp')
58+
--
59+
-- Module functions
60+
--
9561

9662
-- | Change current working directory.
9763
chdir :: FilePath -> Lua ()
@@ -114,6 +80,12 @@ env = do
11480
getenv :: String -> Lua (Optional String)
11581
getenv name = ioToLua (Optional <$> Env.lookupEnv name)
11682

83+
-- | List the contents of a directory.
84+
ls :: Optional FilePath -> Lua [FilePath]
85+
ls fp = do
86+
let fp' = fromMaybe "." (fromOptional fp)
87+
ioToLua (Directory.listDirectory fp')
88+
11789
-- | Create a new directory which is initially empty, or as near to
11890
-- empty as the operating system allows.
11991
--
@@ -140,3 +112,35 @@ setenv name value = ioToLua (Env.setEnv name value)
140112
-- | Get the current directory for temporary files.
141113
tmpdirname :: Lua FilePath
142114
tmpdirname = ioToLua Directory.getTemporaryDirectory
115+
116+
-- | Run an action, then restore the old environment variable values.
117+
with_env :: Map.Map String String -> Callback -> Lua NumResults
118+
with_env environment callback =
119+
bracket (Lua.liftIO Env.getEnvironment)
120+
setEnvironment
121+
(\_ -> setEnvironment (Map.toList environment) >> invoke callback)
122+
where
123+
setEnvironment newEnv = Lua.liftIO $ do
124+
-- Crude, but fast enough: delete all entries in new environment,
125+
-- then restore old environment one-by-one.
126+
curEnv <- Env.getEnvironment
127+
forM_ curEnv (Env.unsetEnv . fst)
128+
forM_ newEnv (uncurry Env.setEnv)
129+
130+
with_tmpdir :: String -- ^ parent dir or template
131+
-> AnyValue -- ^ template or callback
132+
-> Optional Callback -- ^ callback or nil
133+
-> Lua NumResults
134+
with_tmpdir parentDir tmpl callback =
135+
case fromOptional callback of
136+
Nothing -> do
137+
-- At most two args. The first arg (parent dir) has probably been
138+
-- omitted, so we shift arguments and use the system's canonical
139+
-- temporary directory.
140+
let tmpl' = parentDir
141+
callback' <- Lua.peek (fromAnyValue tmpl)
142+
Temp.withSystemTempDirectory tmpl' (invokeWithFilePath callback')
143+
Just callback' -> do
144+
-- all args given. Second value must be converted to a string.
145+
tmpl' <- Lua.peek (fromAnyValue tmpl)
146+
Temp.withTempDirectory parentDir tmpl' (invokeWithFilePath callback')

0 commit comments

Comments
 (0)