9 import Control.Monad.Trans (liftIO)
10 import qualified Control.Monad.State as St
12 import qualified Codec.Archive.LibZip as Zip
15 import Java.JAR.Archive
18 readManifest :: Zip.Archive (Maybe Manifest)
20 let manifestPath = "META-INF/MANIFEST.MF"
21 files <- Zip.fileNames []
22 if manifestPath `elem` files
24 content <- Zip.fileContents [] manifestPath
25 case parseMeta content of
26 Left e -> fail $ show e
27 Right meta -> return $ Just (loadSpec meta)
30 readOne :: FilePath -> String -> Zip.Archive [Tree CPEntry]
31 readOne jarfile str = do
32 files <- Zip.fileNames []
33 return $ mapF (NotLoadedJAR jarfile) (buildTree $ filter good files)
35 good name = (str `isPrefixOf` name) && (".class" `isSuffixOf` name)
37 -- | Read MainClass Entry of a MANIFEST.MF file
38 readMainClass :: FilePath -> IO (Maybe String)
39 readMainClass jarfile = do
40 Zip.withArchive [] jarfile $ do
43 Nothing -> return Nothing
44 Just mf -> return $ mainClass mf
46 -- | Read entries from JAR file, using MANIFEST.MF if it exists.
47 readJAR :: FilePath -> IO [Tree CPEntry]
49 r <- Zip.withArchive [] jarfile $ do
52 Nothing -> return Nothing
54 trees <- mapM (readOne jarfile) (map meName $ manifestEntries mf)
55 let forest = merge (concat trees)
58 Nothing -> readAllJAR jarfile
59 Just [] -> readAllJAR jarfile
62 -- | Add given JAR file to ClassPath
63 addJAR :: FilePath -> ClassPath ()
65 classes <- liftIO $ readJAR jarfile
67 let cp' = merge $ cp ++ classes