nitc :: CommandInstall :: defaultinit
# 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