4

IO を使用し、ファイルに依存する TemplateHaskell 式が、MyDependency.txtそのファイルが変更されたときに再計算されるようにします。

したがってaddDependentFile "MyDependency.txt"、コードをコンパイルするときにそのファイルの変更をチェックするように ghc に指示するために使用しています。

addDependentFile残念ながら、 ghc が呼び出されたディレクトリに対してのみ動作するため、これは動作しません。

コンパイルしているファイルのすぐ隣(同じディレクトリ内) にあるファイルに依存するためにどのように使用できますか?

4

1 に答える 1

5

locationfromを使用しLanguage.Haskell.TH.Syntaxてコンパイル中のファイルのファイル名を抽出し、これを使用して正しいパスを組み立てることができます。

-- | Uses 'addDependentFile' on a file relative to the current file
-- to mark it as being checked for changes when compiled with TemplateHaskell.
--
-- Returns an empty list of declarations so that it can be used with:
--
-- >$(addDependentFileRelative "MyDependency.txt")
addDependentFileRelative :: FilePath -> Q [Dec]
addDependentFileRelative relativeFile = do
    currentFilename <- loc_filename <$> location
    pwd             <- runIO getCurrentDirectory

    let invocationRelativePath = takeDirectory (pwd </> currentFilename) </> relativeFile

    addDependentFile invocationRelativePath

    return []

(パブリック ドメイン コード)

于 2013-04-23T07:56:35.950 に答える