Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions app/Curator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ type PackageSetMap = Map PackageName Package
data SpagoUpdaterMessage
= MStart

data FetcherMessage
= MPackageSetTag !Text
newtype FetcherMessage
= MPackageSetTag Text

data MetadataUpdaterMessage
= MMetadata !PackageName !RepoMetadataV1
Expand Down Expand Up @@ -72,7 +72,7 @@ main = do
ensureRepo "purescript" "package-sets"

-- Read GitHub Auth Token
token <- fmap Text.pack $ Env.getEnv "SPACCHETTIBOTTI_TOKEN"
token <- Text.pack <$> Env.getEnv "SPACCHETTIBOTTI_TOKEN"

-- Set up comms channels
chanFetcher <- Queue.newTBQueueIO 10
Expand Down Expand Up @@ -120,7 +120,7 @@ main = do
ensureRepo org repo = do
isThere <- testdir $ Turtle.decodeString $ "data" </> repo
-- clone if needed
when (not isThere) $ do
unless isThere $ do
(code, _out, _err) <- runWithCwd "data" $ "git clone git@github.com:" <> org <> "/" <> repo <> ".git"
case code of
ExitSuccess -> echoStr $ "Cloned " <> org <> "/" <> repo
Expand All @@ -133,7 +133,7 @@ spagoUpdater :: Text -> Queue.TBQueue SpagoUpdaterMessage -> Queue.TBQueue Fetch
spagoUpdater token controlChan fetcherChan = go Nothing
where
go maybeOldTag = do
(atomically $ Queue.readTBQueue controlChan) >>= \case
atomically (Queue.readTBQueue controlChan) >>= \case
MStart -> do
-- Get which one is the latest release of package-sets and download it
echo "Update has been kickstarted by main thread."
Expand Down Expand Up @@ -199,7 +199,7 @@ spagoUpdater token controlChan fetcherChan = go Nothing

fetcher :: MonadIO m => Text -> Queue.TBQueue FetcherMessage -> Queue.TQueue MetadataUpdaterMessage -> Queue.TQueue PackageSetsUpdaterMessage -> m b
fetcher token controlChan metadataChan psChan = liftIO $ forever $ do
(atomically $ Queue.readTBQueue controlChan) >>= \case
atomically (Queue.readTBQueue controlChan) >>= \case
MPackageSetTag tag -> do
echo "Downloading and parsing package set.."
packageSet <- fetchPackageSet tag
Expand Down Expand Up @@ -274,7 +274,7 @@ packageSetsUpdater token dataChan = go mempty mempty
updateVersion _ _ other = pure other

go packageSet banned = do
(atomically $ Queue.readTQueue dataChan) >>= \case
atomically (Queue.readTQueue dataChan) >>= \case
MPackageSet newSet -> do
echo "Received new package set, updating.."
go newSet banned
Expand Down Expand Up @@ -363,7 +363,7 @@ metadataUpdater dataChan = go mempty
where
go :: ReposMetadataV1 -> IO ()
go state = do
(atomically $ Queue.readTQueue dataChan) >>= \case
atomically (Queue.readTQueue dataChan) >>= \case
MMetadata packageName meta -> do
go $ Map.insert packageName meta state
MEnd -> do
Expand Down
4 changes: 2 additions & 2 deletions src/Spago/Bower.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ runBower args = do

generateBowerJson :: Spago m => m ByteString.ByteString
generateBowerJson = do
echo $ "Generating a new Bower config using the package set versions.."
echo "Generating a new Bower config using the package set versions.."
config@Config{..} <- Config.ensureConfig
PublishConfig{..} <- throws publishConfig

Expand All @@ -68,7 +68,7 @@ generateBowerJson = do
when ignored $ do
die $ path <> " is being ignored by git - change this before continuing"

echo $ "Generated a valid Bower config using the package set"
echo "Generated a valid Bower config using the package set"
pure bowerJson


Expand Down
18 changes: 9 additions & 9 deletions src/Spago/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import System.Directory (getCurrentDirectory)
import qualified System.FilePath.Glob as Glob
import qualified System.IO as Sys
import qualified System.IO.Temp as Temp
import qualified Turtle as Turtle
import qualified Turtle
import qualified Web.Browser as Browser

import qualified Spago.Config as Config
Expand Down Expand Up @@ -204,16 +204,16 @@ runWithNode defaultModuleName maybeSuccessMessage failureMessage maybeModuleName
where
moduleName = fromMaybe defaultModuleName maybeModuleName
args = Text.intercalate " " $ map Purs.unExtraArg nodeArgs
contents = \outputPath'
-> let path = fromMaybe "output" outputPath'
contents outputPath' =
let path = fromMaybe "output" outputPath'
in "#!/usr/bin/env node\n\n" <> "require('../" <> Text.pack path <> "/" <> Purs.unModuleName moduleName <> "').main()"
cmd = "node .spago/run.js " <> args
nodeAction outputPath' = do
echoDebug $ "Writing .spago/run.js"
echoDebug "Writing .spago/run.js"
writeTextFile ".spago/run.js" (contents outputPath')
chmod executable ".spago/run.js"
shell cmd empty >>= \case
ExitSuccess -> fromMaybe (pure ()) (echo <$> maybeSuccessMessage)
ExitSuccess -> maybe (pure ()) echo maybeSuccessMessage
ExitFailure n -> die $ failureMessage <> repr n

-- | Bundle the project to a js file
Expand Down Expand Up @@ -250,7 +250,7 @@ bundleModule maybeModuleName maybeTargetPath noBuild buildOpts = do
-- Here we append the CommonJS export line at the end of the bundle
try (with
(appendonly $ pathFromText $ Purs.unTargetPath targetPath)
((flip hPutStrLn) jsExport))
(flip hPutStrLn jsExport))
>>= \case
Right _ -> echo $ "Make module succeeded and output file to " <> Purs.unTargetPath targetPath
Left (n :: SomeException) -> die $ "Make module failed: " <> repr n
Expand Down Expand Up @@ -351,7 +351,7 @@ getOutputPath buildOpts = do
-- | which should be the output folder
findOutputFlag :: [Purs.ExtraArg] -> Maybe Sys.FilePath
findOutputFlag [] = Nothing
findOutputFlag (_:[]) = Nothing
findOutputFlag [_] = Nothing
findOutputFlag (x:y:xs)
= if isOutputFlag x
then Just $ Text.unpack (Purs.unExtraArg y)
Expand All @@ -378,8 +378,8 @@ getBuildArgsForSharedFolder buildOpts = do
let pursArgs'
= pursArgs buildOpts
pathToOutputArg
= Purs.ExtraArg . Text.pack . ((++) "--output ")
if (or $ isOutputFlag <$> pursArgs')
= Purs.ExtraArg . Text.pack . ("--output " <>)
if any isOutputFlag pursArgs'
then do
echo "Output path set explicitly - not using shared output path"
pure pursArgs'
Expand Down
8 changes: 4 additions & 4 deletions src/Spago/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ parseConfig = do
let metadataPackageName = PackageSet.PackageName "metadata"
let (metadataMap, packagesDB) = Map.partitionWithKey (\k _v -> k == metadataPackageName) packages
let packagesMinPursVersion = join
$ fmap (hush . Version.semver . (Text.replace "v" "") . PackageSet.version . PackageSet.location)
$ fmap (hush . Version.semver . Text.replace "v" "" . PackageSet.version . PackageSet.location)
$ Map.lookup metadataPackageName metadataMap
let packageSet = PackageSet.PackageSet{..}

pure Config{..}
_ -> case Dhall.TypeCheck.typeOf expr of
Right e -> throwM $ Dhall.ConfigIsNotRecord e
Left err -> throwM $ err
Left err -> throwM err


-- | Checks that the Spago config is there and readable
Expand All @@ -151,7 +151,7 @@ ensureConfig = do
path <- asks globalConfigPath
exists <- testfile path
unless exists $ do
die $ Messages.cannotFindConfig
die Messages.cannotFindConfig
try parseConfig >>= \case
Right config -> do
PackageSet.ensureFrozen $ Text.unpack path
Expand Down Expand Up @@ -258,7 +258,7 @@ showBowerErrors (List.sort -> errors)
= "\n\nSpago encountered some errors while trying to migrate your Bower config.\n"
<> "A Spago config has been generated but it's recommended that you apply the suggestions here\n\n"
<> (Text.unlines $ map (\errorGroup ->
(case (head errorGroup) of
(case head errorGroup of
UnparsableRange _ _ -> "It was not possible to parse the version range for these packages:"
NonPureScript _ -> "These packages are not PureScript packages, so you should install them with `npm` instead:"
MissingFromTheSet _ -> "These packages are missing from the package set. You should add them in your local package set:\n(See here for how: https://github.com/spacchetti/spago#add-a-package-to-the-package-set)"
Expand Down
6 changes: 3 additions & 3 deletions src/Spago/Dhall.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ readRawExpr pathText = do
then (do
packageSetText <- readTextFile $ pathFromText pathText
fmap Just $ throws $ Parser.exprAndHeaderFromText mempty packageSetText)
else (pure Nothing)
else pure Nothing


writeRawExpr :: Text -> (Text, DhallExpr Dhall.Import) -> IO ()
Expand Down Expand Up @@ -117,7 +117,7 @@ requireKey
-> Text
-> (DhallExpr b -> m a)
-> m a
requireKey ks name f = case (Dhall.Map.lookup name ks) of
requireKey ks name f = case Dhall.Map.lookup name ks of
Just v -> f v
Nothing -> throwM (RequiredKeyMissing name ks)

Expand Down Expand Up @@ -198,7 +198,7 @@ instance (Pretty a) => Show (ReadError a) where
, ""
, "The type was the following:"
, ""
, "↳ " <> (pretty $ Dhall.expected typ)
, "↳ " <> pretty (Dhall.expected typ)
, ""
, "And the expression was the following:"
, ""
Expand Down
14 changes: 7 additions & 7 deletions src/Spago/FetchPackage.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import qualified Data.List as List
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import qualified Data.Versions as Version
import qualified Numeric as Numeric
import qualified Numeric
import qualified System.FilePath as FilePath
import qualified System.IO.Temp as Temp
import qualified System.Process as Process
Expand Down Expand Up @@ -47,12 +47,12 @@ fetchPackages globalCacheFlag allDeps minPursVersion = do
PackageSet.checkPursIsUpToDate minPursVersion

-- Ensure both local and global cache dirs are there
GlobalCache.getGlobalCacheDir >>= assertDirectory
(pure localCacheDir) >>= assertDirectory
assertDirectory =<< GlobalCache.getGlobalCacheDir
assertDirectory localCacheDir

-- We try to fetch a dep only if their local cache directory doesn't exist
-- (or their local path, which is the same thing)
depsToFetch <- (flip filterM) allDeps $ \dep -> do
depsToFetch <- flip filterM allDeps $ \dep -> do
exists <- Directory.doesDirectoryExist $ getLocalCacheDir dep
pure $ not exists

Expand Down Expand Up @@ -104,7 +104,7 @@ fetchPackage metadata pair@(packageName'@PackageName{..}, Package{ location = Re
packageLocalCacheDir <- makeAbsolute $ getLocalCacheDir pair

inGlobalCache <- testdir $ Turtle.decodeString packageGlobalCacheDir
Temp.withTempDirectory localCacheDir (Text.unpack ("__download-" <> packageName <> "-" <> (getCacheVersionDir version))) $ \path -> do
Temp.withTempDirectory localCacheDir (Text.unpack ("__download-" <> packageName <> "-" <> getCacheVersionDir version)) $ \path -> do
let downloadDir = path </> "download"

-- * if a Package is in the global cache, copy it to the local cache
Expand All @@ -114,7 +114,7 @@ fetchPackage metadata pair@(packageName'@PackageName{..}, Package{ location = Re
cptree packageGlobalCacheDir downloadDir
assertDirectory (localCacheDir </> Text.unpack packageName)
mv downloadDir packageLocalCacheDir
else Temp.withTempDirectory globalDir (Text.unpack ("__temp-" <> "-" <> packageName <> (getCacheVersionDir version))) $ \globalTemp -> do
else Temp.withTempDirectory globalDir (Text.unpack ("__temp-" <> "-" <> packageName <> getCacheVersionDir version)) $ \globalTemp -> do
-- * otherwise, check if the Package is on GitHub and an "immutable" ref
-- * if yes, download the tar archive and copy it to global and then local cache
let cacheableCallback :: Spago m => FilePath.FilePath -> m ()
Expand Down Expand Up @@ -143,7 +143,7 @@ fetchPackage metadata pair@(packageName'@PackageName{..}, Package{ location = Re
let processWithNewCwd = (Process.shell (Text.unpack git))
{ Process.cwd = Just downloadDir }

(systemStrictWithErr processWithNewCwd empty) >>= \case
systemStrictWithErr processWithNewCwd empty >>= \case
(ExitSuccess, _, _) -> mv downloadDir packageLocalCacheDir
(_, _stdout, stderr) -> die $ Messages.failedToInstallDep quotedName stderr

Expand Down
39 changes: 14 additions & 25 deletions src/Spago/PackageSet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ module Spago.PackageSet

import Spago.Prelude

import qualified Data.Text as Text
import qualified Data.Versions as Version
import Data.Ord (comparing)
import qualified Data.Text as Text
import qualified Data.Versions as Version
import qualified Dhall.Freeze
import qualified Dhall.Pretty
import qualified Safe

import qualified Spago.Dhall as Dhall
import qualified Spago.GitHub as GitHub
import Spago.Messages as Messages
import qualified Spago.Purs as Purs
import qualified Spago.Templates as Templates
import qualified System.IO
import qualified Spago.Dhall as Dhall
import qualified Spago.GitHub as GitHub
import Spago.Messages as Messages
import qualified Spago.Purs as Purs
import qualified Spago.Templates as Templates
import qualified System.FilePath
import qualified System.IO

packagesPath :: IsString t => t
packagesPath = "packages.dhall"
Expand Down Expand Up @@ -64,7 +66,7 @@ upgradePackageSet = do
Nothing -> die Messages.cannotFindPackages
-- Skip the check if the tag is already the newest
Just (_, expr)
| (currentTag:_) <- (foldMap getCurrentTag expr)
| (currentTag:_) <- foldMap getCurrentTag expr
, currentTag == releaseTagName
-> echo $ "Skipping package set version upgrade, already on latest version: " <> quotedTag
Just (header, expr) -> do
Expand Down Expand Up @@ -231,31 +233,18 @@ rootPackagePath (Dhall.Import
rootPackagePath _ = Nothing


-- | In a Monorepo we don't wish to rebuild our shared packages over and over,
-- | In a Monorepo we don't wish to rebuild our shared packages over and over,
-- | so we build into an output folder where our root packages.dhall lives
findRootOutputPath :: Spago m => System.IO.FilePath -> m (Maybe System.IO.FilePath)
findRootOutputPath path = do
echoDebug "Locating root path of packages.dhall"
imports <- liftIO $ Dhall.readImports $ Text.pack path
let localImports = mapMaybe rootPackagePath imports
pure $ (flip System.FilePath.replaceFileName) "output" <$> (findRootPath localImports)
pure $ flip System.FilePath.replaceFileName "output" <$> findRootPath localImports

-- | Given a list of filepaths, find the one with the least folders
findRootPath :: [System.IO.FilePath] -> Maybe System.IO.FilePath
findRootPath paths
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only change not suggested by hlint, but I couldn't resist to simplify this to more readable one-liner..

= foldr comparePaths Nothing paths
where
isLessThan :: Ord a => a -> Maybe a -> Bool
isLessThan a maybeA
= isNothing maybeA
|| fromMaybe False (fmap (\a' -> a < a') maybeA)

comparePaths path current
= if isLessThan
(length (System.FilePath.splitSearchPath path))
(length <$> current)
then Just path
else current
findRootPath = Safe.minimumByMay (comparing (length . System.FilePath.splitSearchPath))

-- | Freeze the package set remote imports so they will be cached
freeze :: Spago m => System.IO.FilePath -> m ()
Expand Down
4 changes: 2 additions & 2 deletions src/Spago/Packages.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ initProject force comments = do
action

copyIfNotExists dest srcTemplate = do
(testfile dest) >>= \case
testfile dest >>= \case
True -> echo $ Messages.foundExistingFile dest
False -> writeTextFile dest srcTemplate

Expand Down Expand Up @@ -272,7 +272,7 @@ listPackages packagesFilter jsonFlag = do
echoDebug "Running `listPackages`"
Config{packageSet = packageSet@PackageSet{..}, ..} <- Config.ensureConfig
packagesToList :: [(PackageName, Package)] <- case packagesFilter of
Nothing -> pure $ Map.toList $ packagesDB
Nothing -> pure $ Map.toList packagesDB
Just TransitiveDeps -> getTransitiveDeps packageSet dependencies
Just DirectDeps -> pure $ Map.toList
$ Map.restrictKeys packagesDB (Set.fromList dependencies)
Expand Down
7 changes: 4 additions & 3 deletions src/Spago/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ import Dhall (Text)
import qualified Dhall.Core
import qualified System.FilePath as FilePath
import qualified System.IO
import qualified Turtle as Turtle
import qualified Turtle
import qualified UnliftIO.Directory as Directory

import Control.Applicative (Alternative, empty, many, (<|>))
Expand Down Expand Up @@ -120,8 +120,9 @@ import Safe (headMay)
import System.FilePath (isAbsolute, pathSeparator, (</>))
import System.IO (hPutStrLn)
import Turtle (ExitCode (..), FilePath, appendonly, chmod,
executable, mktree, repr, shell, shellStrict,
shellStrictWithErr, systemStrictWithErr, testdir)
executable, mktree, repr, shell,
shellStrict, shellStrictWithErr,
systemStrictWithErr, testdir)
import UnliftIO (MonadUnliftIO, withRunInIO)
import UnliftIO.Directory (getModificationTime, makeAbsolute)
import UnliftIO.Exception (IOException, handleAny, try, tryIO)
Expand Down
6 changes: 3 additions & 3 deletions src/Spago/Purs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ bundle withMain (ModuleName moduleName) (TargetPath targetPath) = do

runWithOutput cmd
("Bundle succeeded and output file to " <> targetPath)
("Bundle failed.")
"Bundle failed."


data DocsFormat
Expand Down Expand Up @@ -120,8 +120,8 @@ versionImpl purs = do
fullVersionText <- shellStrictWithErr (purs <> " --version") empty >>= \case
(ExitSuccess, out, _err) -> pure out
(_, _out, err) -> die $ "Failed to run '" <> purs <> " --version'. Error:" <> err
versionText <- pure $ headMay $ Text.split (== ' ') fullVersionText
parsed <- pure $ versionText >>= (hush . Version.semver)
let versionText = headMay $ Text.split (== ' ') fullVersionText
parsed = versionText >>= (hush . Version.semver)

when (isNothing parsed) $ do
echo $ Messages.failedToParseCommandOutput (purs <> " --version") fullVersionText
Expand Down
2 changes: 1 addition & 1 deletion src/Spago/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Spago.Prelude

import qualified Data.Text as Text
import qualified Data.Versions as Version
import qualified Dhall as Dhall
import qualified Dhall
import qualified Network.URI as URI

import qualified Spago.Messages as Messages
Expand Down
Loading