repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

neovim/puk.nu

Raw
#!/usr/bin/env nu

# 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
	print 'Installing plugins from puk.plugins.nuon.'
	$plugin_specs | par-each { install-spec $in $plugins_path $cache_path }
	print 'Cleaning plugins, that are not part of puk.plugins.nuon.'
	clean-plugins $plugins_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
}

# 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' [] {
	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 --interactive --trash --recursive $row.name
		}
	}
}

# 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 => {
			# http get $feed | lines | parse --regex "Commit/(?<commit>[a-z0-9]+)" | first | get commit
			curl --silent --location $feed | lines | parse --regex "Commit/(?<commit>[a-z0-9]+)" | first | get 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 download-plugin [
	spec: record # a plugin to install
	cache_path: path # folder to cache downloaded archives into
] {
	let archive_path = [ $cache_path ($spec.name + '.tar.gz') ] | path join
	if ($archive_path | path exists) {
		print $"($spec.name) already cached in ($archive_path)"
		return $archive_path
	}
	let url = generate-download-url $spec
	http get --raw $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 allowed_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
	try { rm --trash --recursive $plugin_path }
	mkdir $plugin_path
	cd $folder_path
	glob $"**/*.{($allowed_filetypes | str join ',')}" --exclude $exclusions |
	path relative-to (pwd) |
	par-each { | file |
		let rebased_parent = [$plugin_path ($file | path dirname | to text)] | path join
		mkdir $rebased_parent
		cp $file $rebased_parent
	}
	let commit_file_path = make-commit-file-path $plugin_path
	$spec.commit | save $commit_file_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) {
		print $"Plugin ($spec.name) is already installed and up-to-date!"
		return
	}
	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)!"
}