version: 2
defaults: &defaults
    machine:
        image: circleci/classic:201711-01

    working_directory: ~/glowstone

    environment:
        JAVA_TOOL_OPTIONS: -Xmx1024m -XX:+AggressiveOpts -XX:+UseG1GC
jobs:
    build:
        <<: *defaults
        steps:
            - run: mvn --version
            - run: java -version

            - checkout

            - restore_cache:
                keys:
                # Use cache for this pom
                - glowstone-{{ checksum "pom.xml" }}
                # Fallback to latest cache is an exact match is not found
                - glowstone-

            - run: mvn -T 1C -B dependency:resolve-plugins dependency:go-offline

            - save_cache:
                paths:
                    - ~/.m2
                key: glowstone-{{ checksum "pom.xml" }}

            - run: mvn -T 1C -B package

            - store_test_results:
                path: target/surefire-reports

            - store_artifacts:
                path: target/glowstone.jar
                destination: glowstone.jar

    build-113-hack:
        <<: *defaults
        steps:
            - run: mvn --version
            - run: java -version

            - checkout

            - run: |
                git config --global user.name "Glowstone Build"
                git config --global user.email "example@glowstone.net"
                git clone https://github.com/GlowstoneMC/Glowkit.git
                cd Glowkit
                git checkout 1.13-hack
                ./glowkit p
                cd Glowkit-Patched
                mvn install

            - restore_cache:
                keys:
                # Use cache for this pom
                - glowstone-{{ checksum "pom.xml" }}
                # Fallback to latest cache is an exact match is not found
                - glowstone-

            - run: mvn -T 1C -B dependency:go-offline

            - save_cache:
                paths:
                    - ~/.m2
                key: glowstone-{{ checksum "pom.xml" }}

            - run: mvn -T 1C -B package

            - store_test_results:
                path: target/surefire-reports

            - store_artifacts:
                path: target/glowstone.jar
                destination: glowstone.jar

    build-deploy:
        <<: *defaults
        steps:
            - checkout

            - restore_cache:
                keys:
                # Use cache for this pom
                - glowstone-{{ checksum "pom.xml" }}
                # Fallback to latest cache is an exact match is not found
                - glowstone-

            - run: mvn -T 1C -B dependency:resolve-plugins dependency:go-offline

            - save_cache:
                paths:
                    - ~/.m2
                key: glowstone-{{ checksum "pom.xml" }}

            # Ensure we are on the right repo before attempting a deploy
            - run: |
                if [ "${CIRCLE_PROJECT_USERNAME}" == "GlowstoneMC" ]; then
                  mvn -T 1C -B -s .circleci/maven.xml source:jar javadoc:jar deploy
                else
                  mvn -T 1C -B package
                fi

            - store_test_results:
                path: target/surefire-reports

            - store_artifacts:
                path: target/glowstone.jar
                destination: glowstone.jar

            - store_artifacts:
                path: target/glowstone-javadoc.jar
                destination: glowstone-javadoc.jar

            - store_artifacts:
                path: target/glowstone-sources.jar
                destination: glowstone-sources.jar
workflows:
    version: 2

    default-branch-build:
        jobs:
            - build-deploy:
                filters:
                    branches:
                        only: dev

    other-branch-build:
        jobs:
            - build:
                filters:
                    branches:
                        ignore:
                            - dev
                            - 1.13-hack

    other-branch-build-113-hack:
        jobs:
            - build-113-hack:
                filters:
                    branches:
                        only: 1.13-hack
