# Copyright © SixtyFPS GmbH <info@slint.dev>
# SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial

name: Upgrade Version Number

on:
    workflow_dispatch:
        inputs:
            new_version:
                description: "The new version number"
                required: true

jobs:
    upgrade_version_number:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v4
            - name: Do replacements
              run: |
                  # Each Cargo.toml need to have the version updated
                  git ls-files | grep Cargo.toml | grep -v helper_crates | xargs sed -i 's/^version = "[0-9]*\.[0-9]*\.[0-9]*"/version = "${{ github.event.inputs.new_version }}"/'
                  # Each dependencies in cargo.toml
                  git ls-files | grep Cargo.toml | xargs sed -i 's/\(slint.*version = \)"=[0-9]*\.[0-9]*\.[0-9]*"/\1"=${{ github.event.inputs.new_version }}"/'

                  # Update the version in CmakeLists.txt
                  sed -i 's/ VERSION [0-9]*\.[0-9]*\.[0-9]*$/ VERSION ${{ github.event.inputs.new_version }}/' api/cpp/CMakeLists.txt
                  sed -i "s/(CPACK_PACKAGE_VERSION_MAJOR [0-9]*)/(CPACK_PACKAGE_VERSION_MAJOR `echo ${{ github.event.inputs.new_version }} | sed "s/\([0-9]*\)\.\([0-9]*\).\([0-9]*\)/\1/"`)/" api/cpp/CMakeLists.txt
                  sed -i "s/(CPACK_PACKAGE_VERSION_MINOR [0-9]*)/(CPACK_PACKAGE_VERSION_MINOR `echo ${{ github.event.inputs.new_version }} | sed "s/\([0-9]*\)\.\([0-9]*\).\([0-9]*\)/\2/"`)/" api/cpp/CMakeLists.txt
                  sed -i "s/(CPACK_PACKAGE_VERSION_PATCH [0-9]*)/(CPACK_PACKAGE_VERSION_PATCH `echo ${{ github.event.inputs.new_version }} | sed "s/\([0-9]*\)\.\([0-9]*\).\([0-9]*\)/\3/"`)/" api/cpp/CMakeLists.txt

                  # The version is also in these files
                  sed -i "s/^version = \"[0-9]*\.[0-9]*\.[0-9]*\"/version = \"${{ github.event.inputs.new_version }}\"/" api/cpp/docs/conf.py docs/reference/conf.py api/python/pyproject.toml

                  # Version in package.json files
                  git ls-files | grep package.json | xargs sed -i 's/"version": ".*"/"version": "${{ github.event.inputs.new_version }}"/'
                  # Update version in Node.js binary package config
                  sed -i 's/"version": ".*"/"version": "${{ github.event.inputs.new_version }}"/' api/node/binaries.json

                  # VersionCheck
                  sed -i "s/VersionCheck_[0-9]*_[0-9]*_[0-9]*;/VersionCheck_`echo ${{ github.event.inputs.new_version }} | sed "s/\([0-9]*\)\.\([0-9]*\).\([0-9]*\)/\1/"`_`echo ${{ github.event.inputs.new_version }} | sed "s/\([0-9]*\)\.\([0-9]*\).\([0-9]*\)/\2/"`_`echo ${{ github.event.inputs.new_version }} | sed "s/\([0-9]*\)\.\([0-9]*\).\([0-9]*\)/\3/"`;/" api/rs/slint/lib.rs

                  # Version in the AboutSlint widget
                  sed -i "s/Version [0-9]*\.[0-9]*\.[0-9]*\\\\n/Version ${{ github.event.inputs.new_version }}\\\\n/" internal/compiler/widgets/common/common.slint

                  # Version in the docs (cargo add slint@<VERSION>):
                  git ls-files | grep "\(^\|/\)docs/.*\.\(md\|rst\)\$" | xargs sed -i 's/slint@[0-9]\+\.[0-9]\+\.[0-9]\+/slint@${{ github.event.inputs.new_version }}/'

                  # Version in esp-idf component
                  sed -i 's/^version: "[0-9]*\.[0-9]*\.[0-9]*.*"/version: "${{ github.event.inputs.new_version }}"/' api/cpp/esp-idf/slint/idf_component.yml
                  sed -i 's/GIT_TAG v[0-9]*\.[0-9]*\.[0-9]*/GIT_TAG v${{ github.event.inputs.new_version }}/' api/cpp/esp-idf/slint/CMakeLists.txt

                  echo "Note that the version is not updated in the documentation and README yet"

            - name: Commit
              run: |
                  git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com"
                  git config --global user.name "${GITHUB_ACTOR}"
                  git commit -a --message "Bump version number to ${{ github.event.inputs.new_version }}"
            - name: Result
              run: |
                  git diff

            - name: Push changes
              uses: ad-m/github-push-action@master
              with:
                  branch: wip/version-bump
