self-extract (empty) → 0.1.0.0
raw patch · 7 files changed
+294/−0 lines, 7 filesdep +Cabaldep +basedep +binary
Dependencies added: Cabal, base, binary, bytestring, extra, file-embed, path, path-io, process, unix-compat
Files
- CHANGELOG.md +3/−0
- LICENSE.md +11/−0
- README.md +105/−0
- self-extract.cabal +36/−0
- src/Codec/SelfExtract.hs +49/−0
- src/Codec/SelfExtract/Distribution.hs +69/−0
- src/Codec/SelfExtract/Tar.hs +21/−0
+ CHANGELOG.md view
@@ -0,0 +1,3 @@+## self-extract 0.1.0.0++* Initial implementation
+ LICENSE.md view
@@ -0,0 +1,11 @@+Copyright 2018 Brandon Chinn++Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.++2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.++3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,105 @@+# self-extract++A Haskell library that can make an executable self-extracting.++## Usage++### .cabal file++```+...+build-type: Custom+...++custom-setup+ setup-depends: base, Cabal, self-extract++...++executable name-of-executable+ build-depends: self-extract, ...+ ...+```++### Setup.hs++Basic:++```+import Codec.SelfExtract.Distribution (bundle)+import Distribution.Simple++main = defaultMainWithHooks simpleUserHooks+ { postBuild = \args bf pd lbi -> do+ postBuild simpleUserHooks args bf pd lbi+ bundle "name-of-executable" "dir-to-bundle" lbi+ }+```++Using the [Path](https://hackage.haskell.org/package/path-0.6.1) library:++```+import Codec.SelfExtract.Distribution (bundle')+import Distribution.Simple+import Path (reldir)++main = defaultMainWithHooks simpleUserHooks+ { postBuild = \args bf pd lbi -> do+ postBuild simpleUserHooks args bf pd lbi+ bundle' "name-of-executable" [reldir|dir-to-bundle|] lbi+ }+```++### Executable file++Basic:++```+import Codec.SelfExtract (extractTo)++main = do+ extractTo "dir" -- will extract to $CWD/dir+ extractTo "/usr/local/lib"+ ...+```++Extract to a temporary directory:++```+import Codec.SelfExtract (withExtractToTemp)+import System.Directory (removeDirectory)++main = do+ withExtractToTemp $ \tmp -> do+ ...+```++Using the [Path](https://hackage.haskell.org/package/path-0.6.1) library:++```+import Codec.SelfExtract (extractTo', withExtractToTemp')+import Path (absdir, reldir)+import Path.IO (removeDir)++main = do+ extractTo' $ [reldir|dir|] -- will extract to $CWD/dir+ extractTo' $ [absdir|/usr/local/lib|]+ withExtractToTemp' $ \tmp -> do+ ...+```++### Details++The above instructions should be a black box, but here is an explanation of the implementation+if you need to know the details of how it works.++When the executable containing `extractTo` is built, some space will be allocated to contain the+size of the binary.++`bundle` will find the executable from `LocalBuildInfo`'s `buildDir`. It will take the directory+specified and run `tar` on it. It will also get the size of the executable and write the size into+the space allocated by `extractTo`. Then `bundle` will replace the executable with the executable+itself concatenated with the tar archive.++When `extractTo` is called, it will read the size of the executable that was written in `bundle`.+After seeking to the size of the binary, the tar archive can be extracted to the desired directory.
+ self-extract.cabal view
@@ -0,0 +1,36 @@+name: self-extract+version: 0.1.0.0+license: BSD3+license-file: LICENSE.md+author: Brandon Chinn <brandon@leapyear.io>+maintainer: Brandon Chinn <brandon@leapyear.io>+category: Distribution+synopsis: A Haskell library to make self-extracting executables+description: A Haskell library to make self-extracting executables.+build-type: Simple+cabal-version: 1.18+extra-doc-files: CHANGELOG.md, README.md++source-repository head+ type: git+ location: https://github.com/brandon-leapyear/self-extract.git++library+ hs-source-dirs: src+ default-language: Haskell2010+ exposed-modules: Codec.SelfExtract+ Codec.SelfExtract.Distribution+ Codec.SelfExtract.Tar+ build-depends: base >= 4.7 && < 5+ , Cabal >= 2.0 && < 3+ , binary >= 0.8.5 && < 0.9+ , bytestring >= 0.10.8 && < 0.11+ , extra >= 1.6 && < 1.7+ , file-embed >= 0.0.10 && < 0.1+ , path >= 0.6 && < 0.7+ , path-io >= 1.3 && < 1.4+ , process >= 1.6 && < 1.7+ , unix-compat >= 0.5 && < 0.6+ ghc-options:+ -Wall -Wincomplete-uni-patterns -Wincomplete-record-updates -Wcompat+ -Wredundant-constraints -Wnoncanonical-monad-instances
+ src/Codec/SelfExtract.hs view
@@ -0,0 +1,49 @@+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE TemplateHaskell #-}++module Codec.SelfExtract+ ( extractTo+ , withExtractToTemp+ , extractTo'+ , withExtractToTemp'+ ) where++import Control.Monad ((>=>))+import Data.Binary (Word32, decode)+import qualified Data.ByteString as BS+import qualified Data.ByteString.Lazy as LBS+import Data.FileEmbed (dummySpaceWith)+import Path (Abs, Dir, Path, fromAbsDir, fromAbsFile, parseAbsFile)+import Path.IO (resolveDir', withSystemTempDir, withSystemTempFile)+import System.Environment (getExecutablePath)+import System.IO (IOMode(..), SeekMode(..), hClose, hSeek, withFile)++import Codec.SelfExtract.Tar (untar)++-- | Extract the self-bundled executable to the given path.+extractTo :: FilePath -> IO ()+extractTo = resolveDir' >=> extractTo'++-- | Extract the self-bundled executable to a temporary path.+withExtractToTemp :: (FilePath -> IO ()) -> IO ()+withExtractToTemp action = withExtractToTemp' (action . fromAbsDir)++-- | Extract the self-bundled executable to the given path.+extractTo' :: Path b Dir -> IO ()+extractTo' dir = do+ self <- getExecutablePath >>= parseAbsFile+ withSystemTempFile "" $ \archive hTemp -> do+ withFile (fromAbsFile self) ReadMode $ \hSelf -> do+ hSeek hSelf AbsoluteSeek $ fromIntegral exeSize+ BS.hGetContents hSelf >>= BS.hPut hTemp++ hClose hTemp+ untar archive dir++-- | Extract the self-bundled executable to a temporary path.+withExtractToTemp' :: (Path Abs Dir -> IO ()) -> IO ()+withExtractToTemp' action = withSystemTempDir "" $ \dir -> extractTo' dir >> action dir++-- | The size of executable that will be rewritten by `bundle`.+exeSize :: Word32+exeSize = decode $ LBS.fromStrict $(dummySpaceWith "self-extract" 32)
+ src/Codec/SelfExtract/Distribution.hs view
@@ -0,0 +1,69 @@+{-# LANGUAGE NamedFieldPuns #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE QuasiQuotes #-}++module Codec.SelfExtract.Distribution+ ( bundle+ , bundle'+ ) where++import Control.Monad.Extra (unlessM)+import Data.Binary (Word32, encode)+import Data.ByteString as BS+import Data.ByteString.Lazy as LBS+import Data.FileEmbed (injectFileWith)+import Distribution.Simple.LocalBuildInfo (LocalBuildInfo(..))+import Path (Dir, File, Path, fromAbsFile, parseRelDir, parseRelFile, relfile, toFilePath, (</>))+import Path.IO (doesFileExist, renameFile, resolveDir', withSystemTempDir)+import System.PosixCompat.Files (fileSize, getFileStatus)++import Codec.SelfExtract.Tar (tar)++-- | Bundle the given directory into the executable with the given name.+--+-- To be used as part of the Setup.hs file.+bundle :: String -> FilePath -> LocalBuildInfo -> IO ()+bundle exe dir lbi = do+ dir' <- resolveDir' dir+ bundle' exe dir' lbi++-- | Bundle the given directory into the executable with the given name.+--+-- To be used as part of the Setup.hs file.+bundle' :: String -> Path b Dir -> LocalBuildInfo -> IO ()+bundle' exeName dir LocalBuildInfo{buildDir} = do+ exeDir <- resolveDir' buildDir+ exeNameDir <- parseRelDir exeName+ exeNameFile <- parseRelFile exeName++ let exe = exeDir </> exeNameDir </> exeNameFile+ unlessM (doesFileExist exe) $ error $ "Executable does not exist: " ++ exeName++ size <- getFileSize exe++ withSystemTempDir "self-extract" $ \tempDir -> do+ let exeWithSize = tempDir </> [relfile|exe_with_size|]+ injectFileWith "self-extract"+ (LBS.toStrict $ encode size)+ (fromAbsFile exe)+ (fromAbsFile exeWithSize)++ let zippedDir = tempDir </> [relfile|bundle.tar.gz|]+ tar dir zippedDir++ let combined = tempDir </> [relfile|exe_and_bundle|]+ cat [exeWithSize, zippedDir] combined++ renameFile combined exe++-- | Get the size of the given file.+getFileSize :: Path b File -> IO Word32+getFileSize = fmap getSize . getFileStatus . toFilePath+ where+ getSize = fromIntegral . fileSize++-- | Concatenate the given files and write to the given file.+cat :: [Path b File] -> Path b File -> IO ()+cat srcs dest = do+ contents <- BS.concat <$> mapM (BS.readFile . toFilePath) srcs+ BS.writeFile (toFilePath dest) contents
+ src/Codec/SelfExtract/Tar.hs view
@@ -0,0 +1,21 @@+module Codec.SelfExtract.Tar+ ( tar+ , untar+ ) where++import Path (Dir, File, Path, toFilePath)+import Path.IO (ensureDir)+import System.Process (callProcess)++-- | Zip the given directory into the given archive.+--+-- Shelling out to `tar` because Haskell Tar packages would need the C header files, which we don't+-- require on the client end.+tar :: Path b0 Dir -> Path b1 File -> IO ()+tar src archive = callProcess "tar" ["-czf", toFilePath archive, "-C", toFilePath src, "."]++-- | Extract the given archive to the given directory.+untar :: Path b0 File -> Path b1 Dir -> IO ()+untar archive dest = do+ ensureDir dest+ callProcess "tar" ["-xzf", toFilePath archive, "-C", toFilePath dest]