@@ -73,6 +73,7 @@ module Data.Path.Pathy
73
73
, viewAbsFile
74
74
, ViewRelFile (..)
75
75
, viewRelFile
76
+ , relativify
76
77
)
77
78
where
78
79
@@ -216,11 +217,11 @@ posixEscaper = Escaper $
216
217
217
218
-- | Creates a path which points to a relative file of the specified name.
218
219
file :: NonEmptyString -> Path Rel File Sandboxed
219
- file f = file' ( Name f)
220
+ file = file' <<< Name
220
221
221
222
-- | Creates a path which points to a relative file of the specified name.
222
223
file' :: Name File -> Path Rel File Sandboxed
223
- file' f = In Current f
224
+ file' = In Current
224
225
225
226
-- | Retrieves the name of a file path.
226
227
fileName :: forall a s . Path a File s -> Name File
@@ -268,11 +269,11 @@ _updateExt ext = case _ of
268
269
269
270
-- | Creates a path which points to a relative directory of the specified name.
270
271
dir :: NonEmptyString -> Path Rel Dir Sandboxed
271
- dir d = dir' ( Name d)
272
+ dir = dir' <<< Name
272
273
273
274
-- | Creates a path which points to a relative directory of the specified name.
274
275
dir' :: Name Dir -> Path Rel Dir Sandboxed
275
- dir' d = In Current d
276
+ dir' = In Current
276
277
277
278
-- | Retrieves the name of a directory path. Not all paths have such a name,
278
279
-- | for example, the root or current directory.
@@ -434,6 +435,21 @@ printPath' = unsafePrintPath'
434
435
identicalPath :: forall a a' b b' s s' . SplitDirOrFile b => SplitDirOrFile b' => Path a b s -> Path a' b' s' -> Boolean
435
436
identicalPath p1 p2 = show p1 == show p2
436
437
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
+
437
453
-- | Makes one path relative to another reference path, if possible, otherwise
438
454
-- | returns `Nothing`. The returned path inherits the sandbox settings of the
439
455
-- | reference path.
@@ -450,19 +466,12 @@ relativeTo p1 p2 = relativeTo' (canonicalize p1) (canonicalize p2)
450
466
| otherwise = do
451
467
Tuple cp1Path name <- unsafePeel cp1
452
468
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
466
475
467
476
-- | Attempts to sandbox a path relative to some directory. If successful, the sandboxed
468
477
-- | directory will be returned relative to the sandbox directory (although this can easily
0 commit comments