
module Language.PureScript.Publish.Utils where

import Data.List
import Data.Maybe
import System.Directory
import qualified System.FilePath.Glob as Glob

-- | Glob relative to the current directory, and produce relative pathnames.
globRelative :: Glob.Pattern -> IO [FilePath]
globRelative pat = do
  currentDir <- getCurrentDirectory
  filesAbsolute <- Glob.globDir1 pat currentDir
  return (mapMaybe (stripPrefix (currentDir ++ "/")) filesAbsolute)

-- | Glob pattern for PureScript source files.
purescriptSourceFiles :: Glob.Pattern
purescriptSourceFiles = Glob.compile "src/**/*.purs"

-- | Glob pattern for PureScript dependency files.
purescriptDepsFiles :: Glob.Pattern
purescriptDepsFiles = Glob.compile "bower_components/*/src/**/*.purs"


module Language.PureScript.Publish.Utils where

import Data.List
import Data.Either (partitionEithers)
import System.Directory
import System.Exit (exitFailure)
import System.IO (hPutStrLn, stderr)
import System.FilePath (pathSeparator)
import qualified System.FilePath.Glob as Glob

-- | Glob relative to the current directory, and produce relative pathnames.
globRelative :: Glob.Pattern -> IO [FilePath]
globRelative pat = do
  currentDir <- getCurrentDirectory
  filesAbsolute <- Glob.globDir1 pat currentDir
  let prefix = currentDir ++ [pathSeparator]
  let (fails, paths) = partitionEithers . map (stripPrefix' prefix) $ filesAbsolute
  if null fails
    then return paths
    else do
      let p = hPutStrLn stderr
      p "Internal error in Language.PureScript.Publish.Utils.globRelative"
      p "Unmatched files:"
      mapM_ p fails
      exitFailure

  where
  stripPrefix' prefix dir =
    maybe (Left dir) Right $ stripPrefix prefix dir

-- | Glob pattern for PureScript source files.
purescriptSourceFiles :: Glob.Pattern
purescriptSourceFiles = Glob.compile "src/**/*.purs"

-- | Glob pattern for PureScript dependency files.
purescriptDepsFiles :: Glob.Pattern
purescriptDepsFiles = Glob.compile "bower_components/*/src/**/*.purs"

Fix psc-publish producing no modules on Windows
Fix psc-publish producing no modules on Windows

Fixes #1326. Amend the globRelative function to use the OS's path
separator, and also to raise an error unless it was able to strip the
current directory prefix successfully
