file-path-th (empty) → 0.1.0.0
raw patch · 3 files changed
+71/−0 lines, 3 filesdep +basedep +directorydep +file-embed
Dependencies added: base, directory, file-embed, filepath, template-haskell
Files
- LICENSE +4/−0
- file-path-th.cabal +34/−0
- src/System/FilePath/TH.hs +33/−0
+ LICENSE view
@@ -0,0 +1,4 @@+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE+WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR+OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ file-path-th.cabal view
@@ -0,0 +1,34 @@+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.31.2.+--+-- see: https://github.com/sol/hpack+--+-- hash: 467442d094a298061e3beddf420a8944d47883cd4a5040c468f7f2483ab4a74e++name: file-path-th+version: 0.1.0.0+synopsis: Template Haskell utilities for filepaths.+description: Template Haskell utilities for filepaths. This package has utilities for embedding relative filepaths using Template Haskell.+category: System+maintainer: Dan Fithian+license: MIT+license-file: LICENSE+build-type: Simple++library+ exposed-modules:+ System.FilePath.TH+ other-modules:+ Paths_file_path_th+ hs-source-dirs:+ src+ default-extensions: ConstraintKinds DataKinds DeriveDataTypeable EmptyDataDecls FlexibleContexts FlexibleInstances GADTs GeneralizedNewtypeDeriving LambdaCase MultiParamTypeClasses MultiWayIf NamedFieldPuns NoImplicitPrelude NoMonomorphismRestriction OverloadedStrings QuasiQuotes RankNTypes RecordWildCards ScopedTypeVariables StandaloneDeriving TemplateHaskell TupleSections TypeFamilies ViewPatterns+ ghc-options: -Wall -fwarn-tabs+ build-depends:+ base <5.0+ , directory+ , file-embed+ , filepath+ , template-haskell+ default-language: Haskell2010
+ src/System/FilePath/TH.hs view
@@ -0,0 +1,33 @@+module System.FilePath.TH where++import Prelude++import Control.Monad ((<=<))+import Data.FileEmbed (makeRelativeToProject)+import Language.Haskell.TH (Loc(Loc), Exp, Q, loc_filename, location, runIO, stringE)+import System.Directory (canonicalizePath, getCurrentDirectory)+import System.FilePath ((</>), takeDirectory)++fileRelativeToAbsolute :: String -> Q Exp+fileRelativeToAbsolute = stringE <=< fileRelativeToAbsoluteStr++-- | Use a path relative to the source file in which you're writing the path instead of relative to the working+-- directory in which that source file will be compiled.+--+-- e.g. if this source file is in `my-project/src/Foo/Bar.hs`, `fileRelativeToAbsoluteStr+-- "../../../config/settings.yml"` will load the path at `my-project/config/settings.yml`.+--+-- If this function is provided an absolute path, it will simply canonicalize that path+-- by calling 'System.Directory.canonicalizePath' rather than compute an absolute path from+-- a relative path.+fileRelativeToAbsoluteStr :: String -> Q String+fileRelativeToAbsoluteStr absoluteFilePath@('/':_) =+ runIO . canonicalizePath $ absoluteFilePath+fileRelativeToAbsoluteStr relativeFilePath = do+ Loc {..} <- location+ currentDir <- runIO getCurrentDirectory+ let baseDir = takeDirectory loc_filename+ runIO $ canonicalizePath $ currentDir </> baseDir </> relativeFilePath++fileRelativeToProject :: FilePath -> Q Exp+fileRelativeToProject = stringE <=< makeRelativeToProject