version: '3'

vars:
  BUILD_IMAGE_SERVER: golang:1.22
  BUILD_IMAGE_WEB: node:20
  PROJECT_NAME: github.com/flipped-aurora/gin-vue-admin/server
  CONFIG_FILE: config.yaml
  IMAGE_NAME: gva
  REPOSITORY: registry.cn-hangzhou.aliyuncs.com/{{.IMAGE_NAME}}
  TAGS_OPT: latest
  PLUGIN: email

tasks:
  # 容器环境前后端共同打包
  build:
    desc: 容器环境前后端共同打包
    cmds:
      - docker run --name build-local --rm -v {{pwd}}:/go/src/{{.PROJECT_NAME}} -w /go/src/{{.PROJECT_NAME}} {{.BUILD_IMAGE_SERVER}} task build-local

  # 容器环境打包前端
  build-web:
    desc: 容器环境打包前端
    cmds:
      - docker run --name build-web-local --rm -v {{pwd}}:/go/src/{{.PROJECT_NAME}} -w /go/src/{{.PROJECT_NAME}} {{.BUILD_IMAGE_WEB}} task build-web-local

  # 容器环境打包后端
  build-server:
    desc: 容器环境打包后端
    cmds:
      - docker run --name build-server-local --rm -v {{pwd}}:/go/src/{{.PROJECT_NAME}} -w /go/src/{{.PROJECT_NAME}} {{.BUILD_IMAGE_SERVER}} task build-server-local

  # 构建web镜像
  build-image-web:
    desc: 构建web镜像
    cmds:
      - |
        cd web/ && \
        docker build -t {{.REPOSITORY}}/web:{{.TAGS_OPT}} .

  # 构建server镜像
  build-image-server:
    desc: 构建server镜像
    cmds:
      - |
        cd server/ && \
        docker build -t {{.REPOSITORY}}/server:{{.TAGS_OPT}} .

  # 本地环境打包前后端
  build-local:
    desc: 本地环境打包前后端
    cmds:
      - |
        if [ -d "build" ]; then rm -rf build; else echo "OK!"; fi
      - |
        if [ -f "/.dockerenv" ]; then echo "OK!"; else task build-web-local && task build-server-local; fi
      - |
        mkdir -p build && \
        cp -r web/dist build/ && \
        cp server/server build/ && \
        cp -r server/resource build/resource

  # 本地环境打包前端
  build-web-local:
    desc: 本地环境打包前端
    cmds:
      - |
        cd web/ && \
        if [ -d "dist" ]; then rm -rf dist; else echo "OK!"; fi
      - |
        cd web/ && \
        yarn config set registry http://mirrors.cloud.tencent.com/npm/ && \
        yarn install && \
        yarn build

  # 本地环境打包后端
  build-server-local:
    desc: 本地环境打包后端
    cmds:
      - |
        cd server/ && \
        if [ -f "server" ]; then rm -rf server; else echo "OK!"; fi
      - |
        cd server/ && \
        go env -w GO111MODULE=on && \
        go env -w GOPROXY=https://goproxy.cn,direct && \
        go env -w CGO_ENABLED=0 && \
        go mod tidy
      - |
        cd server/ && \
        go build -ldflags "-B 0x$(shell head -c20 /dev/urandom|od -An -tx1|tr -d ' \n') -X main.Version={{.TAGS_OPT}}" -v

  # 打包前后端二合一镜像
  image:
    desc: 打包前后端二合一镜像
    cmds:
      - task: build
      - |
        docker build -t {{.REPOSITORY}}/gin-vue-admin:{{.TAGS_OPT}} -f deploy/docker/Dockerfile .

  # 尝鲜版
  images:
    desc: 尝鲜版 - 构建所有镜像
    cmds:
      - task: build
      - task: build-image-web
      - task: build-image-server
      - |
        docker build -t {{.REPOSITORY}}/all:{{.TAGS_OPT}} -f deploy/docker/Dockerfile .

  # swagger 文档生成
  doc:
    desc: swagger 文档生成
    cmds:
      - |
        cd server && \
        swag init

  # 插件快捷打包
  plugin:
    desc: 插件快捷打包
    vars:
      PLUGIN: '{{.PLUGIN | default "email"}}'
    cmds:
      - |
        if [ -d ".plugin" ]; then rm -rf .plugin; else echo "OK!"; fi && \
        mkdir -p .plugin/{{.PLUGIN}}/{server/plugin,web/plugin}
      - |
        if [ -d "server/plugin/{{.PLUGIN}}" ]; then cp -r server/plugin/{{.PLUGIN}} .plugin/{{.PLUGIN}}/server/plugin/; else echo "OK!"; fi
      - |
        if [ -d "web/src/plugin/{{.PLUGIN}}" ]; then cp -r web/src/plugin/{{.PLUGIN}} .plugin/{{.PLUGIN}}/web/plugin/; else echo "OK!"; fi
      - |
        cd .plugin && \
        zip -r {{.PLUGIN}}.zip {{.PLUGIN}} && \
        mv {{.PLUGIN}}.zip ../ && \
        cd ..

  # 帮助信息
  help:
    desc: 显示帮助信息
    cmds:
      - |
        echo "可用任务:"
        task --list
