|
| 1 | +#!/usr/bin/env cabal |
| 2 | +{- cabal: |
| 3 | +build-depends: base |
| 4 | + , rio |
| 5 | + , text |
| 6 | + , bytestring |
| 7 | + , transformers |
| 8 | +default-language: GHC2021 |
| 9 | +default-extensions: |
| 10 | + NoImplicitPrelude |
| 11 | + OverloadedStrings |
| 12 | +-} |
| 13 | + |
| 14 | +module Main where |
| 15 | + |
| 16 | +import Data.ByteString (ByteString) |
| 17 | +import Data.ByteString qualified as B |
| 18 | +import Data.Text (Text) |
| 19 | +import Data.Text.Encoding qualified as T |
| 20 | +import Data.Text qualified as T |
| 21 | +import Data.Text.IO qualified as T |
| 22 | +import GHC.Stack |
| 23 | +import System.IO |
| 24 | +import RIO |
| 25 | +import RIO.FilePath |
| 26 | +import RIO.Directory |
| 27 | +import Data.List(isSuffixOf) |
| 28 | +import Control.Monad.Trans.Maybe (runMaybeT) |
| 29 | + |
| 30 | +type ModuleName = Text |
| 31 | + |
| 32 | +-- renames without prefix: [(new name, old name)] |
| 33 | +renames :: [(ModuleName, ModuleName)] |
| 34 | +renames = map (bimap prependModulePrefix prependModulePrefix) |
| 35 | + [ ("Byron.Internal.Key", "Internal.Keys.Byron") |
| 36 | + ,("Address", "Internal.Address") |
| 37 | + , ("Block", "Internal.Block") |
| 38 | + |
| 39 | + -- TODO reexport through Cardano.Api.Tx |
| 40 | + , ("Tx.Internal.Convenience", "Internal.Convenience.Construction") |
| 41 | + , ("Tx.Internal.Fees", "Internal.Fees") |
| 42 | + -- TODO reexport through Cardano.Api.Query |
| 43 | + ,("Query.Internal.Convenience", "Internal.Convenience.Query") |
| 44 | + |
| 45 | + -- TODO reexport everything through Cardano.Api.Era |
| 46 | + , ("Era", "Internal.Eras") |
| 47 | + , ("Era.Internal.Case", "Internal.Eras.Case") |
| 48 | + , ("Era.Internal.Core", "Internal.Eras.Core") |
| 49 | + , ("Era.Internal.Eon.AllegraEraOnwards" ,"Internal.Eon.AllegraEraOnwards") |
| 50 | + , ("Era.Internal.Eon.AlonzoEraOnwards" ,"Internal.Eon.AlonzoEraOnwards") |
| 51 | + , ("Era.Internal.Eon.BabbageEraOnwards" ,"Internal.Eon.BabbageEraOnwards") |
| 52 | + , ("Era.Internal.Eon.ByronToAlonzoEra" ,"Internal.Eon.ByronToAlonzoEra") |
| 53 | + , ("Era.Internal.Eon.Convert" ,"Internal.Eon.Convert") |
| 54 | + , ("Era.Internal.Eon.ConwayEraOnwards" ,"Internal.Eon.ConwayEraOnwards") |
| 55 | + , ("Era.Internal.Eon.MaryEraOnwards" ,"Internal.Eon.MaryEraOnwards") |
| 56 | + , ("Era.Internal.Eon.ShelleyBasedEra" ,"Internal.Eon.ShelleyBasedEra") |
| 57 | + , ("Era.Internal.Eon.ShelleyEraOnly" ,"Internal.Eon.ShelleyEraOnly") |
| 58 | + , ("Era.Internal.Eon.ShelleyToAllegraEra" ,"Internal.Eon.ShelleyToAllegraEra") |
| 59 | + , ("Era.Internal.Eon.ShelleyToAlonzoEra" ,"Internal.Eon.ShelleyToAlonzoEra") |
| 60 | + , ("Era.Internal.Eon.ShelleyToBabbageEra" ,"Internal.Eon.ShelleyToBabbageEra") |
| 61 | + , ("Era.Internal.Eon.ShelleyToMaryEra" ,"Internal.Eon.ShelleyToMaryEra") |
| 62 | + |
| 63 | + , ("Error", "Internal.Error") |
| 64 | + |
| 65 | + , ("Genesis", "Internal.Genesis") |
| 66 | + -- TODO reexport through Genesis module |
| 67 | + , ("Genesis.Internal.Parameters", "Internal.GenesisParameters") |
| 68 | + |
| 69 | + , ("Governance.Internal.Metadata.Validation", "Internal.Governance.Metadata.Validation") |
| 70 | + |
| 71 | + , ("IO", "Internal.IO") |
| 72 | + |
| 73 | + , ("IPC", "Internal.IPC") |
| 74 | + -- TODO reexport through IPC module |
| 75 | + , ("IPC.Internal.Monad", "Internal.IPC.Monad") |
| 76 | + |
| 77 | + , ("LedgerState", "Internal.LedgerState") |
| 78 | + |
| 79 | + , ("Consensus", "Internal.LedgerState") |
| 80 | + ] |
| 81 | + |
| 82 | + -- TODO remove Cardano.Api.Shelley |
| 83 | + where |
| 84 | + prependModulePrefix t = modulePrefix <> "." <> t |
| 85 | + modulePrefix = "Cardano.Api" |
| 86 | + |
| 87 | +-- TODO commits order: |
| 88 | +-- 1. Update refactor.hs |
| 89 | +-- 2. Adjust single function exports from modules, delete modules |
| 90 | +-- 3. Move modules not being exported to other-modules in cardano-api.cabal |
| 91 | +-- * caveat, some modules will still have to be exported |
| 92 | +-- 4. Run refactor.hs renaming modules |
| 93 | + |
| 94 | +type MonadApp e m = (HasCallStack, MonadIO m, MonadReader e m, HasLogFunc e) |
| 95 | + |
| 96 | +substituteModuleNames :: [(ModuleName, ModuleName)] -> Text -> Text |
| 97 | +substituteModuleNames renames' text = foldl' f text renames' |
| 98 | + where |
| 99 | + f :: Text -> (ModuleName, ModuleName) -> Text |
| 100 | + f text' (nameTo, nameFrom) = |
| 101 | + T.replace nameFrom nameTo text' |
| 102 | + |
| 103 | +toFilePath :: ModuleName -> FilePath |
| 104 | +toFilePath = |
| 105 | + T.unpack |
| 106 | + . (<> ".hs") |
| 107 | + . ("cardano-api/src/" <>) |
| 108 | + . T.replace "." "/" |
| 109 | + |
| 110 | +main :: IO () |
| 111 | +main = runSimpleApp $ do |
| 112 | + logInfo "Updating cabal file" |
| 113 | + updateFile renames "cardano-api/cardano-api.cabal" |
| 114 | + |
| 115 | + logInfo "Renaming modules" |
| 116 | + mapM_ renameModule renames |
| 117 | + |
| 118 | + logInfo "Updating modules' references" |
| 119 | + allFiles <- listDirectoryRecursively "cardano-api" |
| 120 | + forM_ allFiles $ \file -> do |
| 121 | + if ".hs" `isSuffixOf` file |
| 122 | + then do |
| 123 | + let shelley = ("Cardano.Api", "Cardano.Api.Shelley") |
| 124 | + updateFile (shelley:renames) file |
| 125 | + else |
| 126 | + removeEmptyDirectory file |
| 127 | + |
| 128 | +renameModule :: MonadApp e m |
| 129 | + => (ModuleName, ModuleName) -- ^ (new, old) |
| 130 | + -> m () |
| 131 | +renameModule (newModule, oldModule) = do |
| 132 | + let oldPath = toFilePath oldModule |
| 133 | + newPath = toFilePath newModule |
| 134 | + isFilePresent <- doesFileExist oldPath |
| 135 | + if isFilePresent |
| 136 | + then do |
| 137 | + createDirectoryIfMissing True $ takeDirectory newPath |
| 138 | + renameFile oldPath newPath |
| 139 | + else |
| 140 | + logWarn $ "Could not find file: " <> display oldPath |
| 141 | + |
| 142 | +listDirectoryRecursively :: MonadApp e m => FilePath -> m [FilePath] |
| 143 | +listDirectoryRecursively dir = do |
| 144 | + -- logInfo $ "Entering: " <> display dir |
| 145 | + entries <- listDirectory dir |
| 146 | + let paths = (dir </>) <$> entries |
| 147 | + foldM (\acc path -> do |
| 148 | + isDirectory <- doesDirectoryExist path |
| 149 | + if isDirectory |
| 150 | + then (acc <>) <$> listDirectoryRecursively path |
| 151 | + else pure $ path : acc |
| 152 | + ) paths paths |
| 153 | + |
| 154 | +removeEmptyDirectory :: MonadApp e m => FilePath -> m () |
| 155 | +removeEmptyDirectory path = void . runMaybeT $ do |
| 156 | + True <- doesDirectoryExist path |
| 157 | + [] <- listDirectory path |
| 158 | + removeDirectory path |
| 159 | + logInfo $ "Removed empty directory" <> display path |
| 160 | + |
| 161 | +filterHaskellFiles :: [FilePath] -> [FilePath] |
| 162 | +filterHaskellFiles = filter (isSuffixOf ".hs") |
| 163 | + |
| 164 | +updateFile :: MonadApp e m |
| 165 | + => [(ModuleName, ModuleName)] |
| 166 | + -> FilePath |
| 167 | + -> m () |
| 168 | +updateFile renames' filePath = do |
| 169 | + contents <- liftIO $ T.decodeUtf8 <$> B.readFile filePath |
| 170 | + liftIO $ T.writeFile filePath $ substituteModuleNames renames' contents |
| 171 | + |
| 172 | +instance Display String where |
| 173 | + display = fromString |
0 commit comments