-
Notifications
You must be signed in to change notification settings - Fork 0
/
import
executable file
·55 lines (40 loc) · 2.1 KB
/
import
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env bash
set -e
SOURCE_DIR="${BASH_SOURCE[0]%/*}"
TEMPLATE_FILE="${SOURCE_DIR}/template.bash"
IMPORT_FILE="${1:?Missing import file argument.}"
if ! [[ -f "${TEMPLATE_FILE}" ]]; then
echo "could not find template file at '${TEMPLATE_FILE}'"
exit 1
fi
TEMPLATE_CONTENT="$(< "${TEMPLATE_FILE}")"
TEMPLATE_CONTENT="$(echo "${TEMPLATE_CONTENT}" | sed -ne '1,/^# completion function$/p' | sed -e '$d')"
if ! [[ -f "${IMPORT_FILE}" ]]; then
echo "could not find source file at '${IMPORT_FILE}'"
exit 1
fi
IMPORT_CONTENT="$(< "${IMPORT_FILE}")"
IMPORT_CONTENT="$(echo "${IMPORT_CONTENT}" | sed -ne '/^# completion function$/,$p')"
NAMESPACE="$(echo "${IMPORT_CONTENT}" | sed -nEe 's/^# NAMESPACE=([^ $]*)$/\1/p')"
COMMAND="$(echo "${IMPORT_CONTENT}" | sed -nEe 's/^# COMMAND=([^ $]*)$/\1/p')"
EXECUTABLE="$(echo "${IMPORT_CONTENT}" | sed -nEe 's/^# EXECUTABLE="([^"$]*)"$/\1/p')"
if [[ -z "${NAMESPACE}" ]]; then
echo "could not find NAMESPACE in source file or it has junk at the end"
exit 1
fi
if [[ -z "${COMMAND}" ]]; then
echo "could not find COMMAND in source file or it has junk at the end"
exit 1
fi
if [[ -z "${EXECUTABLE}" ]]; then
echo "could not find EXECUTABLE in source file or it has junk at the end"
exit 1
fi
IMPORT_CONTENT="$(echo "${IMPORT_CONTENT}" | sed -e "s/^# NAMESPACE=${NAMESPACE}\$/# NAMESPACE={{bc_namespace}}/")"
IMPORT_CONTENT="$(echo "${IMPORT_CONTENT}" | sed -e "s/^# COMMAND=${COMMAND}\$/# COMMAND={{bc_command}}/")"
IMPORT_CONTENT="$(echo "${IMPORT_CONTENT}" | sed -e "s/^# EXECUTABLE=\"${EXECUTABLE}\"\$/# EXECUTABLE=\"{{bc_executable}}\"/")"
IMPORT_CONTENT="$(echo "${IMPORT_CONTENT}" | sed -e "s/command_current=${COMMAND} /command_current={{bc_command}} /")"
IMPORT_CONTENT="$(echo "${IMPORT_CONTENT}" | sed -e "s/_${NAMESPACE}__time ${EXECUTABLE}/_{{bc_namespace}}__time {{bc_executable}}/")"
IMPORT_CONTENT="$(echo "${IMPORT_CONTENT}" | sed -e "s/_${NAMESPACE}_${COMMAND}/_{{bc_namespace}}_{{bc_command}}/")"
IMPORT_CONTENT="$(echo "${IMPORT_CONTENT}" | sed -e "s/_${NAMESPACE}/_{{bc_namespace}}/")"
echo -e "${TEMPLATE_CONTENT}\n\n${IMPORT_CONTENT}" > "${TEMPLATE_FILE}"