Skip to content

Commit

Permalink
Outline program
Browse files Browse the repository at this point in the history
  • Loading branch information
shterrett committed Nov 12, 2018
1 parent 563fcdc commit 2315b69
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 12 deletions.
28 changes: 25 additions & 3 deletions app/Main.hs
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
module Main where

import Control.Concurrent.Async (race_)
import GHC.Conc.Sync (atomically, STM)
import Options.Applicative (execParser)
import Args
import Itunes
import Actions (getAction)
import Channel

main :: IO ()
main = execParser app >>= run

run :: Args -> IO ()
run = putStrLn . show
run args = let
action = getAction (actionType args)
channel = newChannel 500 -- wild ass guess
transform = mkTransform (target args) (attributes args)
in
(race_ (readSources channel transform [source args])
(doAction channel action)) >>
putStrLn "finished"

escape :: Transform -> Action -> [Directory] -> IO ()
escape = undefined
readSources :: STM (Channel Copy) -> Transform -> [Directory] -> IO ()
readSources _ _ [] = return ()
readSources chan t (d:ds) = do
(dirs, copies) <- handleDirectory d t
atomically $ enqueueCopies chan copies
readSources chan t (ds ++ dirs)

enqueueCopies :: STM (Channel Copy) -> [Copy] -> STM ()
enqueueCopies chan cs = chan >>= writeChan cs

doAction :: STM (Channel Copy) -> Action -> IO ()
doAction chan action =
atomically (chan >>= readChan) >>= action
1 change: 1 addition & 0 deletions escape-from-itunes.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ executable escape-from-itunes
build-depends: base
, escape-from-itunes
, optparse-applicative == 0.14.3.0
, async == 2.2.1
default-language: Haskell2010

test-suite escape-from-itunes-test
Expand Down
6 changes: 4 additions & 2 deletions src/Actions.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module Actions where

import Itunes (Copy (Copy))
import System.Directory (copyFile)
import System.Directory (createDirectoryIfMissing, copyFile)
import System.FilePath (takeDirectory)

data ActionType = DryRun
| CopyFiles
Expand All @@ -12,4 +13,5 @@ getAction DryRun = putStrLn . show
getAction CopyFiles = copy

copy :: Copy -> IO ()
copy (Copy from to) = copyFile from to
copy (Copy from to) = createDirectoryIfMissing True (takeDirectory to) >>
copyFile from to
2 changes: 1 addition & 1 deletion src/Args.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ data Args = Args {
source :: String
, target :: String
, attributes :: [Attribute]
, action :: ActionType
, actionType :: ActionType
}
deriving (Show, Eq)

Expand Down
11 changes: 6 additions & 5 deletions src/Channel.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ data Channel a = Channel (TVar Int) (TVar [a]) (TVar [a])
newChannel :: Int -> STM (Channel a)
newChannel size = Channel <$> (newTVar size) <*> (newTVar []) <*> (newTVar [])

writeChan :: Channel a -> a -> STM ()
writeChan (Channel size _read write) a = do
writeChan :: [a] -> Channel a -> STM ()
writeChan as (Channel size _read write) = do
let count = length as
avail <- readTVar size
if avail == 0
if avail < count
then retry
else writeTVar size (avail - 1)
else writeTVar size (avail - count)
listend <- readTVar write
writeTVar write (a:listend)
writeTVar write (as ++ listend)

readChan :: Channel a -> STM a
readChan (Channel size read write) = do
Expand Down
8 changes: 7 additions & 1 deletion src/Itunes.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
module Itunes where
module Itunes ( handleDirectory
, mkTransform
, Copy (Copy)
, Directory
, Transform
, Action
) where

import Control.Monad (filterM)
import Data.List (foldl')
Expand Down
1 change: 1 addition & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ extra-deps: [
, directory-1.3.3.1
, filepath-1.4.2
, stm-2.5.0.0
, async-2.2.1
]

# Override default flag values for local packages and extra-deps
Expand Down

0 comments on commit 2315b69

Please sign in to comment.