cabalg 0.2.6 → 0.2.7
raw patch · 5 files changed
+84/−34 lines, 5 files
Files
- cabalg.cabal +9/−5
- src/Args.hs +31/−0
- src/Git.hs +11/−14
- src/Main.hs +13/−11
- test/Test.hs +20/−4
cabalg.cabal view
@@ -1,5 +1,5 @@ name: cabalg-version: 0.2.6+version: 0.2.7 synopsis: alias for cabal install from given git repo license: MIT license-file: LICENSE@@ -19,7 +19,7 @@ . @$> mktemp $> git clone --single-branch --depth=1 --quiet https:\/\/github.com\/biegunka\/biegunka \<tempdirname\>- $> cabal install <tempdirname>/<cabalfilename>+ $> cabal install \<tempdirname\>/\<cabalfilename\> @ . It also supports arbitrary git revisions mentioning like@@ -39,15 +39,18 @@ executable cabalg main-is: Main.hs hs-source-dirs: src- other-modules: Git System.Directory.NonExistent+ other-modules:+ Args+ Git+ System.Directory.NonExistent ghc-options: -Wall default-language: Haskell2010 build-depends: base >= 4 && < 5- , process , directory , filepath+ , process library hs-source-dirs: src@@ -57,9 +60,9 @@ default-language: Haskell2010 build-depends: base >= 4 && < 5- , process , directory , filepath+ , process source-repository head type: git@@ -74,5 +77,6 @@ base >= 4 && < 5 , cabalg , directory+ , filepath , process
+ src/Args.hs view
@@ -0,0 +1,31 @@+module Args where++import Control.Arrow (second)++data Action+ = Help+ | Version+ | Install [String] (Maybe String)+++fromArgs :: [String] -> Action+fromArgs [x] | x `elem` ["--help", "--usage", "-h"] = Help+fromArgs ["--version"] = Version+fromArgs x = uncurry Install . second flags . span (/= "--") $ x+ where+ flags [] = Nothing+ flags ["--"] = Nothing+ flags (_:xs) = Just $ unwords xs+++printHelp :: IO ()+printHelp = putStrLn $ unlines+ [ "cabalg is an alias for installing cabal package from git source repository."+ , ""+ , "Usage:"+ , " $> cabalg <repo1> ... <repoN> [-- <cabal-install args>]"+ , ""+ , "Example:"+ , " $> cabalg https://github.com/biegunka/biegunka@f524f97"+ ]+
src/Git.hs view
@@ -1,31 +1,28 @@ module Git where -import System.Directory-import System.FilePath()-import System.Process+import System.Directory+import System.FilePath ()+import System.Process -- | git clone "master" branch-clone :: String -- | url- -> FilePath -- | directory where repository will be cloned to+clone :: String -- ^ url+ -> FilePath -- ^ directory where repository will be cloned to -> IO ()-clone url dir = putStrLn =<< readProcess "git"+clone url dir = putStr =<< readProcess "git" [ "clone"- , "--branch", "master" , "--single-branch" , "--depth=1"- , "--quiet" , url , dir] [] -- | git clone and checkout particular revision (could be much slower than 'clone')-cloneRevision :: String -- | url- -> String -- | revision- -> FilePath -- | directory where repository will be cloned to+cloneRevision :: String -- ^ url+ -> String -- ^ revision+ -> FilePath -- ^ directory where repository will be cloned to -> IO () cloneRevision url revision dir = do- putStrLn =<< readProcess "git" [ "clone", url, dir ] []+ putStr =<< readProcess "git" [ "clone", url, dir ] [] currentDir <- getCurrentDirectory setCurrentDirectory dir- putStrLn =<< readProcess "git" [ "checkout", revision, "--force", "--quiet" ] []+ putStr =<< readProcess "git" [ "checkout", revision, "--force" ] [] setCurrentDirectory currentDir-
src/Main.hs view
@@ -1,31 +1,30 @@ import Control.Applicative-import Control.Arrow import Control.Monad import Data.List import Data.Maybe+import Data.Version (showVersion)+import Paths_cabalg (version) import System.Directory import System.Environment import System.FilePath import System.Process +import Args import Git import System.Directory.NonExistent main :: IO () main = do- (repos, args) <- reposAndArgs <$> getArgs- cabalFiles <- mapM fetch repos- cabalInstall (catMaybes cabalFiles) args+ action <- fromArgs <$> getArgs+ case action of+ Help -> printHelp+ Version -> putStrLn $ showVersion version+ Install repos args -> do+ cabalFiles <- mapM fetch repos+ cabalInstall (catMaybes cabalFiles) args -reposAndArgs :: [String] -> ([String], Maybe String)-reposAndArgs = second flags . span (/= "--")- where- flags [] = Nothing- flags ["--"] = Nothing- flags (_:xs) = Just $ unwords xs- fetch :: String -> IO (Maybe FilePath) fetch urlAndMaybeRevision = do currentDir <- getCurrentDirectory@@ -40,12 +39,14 @@ _ -> Nothing url = takeWhile (/= '@') urlAndMaybeRevision + cabalInstall :: [String] -> Maybe String -> IO () cabalInstall cabalFiles args = do let process = proc "cabal" ("install" : cabalFiles ++ words (fromMaybe "" args)) (_, _, _, procHandle) <- createProcess process void $ waitForProcess procHandle + repoName :: String -> String repoName = takeWhile (/= '@') . lastSplitOn '/' where lastSplitOn :: Eq a => a -> [a] -> [a]@@ -54,6 +55,7 @@ go acc (x:xs) | x == c = go [] xs | otherwise = go (acc ++ [x]) xs go acc [] = acc+ findCabalFile :: FilePath -> IO (Maybe FilePath) findCabalFile path = ((path </>) `fmap`) `fmap` find (".cabal" `isSuffixOf`) `fmap` getDirectoryContents path
test/Test.hs view
@@ -2,14 +2,30 @@ import Control.Monad import System.Directory+import System.FilePath import System.Process import System.Directory.NonExistent + main :: IO () main = do currentDir <- getCurrentDirectory- tmpDir <- createNonExistentDirectory currentDir "cabalgtest"- setCurrentDirectory tmpDir- void $ readProcess "cabalg" ["https://github.com/dmalikov/dotfiles@master", "https://github.com/biegunka/biegunka@develop"] []- setCurrentDirectory currentDir+ exec <- findExecutableCabalg currentDir+ case exec of+ Nothing -> error "unable to find cabalg executable in dist/build"+ Just cabalg -> do+ tmpDir <- createNonExistentDirectory currentDir "cabalgtest"+ setCurrentDirectory tmpDir+ void $ readProcess cabalg ["https://github.com/dmalikov/dotfiles@master", "https://github.com/biegunka/biegunka@develop"] []+ setCurrentDirectory currentDir+++findExecutableCabalg :: FilePath -> IO (Maybe FilePath)+findExecutableCabalg dir = do+ exists <- doesFileExist executable+ if (exists)+ then return $ Just $ executable+ else return Nothing+ where+ executable = dir </> "dist" </> "build" </> "cabalg" </> "cabalg"