Skip to content

Commit c45c388

Browse files
committed
add relativify
1 parent 5f1fc17 commit c45c388

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

src/Data/Path/Pathy.purs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ module Data.Path.Pathy
7373
, viewAbsFile
7474
, ViewRelFile(..)
7575
, viewRelFile
76+
, relativify
7677
)
7778
where
7879

@@ -216,11 +217,11 @@ posixEscaper = Escaper $
216217

217218
-- | Creates a path which points to a relative file of the specified name.
218219
file :: NonEmptyString -> Path Rel File Sandboxed
219-
file f = file' (Name f)
220+
file = file' <<< Name
220221

221222
-- | Creates a path which points to a relative file of the specified name.
222223
file' :: Name File -> Path Rel File Sandboxed
223-
file' f = In Current f
224+
file' = In Current
224225

225226
-- | Retrieves the name of a file path.
226227
fileName :: forall a s. Path a File s -> Name File
@@ -268,11 +269,11 @@ _updateExt ext = case _ of
268269

269270
-- | Creates a path which points to a relative directory of the specified name.
270271
dir :: NonEmptyString -> Path Rel Dir Sandboxed
271-
dir d = dir' (Name d)
272+
dir = dir' <<< Name
272273

273274
-- | Creates a path which points to a relative directory of the specified name.
274275
dir' :: Name Dir -> Path Rel Dir Sandboxed
275-
dir' d = In Current d
276+
dir' = In Current
276277

277278
-- | Retrieves the name of a directory path. Not all paths have such a name,
278279
-- | for example, the root or current directory.
@@ -434,6 +435,21 @@ printPath' = unsafePrintPath'
434435
identicalPath :: forall a a' b b' s s'. SplitDirOrFile b => SplitDirOrFile b' => Path a b s -> Path a' b' s' -> Boolean
435436
identicalPath p1 p2 = show p1 == show p2
436437

438+
relativify :: forall a. SplitDirOrFile a => Path Abs a Sandboxed -> Path Rel a Sandboxed
439+
relativify p = case dirOrFile p of
440+
Left d ->
441+
joinSplit $ asRel $ viewAbsDir d
442+
Right f ->
443+
let (ViewAbsFileIn d name) = viewAbsFile f
444+
in joinSplit $ asRel d </> file' name
445+
where
446+
joinSplit :: forall x. Path Rel x Sandboxed -> Path Rel a Sandboxed
447+
joinSplit = unsafeCoerce
448+
asRel :: ViewAbsDir -> Path Rel Dir Sandboxed
449+
asRel = case _ of
450+
ViewAbsDirRoot -> currentDir
451+
ViewAbsDirIn d n -> asRel d </> dir' n
452+
437453
-- | Makes one path relative to another reference path, if possible, otherwise
438454
-- | returns `Nothing`. The returned path inherits the sandbox settings of the
439455
-- | reference path.
@@ -450,19 +466,12 @@ relativeTo p1 p2 = relativeTo' (canonicalize p1) (canonicalize p2)
450466
| otherwise = do
451467
Tuple cp1Path name <- unsafePeel cp1
452468
rel <- relativeTo' cp1Path cp2
453-
pure $ overName name
454-
(\dirN -> rel </> In (Current :: Path Rel Dir s') dirN)
455-
(\fileN -> rel </> In (Current :: Path Rel Dir s') fileN)
456-
overName
457-
:: forall n a' s''
458-
. SplitDirOrFile n
459-
=> Name n
460-
-> (Name Dir -> Path a' Dir s'')
461-
-> (Name File -> Path a' File s'')
462-
-> Path a' n s''
463-
overName p onDir onFile = case dirOrFileName p of
464-
Left p' -> unsafeCoerce $ onDir p'
465-
Right p' -> unsafeCoerce $ onFile p'
469+
pure case dirOrFileName name of
470+
Left dirN -> joinSplit $ rel </> In (Current :: Path Rel Dir s') dirN
471+
Right fileN -> joinSplit $ rel </> In (Current :: Path Rel Dir s') fileN
472+
where
473+
joinSplit :: forall a_ b_ s_. Path a_ b_ s_ -> Path a_ b' s_
474+
joinSplit = unsafeCoerce
466475

467476
-- | Attempts to sandbox a path relative to some directory. If successful, the sandboxed
468477
-- | directory will be returned relative to the sandbox directory (although this can easily

0 commit comments

Comments
 (0)