name: format

on:
    #    workflow_dispatch:
    #    schedule:
    #        - cron: "42 19 * * *"
    push:
        branches: ["master"]
jobs:
    update-dependencies:
        runs-on: ubuntu-latest
        strategy:
            matrix:
                node-version: [18.x]
        steps:
            - uses: actions/checkout@v3

            - uses: actions/cache@v3
              with:
                  path: |
                      ~/.deno
                      ~/.cache/deno
                  key: ${{ runner.os }}-deno-${{ hashFiles('**/deps.ts') }}-${{ hashFiles('**/import_map.json') }}
            - uses: denoland/setup-deno@v1
              with:
                  deno-version: v1.32.1
            - name: download lua-format
              run: |
                  mkdir -p ./temp/lua-format
                  wget -O ./temp/lua-format/lua-format https://cdn.jsdelivr.net/gh/Koihik/vscode-lua-format@master/bin/linux/lua-format
                  chmod 777 ./temp/lua-format/lua-format
            - run: deno run -A lua-format.ts --executable=./temp/lua-format/lua-format
            - name: download llvm 16
              run: |
                  mkdir -p ./temp/llvm
                  wget -O ./temp/llvm/llvm.sh https://apt.llvm.org/llvm.sh
                  chmod +x ./temp/llvm/llvm.sh
                  sudo ./temp/llvm/llvm.sh 16
            - name: download clang-formatter
              run: |
                  mkdir -p ./temp
                  wget -O ./temp/clang-formatter_Linux_x86_64.tar.gz  https://github.com/masx200/clang-formatter/releases/download/Continuous-integration-15fd2be5fd7385143d90a352d1d9160cfb805bdf/clang-formatter_Linux_x86_64.tar.gz
                  mkdir -p -v ./temp/clang-formatter
                  tar zxvf ./temp/clang-formatter_Linux_x86_64.tar.gz -C ./temp/clang-formatter

            - name: clang-formatter
              continue-on-error: true
              run: ./temp/clang-formatter/clang-formatter
            - name: Set up Go
              uses: actions/setup-go@v3

              with:
                  go-version: 1.20.4
                  cache: true
            - run: deno task cache
            - run: deno fmt

            - run: go fmt ./...

            - name: Set up JDK 19
              uses: actions/setup-java@v3
              with:
                  java-version: "19"
                  distribution: "temurin"
                  cache: maven

            - name: download ktlint
              run: |
                  mkdir -p ./temp/ktlint
                  wget -O ./temp/ktlint/ktlint.jar https://github.com/pinterest/ktlint/releases/download/0.49.0/ktlint

            - name: ktlint
              continue-on-error: true
              run: java --add-opens=java.base/java.lang=ALL-UNNAMED -jar "./temp/ktlint/ktlint.jar" -F

            #            - name: Update dependencies

            #              run: deno run -A https://deno.land/x/udd/main.ts deps.ts *.ts '*/*.ts'  '*.json'
            - name: Use Node.js ${{ matrix.node-version }}
              uses: actions/setup-node@v3
              with:
                  node-version: ${{ matrix.node-version }}
            - run: |
                  npm i -g cnpm
                  npx cnpm  i
                  npm run format
            - name: Create Pull Request
              uses: peter-evans/create-pull-request@v3
              id: pr
              with:
                  commit-message: "format"
                  title: format
                  body: >
                      format
                  branch: format-updates
                  author: GitHub <noreply@github.com>
                  delete-branch: true
            - name: Retrieve commit sha
              id: commit
              run: |
                  echo "::set-output name=sha::$(git rev-parse HEAD)"
            - name: Set commit status with pending
              uses: Sibz/github-status-action@v1
              with:
                  authToken: ${{ secrets.GITHUB_TOKEN }}
                  context: "Basic tests"
                  state: "pending"
                  sha: ${{ steps.commit.outputs.sha }}
            #- name: Basic tests
              #id: test
              #continue-on-error: true
              #run: |
                  #go test ./... -v
                  #deno task test
                  #mvn test --file pom.xml
            - name: Set commit status with outcome
              uses: Sibz/github-status-action@v1
              with:
                  authToken: ${{ secrets.GITHUB_TOKEN }}
                  context: "Basic tests"
                  description: "To run other CI actions close/reopen this PR"
                  state: ${{ steps.test.outcome }}
                  sha: ${{ steps.commit.outputs.sha }}
