Files
platforms/scripts/build-apps.sh

91 lines
2.3 KiB
Bash

#!/usr/bin/env bash
set -Eeuo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
OUTPUT_ROOT="${PROJECT_ROOT}/output/apps"
TARGET="${1:-all}"
PLATFORM="${2:-all}"
require_command() {
local command_name="$1"
if ! command -v "${command_name}" >/dev/null 2>&1; then
echo "缺少命令:${command_name}" >&2
exit 1
fi
}
require_value() {
local value_name="$1"
local value="${!value_name:-}"
if [[ -z "${value}" ]]; then
echo "缺少环境变量:${value_name}" >&2
exit 1
fi
}
copy_output() {
local source="$1"
local target="$2"
if [[ "${target}" != "${OUTPUT_ROOT}/"* ]]; then
echo "拒绝清理非预期输出目录:${target}" >&2
exit 1
fi
rm -rf "${target}"
mkdir -p "${target}"
cp -R "${source}/." "${target}/"
}
build_app() {
local app_name="$1"
local app_dir="${PROJECT_ROOT}/apps/${app_name}"
local app_output="${OUTPUT_ROOT}/${app_name}"
echo "开始构建:${app_name}"
(
cd "${app_dir}"
flutter pub get
flutter analyze
flutter test
if [[ "${PLATFORM}" == "all" || "${PLATFORM}" == "android" ]]; then
flutter build apk --release --no-pub --dart-define="API_BASE_URL=${API_BASE_URL}"
mkdir -p "${app_output}"
cp "build/app/outputs/flutter-apk/app-release.apk" "${app_output}/${app_name}-release.apk"
fi
if [[ "${PLATFORM}" == "all" || "${PLATFORM}" == "web" ]]; then
flutter build web --release --no-pub --dart-define="API_BASE_URL=${API_BASE_URL}"
copy_output "${app_dir}/build/web" "${app_output}/web"
fi
)
echo "构建完成:${app_name}"
}
if [[ "${TARGET}" != "all" && "${TARGET}" != "user_app" && "${TARGET}" != "service_app" ]]; then
echo "应用参数只能是 all、user_app 或 service_app。" >&2
exit 1
fi
if [[ "${PLATFORM}" != "all" && "${PLATFORM}" != "android" && "${PLATFORM}" != "web" ]]; then
echo "平台参数只能是 all、android 或 web。" >&2
exit 1
fi
require_command flutter
require_value API_BASE_URL
if [[ "${PLATFORM}" == "all" || "${PLATFORM}" == "web" ]]; then
flutter config --enable-web
fi
mkdir -p "${OUTPUT_ROOT}"
if [[ "${TARGET}" == "all" || "${TARGET}" == "user_app" ]]; then
build_app user_app
fi
if [[ "${TARGET}" == "all" || "${TARGET}" == "service_app" ]]; then
build_app service_app
fi
echo "产物目录:${OUTPUT_ROOT}"