#!/bin/bash
# dev/package/edit - Package Editing Support

set -e

lib_load 'dev/package/search'
lib_load 'sys/tool/sed'


######
# Library Configuration

# dev_package_edit_config_init() - Defines, documents, and initializes library settings.
dev_package_edit_config_init() {
	# package file callback entrypoints
	local -a entrypoint_common=( config_init config_check )

	lib_setting_arrays package_entrypoint_libs package_entrypoint_tools
	package_entrypoint_libs=( lib_init "${entrypoint_common[@]}" )
	package_entrypoint_tools=( init "${entrypoint_common[@]}" )
}

######
# Package File Manipulations

# package_sed() - Runs ``sed`` to globally replace a pattern ($1) with a
# string ($2).  See ``sed_replace_global()``.
package_sed() {
	local pat=$1
	local str=$2

	local tmp
	tmp=$(cmd_tempfile)

	if ! package_grep "$pat" -l >"$tmp"; then
		debug "$pat: not found"
		return 0
	fi

	[ -s "$tmp" ]

	local -a files
	readarray -t files <"$tmp"
	sed_replace_global "$pat" "$str" -i "${files[@]}"
}


######
# Package Symbol Renaming

# package_symbol_rename() - Renames a single package script symbol.
# $1 - Old symbol
# $2 - New symbol
# $3 - Suffix (optional)
package_symbol_rename() {
	min_max_args 2 3 "$@"
	package_sed "${1}${3:+_}$3" "${2}${3:+_}$3" || true
}


######
# Source File Renaming

# package_file_rename() - Renames package source code files.
# $1 - Kind of package file: `tools`, `libs`
# $2 - Old name
# $3 - New name
package_file_rename() {
	min_args 1 "$@"
	local kind=$1
	shift
	package_file_kind_valid "$kind"

	has_args 2 "$@"
	local old=$1
	local new=$2

	package_file_rename_module "$kind" "$old" "$new"

	local func="package_file_rename_$kind"
	if is_function "$func"; then
		$func "$old" "$new"
	else
		error "$kind: rename: unimplemented for this kind of file"
	fi

	sed_replace "^[\\t ]*$old$" $'\t'"$new" -i "$package_i7"

	local -a files
	files=( $(package_files_sources) "$package_i7" )
	git_add "${files[@]}"
}

# package_file_rename_libs() - Renames a library script.
# $1 - Old name
# $2 - New name
package_file_rename_libs() {
	#; rename library symbols
	package_file_rename_symbols libs "$old" "$new"

	#; update lib_load references
	package_sed '^\(.*\)lib_load '"[\"']$1[\"']" "\\1lib_load '$2'"
}

# package_file_rename_tools() - Renames a tool script.
# $1 - Old name
# $2 - New name
package_file_rename_tools() {
	package_file_rename_module "configs" "$1" "$2"
}

# package_file_rename_module() - Moves a source file in the package
# repository.
# $1 - Kind of package file: `tools`, `libs`
# $2 - Old name
# $3 - New name
package_file_rename_module() {
	local kind=$1
	local old=$2
	local new=$3

	local src dst
	src=$(package_file_name "$kind" "$old")
	dst=$(package_file_name "$kind" "$new")
	debug "$kind: rename '$src' '$dst'"

	[ -f "$src" ] || error "$kind: $old: source file missing: $src"
	[ ! -f "$dst" ] || error "$kind: $new: destination file exists: $dst"

	git_mv "$src" "$dst"
}

# package_file_rename_symbols() - Renames library entrypoint symbols.
# repository.
# $1 - Kind of package file: `tools`, `libs`
# $2 - Old name
# $3 - New name
package_file_rename_symbols() {
	local kind=$1
	local old=$(lib_symbol "$2")
	local new=$(lib_symbol "$3")

	local -a funcs
	eval "funcs=( \"\${package_entrypoint_$kind[@]}\" )"

	local package_grep_file_kind=$kind
	for_each "package_symbol_rename $old $new" "${funcs[@]}"
}

View the Developer Guide Index

View the Reference Manual Index


Generated on Fri Jul 28 14:35:21 PDT 2017 by mcsh d14 v0.23.0.