|
| 1 | +// $begin{copyright} |
| 2 | +// |
| 3 | +// This file is part of Bolero |
| 4 | +// |
| 5 | +// Copyright (c) 2018 IntelliFactory and contributors |
| 6 | +// |
| 7 | +// Licensed under the Apache License, Version 2.0 (the "License"); you |
| 8 | +// may not use this file except in compliance with the License. You may |
| 9 | +// obtain a copy of the License at |
| 10 | +// |
| 11 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +// |
| 13 | +// Unless required by applicable law or agreed to in writing, software |
| 14 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 16 | +// implied. See the License for the specific language governing |
| 17 | +// permissions and limitations under the License. |
| 18 | +// |
| 19 | +// $end{copyright} |
| 20 | + |
| 21 | +module Bolero.Templating.Path |
| 22 | + |
| 23 | +open System.IO |
| 24 | + |
| 25 | +/// Canonicalize a path: remove . and .. components, unify slashes. |
| 26 | +let Canonicalize (path: string) = |
| 27 | + FileInfo(path).FullName |
| 28 | + |
| 29 | +/// Given a base directory and a full path, return the corresponding relative path. |
| 30 | +/// eg: if baseDir = "c:/foo" and fullPath = "c:/foo/bar/baz.html", then return "bar/baz.html". |
| 31 | +/// Assumes that both fullPath and baseDir are canonical (see Canonicalize above). |
| 32 | +/// Fails if fullPath is not a subdirectory of baseDir. |
| 33 | +let GetRelativePath (baseDir: string) (fullPath: string) = |
| 34 | + let rec go (thisDir: string) = |
| 35 | + if thisDir = baseDir then |
| 36 | + fullPath.[thisDir.Length + 1..] |
| 37 | + elif thisDir.Length <= baseDir.Length then |
| 38 | + invalidArg "fullPath" (sprintf "'%s' is not a subdirectory of '%s'" fullPath baseDir) |
| 39 | + else |
| 40 | + go (Path.GetDirectoryName thisDir) |
| 41 | + go fullPath |
0 commit comments