From 4c7d0db4bcf44eea30bb1899be623aed11d9945c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20H=C3=B6ck?= Date: Thu, 4 Mar 2021 20:09:15 +0100 Subject: [PATCH] [ fix #1148 ] Support hyphenated package names (#1151) --- idris2api.ipkg | 1 + libs/base/Data/List1.idr | 7 ++++ src/Idris/SetOptions.idr | 34 +++++++++++-------- src/Libraries/Data/List1.idr | 14 ++++++++ .../pkg006/depends/bar-baz/bar-baz.ipkg | 2 ++ .../pkg006/depends/foo-bar-1.3.1/foo-bar.ipkg | 2 ++ tests/idris2/pkg006/depends/quux/quux.ipkg | 2 ++ tests/idris2/pkg006/test1.ipkg | 2 +- tests/idris2/pkg006/test2.ipkg | 6 +++- 9 files changed, 53 insertions(+), 17 deletions(-) create mode 100644 src/Libraries/Data/List1.idr create mode 100644 tests/idris2/pkg006/depends/bar-baz/bar-baz.ipkg create mode 100644 tests/idris2/pkg006/depends/foo-bar-1.3.1/foo-bar.ipkg create mode 100644 tests/idris2/pkg006/depends/quux/quux.ipkg diff --git a/idris2api.ipkg b/idris2api.ipkg index 59d032908..310d482e9 100644 --- a/idris2api.ipkg +++ b/idris2api.ipkg @@ -109,6 +109,7 @@ modules = Libraries.Data.LengthMatch, Libraries.Data.List.Extra, Libraries.Data.List.Lazy, + Libraries.Data.List1, Libraries.Data.NameMap, Libraries.Data.SortedMap, Libraries.Data.SortedSet, diff --git a/libs/base/Data/List1.idr b/libs/base/Data/List1.idr index 60c619bc6..22f1818a8 100644 --- a/libs/base/Data/List1.idr +++ b/libs/base/Data/List1.idr @@ -86,6 +86,13 @@ export snoc : (xs : List1 a) -> (x : a) -> List1 a snoc xs x = append xs (singleton x) +public export +unsnoc : (xs : List1 a) -> (List a, a) +unsnoc (h ::: Nil) = (Nil, h) +unsnoc (h ::: (x :: xs)) = + let (ini,lst) = unsnoc (x ::: xs) + in (h :: ini, lst) + ------------------------------------------------------------------------ -- Reverse diff --git a/src/Idris/SetOptions.idr b/src/Idris/SetOptions.idr index 51e368932..02fd36827 100644 --- a/src/Idris/SetOptions.idr +++ b/src/Idris/SetOptions.idr @@ -6,6 +6,7 @@ import Core.Metadata import Core.Options import Core.Unify import Libraries.Utils.Path +import Libraries.Data.List1 as Lib import Idris.CommandLine import Idris.REPL @@ -33,23 +34,26 @@ candidateDirs dname pkg bounds | Left err => pure [] getFiles d [] where - toVersionNum : String -> List Nat - toVersionNum str - = case span (/= '.') str of - (num, rest) => - case strM rest of - StrNil => [stringToNatOrZ num] - StrCons _ rest => - (stringToNatOrZ num :: toVersionNum rest) + toVersion : String -> Maybe PkgVersion + toVersion = map MkPkgVersion + . traverse parsePositive + . forget + . split (== '.') getVersion : String -> (String, PkgVersion) - getVersion str - = case span (/= '-') str of - (name, ver) => case strM ver of - StrNil => (name, MkPkgVersion [0]) - StrCons _ ver => - (name, MkPkgVersion (toVersionNum ver)) - _ => (str, MkPkgVersion [0]) + getVersion str = + -- Split the dir name into parts concatenated by "-" + -- treating the last part as the version number + -- and the initial parts as the package name. + -- For reasons of backwards compatibility, we also + -- accept hyphenated directory names without a part + -- corresponding to a version number. + case Lib.unsnoc $ split (== '-') str of + (Nil, last) => (last, MkPkgVersion [0]) + (init,last) => + case toVersion last of + Just v => (concat $ intersperse "-" init, v) + Nothing => (str, MkPkgVersion [0]) -- Return a list of paths that match the version spec -- (full name, version string) diff --git a/src/Libraries/Data/List1.idr b/src/Libraries/Data/List1.idr new file mode 100644 index 000000000..261b75db3 --- /dev/null +++ b/src/Libraries/Data/List1.idr @@ -0,0 +1,14 @@ +module Libraries.Data.List1 + +import public Data.List1 + +%default total + +-- TODO: Remove this, once Data.List1.unsnoc from base is available +-- to the compiler +export +unsnoc : (xs : List1 a) -> (List a, a) +unsnoc (h ::: Nil) = (Nil, h) +unsnoc (h ::: (x :: xs)) = + let (ini,lst) = Libraries.Data.List1.unsnoc (x ::: xs) + in (h :: ini, lst) diff --git a/tests/idris2/pkg006/depends/bar-baz/bar-baz.ipkg b/tests/idris2/pkg006/depends/bar-baz/bar-baz.ipkg new file mode 100644 index 000000000..b7f21e4eb --- /dev/null +++ b/tests/idris2/pkg006/depends/bar-baz/bar-baz.ipkg @@ -0,0 +1,2 @@ +package bar-baz +version = 1.0.1 diff --git a/tests/idris2/pkg006/depends/foo-bar-1.3.1/foo-bar.ipkg b/tests/idris2/pkg006/depends/foo-bar-1.3.1/foo-bar.ipkg new file mode 100644 index 000000000..7c74b9b8b --- /dev/null +++ b/tests/idris2/pkg006/depends/foo-bar-1.3.1/foo-bar.ipkg @@ -0,0 +1,2 @@ +package foo-bar +version = 1.3.1 diff --git a/tests/idris2/pkg006/depends/quux/quux.ipkg b/tests/idris2/pkg006/depends/quux/quux.ipkg new file mode 100644 index 000000000..9f1c8cc37 --- /dev/null +++ b/tests/idris2/pkg006/depends/quux/quux.ipkg @@ -0,0 +1,2 @@ +package quux +version = 0.0.0 diff --git a/tests/idris2/pkg006/test1.ipkg b/tests/idris2/pkg006/test1.ipkg index f75fe7a04..89e318f34 100644 --- a/tests/idris2/pkg006/test1.ipkg +++ b/tests/idris2/pkg006/test1.ipkg @@ -1,3 +1,3 @@ package test1 -depends = foo, bar +depends = foo, bar, foo-bar, quux, bar-baz diff --git a/tests/idris2/pkg006/test2.ipkg b/tests/idris2/pkg006/test2.ipkg index ba3b4612c..33c179b51 100644 --- a/tests/idris2/pkg006/test2.ipkg +++ b/tests/idris2/pkg006/test2.ipkg @@ -1,4 +1,8 @@ package test2 depends = foo >= 0.5 && < 0.6, - bar >= 1 + bar >= 1, + foo-bar == 1.3.1, + quux == 0, + bar-baz == 0 +