name: Generate files from the domain list

on:
  push:
    branches:
      - master
    paths:
      - domain-list.yml
      - .github/workflows/generate-files.yml

jobs:
  generate-files:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.1.2'

      - name: Generate uBlacklist.txt
        run: |
          cat domain-list.yml | \
          ruby -r yaml -e '
            puts YAML.load(STDIN).map { "*://#{_1["domain"]}/*" }
          ' \
          > uBlacklist.txt

      - name: Generate evidence.md
        run: |
          cat domain-list.yml | \
          ruby -r yaml -e '
            puts "# Why are these listed?"
            puts
            puts "| Domain | Page | Original | Note |"
            puts "| ------ | ---- | -------- | ---- |"
            puts YAML.load(STDIN).map { "| `#{_1["domain"]}` | #{_1["evidence"]} | #{_1["original"]} | #{_1["note"]&.strip} |" }
          ' \
          > evidence.md

      - name: Run contrib/ublock
        working-directory: contrib/ublock
        run: ./generate.sh

      - name: Push generated files
        run: |
          git config user.name github-actions[bot]
          git config user.email github-actions[bot]@users.noreply.github.com
          git add .
          if git commit -m "Generate files by GitHub Actions"; then
            git push
          fi
