module Ribosome.App.Boot where

import Exon (exon)
import Rainbow (Chunk)

import Ribosome.App.Data (Github (Github), GithubOrg (GithubOrg), GithubRepo (GithubRepo), Global, Project (..))
import Ribosome.App.Error (RainbowError)
import Ribosome.App.TemplateTree (writeTemplateTree)
import Ribosome.App.Templates (bootTemplates)
import Ribosome.App.UserInput (infoMessage, linkChunk, neovimChunk, pathColor, putStderr)

githubMessage :: [Chunk]
githubMessage :: [Chunk]
githubMessage =
  [
    Item [Chunk]
"Github Actions files are in ",
    Chunk -> Chunk
pathColor Chunk
".github/workflows",
    Item [Chunk]
". These will release static binaries for each commit and tag."
  ]

githubBootMessage :: Github -> [Chunk]
githubBootMessage :: Github -> [Chunk]
githubBootMessage (Github (GithubOrg Text
org) (GithubRepo Text
repo)) =
  [
    Item [Chunk]
"When ",
    Item [Chunk]
Chunk
neovimChunk,
    Item [Chunk]
" starts, a binary will be fetched from ",
    Text -> Chunk
linkChunk [exon|github.com/#{org}/#{repo}|],
    Item [Chunk]
" if Nix isn't available."
  ]

secretsUrl :: Github -> Chunk
secretsUrl :: Github -> Chunk
secretsUrl (Github (GithubOrg Text
org) (GithubRepo Text
repo)) =
  Text -> Chunk
linkChunk [exon|https://github.com/#{org}/#{repo}/settings/secrets/actions|]

generateBoot ::
  Members [Stop RainbowError, Embed IO] r =>
  Global ->
  Project ->
  Sem r ()
generateBoot :: forall (r :: EffectRow).
Members '[Stop RainbowError, Embed IO] r =>
Global -> Project -> Sem r ()
generateBoot Global
global Project {Maybe Cachix
Maybe Github
Path Abs Dir
Branch
ProjectNames
$sel:branch:Project :: Project -> Branch
$sel:directory:Project :: Project -> Path Abs Dir
$sel:cachix:Project :: Project -> Maybe Cachix
$sel:github:Project :: Project -> Maybe Github
$sel:names:Project :: Project -> ProjectNames
branch :: Branch
directory :: Path Abs Dir
cachix :: Maybe Cachix
github :: Maybe Github
names :: ProjectNames
..} = do
  Global -> Path Abs Dir -> TemplateTree -> Sem r ()
forall (r :: EffectRow).
Members '[Stop RainbowError, Embed IO] r =>
Global -> Path Abs Dir -> TemplateTree -> Sem r ()
writeTemplateTree Global
global Path Abs Dir
directory (ProjectName
-> Branch -> Maybe Github -> Maybe Cachix -> TemplateTree
bootTemplates (ProjectNames
names ProjectNames
-> Getting ProjectName ProjectNames ProjectName -> ProjectName
forall s a. s -> Getting a s a -> a
^. IsLabel "name" (Getting ProjectName ProjectNames ProjectName)
Getting ProjectName ProjectNames ProjectName
#name) Branch
branch Maybe Github
github Maybe Cachix
cachix)
  Bool -> Sem r () -> Sem r ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Global
global Global -> Getting Bool Global Bool -> Bool
forall s a. s -> Getting a s a -> a
^. IsLabel "quiet" (Getting Bool Global Bool)
Getting Bool Global Bool
#quiet) do
    [Chunk] -> Sem r ()
forall (r :: EffectRow).
Members '[Stop RainbowError, Embed IO] r =>
[Chunk] -> Sem r ()
infoMessage [
      Item [Chunk]
"The ",
      Item [Chunk]
Chunk
neovimChunk,
      Item [Chunk]
" boot file is at ",
      Chunk -> Chunk
pathColor Chunk
"plugin/boot.vim",
      Item [Chunk]
"."
      ]
    [Chunk] -> Sem r ()
forall (r :: EffectRow).
Members '[Stop RainbowError, Embed IO] r =>
[Chunk] -> Sem r ()
infoMessage [Item [Chunk]
"Place the project directory in one of your ", Chunk -> Chunk
pathColor Chunk
"packpath", Item [Chunk]
" directories to start it."]
    Text -> Sem r ()
forall (r :: EffectRow). Member (Embed IO) r => Text -> Sem r ()
putStderr Text
""
    Maybe Github -> (Github -> Sem r ()) -> Sem r ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ Maybe Github
github \ Github
gh -> do
      [Chunk] -> Sem r ()
forall (r :: EffectRow).
Members '[Stop RainbowError, Embed IO] r =>
[Chunk] -> Sem r ()
infoMessage [Chunk]
githubMessage
      [Chunk] -> Sem r ()
forall (r :: EffectRow).
Members '[Stop RainbowError, Embed IO] r =>
[Chunk] -> Sem r ()
infoMessage (Github -> [Chunk]
githubBootMessage Github
gh)
      Bool -> Sem r () -> Sem r ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Maybe Cachix -> Bool
forall a. Maybe a -> Bool
isJust Maybe Cachix
cachix) do
        Text -> Sem r ()
forall (r :: EffectRow). Member (Embed IO) r => Text -> Sem r ()
putStderr Text
""
        [Chunk] -> Sem r ()
forall (r :: EffectRow).
Members '[Stop RainbowError, Embed IO] r =>
[Chunk] -> Sem r ()
infoMessage [Item [Chunk]
"You have to add your Cachix signing key here: ", Github -> Chunk
secretsUrl Github
gh, Item [Chunk]
"."]
        [Chunk] -> Sem r ()
forall (r :: EffectRow).
Members '[Stop RainbowError, Embed IO] r =>
[Chunk] -> Sem r ()
infoMessage [Item [Chunk]
"The secret name should be ", Chunk -> Chunk
pathColor Chunk
"CACHIX_SIGNING_KEY", Item [Chunk]
"."]
        [Chunk] -> Sem r ()
forall (r :: EffectRow).
Members '[Stop RainbowError, Embed IO] r =>
[Chunk] -> Sem r ()
infoMessage [Item [Chunk]
"A key can be created with ", Chunk -> Chunk
pathColor Chunk
"cachix generate-keypair", Item [Chunk]
"."]