nitpm: install packages listed in package.ini by default
[nit.git] / src / picnit.nit
index f5e28c8..bac74eb 100644 (file)
@@ -22,56 +22,6 @@ import curl
 
 import picnit_shared
 
-redef class Text
-
-       # Does `self` look like a package name?
-       #
-       # ~~~
-       # assert "gamnit".is_package_name
-       # assert "n1t".is_package_name
-       # assert not ".".is_package_name
-       # assert not "./gamnit".is_package_name
-       # assert not "https://github.com/nitlang/nit.git".is_package_name
-       # assert not "git://github.com/nitlang/nit".is_package_name
-       # assert not "git@gitlab.com:xymus/gamnit.git".is_package_name
-       # assert not "4it".is_package_name
-       # ~~~
-       private fun is_package_name: Bool
-       do
-               if is_empty then return false
-               if not chars.first.is_alpha then return false
-
-               for c in chars do
-                       if not (c.is_alphanumeric or c == '_') then return false
-               end
-
-               return true
-       end
-
-       # Get package name from the Git address `self`
-       #
-       # Return `null` on failure.
-       #
-       # ~~~
-       # assert "https://github.com/nitlang/nit.git".git_name == "nit"
-       # assert "git://github.com/nitlang/nit".git_name == "nit"
-       # assert "gamnit".git_name == "gamnit"
-       # assert "///".git_name == null
-       # assert "file:///".git_name == "file:"
-       # ~~~
-       private fun git_name: nullable String
-       do
-               var parts = split("/")
-               for part in parts.reverse_iterator do
-                       if not part.is_empty then
-                               return part.strip_extension(".git")
-                       end
-               end
-
-               return null
-       end
-end
-
 # Command line action, passed after `picnit`
 abstract class Command
 
@@ -105,17 +55,50 @@ class CommandInstall
        super Command
 
        redef fun name do return "install"
-       redef fun usage do return "picnit install [package or git-repository]"
-       redef fun description do return "Install a package by its name or from a git-repository"
+       redef fun usage do return "picnit install [package0 [package1 ...]]"
+       redef fun description do return "Install packages by name, Git repository address or from the local package.ini"
 
        redef fun apply(args)
        do
-               if args.length != 1 then
-                       print_local_help
-                       exit 1
+               if args.not_empty then
+                       # Install each package
+                       for arg in args do
+                               # Parse each arg as an import string, with versions and commas
+                               install_packages arg
+                       end
+               else
+                       # Install packages from local package.ini
+                       var ini_path = "package.ini"
+                       if not ini_path.file_exists then
+                               print_error "Local `package.ini` not found."
+                               print_local_help
+                               exit 1
+                       end
+
+                       var ini = new ConfigTree(ini_path)
+                       var import_line = ini["package.import"]
+                       if import_line == null then
+                               print_error "The local `package.ini` declares no external dependencies."
+                               exit 0
+                               abort
+                       end
+
+                       install_packages import_line
                end
+       end
 
-               var package_id = args.first
+       # Install packages defined by the `import_line`
+       private fun install_packages(import_line: String)
+       do
+               var imports = import_line.parse_import
+               for name, ext_package in imports do
+                       install_package(ext_package.id, ext_package.version)
+               end
+       end
+
+       # Install the `package_id` at `version`
+       private fun install_package(package_id: String, version: nullable String)
+       do
                if package_id.is_package_name then
                        # Ask a centralized server
                        # TODO choose a future safe URL