Skip to content

Commit 10758b7

Browse files
committed
add move moveFile
1 parent 032bf07 commit 10758b7

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

src/Pathy.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module Pathy
77
, module Pathy.Sandboxed
88
) where
99

10-
import Pathy.Path (AbsDir, AbsFile, AbsPath, AnyPath, AnyDir, AnyFile, Path, RelDir, RelFile, RelPath, appendPath, currentDir, dir, dir', extendPath, file, file', in', fileName, foldPath, name, parentAppend, parentOf, peel, peelFile, refine, relativeTo, rename, renameTraverse, rootDir, setExtension, (<..>), (<.>), (</>))
10+
import Pathy.Path (AbsDir, AbsFile, AbsPath, AnyPath, AnyDir, AnyFile, Path, RelDir, RelFile, RelPath, appendPath, currentDir, dir, dir', extendPath, file, file', in', fileName, foldPath, name, parentAppend, move, moveFile, parentOf, peel, peelFile, refine, relativeTo, rename, renameTraverse, rootDir, setExtension, (<..>), (<.>), (</>))
1111
import Pathy.Name (Name(..), joinName, splitName, alterExtension, extension)
1212
import Pathy.Printer (Escaper(..), Printer, debugPrintPath, posixPrinter, printPath, unsafePrintPath, windowsPrinter)
1313
import Pathy.Parser (Parser(..), parseAbsDir, parseAbsFile, parsePath, parseRelDir, parseRelFile, posixParser)

src/Pathy/Gen.purs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ module Pathy.Gen
66
, genRelFilePath
77
, genRelAnyPath
88
, genName
9+
, genDirName
10+
, genFileName
911
) where
1012

1113
import Prelude
@@ -20,14 +22,20 @@ import Data.List as L
2022
import Data.NonEmpty ((:|))
2123
import Data.String.Gen as SG
2224
import Data.String.NonEmpty (cons)
23-
import Pathy (AbsDir, AbsFile, AbsPath, Dir, RelDir, RelFile, RelPath, (</>))
25+
import Pathy (AbsDir, AbsFile, AbsPath, Dir, File, RelDir, RelFile, RelPath, (</>))
2426
import Pathy as P
2527

2628
genName m a. MonadGen m MonadRec m m (P.Name a)
2729
genName = map P.Name $ cons <$> genChar <*> SG.genString genChar
2830
where
2931
genChar = Gen.oneOf $ CG.genDigitChar :| [CG.genAlpha]
3032

33+
genDirName :: m. MonadGen m MonadRec m m (P.Name Dir)
34+
genDirName = genName
35+
36+
genFileName :: m. MonadGen m MonadRec m m (P.Name File)
37+
genFileName = genName
38+
3139
genAbsDirPath :: forall m. MonadGen m => MonadRec m => m AbsDir
3240
genAbsDirPath = Gen.sized \size → do
3341
newSize ← Gen.chooseInt 0 size

src/Pathy/Path.purs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ module Pathy.Path
2020
, extendPath
2121
, appendPath, (</>)
2222
, parentAppend, (<..>)
23+
, move
24+
, moveFile
2325
, foldPath
2426
, peel
2527
, peelFile
@@ -179,6 +181,28 @@ parentAppend d p = parentOf d </> p
179181

180182
infixl 6 parentAppend as <..>
181183

184+
-- | Moves path by putting target path before the last segment
185+
-- |
186+
-- | ``` purescript
187+
-- | move (h </> in' n) target == Just (h </> target </> in' n)
188+
-- | ```
189+
-- | In case input is `rootDir` or `currentDir`, `Nothing` will be returned
190+
move a b. IsRelOrAbs a IsDirOrFile b Path a b Path Rel Dir Maybe (Path a b)
191+
move p t = onDirOrFile
192+
(\coe dir → peel dir <#> \(Tuple root name) → coe $ root </> t </> in' name)
193+
(\coe file → Just $ coe $ moveFile file t)
194+
p
195+
196+
-- | Moves file path by putting target path before the file segment
197+
-- |
198+
-- | ``` purescript
199+
-- | moveFile (h </> file' n) target == h </> target </> file' n
200+
-- | ```
201+
moveFile a. IsRelOrAbs a Path a File Path Rel Dir Path a File
202+
moveFile p t =
203+
let (Tuple root name) = peelFile p
204+
in root </> t </> in' name
205+
182206
-- | A fold over `Path`s. Since `Path` has private constructors, this allows for
183207
-- | functions to be written over its constructors, similar to a total pattern
184208
-- | match.

test/Main.purs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import Data.String.NonEmpty (NonEmptyString)
1313
import Data.String.NonEmpty as NES
1414
import Data.Symbol (SProxy(..))
1515
import Data.Tuple (Tuple(..))
16-
import Pathy (class IsDirOrFile, class IsRelOrAbs, Abs, Dir, Name(..), Path, Rel, alterExtension, currentDir, debugPrintPath, dir, extension, file, in', joinName, parentOf, parseAbsDir, parseAbsFile, parseRelDir, parseRelFile, peel, posixParser, posixPrinter, printPath, relativeTo, rename, rootDir, sandbox, sandboxAny, splitName, unsandbox, windowsPrinter, (<..>), (<.>), (</>))
16+
import Pathy (class IsDirOrFile, class IsRelOrAbs, Abs, Dir, File, Name(..), Path, Rel, alterExtension, currentDir, debugPrintPath, dir, extension, file, file', in', joinName, move, moveFile, parentOf, parseAbsDir, parseAbsFile, parseRelDir, parseRelFile, peel, posixParser, posixPrinter, printPath, relativeTo, rename, rootDir, sandbox, sandboxAny, splitName, unsandbox, windowsPrinter, (<..>), (<.>), (</>))
17+
import Pathy.Gen (genName)
1718
import Pathy.Gen as PG
1819
import Pathy.Name (reflectName)
1920
import Test.QuickCheck ((===))
@@ -101,6 +102,34 @@ checkPeelIn gen = do
101102
p <- gen
102103
pure $ p === maybe p (\(Tuple r n) -> r </> in' n) (peel p)
103104

105+
checkMove
106+
:: forall b a
107+
. IsRelOrAbs a
108+
=> IsDirOrFile b
109+
=> Gen.Gen (Path a Dir)
110+
-> Gen.Gen (Name b)
111+
-> Gen.Gen (Path Rel Dir)
112+
-> Gen.Gen QC.Result
113+
checkMove genRoot genName genTarget = do
114+
h <- genRoot
115+
n <- genName
116+
target <- genTarget
117+
pure $ move (h </> in' n) target === Just (h </> target </> in' n)
118+
119+
checkMoveFile
120+
:: forall a b
121+
. IsRelOrAbs a
122+
=> Gen.Gen (Path a Dir)
123+
-> Gen.Gen (Name File)
124+
-> Gen.Gen (Path Rel Dir)
125+
-> Gen.Gen QC.Result
126+
checkMoveFile genRoot genName genTarget = do
127+
h <- genRoot
128+
n <- genName
129+
target <- genTarget
130+
pure $ moveFile (h </> file' n) target === (h </> target </> file' n)
131+
132+
104133
checkRelative :: forall b. IsDirOrFile b => Gen.Gen (Path Abs b) -> Gen.Gen QC.Result
105134
checkRelative gen = do
106135
p1 <- gen
@@ -124,6 +153,12 @@ main = do
124153
info "checking `parse <<< print` for `AbsFile`" *> QC.quickCheck parsePrintAbsFilePath
125154
info "checking `parse <<< print` for `RelDir`" *> QC.quickCheck parsePrintRelDirPath
126155
info "checking `parse <<< print` for `RelFile`" *> QC.quickCheck parsePrintRelFilePath
156+
info "checking `move (absPath </> file' n) target == Just (absPath </> target </> file' n)`" *> QC.quickCheck (checkMove PG.genAbsDirPath PG.genFileName PG.genRelDirPath)
157+
info "checking `move (relPath </> file' n) target == Just (relPath </> target </> file' n)`" *> QC.quickCheck (checkMove PG.genRelDirPath PG.genFileName PG.genRelDirPath)
158+
info "checking `move (absPath </> dir' n) target == Just (absPath </> target </> dir' n)`" *> QC.quickCheck (checkMove PG.genAbsDirPath PG.genDirName PG.genRelDirPath)
159+
info "checking `move (relPath </> dir' n) target == Just (relPath </> target </> dir' n)`" *> QC.quickCheck (checkMove PG.genRelDirPath PG.genDirName PG.genRelDirPath)
160+
info "checking `moveFile (absPath </> file' n) target == absPath </> target </> file' n`" *> QC.quickCheck (checkMoveFile PG.genAbsDirPath genName PG.genRelDirPath)
161+
info "checking `moveFile (relPath </> file' n) target == relPath </> target </> file' n`" *> QC.quickCheck (checkMoveFile PG.genRelDirPath genName PG.genRelDirPath)
127162
info "checking `relativeTo` for `AbsDir`" *> QC.quickCheck (checkRelative PG.genAbsDirPath)
128163
info "checking `relativeTo` for `AbsFile`" *> QC.quickCheck (checkRelative PG.genAbsFilePath)
129164
info "checking `p === maybe p (\\(Tuple r n) -> r </> in' n) (peel p)` for `AbsDir`" *> QC.quickCheck (checkPeelIn PG.genAbsDirPath)

0 commit comments

Comments
 (0)