make relativePath more robust (#115)

This commit is contained in:
Timothee Cour 2019-02-08 14:48:24 -08:00 • committed by genotrance
commit 5e212ce606
3 changed files with 26 additions and 12 deletions

View file

@ -4,4 +4,4 @@ Module that should import everything so that `nim doc --project nimtero/all` run
# TODO: make sure it does import everything.
import "."/[cimport, git, types, plugin]
import "."/[cimport, git, types, plugin, compat]

24
nimterop/compat.nim Normal file
View file

@ -0,0 +1,24 @@
#[
module for backward compatibility
put everything that requires `when (NimMajor, NimMinor, NimPatch)` here
]#
import os
when (NimMajor, NimMinor, NimPatch) >= (0, 19, 9):
export relativePath
else:
import std/[ospaths,strutils]
proc relativePath*(file, base: string): string =
## naive version of `os.relativePath` ; remove after nim >= 0.19.9
runnableExamples:
import ospaths, unittest
check:
"/foo/bar/baz/log.txt".unixToNativePath.relativePath("/foo/bar".unixToNativePath) == "baz/log.txt".unixToNativePath
"foo/bar/baz/log.txt".unixToNativePath.relativePath("foo/bar".unixToNativePath) == "baz/log.txt".unixToNativePath
var base = base.normalizedPath
var file = file.normalizedPath
if not base.endsWith DirSep: base.add DirSep
doAssert file.startsWith base
result = file[base.len .. ^1]

View file

@ -1,6 +1,6 @@
import macros, os, osproc, regex, strformat, strutils
import "."/paths
import "."/[paths, compat]
proc execAction*(cmd: string, nostderr=false): string =
var
@ -49,16 +49,6 @@ proc cpFile*(source, dest: string, move=false) =
proc mvFile*(source, dest: string) =
cpFile(source, dest, move=true)
when (NimMajor, NimMinor, NimPatch) < (0, 19, 9):
proc relativePath*(file, base: string): string =
## naive version of `os.relativePath` ; remove after nim >= 0.19.9
runnableExamples:
doAssert "/foo/bar/baz/log.txt".relativePath("/foo/bar") == "baz/log.txt"
var base = base
if not base.endsWith "/": base.add "/"
doAssert file.startsWith base
result = file[base.len .. ^1]
proc extractZip*(zipfile, outdir: string) =
var cmd = "unzip -o $#"
if defined(Windows):