packages feed

Hs2lib-0.4.8: WinDll/Builder.hs

-----------------------------------------------------------------------------
-- |
-- Module      :  Windll
-- Copyright   :  (c) Tamar Christina 2009 - 2010
-- License     :  BSD3
-- 
-- Maintainer  :  tamar@zhox.com
-- Stability   :  experimental
-- Portability :  portable
--
-- Contains a pipeline to be used for the process of writing a file to disk
--
-----------------------------------------------------------------------------

module WinDll.Builder where

import WinDll.Session
import WinDll.Utils.Feedback
import WinDll.CmdArgs
import WinDll.Version

import Control.Monad

import System.IO.Error
import System.Random
import System.FilePath
import System.Directory

type Component = Exec ()
type Robot   = FilePath -> Exec String -> Component

-- | Create a Component from a robot
createComponent :: Robot
createComponent file robot = 
 do session <- get
    let build = pipeline session
        fullfile = dirPath build ++ file    
    value <- robot
    inform _normal ("*** Writing " ++ fullfile ++ "...")
    result <- liftIO $ try $ writeFile fullfile value
    case result of
      Left err -> die ("Error when creating component '" ++ file ++ "': \n\t" ++ show err)
      Right _  -> return ()
    put $ session { pipeline = build { files = file:(files build) } }
       
       
-- | Shorter alias for createComponent
cc :: Robot
cc = createComponent
    
-- | Create a new folder to store temp files in and updates the temp path
initialize :: Component
initialize = 
 do session <- get
    nums    <- liftIO $ randomRIO (0::Int,1000000)
    let dir = (tempDIR session) ++ namespace session ++ show nums ++ [pathSeparator]
    inform _normal ("*** Creating directory " ++ dir ++ "...")
    liftIO $ createDirectoryIfMissing True dir
    let build  = pipeline session
        output = if null (outputFile session)
                    then case platform session of
                        Unix    -> namespace session ++ ".a"
                        Windows -> namespace session ++ ".dll"
                    else outputFile session
    odir <-   case outputDIR session == "." of
                    True  -> liftIO getCurrentDirectory
                    False -> return $ outputDIR session
                    
    put $ session { pipeline   = build { dirPath = dir }
                  , outputFile = output
                  , outputDIR  = odir }
    
-- | A function to run the pipeline given to it, always calls cleanup at the end and initialize at the start
runComponent :: Component -> Exec Bool
runComponent body = initialize >> body >> return True -- >> cleanup >> return True

-- | Generate the header file for the C programs
mkHeader :: String           
mkHeader = unlines $  ["//////////////////////////////////////////////"
                      ,"//     __  __   _          ___  __    __    //"
                      ,"//    / / /\\ \\ (_)_ __    /   \\/ /   / /    //"
                      ,"//    \\ \\/  \\/ / | '_ \\  / /\\ / /   / /     //"
                      ,"//     \\  /\\  /| | | | |/ /_// /___/ /___   //"
                      ,"//      \\/  \\/ |_|_| |_/___,'\\____/\\____/   //"
                      ,"//                                          //"
                      ,"//   This software is still BETA software   //"
                      ,"//                                          //"
                      ,"//     GENERATED BY WinDLL VERSION "++ verStr ++ "    //"
                      ,"// DO NOT MODIFY, IF YOU DO THE MARSHALLING //"
                      ,"// MIGHT FAIL BECAUSE OF INCOMPATIBILITIES  //"
                      ,"//////////////////////////////////// WinDLL //"
                      ]