repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

tools/packages.nu

Raw
#
# This "package manager" can only isntall single executable files for now.
# TODO: add support for "folder based" software
# TODO: create symlinks
#

let OS = sys host | get name

def main [] {
	let install_path = make-install-path
	let cache_path = make-cache-path 'net.bugabinga.packages'
	let packages = open 'packages.nuon'

	install-packages $install_path $cache_path $packages
	null
}

def make-install-path [] {
	let install_path = $env.TOOLS | path join bin
	mkdir $install_path
	$install_path
}

def make-cache-path [
	name: string # namespace for new cache location in systems cache
] {
	let os = sys host | get name
	let system_location = if $OS =~ "Windows" {
		$env.LOCALAPPDATA
	} else if $os =~ "Linux" or $os =~ "Pixel" {
		$env | get-or-else XDG_CACHE_HOME ($env.HOME | path join .cache)
	} else {
		error make {
			msg: "unsupported operating system detected! initialising self destruct sequence..."
		}
	}
	let cache_path = $system_location | path join $name
	mkdir $cache_path
	$cache_path
}

# Gets value of a column, or falls back to given alternative value.
def get-or-else [
	column: string # name of the column to get its value from
	value: any # alternative value to return, in case `get` gets nothing
]: [
	record -> any
	table -> any
] {
	select -i $column | update $column { if ( $in | is-empty ) { $value } else { $in } } | get $column
}

def install-packages [
	install_path: path # path where to install packages to
	cache_path: path # path where to cache downloaded packed in
	packages: list<any> # packages to install
] {
	$packages | par-each {
		| package |
		if ($package | is-installed-in $install_path $cache_path) {
			print $"Package ($package.name) is already installed."
		} else {
			let artifact = download-package? $package $cache_path
			if $artifact == null {
				print $'Package ($package.name) is not available for current operating system. Skipping it!'
				return
			}
			let executable = unpack-executable $package $artifact
			install-executable $package $executable $install_path
		}
	}
}

def download-package? [
	package: record # package specification
	cache: path # cache path for packages
] {
	let artifact_path = make-artifact-path? $package $cache
	if $artifact_path == null {
		return null
	}
	if ($artifact_path | path exists) {
		return $artifact_path
	} else {
		mkdir ($artifact_path | path dirname)
		let url = make-url? $package
		http get --raw --max-time 30sec $url | save --progress --raw $artifact_path
		return $artifact_path
	}
}

def make-url? [
	package: record # package specification
] {
	let url_format = match $OS {
		"Windows" => { $package.win32_url }
		$os if $os =~ "Linux" => { $package.lnx_url }
		$os if $os =~ "Pixel" => { $package.lnx_url }
		_ => { error make { msg: "unknown operating system" } }
	}
	if $url_format == null {
		return null
	}
	$url_format | str replace --all --regex '\{\s*version\s*\}' $package.version
}

def make-artifact-path? [
	package: record # package specification
	cache: path # cache path for packages
] {
	let url = make-url? $package
	if $url == null {
		return null
	}
	let artifact_parent_path = [ $cache $package.name ] | path join
	let extension = $url | url parse | get path | get-extension
	($artifact_parent_path | path join $package.version) + "." + $extension
}

def get-extension []: path -> string {
	let path = $in | path parse
	if ($path.stem | str ends-with '.tar') {
		return ('tar.' + $path.extension)
	} else {
		return $path.extension
	}
}

def unpack-executable [
	package: record # package specification
	artifact: path # archive or executable
] {
	if ($artifact | is-archive ) {
		let tmpdir = mktemp --directory --tmpdir 'net.bugabinga.packages.XXXXXX'
		run-external ouch '--quiet' decompress '--dir' $tmpdir $artifact
		let exe_glob = '(?i)' + $'($tmpdir)/**/*($package.binary)*' | str replace --all '\' '/' | str replace ':' '\:'
		let executable = glob --no-dir --no-symlink $exe_glob |
		filter { is-executable } |
		first
		return $executable

	} else if ($artifact | is-executable) {
		let tmpdir = mktemp --directory --tmpdir 'net.bugabinga.packages.XXXXXX'
		cp $artifact $tmpdir
		let copy = [ $tmpdir ($artifact | path basename)] | path join
		return $copy
	} else {
		error make {
			msg: "unknown package type"
			label: {
				text: "trying to unpack this artifact failed"
				span: (metadata $artifact).span
			}
		}
	}
}

def is-archive []: path -> bool {
	let magic_headers = [
		0x[FD 37 7A 58 5A 00] # xz
		0x[50 4B]							# ZIP
		0x[1F 8B]							# gzip
		0x[75 73 74 61 72]		# tar
	]
	$in | is-magic-file $magic_headers
}

def is-executable []: path -> bool {
	let magic_headers = [
		0x[7F 45 4C 46]	# elf
		0x[4D 5A]				# DOS MZ
	]
	$in | is-magic-file $magic_headers
}

def is-magic-file [
	magic_headers: list<binary> # list of magic headers to check file against
]: path -> bool {
	let max_size = $magic_headers | each { length } | math max
	let header = open $in | into binary | first $max_size
	$magic_headers | any { |magic| $header | bytes starts-with $magic  }
}

def install-executable [
	package: record # package specification
	executable: path # path to an executable to install
	installs: path # path to all installed packages
] {
	let target = make-target-path $package $installs
	mv --interactive --verbose --update $executable $target
}

def is-installed-in [
	installs: path # path to all installed packages
	cache: path # path to cache of all packages
]: record -> bool {
	let package = $in
	let is_installed = make-target-path $package $installs | path exists
	let artifact_path = make-artifact-path? $package $cache
	if $artifact_path == null {
		return false
	}
	let is_cached = $artifact_path | path exists
	$is_installed and $is_cached
}

def make-target-path [
	package: record # package specification
	installs: path # path to all installed packages
] {
	let target = $installs | path join ($package | get binary | to-executable-name)
	return $target
}

def to-executable-name []: string -> string {
	[ $in (if $OS =~ "Windows" { '.exe' } else { '' } ) ] | str join
}