version: 2.1
executors:
  node-executor:
    docker:
      - image: cimg/node:20.2.0
jobs:
  build:
    executor: node-executor
    steps:
      - checkout
      - run: git submodule sync
      - run: git submodule update --init --recursive
      # 创建必要的目录
      - run: mkdir -p dist
      # 首先在根目录安装依赖
      - run: yarn install
      # 然后进入 gui 目录安装依赖并构建
      - run: |
          cd gui
          yarn install
          yarn vscode
          cd ..
      - persist_to_workspace:
          root: .
          paths:
            - .
  test:
    executor: node-executor
    steps:
      - attach_workspace:
          at: .
      - run:
          name: Install Python
          command: |
            sudo apt-get update
            sudo apt-get install -y python3 python3-pip
      - run:
          name: update workflows
          command: |
            export PYTHONPATH="$PYTHONPATH:$(pwd)/tools/site-packages"
            mkdir -p ~/.chat/workflows
            git clone https://github.com/devchat-ai/workflows.git ~/.chat/workflows/sys
            cd ~/.chat/workflows/sys
      - run:
          name: Run workflow tests
          command: |
            export PYTHONPATH="$PYTHONPATH:$(pwd)/tools/site-packages"
            git config --global user.email "tester@merico.dev"
            git config --global user.name "tester"
            python3 test/workflows/workflow_test.py
      - persist_to_workspace:
          root: .
          paths:
            - .
  publish:
    executor: node-executor
    steps:
      - attach_workspace:
          at: .
      - run:
          name: "Update version in package.json"
          command: |
            sed -i "s/\"version\": \".*\",/\"version\": \"${CIRCLE_TAG:1}\",/" package.json
      - run:
          name: "Publish to Marketplace"
          command: npx vsce publish -p $VSCE_TOKEN --allow-star-activation --pre-release
workflows:
  version: 2
  build-and-publish:
    jobs:
      - build:
          filters:
            tags:
              only: /.*/
            branches:
              ignore: []
      - test:
          requires:
            - build
          filters:
            tags:
              only: /.*/
            branches:
              ignore: /.*/
      - publish:
          requires:
            - build
          filters:
            tags:
              only: /.*/
            branches:
              ignore: /.*/