Skip to content

Commit

Permalink
Add option to genLibDeps to remove packages already updated
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanMartinez committed Oct 20, 2020
1 parent 15c54b6 commit 5b11dae
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
21 changes: 15 additions & 6 deletions src/Application.purs
Expand Up @@ -4,7 +4,7 @@ import Prelude

import CLI (Command(..), Env, LibraryDepsOptions, SpagoFilesOptions)
import Control.Monad.Rec.Class (Step(..), tailRec)
import Data.Array (elemIndex, foldl, intercalate, length, nub, snoc, sort, sortBy, uncons)
import Data.Array (difference, elemIndex, foldl, intercalate, length, nub, snoc, sort, sortBy, uncons)
import Data.Either (Either(..))
import Data.FoldableWithIndex (foldlWithIndex)
import Data.HashMap (HashMap, filterKeys, lookup, toArrayBy)
Expand All @@ -13,7 +13,7 @@ import Data.List (List(..))
import Data.List as List
import Data.Maybe (Maybe(..), fromMaybe, isJust)
import Data.Monoid (power)
import Data.String (joinWith)
import Data.String (Pattern(..), joinWith, split, trim)
import Data.String.CodeUnits (drop, take)
import Data.String.Utils (padEnd)
import Data.Traversable (for_)
Expand All @@ -39,16 +39,15 @@ runApp env = do
log $ take (width * 2) $ drop (e.pos - width) packageJson
log $ power " " (width - 1) <> "^"
Right { result } -> do
let allDepsKnown = findAllTransitiveDeps result
case env.command of
GenLibraryDeps options -> do
runGenLibDeps options allDepsKnown
runGenLibDeps options result
GenSpagoFiles options -> do
runSpagoFiles options allDepsKnown
runSpagoFiles options $ findAllTransitiveDeps result

where
runGenLibDeps :: LibraryDepsOptions -> HashMap String PackageMeta -> Aff Unit
runGenLibDeps { libraryDepFile } allDepsKnown = do
runGenLibDeps { libraryDepFile, finishedDepsFile } result = do
fileExists <- exists libraryDepFile
if fileExists && not env.force
then liftEffect do
Expand All @@ -57,6 +56,10 @@ runApp env = do
\To overwrite this file, use the `--force` flag."
log "Exiting program."
else do
finishedDeps <- readTextFile UTF8 finishedDepsFile
let depsToRemove = map trim $ split (Pattern "\n") finishedDeps
let removedDeps = removeFinishedDeps depsToRemove result
let allDepsKnown = findAllTransitiveDeps removedDeps
let orderedContent = mkOrderedContent $ mkSortedPackageArray allDepsKnown
writeTextFile UTF8 libraryDepFile orderedContent

Expand Down Expand Up @@ -133,6 +136,12 @@ findAllTransitiveDeps packageMap = foldlWithIndex buildMap HashMap.empty package
getDeps packageName =
fromMaybe [] $ map (_.dependencies) $ lookup packageName packageMap

removeFinishedDeps :: Array String -> HashMap String PackageMeta -> HashMap String PackageMeta
removeFinishedDeps depsToRemove packageMap = do
let removedKeys = foldl (flip HashMap.delete) packageMap depsToRemove
removedKeys <#> \packageMeta ->
packageMeta { dependencies = difference packageMeta.dependencies depsToRemove }

mkSortedPackageArray :: HashMap String PackageMeta -> Array { package :: String, meta :: PackageMeta, depCount :: Int }
mkSortedPackageArray =
toArrayBy (\k v ->
Expand Down
12 changes: 11 additions & 1 deletion src/CLI.purs
Expand Up @@ -26,6 +26,7 @@ data Command

type LibraryDepsOptions =
{ libraryDepFile :: String
, finishedDepsFile :: String
}

type SpagoFilesOptions =
Expand Down Expand Up @@ -102,7 +103,16 @@ parseGenLibDeps = map GenLibraryDeps ado
, value "./ordered-content.txt"
, showDefault
]
in { libraryDepFile }
finishedDepsFile <- strOption $ fold
[ long "finished-dependencies-file"
, short 'd'
, metavar "FILE"
, help "Indicates the file that lists packages that have already been \
\updated on a separate line."
, value "./finished-dependencies.txt"
, showDefault
]
in { libraryDepFile, finishedDepsFile }

parseGenSpagoFiles :: OA.Parser Command
parseGenSpagoFiles = map GenSpagoFiles ado
Expand Down

0 comments on commit 5b11dae

Please sign in to comment.