repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

nushell/modules/package/installer.nu

Raw
# Installs manifests.
# Installign a manifest can mean to download the package and place the files
# into special folders managed by `package.nu`, or delegating to the native package
# manager.
# The precedence of installation method is:
# native package manager > pachages > language package manager

use ./lib.nu *
use ./extractor.nu
use ./folders.nu

export def install [
	query: string
] {
	cd (folders manifests)
	let candidates = ls | where name =~ $"\(?i\)($query)" | get name
	if ($candidates | is-empty) {
		print --stderr $"No manifest for ($query) found!"
		return
	}

	mut package = {}

	if ($candidates | has-one) {
		let candidate = ($candidates | first)
		print $"Found only match for ($query): ($candidate)."
		$package = $candidate
	} else {
		print $"Found multiple matches for ($query). Pick one!"
		$package = ($candidates | input list --fuzzy "Which package to install? ")
	}

	let manifest = open $package | from nuon
	install-manifest $manifest
}

export def install-manifest [
	manifest: record<
		organisation: string
		name: string
		source: string
		version: string
		'version extractor': any
		'relative binary path': path
		homepage: string
		description: string
		'linux x64 url': any
		'linux aarch64 url': any
		'win32 x64 url': any
		'fedora package name': any
		'arch package name': any
		'termux package name': any
		'win32 package name': any
	>
] {
	mut url = match ($nu.os-info.family) {
	'unix' => {
		match ($nu.os-info.arch) {
			'x86_64' => $manifest.'linux x64 url'
			'aarch64' => $manifest.'linux aarch64 url'
			}
		}
	'windows' => $manifest.'win32 x64 url'
	}

	if ($url | is-empty) {
		error make { msg:  $"Manifest for ($manifest.name) coontains no suitable url for ($nu.os-info.name) ($nu.os-info.arch). Aborting." }
	}

	$url = ($url | str replace --regex --all '\{\s*version\s*\}' $manifest.version)

	let artifact_path = [ (folders cache) ($url | path basename) ] | path join
	if (not ($artifact_path | path exists)) {
		http get --raw --max-time 30sec $url | save --progress --raw $artifact_path
	}
	
	let destination = [(folders programs) $manifest.organisation $manifest.name $manifest.version] | path join
	mkdir $destination
	extractor unpack $artifact_path $destination

	let bin = $manifest.'relative binary path'
	if ($bin | is-empty) {
		print $"Manifest for ($manifest.organisation)/($manifest.name) contains to binary path."
		return
	}
	let source = [$destination $bin] | path join
	if ( not ($source | path exists)) {
		error make { msg:  $"Faulty manifest! Relative binary path [($source)] points to non-existent path in ($destination)." }
	}

	match ($source | path type) {
		'file' => {
			let target = ([(folders binaries) $bin] | path join)
			slink $source $target
		}
		'directory' => {
			ls $source
			| where type == 'file'
			| get name
			| filter { is-executable }
			| par-each {
				|file|
				let target = ([(folders binaries) ($file | path basename)] | path join)
				slink ($file | path expand) $target
			}
		}
		_ => {
			error make { msg: $"Weird, ($source) is neither file nor directory..." }
		}
	}
}