repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

nushell/modules/package/extractor.nu

Raw
# extracts various types of archives

export def unpack [
	archive: path
	target: path
] {
	# FIXME: maybe it is unnecessary to give type?
	# let extension = $archive | path parse | get extension
	# let type = match $extension {
	# 	'gz' => 'gzip'
	# 	'bz' => 'bzip2'
	# 	'xz' => 'xz'
	# 	_ => { error make { msg: $"Unsupported extension type ($extension)!" } }
	# }
	# run-external tar '--extract' $"--($type)" $"--file=($archive)" $"--directory=($target)" '--strip-components=1'

	# TODO: test on win32
	let files = ( run-external tar '--list' $'--file=($archive)' | lines )
	let uniq_parents = (
		$files
		| path split
		| each { first }
		| uniq
		| length
	)
	let is_single_dir = (($files | length) > 1) and ($uniq_parents) == 1
	let strip = if $is_single_dir { 1 } else { 0 }
	print $'Unpacking ($archive) into ($target) with strip ($strip)'
	run-external tar '--extract' $"--file=($archive)" $"--directory=($target)" $'--strip-components=($strip)'
}