Skip to content

Commit 9a387f8

Browse files
Merge pull request #4 from YusukeHosonuma/improve
2 parents 584f68f + 71eb061 commit 9a387f8

File tree

7 files changed

+63
-36
lines changed

7 files changed

+63
-36
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ A CLI tool that auto-fix module name of .hs files from the definition of `packag
44

55
```bash
66
$ cd <stack-project-root>
7-
$ fix-module # Warning: This command will overwrite .hs files
7+
$ fix-module # Warning: This command will overwrite .hs files
8+
$ fix-module --verbose # With verbose log
89
```
910

1011
## TODO
11-
- [ ] Support `.cabal`
12+
- [ ] Add support to `.cabal`.
13+
- [ ] Auto-fix import. (undecided)
1214

1315
## Installation
1416

app/Main.hs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
module Main where
22

3+
import Control.Exception
34
import Control.Monad
4-
import FixModule
5+
import Control.Monad.Trans.Reader
6+
import FixModule.Module
57
import System.Directory
68
import System.Environment
79
import System.Exit
@@ -10,17 +12,25 @@ main :: IO ()
1012
main = do
1113
args <- getArgs
1214
when ("--version" `elem` args) printVersion
15+
let verbose = "--verbose" `elem` args
1316
exist <- doesFileExist "package.yaml"
1417
unless exist exitPackageYamlNotFound
1518
pwd <- getCurrentDirectory
16-
fixModule pwd -- TODO: 例外処理
19+
runReaderT (fixModule pwd) (Env verbose) `catch` reportException
1720

1821
printVersion :: IO ()
1922
printVersion = do
20-
putStrLn "fix-module 0.1.0.0"
23+
putStrLn "fix-module 0.1.0"
2124
exitSuccess
2225

2326
exitPackageYamlNotFound :: IO ()
2427
exitPackageYamlNotFound = do
25-
putStrLn "package.yaml is not found. (not support .cabal yet)"
28+
putStrLn "A package.yaml is not found. (Not support .cabal yet)"
2629
exitFailure
30+
31+
reportException :: SomeException -> IO ()
32+
reportException e = do
33+
putStrLn "Fatal error.\n"
34+
putStrLn $ "[Cause]\n" ++ displayException e ++ "\n"
35+
putStrLn "Please report issue when reproducible."
36+
putStrLn "https://github.com/YusukeHosonuma/fix-module/issues/new"

fix-module.cabal

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ source-repository head
2525

2626
library
2727
exposed-modules:
28-
FixModule
2928
FixModule.Module
3029
FixModule.Package
3130
other-modules:
@@ -38,6 +37,7 @@ library
3837
, extra
3938
, filepath
4039
, text
40+
, transformers
4141
, unordered-containers
4242
, yaml
4343
default-language: Haskell2010
@@ -53,6 +53,7 @@ executable fix-module
5353
base >=4.7 && <5
5454
, directory
5555
, fix-module
56+
, transformers
5657
default-language: Haskell2010
5758

5859
test-suite fix-module-test
@@ -69,4 +70,5 @@ test-suite fix-module-test
6970
, process
7071
, tasty
7172
, tasty-hunit
73+
, transformers
7274
default-language: Haskell2010

package.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ library:
2929
- extra
3030
- filepath
3131
- text
32+
- transformers
3233
- unordered-containers
3334
- yaml
3435

@@ -44,6 +45,7 @@ executables:
4445
dependencies:
4546
- fix-module
4647
- directory
48+
- transformers
4749

4850
tests:
4951
fix-module-test:
@@ -56,6 +58,7 @@ tests:
5658
- -Wall
5759
dependencies:
5860
- fix-module
61+
- process
5962
- tasty
6063
- tasty-hunit
61-
- process
64+
- transformers

src/FixModule.hs

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/FixModule/Module.hs

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,49 @@
1-
module FixModule.Module (fixModuleRecursive) where
1+
module FixModule.Module
2+
( fixModule
3+
, Env (..)
4+
) where
25

36
import Control.Monad
7+
import Control.Monad.Trans.Class
8+
import Control.Monad.Trans.Reader
49
import Data.List
10+
import FixModule.Package
511
import System.Directory
612
import System.FilePath.Posix
713
import System.IO.Extra
814

915
type ModuleName = String
1016

11-
fixModuleRecursive :: FilePath -> IO ()
17+
newtype Env = Env { isVerbose :: Bool }
18+
19+
fixModule :: FilePath -> ReaderT Env IO ()
20+
fixModule rootDir = do
21+
dirs <- lift lookupSourceDirs
22+
verbose $ "Found source-diretories:\n" ++ unlines (map ("- " <>) dirs)
23+
mapM_ (fixModuleRecursive . (rootDir </>)) dirs
24+
25+
fixModuleRecursive :: FilePath -> ReaderT Env IO ()
1226
fixModuleRecursive rootDir = do
13-
ps <- findHaskellFilePathes rootDir
27+
ps <- lift $ findHaskellFilePathes rootDir
1428
mapM_ (fixModuleFile rootDir) ps
1529

1630
--------------------------------------------------------------------------------
1731

18-
findHaskellFilePathes :: FilePath -> IO [FilePath]
19-
findHaskellFilePathes path = do
20-
xs <- map (path </>) . filter (`notElem` [".", ".."]) <$> getDirectoryContents path
21-
let hsFiles = filter (".hs" `isExtensionOf`) xs
22-
dirs <- filterM doesDirectoryExist xs
23-
subHsFiles <- concat <$> mapM findHaskellFilePathes dirs
24-
return $ hsFiles ++ subHsFiles
25-
26-
fixModuleFile :: FilePath -> FilePath -> IO ()
32+
fixModuleFile :: FilePath -> FilePath -> ReaderT Env IO ()
2733
fixModuleFile rootDir target = do
2834
let mName = moduleName rootDir target
29-
valid <- isValidModule mName target
35+
valid <- lift $ isValidModule mName target
3036
if valid
3137
then
32-
putStrLn $ "Skip: " ++ target -- verbose に対応
38+
verbose $ "[skip] " ++ target
3339
else do
34-
updateFileWith target (fixModuleContent mName)
35-
putStrLn $ "Done: " ++ target
40+
lift $ updateFileWith target (fixModuleContent mName)
41+
verbose $ "[done] " ++ target
42+
43+
verbose :: String -> ReaderT Env IO ()
44+
verbose s = do
45+
v <- asks isVerbose
46+
when v $ lift $ putStrLn s
3647

3748
fixModuleContent :: ModuleName -> String -> String
3849
fixModuleContent mName = unlines . map (fixModuleLine mName) . lines
@@ -85,6 +96,14 @@ moduleName root filePath =
8596

8697
--------------------------------------------------------------------------------
8798

99+
findHaskellFilePathes :: FilePath -> IO [FilePath]
100+
findHaskellFilePathes path = do
101+
xs <- map (path </>) . filter (`notElem` [".", ".."]) <$> getDirectoryContents path
102+
let hsFiles = filter (".hs" `isExtensionOf`) xs
103+
dirs <- filterM doesDirectoryExist xs
104+
subHsFiles <- concat <$> mapM findHaskellFilePathes dirs
105+
return $ hsFiles ++ subHsFiles
106+
88107
updateFileWith :: FilePath -> (String -> String) -> IO ()
89108
updateFileWith fp f = do
90109
contents <- readFile' fp

test/Main.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ module Main where
55
import Test.Tasty
66
import Test.Tasty.HUnit
77
import System.Process
8-
import FixModule
8+
import FixModule.Module
99
import Control.Exception
10+
import Control.Monad.Trans.Reader
1011

1112
main :: IO ()
1213
main = defaultMain $
1314
testCase "fixModule" $ do
14-
fixModule "./test-asset"
15+
runReaderT (fixModule "./test-asset") $ Env True -- enable `--verbose `
1516
callCommand "stack build --stack-yaml ./test-asset/stack.yaml"
1617
`catch` (\(e :: SomeException) -> assertFailure $ displayException e)
1718
`finally` callCommand "git checkout HEAD ./test-asset" -- clean-up

0 commit comments

Comments
 (0)