version: "3"

vars:
    APP_NAME: "Peachy"
    BIN_DIR: "bin"

tasks:
    ## -------------------------- Build -------------------------- ##

    build:
        summary: Builds the application
        cmds:
            # Build for current OS
            - task: build:{{OS}}

            # Uncomment to build for specific OSes
            # - task: build:linux
            # - task: build:windows
            # - task: build:darwin

    ## ------> Windows <-------

    build:windows:
        summary: Builds the application for Windows
        deps:
            - task: go:mod:tidy
              vars:
                  BUILD_FLAGS: "{{.BUILD_FLAGS}}"
            - task: generate:icons
            - task: generate:syso
              vars:
                  ARCH: "{{.ARCH}}"
        cmds:
            - go build {{.BUILD_FLAGS}} -o {{.BIN_DIR}}/Peachy.exe
        vars:
            BUILD_FLAGS: '{{if eq .PRODUCTION "true"}}-tags production -trimpath -ldflags="-w -s -H windowsgui"{{else}}-gcflags=all="-l"{{end}}'
        env:
            GOOS: windows
            CGO_ENABLED: 0
            GOARCH: "{{.ARCH | default ARCH}}"
            PRODUCTION: '{{.PRODUCTION | default "false"}}'

    build:windows:prod:arm64:
        summary: Creates a production build of the application
        cmds:
            - task: build:windows
              vars:
                  ARCH: arm64
                  PRODUCTION: "true"

    build:windows:prod:amd64:
        summary: Creates a production build of the application
        cmds:
            - task: build:windows
              vars:
                  ARCH: amd64
                  PRODUCTION: "true"

    build:windows:debug:arm64:
        summary: Creates a debug build of the application
        cmds:
            - task: build:windows
              vars:
                  ARCH: arm64

    build:windows:debug:amd64:
        summary: Creates a debug build of the application
        cmds:
            - task: build:windows
              vars:
                  ARCH: amd64

    ## ------> Darwin <-------

    build:darwin:
        summary: Creates a production build of the application
        deps:
            - task: go:mod:tidy
              vars:
                  BUILD_FLAGS: "{{.BUILD_FLAGS}}"
            - task: generate:icons
        cmds:
            - go build {{.BUILD_FLAGS}} -o {{.BIN_DIR}}/{{.APP_NAME}}
        vars:
            BUILD_FLAGS: '{{if eq .PRODUCTION "true"}}-tags production -trimpath -ldflags="-w -s"{{else}}-gcflags=all="-l"{{end}}'
        env:
            GOOS: darwin
            CGO_ENABLED: 1
            GOARCH: "{{.ARCH | default ARCH}}"
            CGO_CFLAGS: "-mmacosx-version-min=10.15"
            CGO_LDFLAGS: "-mmacosx-version-min=10.15"
            MACOSX_DEPLOYMENT_TARGET: "10.15"
            PRODUCTION: '{{.PRODUCTION | default "false"}}'

    build:darwin:prod:arm64:
        summary: Creates a production build of the application
        cmds:
            - task: build:darwin
        vars:
            ARCH: arm64
            PRODUCTION: "true"

    build:darwin:prod:amd64:
        summary: Creates a production build of the application
        cmds:
            - task: build:darwin
        vars:
            ARCH: amd64
            PRODUCTION: "true"

    build:darwin:debug:arm64:
        summary: Creates a debug build of the application
        cmds:
            - task: build:darwin
        vars:
            ARCH: arm64

    build:darwin:debug:amd64:
        summary: Creates a debug build of the application
        cmds:
            - task: build:darwin
        vars:
            ARCH: amd64

    ## ------> Linux <-------

    build:linux:
        summary: Builds the application for Linux
        deps:
            - task: go:mod:tidy
              vars:
                  BUILD_FLAGS: "{{.BUILD_FLAGS}}"
            - task: generate:icons
              vars:
                  ARCH: "{{.ARCH}}"
        cmds:
            - go build {{.BUILD_FLAGS}} -o {{.BIN_DIR}}/Peachy
        vars:
            BUILD_FLAGS: '{{if eq .PRODUCTION "true"}}-tags production -trimpath -ldflags="-w -s"{{else}}-gcflags=all="-l"{{end}}'
        env:
            GOOS: linux
            CGO_ENABLED: 1
            GOARCH: "{{.ARCH | default ARCH}}"
            PRODUCTION: '{{.PRODUCTION | default "false"}}'

    build:linux:prod:arm64:
        summary: Creates a production build of the application
        cmds:
            - task: build:linux
              vars:
                  ARCH: arm64
                  PRODUCTION: "true"

    build:linux:prod:amd64:
        summary: Creates a production build of the application
        cmds:
            - task: build:linux
              vars:
                  ARCH: amd64
                  PRODUCTION: "true"

    build:linux:debug:arm64:
        summary: Creates a debug build of the application
        cmds:
            - task: build:linux
              vars:
                  ARCH: arm64

    build:linux:debug:amd64:
        summary: Creates a debug build of the application
        cmds:
            - task: build:linux
              vars:
                  ARCH: amd64

    ## -------------------------- Package -------------------------- ##

    package:
        summary: Packages a production build of the application into a bundle
        cmds:
            # Package for current OS
            - task: package:{{OS}}

            # Package for specific os/arch
            # - task: package:darwin:arm64
            # - task: package:darwin:amd64
            # - task: package:windows:arm64
            # - task: package:windows:amd64

    ## ------>  Windows <------

    package:windows:
        summary: Packages a production build of the application into a `.exe` bundle
        cmds:
            - task: create:nsis:installer
              vars:
                  ARCH: "{{.ARCH}}"
        vars:
            ARCH: "{{.ARCH | default ARCH}}"

    package:windows:arm64:
        summary: Packages a production build of the application into a `.exe` bundle
        cmds:
            - task: package:windows
              vars:
                  ARCH: arm64

    package:windows:amd64:
        summary: Packages a production build of the application into a `.exe` bundle
        cmds:
            - task: package:windows
              vars:
                  ARCH: amd64

    generate:syso:
        summary: Generates Windows `.syso` file
        dir: build
        cmds:
            - wails3 generate syso -arch {{.ARCH}} -icon icon.ico -manifest wails.exe.manifest -info info.json -out ../wails.syso
        vars:
            ARCH: "{{.ARCH | default ARCH}}"

    create:nsis:installer:
        summary: Creates an NSIS installer
        label: "NSIS Installer ({{.ARCH}})"
        dir: build/nsis
        sources:
            - "{{.ROOT_DIR}}\\bin\\{{.APP_NAME}}.exe"
        generates:
            - "{{.ROOT_DIR}}\\bin\\{{.APP_NAME}}-{{.ARCH}}-installer.exe"
        deps:
            - task: build:windows
              vars:
                  PRODUCTION: "true"
                  ARCH: "{{.ARCH}}"
        cmds:
            - makensis -DARG_WAILS_'{{.ARG_FLAG}}'_BINARY="{{.ROOT_DIR}}\{{.BIN_DIR}}\{{.APP_NAME}}.exe" project.nsi
        vars:
            ARCH: "{{.ARCH | default ARCH}}"
            ARG_FLAG: '{{if eq .ARCH "amd64"}}AMD64{{else}}ARM64{{end}}'

    ## ------>  Darwin <------

    package:darwin:
        summary: Packages a production build of the application into a `.app` bundle
        platforms: [darwin]
        deps:
            - task: build:darwin
              vars:
                  PRODUCTION: "true"
        cmds:
            - task: create:app:bundle

    package:darwin:arm64:
        summary: Packages a production build of the application into a `.app` bundle
        platforms: [darwin/arm64]
        deps:
            - task: package:darwin
              vars:
                  ARCH: arm64

    package:darwin:amd64:
        summary: Packages a production build of the application into a `.app` bundle
        platforms: [darwin/amd64]
        deps:
            - task: package:darwin
              vars:
                  ARCH: amd64

    create:app:bundle:
        summary: Creates an `.app` bundle
        cmds:
            - mkdir -p {{.BIN_DIR}}/{{.APP_NAME}}.app/Contents/{MacOS,Resources}
            - cp build/icons.icns {{.BIN_DIR}}/{{.APP_NAME}}.app/Contents/Resources
            - cp {{.BIN_DIR}}/{{.APP_NAME}} {{.BIN_DIR}}/{{.APP_NAME}}.app/Contents/MacOS
            - cp build/Info.plist {{.BIN_DIR}}/{{.APP_NAME}}.app/Contents

    ## ------>  Linux <------

    package:linux:
        summary: Packages a production build of the application for Linux
        platforms: [linux]
        deps:
            - task: build:linux
              vars:
                  PRODUCTION: "true"
        cmds:
            - task: create:appimage

    create:appimage:
        summary: Creates an AppImage
        dir: build/appimage
        platforms: [linux]
        deps:
            - task: build:linux
              vars:
                  PRODUCTION: "true"
            - task: generate:linux:dotdesktop
        cmds:
            # Copy binary + icon to appimage dir
            - cp {{.APP_BINARY}} {{.APP_NAME}}
            - cp ../appicon.png appicon.png
            # Generate AppImage
            - wails3 generate appimage -binary {{.APP_NAME}} -icon {{.ICON}} -desktopfile {{.DESKTOP_FILE}} -outputdir {{.OUTPUT_DIR}} -builddir {{.ROOT_DIR}}/build/appimage
        vars:
            APP_NAME: "{{.APP_NAME}}"
            APP_BINARY: "../../bin/{{.APP_NAME}}"
            ICON: "../appicon.png"
            DESKTOP_FILE: "{{.APP_NAME}}.desktop"
            OUTPUT_DIR: "../../bin"

    generate:linux:dotdesktop:
        summary: Generates a `.desktop` file
        dir: build
        sources:
            - "appicon.png"
        generates:
            - "{{.ROOT_DIR}}/build/appimage/{{.APP_NAME}}.desktop"
        cmds:
            - mkdir -p {{.ROOT_DIR}}/build/appimage
            # Run `wails3 generate .desktop -help` for all the options
            - wails3 generate .desktop -name "{{.APP_NAME}}" -exec "{{.EXEC}}" -icon "{{.ICON}}" -outputfile {{.ROOT_DIR}}/build/appimage/{{.APP_NAME}}.desktop -categories "{{.CATEGORIES}}"
            # -comment "A comment"
            # -terminal "true"
            # -version "1.0"
            # -genericname "Generic Name"
            # -keywords "keyword1;keyword2;"
            # -startupnotify "true"
            # -mimetype "application/x-extension1;application/x-extension2;"

        vars:
            APP_NAME: "{{.APP_NAME}}"
            EXEC: "{{.APP_NAME}}"
            ICON: "appicon"
            CATEGORIES: "Development;"
            OUTPUTFILE: "{{.ROOT_DIR}}/build/appimage/{{.APP_NAME}}.desktop"

    ## -------------------------- Misc -------------------------- ##

    generate:icons:
        summary: Generates Windows `.ico` and Mac `.icns` files from an image
        dir: build
        sources:
            - "appicon.png"
        generates:
            - "icons.icns"
            - "icons.ico"
        cmds:
            # Generates both .ico and .icns files
            - wails3 generate icons -input appicon.png

    go:mod:tidy:
        summary: Runs `go mod tidy`
        internal: true
        generates:
            - go.sum
        sources:
            - go.mod
        cmds:
            - go mod tidy

    # ----------------------- dev ----------------------- #

    run:
        summary: Runs the application
        cmds:
            - task: run:{{OS}}

    run:windows:
        cmds:
            - '{{.BIN_DIR}}\\{{.APP_NAME}}.exe'

    run:linux:
        cmds:
            - "{{.BIN_DIR}}/{{.APP_NAME}}"

    run:darwin:
        cmds:
            - "{{.BIN_DIR}}/{{.APP_NAME}}"

    dev:
        summary: Runs the application in development mode
        cmds:
            - wails3 dev -config ./build/devmode.config.yaml -port 9245

    dev:reload:
        summary: Reloads the application
        cmds:
            - task: run
