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:
# packages > native package manager  > 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)
		$package = $candidate
	} else {
		print $"Found multiple matches for ($query). Pick one!"
		$package = ($candidates | input list --fuzzy "Which package to install? ")
	}

	let manifest = open $package
	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
	>
] {
	let destination = [(folders programs) $manifest.organisation $manifest.name $manifest.version] | path join
	let bin = $manifest.'relative binary path'
	let source = [$destination $bin] | path join

	if ($destination | path exists) {
		# Relink, in case activate different version
		force_slink $source $bin
		print $'($manifest.name)/($manifest.organisation) already installed. Doing nothing.'
		return
	}
	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
	}

	mkdir $destination
	extractor unpack $artifact_path $destination

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

def force_slink [
	source: path
	target: path
] {
	match ($source | path type) {
		'file' => {
			let target = ([(folders binaries) ($target | path basename)] | path join)
			print $'Linking ($source) to ($target)'
			if ($target | path exists) {
				rm $target
			}
			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)
				print $'Linking ($source) to ($target)'
				if ($target | path exists) {
					rm $target
				}
				slink ($file | path expand) $target
			}
		}
		_ => {
			error make { msg: $"Weird, ($source) is neither file nor directory..." }
		}
	}
}