addLicenseInfo (empty) → 0.0
raw patch · 4 files changed
+165/−0 lines, 4 filesdep +basedep +processsetup-changed
Dependencies added: base, process
Files
- LICENSE +30/−0
- Main.hs +115/−0
- Setup.lhs +3/−0
- addLicenseInfo.cabal +17/−0
+ LICENSE view
@@ -0,0 +1,30 @@+++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions+are met:++Redistributions of source code must retain the above copyright+notice, this list of conditions and the following disclaimer.++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.++Neither the name of the ; 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 OWNER 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.++
+ Main.hs view
@@ -0,0 +1,115 @@+module Main where++import Data.List (find, span, isPrefixOf, stripPrefix)+import Data.Maybe (fromJust)+import System.IO (hClose, hPutStr, hFileSize, hGetContents, IOMode(ReadMode, WriteMode), openFile)+import System.Environment (getArgs)+import System.Cmd (system)++data LicenseInfo = LI { modName :: String+ , author :: String+ , copyHold :: String+ , licenseType :: String+ , maintainer :: String+ , stability :: String+ , portability :: String+ , description :: String}+ deriving (Eq, Show)+++main = do+ ls <- getArgs+ let fp = head ls + let li = if null (tail ls) then "" else (head $ tail ls)+ fileHdl <- openFile fp ReadMode+ fileToModify <- hGetContents fileHdl+ licenseInfo <- if li == "" then getLicenseInfo + else (readFile li) >>= parseLicenseInfoFile+ let licenseInfoWithModName = getModule fileToModify licenseInfo+ let formatInfo = format licenseInfoWithModName+ let formatted = (formatInfo ++ fileToModify)+ writeFile (fp++"tmp") formatted+ hClose fileHdl+ system ("mv " ++ fp ++ "tmp" ++ " " ++ fp) -- Because the Lazy Gods can't read and write+ -- to the same goddamn file without getting+ -- bitchy.+ + +getModule :: String -> LicenseInfo -> LicenseInfo+getModule p (LI _ a c l ma s po d) = LI mod a c l ma s po d+ where mod = filter (/='\n') . getModname . maybeStringToString . findPrefix "module" $ (lines p)+ + ++getModname :: String -> String+getModname s = takeWhile (\x -> not $ elem x " (") $ drop (length "module ") s++findPrefix :: String -> [String] -> Maybe String+findPrefix _ [] = Nothing+findPrefix s (x:xs) | s `isPrefixOf` x = Just x+ | otherwise = findPrefix s xs++ ++maybeToString :: Show a => Maybe a -> String+maybeToString Nothing = ""+maybeToString (Just a) = show a++maybeStringToString :: Maybe String -> String+maybeStringToString Nothing = ""+maybeStringToString (Just s) = s++ +format :: LicenseInfo -> String+format li = (\x -> x ++ "\n") + $ unlines + $ map ("--"++) + $ [ bar+ , bar+ , " |"+ , "Module : " ++ modName li+ , "Author : " ++ author li+ , "License : " ++ licenseType li+ , "Copyright : " ++ copyHold li+ , ""+ , "Maintainer : " ++ maintainer li+ , "Stability : " ++ stability li+ , "Portability : " ++ portability li+ , ""+ , bar+ , "Description : " ++ description li+ , bar+ , bar+ ]+ where bar = replicate 78 '-' + ++ +parseLicenseInfoFile :: String -> IO LicenseInfo+parseLicenseInfoFile s = do + let ls = lines s+ let ls_parsed = map parse ls+ let filt = flip filt' $ ls_parsed+ return $ LI (filt "Module") (filt "Author") (filt "Copyright") (filt "License") (filt "Maintainer") (filt "Stability") (filt "Portability") (filt "Description")+ where parse s = (\x -> (fst x, dropWhile (==' ') $ tail $ snd x)) + $ span (/=':') $ s+ filt' ss l = maybeStringToString $ lookup ss l++getLicenseInfo :: IO LicenseInfo+getLicenseInfo = do + putStrLn "" + license <- grab "Choose a license type?"+ author <- grab "Author?"+ copy <- grab "Copyright holder?"+ mntnr <- grab "Maintainer?"+ stblt <- grab "Stability Level?"+ port <- grab "Portability Level?"+ desc <- grab "Description?"+ return $ LI "" author copy license mntnr stblt port desc+++grab :: String -> IO String+grab s = do + putStrLn s+ get <- getLine+ return get
+ Setup.lhs view
@@ -0,0 +1,3 @@+#!/usr/bin/env runhaskell+> import Distribution.Simple+> main = defaultMain
+ addLicenseInfo.cabal view
@@ -0,0 +1,17 @@+name: addLicenseInfo+version: 0.0+synopsis: Adds license info to the top of a file.+description: Adds license info to the top of a file.+category: Development+license: BSD3+license-file: LICENSE+author: Joe Fredette+maintainer: jfredett@gmail.com+build-depends: base,+ process >= 1.0.1.0++build-type: Simple++executable: addLicenseInfo+main-is: Main.hs+