|
| 1 | +{-# LANGUAGE DeriveAnyClass #-} |
| 2 | +{-# LANGUAGE DerivingVia #-} |
| 3 | +{-# LANGUAGE OverloadedStrings #-} |
| 4 | +{-# LANGUAGE TypeFamilies #-} |
| 5 | + |
| 6 | +-- | Clone a github repository into a cache directory. |
| 7 | +module Foliage.GitClone ( |
| 8 | + gitClone, |
| 9 | + addGitCloneRule, |
| 10 | +) |
| 11 | +where |
| 12 | + |
| 13 | +import Control.Monad (unless) |
| 14 | +import Development.Shake |
| 15 | +import Development.Shake.Classes |
| 16 | +import Development.Shake.FilePath |
| 17 | +import Development.Shake.Rule |
| 18 | +import Foliage.Meta (GitHubRepo, GitHubRev) |
| 19 | +import GHC.Generics (Generic) |
| 20 | + |
| 21 | +data GitClone = GitClone {repo :: GitHubRepo, rev :: GitHubRev} |
| 22 | + deriving (Eq, Generic, NFData) |
| 23 | + |
| 24 | +instance Show GitClone where |
| 25 | + show GitClone{repo, rev} = "gitClone " <> show repo <> " " <> show rev |
| 26 | + |
| 27 | +instance Hashable GitClone |
| 28 | + |
| 29 | +instance Binary GitClone |
| 30 | + |
| 31 | +type instance RuleResult GitClone = FilePath |
| 32 | + |
| 33 | +-- | Clone given repo at given revision into the cache directory and return the working copy path. |
| 34 | +gitClone :: GitHubRepo -> GitHubRev -> Action FilePath |
| 35 | +gitClone repo rev = apply1 GitClone{repo, rev} |
| 36 | + |
| 37 | +-- | Set up the 'GitClone' rule with a cache directory. |
| 38 | +addGitCloneRule |
| 39 | + :: FilePath |
| 40 | + -- ^ Cache directory |
| 41 | + -> Rules () |
| 42 | +addGitCloneRule cacheDir = addBuiltinRule noLint noIdentity run |
| 43 | + where |
| 44 | + run :: BuiltinRun GitClone FilePath |
| 45 | + run GitClone{repo, rev} _old _mode = do |
| 46 | + let path = cacheDir </> "git" </> show repo |
| 47 | + |
| 48 | + alreadyCloned <- doesDirectoryExist path |
| 49 | + unless alreadyCloned $ do |
| 50 | + let url = "https://github.com/" <> show repo <> ".git" |
| 51 | + command_ [] "git" ["clone", "--recursive", url, path] |
| 52 | + |
| 53 | + command_ [Cwd path] "git" ["checkout", show rev] |
| 54 | + command_ [Cwd path] "git" ["submodule", "update"] |
| 55 | + |
| 56 | + return $ RunResult{runChanged = ChangedRecomputeDiff, runStore = "", runValue = path} |
0 commit comments