Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d9ad9a3

Browse files
piq9117Gitea
piq9117
authored and
Gitea
committed
refactor: file generation operation encapsulated (#20)
- file generation operation encapsulated in a utility function, it was littered in every file generation function.
1 parent 8115a2c commit d9ad9a3

File tree

5 files changed

+62
-78
lines changed

5 files changed

+62
-78
lines changed

‎src/UmuReactBasic.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module UmuReactBasic where
33

44
import Import
55
import Options.Applicative
6-
import UmuReactBasic.Capability.LogMessage
7-
import UmuReactBasic.Capability.ManageCommand
6+
import UmuReactBasic.Capability.Log
7+
import UmuReactBasic.Capability.Command
88
import UmuReactBasic.Log
99
import UmuReactBasic.Parser
1010
import Lens.Micro

‎src/UmuReactBasic/Capability/ManageCommand.hs renamed to ‎src/UmuReactBasic/Capability/Command.hs

Lines changed: 30 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{-# LANGUAGE ScopedTypeVariables #-}
2-
module UmuReactBasic.Capability.ManageCommand
2+
module UmuReactBasic.Capability.Command
33
( ManageCommand (..)
44
, generateProj
55
) where
66

77
import Import
88
-- Turtle
99
import qualified Turtle
10-
import qualified Turtle.Prelude as TP
10+
import qualified Turtle.Prelude as TP
1111
-- Umu
12-
import UmuReactBasic.Capability.LogMessage
12+
import UmuReactBasic.Capability.Log
1313
import UmuReactBasic.Templates
1414
import UmuReactBasic.Util
1515

@@ -122,116 +122,73 @@ writeTestDir mLoc = do
122122
--- FILE GENERATION
123123
------------------------------------------
124124
writeIndexHtml :: ( MonadIO m, LogMessage m ) => Maybe Text -> m ()
125-
writeIndexHtml mLoc = do
126-
isExists <- TP.testfile $ Turtle.fromText $ mkPathName mLoc filePath
127-
if isExists
128-
then logError $ filePath <> " already exists!"
129-
else do
130-
liftIO $ TP.writeTextFile
131-
( Turtle.fromText $ mkPathName mLoc filePath )
132-
indexHtmlFile
133-
logInfo $ "Generating " <> filePath <> "..."
125+
writeIndexHtml mPathInput = do
126+
isExists <- isFileExists mPathInput filePath
127+
generateWhenFileNotExists isExists mPathInput filePath indexHtmlFile
134128
where
135129
filePath :: Text
136130
filePath = "assets/index.html"
137131

138132
writeSrcMainFile :: ( MonadIO m, LogMessage m ) => Maybe Text -> m ()
139-
writeSrcMainFile mLoc = do
140-
isExists <- TP.testfile $ Turtle.fromText $ mkPathName mLoc filePath
141-
if isExists
142-
then logError $ filePath <> " already exists!"
143-
else do
144-
liftIO $ TP.writeTextFile
145-
( Turtle.fromText $ mkPathName mLoc filePath ) srcMainFile
146-
logInfo $ "Generating " <> filePath <> "..."
133+
writeSrcMainFile mPathInput = do
134+
isExists <- isFileExists mPathInput filePath
135+
generateWhenFileNotExists isExists mPathInput filePath srcMainFile
147136
where
148137
filePath :: Text
149138
filePath = "src/Main.purs"
150139

151140
writeTitleComponentFile :: ( MonadIO m, LogMessage m ) => Maybe Text -> m ()
152-
writeTitleComponentFile mLoc = do
153-
isExists <- TP.testfile $ Turtle.fromText $ mkPathName mLoc filePath
154-
if isExists
155-
then logError $ filePath <> " already exists!"
156-
else do
157-
liftIO $ TP.writeTextFile
158-
( Turtle.fromText $ mkPathName mLoc filePath )
159-
titleComponentFile
160-
logInfo $ "Generating " <> filePath <> "..."
141+
writeTitleComponentFile mPathInput = do
142+
isExists <- isFileExists mPathInput filePath
143+
generateWhenFileNotExists isExists mPathInput filePath titleComponentFile
161144
where
162145
filePath :: Text
163146
filePath = "src/Component/Title.purs"
164147

165148
writeSpagoDhallFile :: ( MonadIO m, LogMessage m ) => Maybe Text -> m ()
166-
writeSpagoDhallFile mLoc = do
167-
isExists <- TP.testfile $ Turtle.fromText $ mkPathName mLoc filePath
168-
if isExists
169-
then logError $ filePath <> " already exists!"
170-
else do
171-
liftIO $ TP.writeTextFile
172-
( Turtle.fromText $ mkPathName mLoc filePath ) spagoDhallFile
173-
logInfo $ "Generating " <> filePath <> "..."
149+
writeSpagoDhallFile mPathInput = do
150+
isExists <- isFileExists mPathInput filePath
151+
generateWhenFileNotExists isExists mPathInput filePath spagoDhallFile
174152
where
175153
filePath :: Text
176154
filePath = "spago.dhall"
177155

178156
writePackagesDhallFile :: ( MonadIO m, LogMessage m ) => Maybe Text -> m ()
179-
writePackagesDhallFile mLoc = do
180-
isExists <- TP.testfile $ Turtle.fromText $ mkPathName mLoc filePath
181-
if isExists
182-
then logError $ filePath <> " already exists!"
183-
else do
184-
liftIO $
185-
TP.writeTextFile ( Turtle.fromText $ mkPathName mLoc filePath ) packagesDhallFile
186-
logInfo $ "Generating " <> filePath <> "..."
157+
writePackagesDhallFile mPathInput = do
158+
isExists <- isFileExists mPathInput filePath
159+
generateWhenFileNotExists isExists mPathInput filePath packagesDhallFile
187160
where
188161
filePath :: Text
189162
filePath = "packages.dhall"
190163

191164
writeTestMainFile :: ( MonadIO m, LogMessage m ) => Maybe Text -> m ()
192-
writeTestMainFile mLoc = do
193-
isExists <- TP.testfile $ Turtle.fromText $ mkPathName mLoc filePath
194-
if isExists
195-
then logError $ filePath <> " already exists!"
196-
else do
197-
liftIO $ TP.writeTextFile ( Turtle.fromText $ mkPathName mLoc filePath ) testMainFile
198-
logInfo $ "Generating " <> filePath <> "..."
165+
writeTestMainFile mPathInput = do
166+
isExists <- isFileExists mPathInput filePath
167+
generateWhenFileNotExists isExists mPathInput filePath testMainFile
199168
where
200169
filePath :: Text
201170
filePath = "test/Main.purs"
202171

203172
writeMakefile :: ( MonadIO m, LogMessage m ) => Maybe Text -> m ()
204-
writeMakefile mLoc = do
205-
isExists <- TP.testfile $ Turtle.fromText $ mkPathName mLoc filePath
206-
if isExists
207-
then logError $ filePath <> " already exists!"
208-
else do
209-
liftIO $ TP.writeTextFile ( Turtle.fromText $ mkPathName mLoc filePath ) makeFile
210-
logInfo $ "Generating " <> filePath <> "..."
173+
writeMakefile mPathInput = do
174+
isExists <- isFileExists mPathInput filePath
175+
generateWhenFileNotExists isExists mPathInput filePath makeFile
211176
where
212177
filePath :: Text
213178
filePath = "Makefile"
214179

215180
writePackageJsonFile :: ( MonadIO m, LogMessage m ) => Maybe Text -> m ()
216-
writePackageJsonFile mLoc = do
217-
isExists <- TP.testfile $ Turtle.fromText $ mkPathName mLoc filePath
218-
if isExists
219-
then logError $ filePath <> " already exists!"
220-
else do
221-
liftIO $ TP.writeTextFile ( Turtle.fromText $ mkPathName mLoc filePath ) packageJsonFile
222-
logInfo $ "Generating " <> filePath <> "..."
181+
writePackageJsonFile mPathInput = do
182+
isExists <- isFileExists mPathInput filePath
183+
generateWhenFileNotExists isExists mPathInput filePath packageJsonFile
223184
where
224185
filePath :: Text
225186
filePath = "package.json"
226187

227188
writeHotRelodingIndexJs :: ( MonadIO m , LogMessage m ) => Maybe Text -> m ()
228-
writeHotRelodingIndexJs mLoc = do
229-
isExists <- TP.testfile $ Turtle.fromText $ mkPathName mLoc filePath
230-
if isExists
231-
then logError $ filePath <> " already exists!"
232-
else do
233-
liftIO $ TP.writeTextFile ( Turtle.fromText $ mkPathName mLoc filePath ) hotReloadIndexJS
234-
logInfo $ "Generating " <> filePath <> "..."
189+
writeHotRelodingIndexJs mPathInput = do
190+
isExists <- isFileExists mPathInput filePath
191+
generateWhenFileNotExists isExists mPathInput filePath hotReloadIndexJS
235192
where
236193
filePath :: Text
237194
filePath = "assets/index.js"

‎src/UmuReactBasic/Capability/LogMessage.hs renamed to ‎src/UmuReactBasic/Capability/Log.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{-# LANGUAGE FlexibleInstances #-}
22
{-# LANGUAGE UndecidableInstances #-}
3-
module UmuReactBasic.Capability.LogMessage where
3+
module UmuReactBasic.Capability.Log where
44

55
import Import hiding (log)
66
import UmuReactBasic.Log

‎src/UmuReactBasic/Util.hs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,36 @@
11
module UmuReactBasic.Util
22
( mkPathName
3+
, generateWhenFileNotExists
4+
, isFileExists
35
) where
46

57
import Import
8+
-- umu-react-basic
9+
import UmuReactBasic.Capability.Log
10+
--turtle
11+
import qualified Turtle
12+
import qualified Turtle.Prelude as TP
613

714
mkPathName :: Maybe Text -> Text -> Text
815
mkPathName mLoc fileName =
916
maybe "./" (\loc -> "./" <> loc <> "/") mLoc <> fileName
17+
18+
isFileExists :: MonadIO m => Maybe Text -> Text -> m Bool
19+
isFileExists mPathInput filePath =
20+
TP.testfile $ Turtle.fromText ( mkPathName mPathInput filePath )
21+
22+
generateWhenFileNotExists
23+
:: ( MonadIO m, LogMessage m )
24+
=> Bool
25+
-> Maybe Text
26+
-> Text
27+
-> Text
28+
-> m ()
29+
generateWhenFileNotExists isExists mPathInput filePath file
30+
| isExists = logError ( filePath <> " already exists!" )
31+
| otherwise = generateFile mPathInput filePath file
32+
33+
generateFile :: ( MonadIO m, LogMessage m ) => Maybe Text -> Text -> Text -> m ()
34+
generateFile mPathInput filePath file = do
35+
liftIO $ TP.writeTextFile ( Turtle.fromText $ mkPathName mPathInput filePath ) file
36+
logInfo ( "Generated " <> filePath )

‎umu-react-basic.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ library
2323
, Paths_umu_react_basic
2424
, UmuReactBasic.Util
2525
, UmuReactBasic.Parser
26-
, UmuReactBasic.Capability.ManageCommand
27-
, UmuReactBasic.Capability.LogMessage
26+
, UmuReactBasic.Capability.Command
27+
, UmuReactBasic.Capability.Log
2828
, UmuReactBasic.TH
2929
, UmuReactBasic.Templates
3030
, UmuReactBasic.Log

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /