module Ribosome.App.Templates.GithubActions where

import Exon (exon)

import Ribosome.App.Data (Branch (Branch), Cachix (Cachix), CachixKey (CachixKey), CachixName (CachixName), ProjectName (ProjectName), cachixName, cachixTek)

cachixStep :: CachixName -> Text
cachixStep :: CachixName -> Text
cachixStep (CachixName Text
name) =
  [exon|
      - uses: cachix/cachix-action@v10
        with:
          name: #{name}
          signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}'|]

cachixConf :: Cachix -> Text
cachixConf :: Cachix -> Text
cachixConf (Cachix (CachixName Text
name) (CachixKey Text
key)) =
  [exon|
            extra-substituters = https://#{name}.cachix.org
            extra-trusted-public-keys = #{key}|]

ga ::
  ProjectName ->
  Maybe Cachix ->
  Text ->
  Text ->
  Text ->
  Text
ga :: ProjectName -> Maybe Cachix -> Text -> Text -> Text -> Text
ga (ProjectName Text
name) Maybe Cachix
cachix Text
actionName Text
push Text
release =
  [exon|---
name: '#{actionName}'

on:
  push:
    #{push}

jobs:
  release:
    name: 'Release'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2.4.0
      - uses: cachix/install-nix-action@v15
        with:
          extra_nix_config: |
            access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}#{cachixConf (fromMaybe cachixTek cachix)}#{foldMap (cachixStep . cachixName) cachix}
      - name: 'build'
        run: nix build .#static
      - uses: 'marvinpinto/action-automatic-releases@latest'
        name: 'create release'
        with:
          repo_token: "${{ secrets.GITHUB_TOKEN }}"
          #{release}
          files: |
            result/bin/#{name}
|]

gaLatest ::
  ProjectName ->
  Branch ->
  Maybe Cachix ->
  Text
gaLatest :: ProjectName -> Branch -> Maybe Cachix -> Text
gaLatest ProjectName
name (Branch Text
branch) Maybe Cachix
cachix =
  ProjectName -> Maybe Cachix -> Text -> Text -> Text -> Text
ga ProjectName
name Maybe Cachix
cachix Text
"latest-release" [exon|branches:
      - '#{branch}'|] [exon|automatic_release_tag: 'latest'
          prerelease: true|]

gaTags ::
  ProjectName ->
  Maybe Cachix ->
  Text
gaTags :: ProjectName -> Maybe Cachix -> Text
gaTags ProjectName
name Maybe Cachix
cachix =
  ProjectName -> Maybe Cachix -> Text -> Text -> Text -> Text
ga ProjectName
name Maybe Cachix
cachix Text
"tagged-release" [exon|tags:
      - '*'|] [exon|prerelease: false|]