Install a new package

Introduced properties

private var _installed: Array[String]

nitc :: CommandInstall :: _installed

Packages installed in this run (identified by the full path)
private fun install_from_git(git_repo: String, name: String, version: nullable String)

nitc :: CommandInstall :: install_from_git

private fun install_package(package_id: String, version: nullable String)

nitc :: CommandInstall :: install_package

Install the package_id at version
private fun install_packages(import_line: String)

nitc :: CommandInstall :: install_packages

Install packages defined by the import_line
private fun installed: Array[String]

nitc :: CommandInstall :: installed

Packages installed in this run (identified by the full path)
private fun installed=(installed: Array[String])

nitc :: CommandInstall :: installed=

Packages installed in this run (identified by the full path)

Redefined properties

redef type SELF: CommandInstall

nitc $ CommandInstall :: SELF

Type of this instance, automatically specialized in every class
redef fun apply(args: Array[String])

nitc $ CommandInstall :: apply

Apply this command consiering the args that follow
redef fun description: String

nitc $ CommandInstall :: description

Command description
redef fun name: String

nitc $ CommandInstall :: name

Short name of the command, specified in the command line
redef fun usage: String

nitc $ CommandInstall :: usage

Short usage description

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
private var _installed: Array[String]

nitc :: CommandInstall :: _installed

Packages installed in this run (identified by the full path)
private fun all_commands=(all_commands: Map[String, Command])

nitc :: Command :: all_commands=

fun apply(args: Array[String])

nitc :: Command :: apply

Apply this command consiering the args that follow
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
init defaultinit(all_commands: Map[String, Command])

nitc :: Command :: defaultinit

abstract fun description: String

nitc :: Command :: description

Command description
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
private fun install_from_git(git_repo: String, name: String, version: nullable String)

nitc :: CommandInstall :: install_from_git

private fun install_package(package_id: String, version: nullable String)

nitc :: CommandInstall :: install_package

Install the package_id at version
private fun install_packages(import_line: String)

nitc :: CommandInstall :: install_packages

Install packages defined by the import_line
private fun installed: Array[String]

nitc :: CommandInstall :: installed

Packages installed in this run (identified by the full path)
private fun installed=(installed: Array[String])

nitc :: CommandInstall :: installed=

Packages installed in this run (identified by the full path)
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
abstract fun name: String

nitc :: Command :: name

Short name of the command, specified in the command line
private intern fun native_class_name: CString

core :: Object :: native_class_name

The class name of the object in CString format.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun print_local_help

nitc :: Command :: print_local_help

Print the help message for this command
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
abstract fun usage: String

nitc :: Command :: usage

Short usage description
package_diagram nitc::CommandInstall CommandInstall nitc::Command Command nitc::CommandInstall->nitc::Command core::Object Object nitc::Command->core::Object ...core::Object ... ...core::Object->core::Object

Ancestors

interface Object

core :: Object

The root of the class hierarchy.

Parents

abstract class Command

nitc :: Command

Command line action, passed after nitpm

Class definitions

nitc $ CommandInstall
# Install a new package
class CommandInstall
	super Command

	redef fun name do return "install"
	redef fun usage do return "nitpm install [package0[=version] [package1 ...]]"
	redef fun description do return "Install packages by name, Git repository address or from the local package.ini"

	# Packages installed in this run (identified by the full path)
	private var installed = new Array[String]

	redef fun apply(args)
	do
		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 IniFile.from_file(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

	# 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 customizable server list
			# TODO parse ini file in memory

			var url = "https://nitlanguage.org/catalog/p/{package_id}.ini"
			var ini_path = "/tmp/{package_id}.ini"

			if verbose then print "Looking for a package description at '{url}'"

			var request = new CurlHTTPRequest(url)
			request.verbose = verbose
			var response = request.download_to_file(ini_path)

			if response isa CurlResponseFailed then
				print_error "Failed to contact the remote server at '{url}': {response.error_msg} ({response.error_code})"
				exit 1
			end

			assert response isa CurlFileResponseSuccess
			if response.status_code == 404 then
				print_error "Package '{package_id}' not found on the server"
				exit 1
			else if response.status_code != 200 then
				print_error "Server side error: {response.status_code}"
				exit 1
			end

			if verbose then
				print "Found a package description:"
				print ini_path.to_path.read_all
			end

			var ini = new IniFile.from_file(ini_path)
			var git_repo = ini["upstream.git"]
			if git_repo == null then
				print_error "Package description invalid, or it does not declare a Git repository"
				exit 1
				abort
			end

			install_from_git(git_repo, package_id, version)
		else
			var name = package_id.git_name
			if name != null and name != "." and not name.is_empty then
				name = name.to_lower
				install_from_git(package_id, name, version)
			else
				print_error "Failed to infer the package name"
				exit 1
			end
		end
	end

	private fun install_from_git(git_repo, name: String, version: nullable String)
	do
		check_git

		var target_dir = nitpm_lib_dir / name
		if version != null then target_dir += "=" + version
		if installed.has(target_dir) then
			# Ignore packages installed in this run
			return
		end
		installed.add target_dir

		if target_dir.file_exists then
			# Warn about packages previously installed,
			# install dependencies anyway in case of a previous error.
			print_error "Package '{name}' is already installed"
		else
			# Actually install it
			var cmd_branch = ""
			if version != null then cmd_branch = "--branch '{version}'"

			var cmd = "git clone --depth 1 {cmd_branch} {git_repo.escape_to_sh} {target_dir.escape_to_sh}"
			if verbose then print "+ {cmd}"

			if "NIT_TESTING".environ == "true" then
				# Silence git output when testing
				cmd += " 2> /dev/null"
			end

			var proc = new Process("sh", "-c", cmd)
			proc.wait

			if proc.status != 0 then
				print_error "Install of '{name}' failed"
				exit 1
			end
		end

		# Recursive install
		var ini = new IniFile.from_file(target_dir/"package.ini")
		var import_line = ini["package.import"]
		if import_line != null then
			install_packages import_line
		end
	end
end
src/nitpm.nit:53,1--204,3