Giter Site home page Giter Site logo

Comments (3)

chris-martin avatar chris-martin commented on August 29, 2024 1

Yes! This will be a great practical followup to threads.

I wonder if we could introduce a library or two to help condense this into a smaller example. Maybe path and path-io would take care of some of the path manipulation and directory searching?

from haskell-phrasebook.

friedbrice avatar friedbrice commented on August 29, 2024

Cool. I'll look into path and path-io and see if i can clean this up.

from haskell-phrasebook.

friedbrice avatar friedbrice commented on August 29, 2024

How's this?

List Git repositories:

{-# LANGUAGE QuasiQuotes #-}

module ListRepos (listRepos, paths) where

import Control.Monad (filterM, join)
import Data.Foldable (traverse_)
import Path (Path, Abs, Rel, Dir, reldir, (</>))
import Path.IO (doesDirExist, getHomeDir, listDir)

paths :: [Path Rel Dir]
paths = [ [reldir|abs/aur|]
        , [reldir|friedbrice|]
        , [reldir|lumi-tech|]
        ]

isGitRepo :: Path Abs Dir -> IO Bool
isGitRepo dir = doesDirExist (dir </> [reldir|.git|])

listRepos :: Path Abs Dir -> IO [Path Abs Dir]
listRepos parentdir = do
    (subdirs, _) <- listDir parentdir
    filterM isGitRepo subdirs

main :: IO ()
main = do
    home <- getHomeDir
    let fullPaths = map (home </>) paths
    repos <- fmap join (traverse listRepos fullPaths)
    traverse_ print repos

Concurrently fetch Git repositories:

module FetchRepos where

import Control.Concurrent.Async (mapConcurrently)
import Control.Monad (join)
import Path (Path, Abs, Dir, toFilePath, (</>))
import Path.IO (getHomeDir)
import System.Exit (ExitCode(ExitSuccess))
import System.Process ( CreateProcess(cwd)
                      , createProcess
                      , shell
                      , waitForProcess
                      )

import ListRepos (listRepos, paths)

fetchRepo :: Path Abs Dir -> CreateProcess
fetchRepo dir =
    (shell "git fetch --prune --all")
    { cwd = Just (toFilePath dir) }

concurrentlyRetryForever :: [CreateProcess] -> IO ()
concurrentlyRetryForever procs = do
    handles <- mapConcurrently createProcess procs

    exitCodes <-
        traverse (waitForProcess . \(_,_,_,h) -> h) handles

    let failures = [ proc
                   | (proc, exitCode) <- zip procs exitCodes
                   , exitCode /= ExitSuccess
                   ]

    if (null failures) then pure ()
    else concurrentlyRetryForever failures

main :: IO ()
main = do
    home <- getHomeDir
    let fullPaths = map (home </>) paths
    repos <- fmap join (traverse listRepos fullPaths)
    concurrentlyRetryForever (map fetchRepo repos)

from haskell-phrasebook.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.