1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Services related to the Nit package manager
18 # Folder where are downloaded nitpm packages
19 fun nitpm_lib_dir
: String
21 var dir
= "NITPM_PATH".environ
22 if not dir
.is_empty
then return dir
24 return "HOME".environ
/ ".local/lib/nit/"
29 # Does `self` look like a package name?
32 # assert "gamnit".is_package_name
33 # assert "n1t".is_package_name
34 # assert not ".".is_package_name
35 # assert not "./gamnit".is_package_name
36 # assert not "https://github.com/nitlang/nit.git".is_package_name
37 # assert not "git://github.com/nitlang/nit".is_package_name
38 # assert not "git@gitlab.com:xymus/gamnit.git".is_package_name
39 # assert not "4it".is_package_name
41 fun is_package_name
: Bool
43 if is_empty
then return false
44 if not chars
.first
.is_alpha
then return false
47 if not (c
.is_alphanumeric
or c
== '_') then return false
53 # Get package name from the Git address `self`
55 # Return `null` on failure.
58 # assert "https://github.com/nitlang/nit.git".git_name == "nit"
59 # assert "git://github.com/nitlang/nit".git_name == "nit"
60 # assert "gamnit".git_name == "gamnit"
61 # assert "///".git_name == null
62 # assert "file:///".git_name == "file:"
64 fun git_name
: nullable String
66 var parts
= split
("/")
67 for part
in parts
.reverse_iterator
do
68 if not part
.is_empty
then
69 return part
.strip_extension
(".git")
76 # Parse the external package declaration, as declared in package.ini
78 # Return a map of `ExternalPackage` organized by the short package name,
79 # as used in imports from Nit code.
80 fun parse_import
: Map[String, ExternalPackage]
82 var res
= new Map[String, ExternalPackage]
83 var ids
= self.split
(",")
86 if id
.is_empty
then continue
88 # Check version suffix (e.g. gamnit=1.0)
89 var match
= id
.search_last
("=")
93 # There's a version suffix
94 package_name
= id
.substring
(0, match
.from
)
95 version
= id
.substring_from
(match
.after
)
101 # Extract a package name from a Git address
102 if not package_name
.is_package_name
then
103 # Assume it's a Git repository
104 var git_name
= package_name
.git_name
105 if git_name
== null then
107 # TODO report error only when used by the parser
110 package_name
= git_name
113 res
[package_name
] = new ExternalPackage(id
, package_name
, version
)
119 # Reference to a nitpm package
120 class ExternalPackage
122 # Package identifier (name or Git address), without the version
125 # Standard Nit package name, as used in importations from Nit
128 # Version string of the package
129 var version
: nullable String
131 # Expected folder name for this package
132 var dir_name
: String is lazy
do
133 var version
= version
134 if version
== null then return name
135 return name
+ "=" + version