222
schangxiang@126.com
2025-05-08 961b4e2cd6d9621b5090fc3477615b649bb05737
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
 
# 定义默认组件数组
default_widget_Array=()
 
# 读取 .build.prod 文件内容
if [[ -f ./.build.prod ]]; then
    while IFS= read -r line || [ -n "$line" ]; do
        default_widget_Array+=("$line")
    done < ./.build.prod
fi
# 从 .build 文件读取内容
buildFile="./.build"
 
build_widget_Array=()
if [ -f "$buildFile" ]; then
    while IFS= read -r line || [ -n "$line" ]; do
        build_widget_Array+=("$line")
    done < "$buildFile"
fi
if [ -f "$buildFile.local" ]; then
    while IFS= read -r line || [ -n "$line" ]; do
        build_widget_Array+=("$line")
    done < "$buildFile.local"
fi
 
# 检查并创建 src/widgets 目录
basePath="src/widgets"
if [ ! -d "$basePath" ]; then
    echo "目录 $basePath 不存在。正在创建..."
    mkdir -p "$basePath"
fi
 
# 定义一个函数来处理组件
process_widget() {
    local widgetName=$1
    local gitUrl=$2
    local widgetPath="$basePath/$widgetName"
 
    if [ -d "$widgetPath" ]; then
        # 如果文件夹存在,进入该文件夹并执行 git pull
        echo "目录 $widgetPath 已存在。正在拉取最新更改..."
        cd "$widgetPath" || exit
 
        # 检查是否设置了跟踪分支
        branch_name=$(git rev-parse --abbrev-ref HEAD)
        upstream_branch=$(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null)
 
        if [ -z "$upstream_branch" ]; then
            echo "当前分支 $branch_name 没有设置上游分支。正在设置上游分支为 origin/$branch_name..."
            git branch --set-upstream-to=origin/$branch_name
        fi
 
        # 检查是否有未提交的更改
        stash_result=1
        if [ -n "$(git status --porcelain)" ]; then
            echo "检测到未提交的更改。正在暂存更改..."
            git stash push -m "在拉取 $widgetName 更改之前自动暂存"
            stash_result=$?
        fi
 
        # 尝试执行 git pull
        git pull --ff-only
        pull_result=$?
 
        # 如果有 stash,则尝试恢复
        if [ "$stash_result" -eq 0 ]; then
            echo "正在恢复暂存的更改..."
            git stash pop
        fi
 
        cd - || exit
 
        # 检查 pull 的结果
        if [ "$pull_result" -ne 0 ]; then
            echo "$widgetName 的 git pull 失败。请手动解决冲突。"
        fi
    else
        # 如果文件夹不存在,执行 git clone
        echo "目录 $widgetPath 不存在。正在克隆仓库..."
        git clone "$gitUrl" "$widgetPath"
    fi
}
 
# 处理默认组件数组
for widgetName in "${default_widget_Array[@]}"; do
    gitUrl="https://gitlab.syc-cms.com/lmes-plugin/web/$widgetName.git"
    process_widget "$widgetName" "$gitUrl"
done
 
# 处理 .build 文件中的组件数组
for widgetName in "${build_widget_Array[@]}"; do
    gitUrl="https://gitlab.syc-cms.com/lmes-plugin/web/business/$widgetName.git"
    process_widget "$widgetName" "$gitUrl"
done