version: 2
steps: &steps
  steps:
    - checkout
    - run:
        name: enable all optional builtins
        command: |
          rm -vf builtins/imports_build/*
          cp -vf builtins/imports_src/* builtins/imports_build/
    - run:
        name: go test
        command: |
          go test ./... -count 1 -race
    - run:
        name: build
        command: |
          go build github.com/lmorg/murex
    - run:
        name: mx tests
        command: |
          ./murex --run-tests
    - run:
        name: behavioural tests
        command: |
          ./murex -c 'g: behavioural/* -> foreach: f { source $f }; try {test: run *}'
    - run:
        name: build all standalone targets
        command: |
          ./murex ./test/build_all_platforms.mx --no-colour
    - run:
        name: build web assembly
        command: |
          GOOS=js GOARCH=wasm go build -o murex.wasm .

env: &env
  environment:
    GOPATH: /go
    MUREX_BUILD_FLAGS: --no-colour
    MUREX_TEST_NO_EXEC_DEPS: true
    MUREX_TEST_SKIP_MAN: true
  
jobs:
  website:
    working_directory: /go/src/github.com/lmorg/murex
    docker:
      - image: ghcr.io/lmorg/murex:ci
        environment:
          GOPATH: /go
          MUREX_BUILD_FLAGS: --no-colour
    steps:
      - run:
          name: Nuke old code
          command: cd .. && rm -rf murex && mkdir murex
      - checkout
      - run:
          name: Build and test murex
          command: test/ci-murex.sh
      - run:
          name: Build and test website
          command: test/ci-website.sh

  codecov:
    working_directory: /go/src/github.com/lmorg/murex
    docker:
      - image: ghcr.io/lmorg/murex:ci
        environment:
          GOPATH: /go
          MUREX_BUILD_FLAGS: --no-colour
    steps:
      - run:
          name: Nuke old code
          command: cd .. && rm -rf murex && mkdir murex
      - checkout
      - run:
          name: Build and test murex
          command: go test ./... -count 1 -race -coverprofile=coverage.txt -covermode=atomic
      - run:
          name: Code Coverage (codecov.io)
          command: bash <(curl -s https://codecov.io/bash)

  latest:
    docker:
      - image: golang:latest
        <<: *env
    working_directory: /go/src/github.com/lmorg/murex
    <<: *steps

  go1.18:
    docker:
      - image: golang:1.18
        <<: *env
    working_directory: /go/src/github.com/lmorg/murex
    <<: *steps

  go1.19:
    docker:
      - image: golang:1.19
        <<: *env
    working_directory: /go/src/github.com/lmorg/murex
    <<: *steps

  go1.20:
    docker:
      - image: golang:1.20
        <<: *env
    working_directory: /go/src/github.com/lmorg/murex
    <<: *steps

workflows:
  version: 2
  build-workflow:
    jobs:
      - website
      - latest
      - go1.18
      - go1.19
      - go1.20
      - codecov
