repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

neovim/puk.nu

Raw
# an out-of-band plugin manager for neovim.
# cannot do a lot, compared to "normal" neovim plugin managers, but:
# - it does not depend on git, only on tar and curl (and nushell right now, but that could change)
# - tar and curl should be available on any system that neovim is installed, even win32
# - does not incur runtime costs on neovim
# - uses built-in vim packages
def main [] {
	let data_path = nvim --headless --clean +'lua print(vim.fn.stdpath "data")' +q | complete | get stderr
	let cache_path = nvim --headless --clean +'lua print(vim.fn.stdpath "cache")' +q | complete | get stderr
	if not ($cache_path | path exists) {
		print $"creating cache path: ($cache_path)"
		mkdir $cache_path
	}
	let plugins_path = [ $data_path site pack puk opt ] | path join
	let plugin_specs = open puk.plugins.nuon
	let total = $plugin_specs | length
	print $"Installing plugins from puk.plugins.nuon. total: ($total)"
	let errors = $plugin_specs | par-each { install-spec $in $plugins_path $cache_path } | flatten
	print 'Cleaning plugins, that are not part of puk.plugins.nuon.'
	clean-plugins $plugins_path $plugin_specs
	print 'Cleaning stale cache archives.'
	clean-cache $cache_path $plugin_specs
	# plugins need to be packadded before helptags can be generated
	print 'Generating helptags.'
	mut packs = $plugin_specs | par-each { | spec |
		$"packadd ($spec.name)"
	} | str join "\n"
	nvim -u NONE --headless --cmd $packs --cmd 'helptags ALL' +q
	if not ($errors | is-empty) {
		let failed = $errors | length
		print $"\n($failed)/($total) plugins FAILED:"
		$errors | each { |e| print $"  - ($e.name): ($e.error)" }
		exit 1
	}
}

# check online for fresh commits. Does not actually mutate or update anything.
# updated plugin specs will be written to a new file.
def 'main update' [] {
	print "Checking for fresh commits of plugins online..."
	let plugin_specs = open puk.plugins.nuon

	let updated = $plugin_specs |
	update commit {
		|spec|
		let fresh_commit = find-latest-commit $spec
		if ($fresh_commit != $spec.commit) {
			print $"Found fresh commit for ($spec.name):($fresh_commit)!"
			return $fresh_commit
		}
		return $spec.commit
	}

	if not ($plugin_specs == $updated) {
		$plugin_specs |
		merge $updated |
		to nuon --indent 2 |
		save --force puk.plugins.updated.nuon
		print "Written updated puk.plugins into puk.plugins.updated.nuon!"
	} else {
		print "Nothing to do."
	}
}

# deletes all existing plugins, that are NOT in the specs!
def clean-plugins [
	plugins_path: path # folder to install plugins into
	plugin_specs: list<any> # all known plugins
] {
	ls $plugins_path | each {
		| row |
		let existing_plugin_name = $row.name | path basename
		if ( $plugin_specs | where name == $existing_plugin_name | is-empty) {
			print $"Found plugin, that is not part of the specs: ($existing_plugin_name)"
			rm --trash --recursive $row.name
		}
	}
}

# deletes all cache archives, that are NOT matching any spec's name+commit!
def clean-cache [
	cache_path: path # folder containing cached archives
	plugin_specs: list<any> # all known plugins
] {
	let expected = $plugin_specs | each { |spec| (make-archive-path $spec $cache_path | path basename) }
	try {
		glob $"($cache_path)/*.tar.gz" | each { |path|
			let name = $path | path basename
			if ($expected | where $it == $name | is-empty) {
				print $"Removing stale cache: ($name)"
				rm $path
			}
		}
	} catch {}
}

# given a plugin spec, and a plugins base install directory, this method generates
# a new path, that points to the location the plugin should be installed into.
def make-plugin-path [
	spec: record # a plugin to install
	plugins_path: path # folder to install plugins into
] { [$plugins_path $spec.name] | path join }

# given a path to an installed plugin, this method generates a path to a file holding
# the commit of the plugin. even though git is not really involved in installing
# plugins here, we use the commit as a kind of version number and lock file in one.
def make-commit-file-path [
	plugin_path: path # path to an installed plugin
] { [$plugin_path .commit.puk] | path join }

def plugin-is-installed [
	spec: record # a plugin to install
	plugins_path: path # folder to install plugins into
] {
	let predicted_plugin_path = make-plugin-path $spec $plugins_path
	if not ( $predicted_plugin_path | path exists ) {
		return false
	}
	let commit_file_path = make-commit-file-path $predicted_plugin_path
	if not ($commit_file_path | path exists) {
		return false
	}
	let commit = open --raw $commit_file_path | str trim
	return ($commit == $spec.commit)
}

def find-latest-commit [
	spec: record # a plugin to install
] {
	let feed = generate-commit-feed-url $spec
	let commit = match $spec.source {
		github => {
			try {
				http get --max-time 5sec $feed |
					lines |
					parse --regex "Commit/(?<commit>[a-f0-9]+)" |
					first |
					get commit
			} catch {
				print $"Timed out or failed fetching ($spec.name), keeping current commit."
				$spec.commit
			}
		},
		codeberg => { 'TODO' },
		srht => { 'TODO' },
		_ => { error make {
				msg: 'unknown plugin source given'
				label: {
					text: 'dunno this one'
					span: (metadata $spec.source).span
				}
			}
		}
	}
	$commit
}

def generate-commit-feed-url [
	spec: record # a plugin to install
] {
	let url_format = match $spec.source {
		github => { 'https://github.com/{repo}/commits/{branch}.atom' },
		codeberg => { 'TODO' },
		srht => { 'TODO' },
		_ => { error make {
				msg: 'unknown plugin source given'
				label: {
					text: 'dunno this one'
					span: (metadata $spec.source).span
				}
			}
		}
	}
	$spec | format pattern $url_format
}

def generate-download-url [
	spec: record # a plugin to install
] {
	let url_format = match $spec.source {
		github => { 'https://github.com/{repo}/archive/{commit}.tar.gz' },
		codeberg => { 'https://codeberg.org/{repo}/archive/{commit}.tar.gz' },
		srht => { 'https://git.sr.ht/{repo}/archive/{commit}.tar.gz' },
		_ => { error make {
				msg: 'unknown plugin source given'
				label: {
					text: 'dunno this one'
					span: (metadata $spec.source).span
				}
			}
		}
	}
	$spec | format pattern $url_format
}

def make-archive-path [
	spec: record # a plugin to install
	cache_path: path # folder to cache downloaded archives into
] {
	[ $cache_path $"($spec.name)-($spec.commit).tar.gz" ] | path join
}

def download-plugin [
	spec: record # a plugin to install
	cache_path: path # folder to cache downloaded archives into
] {
	let archive_path = make-archive-path $spec $cache_path
	if ($archive_path | path exists) {
		print $"($spec.name) already cached in ($archive_path)"
		return $archive_path
	}
	let url = generate-download-url $spec
	try {
		http get --raw --max-time 5sec $url | save $archive_path
	} catch {
		print $"Download of ($spec.name) timed out, retrying..."
		http get --raw --max-time 10sec $url | save $archive_path
	}
	$archive_path
}

def unpack-plugin-archive [
	spec: record # a plugin to install
	archive_path: path # path to a plugin archive
] {
	let tmp_path = mktemp --directory
	# hopefully strip-componets works on win32 as well
	print $archive_path $tmp_path
	tar --extract --file $archive_path --directory $tmp_path --strip-components=1

	if $spec.build != null {
		print $"Build step for plugin ($spec.name) detected! Executing now..."
		cd $tmp_path
		run-external $spec.build.0 ...($spec.build | slice 1.. )
	}
	$tmp_path
}

let allowed_shared_lib_type = match ( sys host | get name ) {
	$name if $name =~ 'Windows' => { 'dll' },
	$name if $name =~ 'Linux' => { 'so' },
	$name if $name =~ 'NixOS' => { 'so' },
	$name if $name =~ 'Mac' => { 'dylib' },
	$name if $name =~ 'Pixel' => { 'so' },
	_ => { error make { msg: 'unknown operating system' } },
}
let base_filetypes = [ lua vim scm txt $allowed_shared_lib_type ]

let exclusions = [
	**/tests/**,
	**/test/**,
	**/perf/**,
	**/scripts/**,
	**/fixtures/**,
	**/scrapers/**,
	**/spec/**,
	**/target/release/deps/**
]

def install-plugin [
	spec: record # a plugin to install
	folder_path: path # unpacked path to plugin folder
	plugins_path: path # folder to install plugins into
] {
	let plugin_path = make-plugin-path $spec $plugins_path
	let tmp_path = [$plugins_path $".tmp.($spec.name)"] | path join
	try { rm --recursive $tmp_path }
	mkdir $tmp_path
	cd $folder_path
	let filetypes = if ($spec.extra_extensions? | default null) != null {
		$base_filetypes | append $spec.extra_extensions
	} else {
		$base_filetypes
	}
	glob $"**/*.{($filetypes | str join ',')}" --exclude $exclusions |
	path relative-to (pwd) |
	par-each { | file |
		let rebased_parent = [$tmp_path ($file | path dirname | to text)] | path join
		mkdir $rebased_parent
		cp $file $rebased_parent
	}
	let commit_file_path = make-commit-file-path $tmp_path
	$spec.commit | save --force $commit_file_path
	try { rm --trash --recursive $plugin_path }
	mv $tmp_path $plugin_path
	$plugin_path
}

def install-spec [
	spec: record # a plugin to install
	plugins_path: path # folder to install plugins into
	cache_path: path # folder to cache downloaded archives into
] {
	if (plugin-is-installed $spec $plugins_path) {
		return []
	}
	try {
		print $"Downloading ($spec.name)..."
		let archive_path = download-plugin $spec $cache_path
		print $"Unpacking ($spec.name)..."
		let unpacked_path = unpack-plugin-archive $spec $archive_path
		print $"Installing ($spec.name)..."
		let installed_plugin_path = install-plugin $spec $unpacked_path $plugins_path
		print $"Plugin ($spec.name) installed into ($installed_plugin_path)!"
		return []
	} catch {
		|e|
		print $"FAILED ($spec.name): ($e.msg)"
		return [{ name: $spec.name, error: $e.msg }]
	}
}