Skip to content

Commit

Permalink
移除了 通用子模块脚本
Browse files Browse the repository at this point in the history
- 移除了 子模块
- 将 子模块脚本 转到到了 Common 目录中
- 优化了 子模块脚本
  • Loading branch information
ztj1993 committed Nov 6, 2018
1 parent 2ac7468 commit 591e459
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 13 deletions.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

2 changes: 1 addition & 1 deletion Docs/文件说明.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
- `Package` 软件下载的包存在目录
- `Plugin` 插件目录
- `Shell` 脚本存放路径
- `CommonShell` 通用脚本子模块
- `Common` 通用脚本
- `Manage` 管理入口文件子脚本
- `Plugin` 插件通用脚本
50 changes: 50 additions & 0 deletions Shell/Common/BatchCopy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash

###############
# Name: 批量路径复制脚本
# Author: ZhangTianJie
# Email: [email protected]
# Params: <BatchCopyPaths>[ArrayString](SrcPath=>DistPath)
# Return=0: success
# Return=!0: failure, failed index
###############

### 定义帮助文本
if [ "${1}" == "help" ]; then
echo ">>> Params: <BatchCopyPaths>[ArrayString](SrcPath=>DistPath)"
echo ">>> Return=0: success"
echo ">>> Return=!0: failure, failed index"
exit 0
fi

### 设置变量
[ -z "${BatchCopyPaths}" ] && BatchCopyPaths=(${*})

### 判断是否定义了路径
if [ "${BatchCopyPaths[*]}" == "" ]; then
echo ">>>>> Error: the var <BatchCopyPaths> does not exist"
exit 1
fi

### 循环数组
for i in ${!BatchCopyPaths[@]}
do
Path=${BatchCopyPaths[$i]}
SrcPath=${Path%=>*}
DistPath=${Path#*=>}
### 跳过不存在路径
if [ "${SkipNotExist}" == "yes" ]; then
if [ ! -e ${SrcPath%\*} ]; then
continue
fi
if [ ! -e ${DistPath} ]; then
continue
fi
fi
### 复制文件
sudo \cp -fR ${Path%=>*} ${Path#*=>}
if [ $? -ne 0 ]; then
echo ">>>>> Error: copy <${Path}> error"
exit $(($i+1))
fi
done
31 changes: 31 additions & 0 deletions Shell/Common/Function.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

###############
# Name: 获取第一个存在的路径
# Params: <Path Path...> 路径
# author: ZhangTianJie
# email: [email protected]
###############
function getFirstExistPath () {
for Path in ${*}
do
[ -e "${Path}" ] && echo "${Path}" && return 0
done
return 1
}

###############
# Name: 获取字符串数组元素
# Params 1: <String> 字符串
# Params 2: <Delimiter> 定界符
# Params 3: <Index> 索引
# author: ZhangTianJie
# email: [email protected]
###############
function getStringArrayItem () {
Str=${1}
Delimiter=${2}
Index=${3}
Arr=(${Str//$Delimiter/ })
echo ${Arr[$Index]}
}
49 changes: 49 additions & 0 deletions Shell/Common/PathBackup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

###############
# Name: 路径备份
# Author: ZhangTianJie
# Email: [email protected]
# Params: <OriginalPath>(FilePath|DirPath|Required)
# Params: <TargetPath|Optional>(DirPath)
# Params: <PathAlias|Optional>(FileName|DirName)
# Return=0: success
# Return=1: failure
# Output: error message
###############

### 定义帮助文本
if [ "${1}" == "help" ]; then
echo ">>> Params: <OriginalPath>(FilePath|DirPath|Required)"
echo ">>> Params: <TargetPath|Optional>(DirPath)"
echo ">>> Params: <PathAlias|Optional>(FileName|DirName)"
echo ">>> Return=0: success"
echo ">>> Return=1: failure"
echo ">>> Output: error message"
exit
fi

### 设置变量
[ -z "${OriginalPath}" ] && OriginalPath="${1}"
[ -z "${TargetPath}" ] && TargetPath="${2}"
[ -z "${PathAlias}" ] && PathAlias="${3}"

### 处理变量
[ -z "${TargetPath}" ] && TargetPath=$(dirname "${OriginalPath}")
[ -z "${PathAlias}" ] && PathAlias=$(basename "${OriginalPath}")

### 判断变量
if [ ! -f "${OriginalPath}" ] && [ ! -d "${OriginalPath}" ]; then
echo ">>>>> Error: the var <OriginalPath> does not exist"
exit 1
fi

### 备份原始文件
if [ ! -f "${TargetPath}/${PathAlias}.bak" ] && [ ! -d "${TargetPath}/${PathAlias}.bak" ]; then
sudo cp -fR "${OriginalPath}" "${TargetPath}/${PathAlias}.bak"
fi

### 按日期备份
DateTime=`date +%Y-%m-%d-%H-%M-%S`
sudo cp -fR "${OriginalPath}" "${TargetPath}/${PathAlias}.bak.${DateTime}"
[ $? -ne 0 ] && echo ">>>>> Error: backup <${OriginalPath}> error" && exit 1
59 changes: 59 additions & 0 deletions Shell/Common/TplEngine.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash

###############
# Name: 简单模板引擎
# Author: ZhangTianJie
# Email: [email protected]
# Params: <TplContent>[TplString|FilePath|Required]
# Params: <OutFile>[FilePath]
# Return=0: success
# Return=1: failure
# Output: result content OR error message
###############

### 定义帮助文本
if [ "${1}" == "help" ]; then
echo ">>> Params: <TplContent>[TplString|FilePath|Required]"
echo ">>> Params: <OutFile>[FilePath]"
echo ">>> Return=0: success"
echo ">>> Return=1: failure"
echo ">>> Output: result content OR error message"
exit 0
fi

### 设置变量
[ -z "${TplContent}" ] && TplContent="${1}"
[ -z "${TplOutFile}" ] && TplOutFile="${2}"
[ -z "${TplVars}" ] && TplVars=("${@:3}")

### 设置模板变量
if [ "${TplVars[*]}" != "" ]; then
for VarString in ${TplVars[@]}
do
eval ${VarString}
done
fi

### 判断是否设置了模板内容变量
[ -f "${TplContent}" ] && TplContent=$(cat ${TplContent})
[ "${TplContent}" == "" ] && echo ">>>>> Error: the var <TplContent> does not exist" && exit 1

### 替换所有的 $ 为 \$
TplContent=${TplContent//\$/\\\$}

### 将所有的变量和语句中的 \$ 替换 $
TplContent=$(echo "${TplContent}" | sed -E '/<[%|{]=.+?=[%|}]>/ s/\\\$/$/g')

### 替换所有的非语句中的 " 为 \"
TplContent=$(echo "${TplContent}" | sed -e '/^<{=/! s/\"/\\\"/g')

### 将所有的非语句 echo 一下
TplContent=$(echo "${TplContent}" | sed -e '/^<{=/! s/^.*$/echo "&"/')

### 替换掉所有的模板标签
TplContent=$(echo "${TplContent}" | sed -e 's/<{=\|=}>\|<%=\|=%>//g')

### 执行字符串
TplContent=$(eval "${TplContent}")

[ "${TplOutFile}" != "" ] && echo "${TplContent}" | sudo tee ${TplOutFile}
1 change: 0 additions & 1 deletion Shell/CommonShell
Submodule CommonShell deleted from aac148
2 changes: 1 addition & 1 deletion Shell/Plugin/Backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ function BackupSoftware_default(){
BatchCopyPaths=(${BatchCopyPaths[*]} "${ConfigFileSystemd}=>${SoftwarePathBackup}/systemd.conf")
fi
# 复制文件
source ${ProjectPathShell}/CommonShell/BatchCopy.sh
source ${ProjectPathShell}/Common/BatchCopy.sh
}
4 changes: 2 additions & 2 deletions Shell/Plugin/Init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function BackupSoftwareConfigFile_init() {
OriginalPath=${SoftwareConfigFile}
TargetPath=${ProjectPathBackup}/Init
PathAlias=Init.${AliasSoftware}.${AliasConfig}
source ${ProjectPathShell}/CommonShell/PathBackup.sh
source ${ProjectPathShell}/Common/PathBackup.sh
}

### 生成软件配置文件
Expand All @@ -26,7 +26,7 @@ function MakeSoftwareConfigFile_init() {
### 生成文件
TplContent=$(cat ${TplFile})
TplOutFile=${SoftwareConfigFile}
source ${ProjectPathShell}/CommonShell/TplEngine.sh
source ${ProjectPathShell}/Common/TplEngine.sh
}

### 默认初始化软件流程
Expand Down
4 changes: 2 additions & 2 deletions Shell/Plugin/Supervisor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function BackupSoftwareConfigFile_supervisor() {
OriginalPath=${ConfigFileSupervisor}
TargetPath=${ProjectPathBackup}/Supervisor
PathAlias=Supervisor.${AliasSoftware}.${AliasConfig}
source ${ProjectPathShell}/CommonShell/PathBackup.sh
source ${ProjectPathShell}/Common/PathBackup.sh
}

### 生成软件配置文件
Expand All @@ -26,7 +26,7 @@ function MakeSoftwareConfigFile_supervisor() {
### 生成文件
TplContent=$(cat ${TplFile})
TplOutFile=${ConfigFileSupervisor}
source ${ProjectPathShell}/CommonShell/TplEngine.sh
source ${ProjectPathShell}/Common/TplEngine.sh
}

### 默认 Supervisor 流程
Expand Down
4 changes: 2 additions & 2 deletions Shell/Plugin/Systemd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function BackupSoftwareConfigFile_systemd() {
OriginalPath=${ConfigFileSystemd}
TargetPath=${ProjectPathBackup}/Systemd
PathAlias=Systemd.${AliasSoftware}.${AliasConfig}
source ${ProjectPathShell}/CommonShell/PathBackup.sh
source ${ProjectPathShell}/Common/PathBackup.sh
}

### 生成软件配置文件
Expand All @@ -26,7 +26,7 @@ function MakeSoftwareConfigFile_supervisor() {
### 生成文件
TplContent=$(cat ${TplFile})
TplOutFile=${ConfigFileSystemd}
source ${ProjectPathShell}/CommonShell/TplEngine.sh
source ${ProjectPathShell}/Common/TplEngine.sh
}

### 默认 Systemd 流程
Expand Down
2 changes: 1 addition & 1 deletion manage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ProjectPathPlugin=${ProjectPathRoot}/Plugin
ProjectPathShell=${ProjectPathRoot}/Shell

### 引入通用脚本
source ${ProjectPathShell}/CommonShell/Function.sh
source ${ProjectPathShell}/Common/Function.sh

### 定义别名变量
AliasSoftware=${1}
Expand Down

0 comments on commit 591e459

Please sign in to comment.