call-plantuml (empty) → 0.0.1
raw patch · 9 files changed
+280/−0 lines, 9 filesdep +basedep +bytestringdep +call-plantuml
Dependencies added: base, bytestring, call-plantuml, filepath, hspec, process
Files
- ChangeLog.md +5/−0
- LICENSE +10/−0
- README.md +11/−0
- call-plantuml.cabal +70/−0
- data/COPYING +27/−0
- data/plantuml.jar too large to diff
- src/Language/PlantUML/Call.hs +132/−0
- test/Language/PlantUML/CallSpec.hs +24/−0
- test/Spec.hs +1/−0
+ ChangeLog.md view
@@ -0,0 +1,5 @@+# Change Log++## Version 0.0.1++- provide basic ability to call PlantUML
+ LICENSE view
@@ -0,0 +1,10 @@+The MIT License (MIT)+=====================++Copyright © 2022 Marcellus Siegburg++Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ README.md view
@@ -0,0 +1,11 @@+# `call-plantuml` []++This is a simple library to call [PlantUML](https://plantuml.com) given a diagram specification.+PlantUML is included (as JAR file) within this library.++## Requirements++- Java Runtime Environment:+ There is currently no warning if you have not set up any Java Runtime Environment.+ However, you will get runtime errors if it is not available when a call to PlantUML happens.+ If you want to force a check, please perform the test cases.
+ call-plantuml.cabal view
@@ -0,0 +1,70 @@+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.34.4.+--+-- see: https://github.com/sol/hpack++name: call-plantuml+version: 0.0.1+synopsis: A simple library to call PlantUML given a diagram specification+description: Please see the README on GitHub at <https://github.com/marcellussiegburg/call-plantuml#readme>+category: Graphics, Language+homepage: https://github.com/marcellussiegburg/call-plantuml#readme+bug-reports: https://github.com/marcellussiegburg/call-plantuml/issues+author: Marcellus Siegburg+maintainer: marcellus.siegburg@uni-due.de+copyright: 2022 Marcellus Siegburg+license: MIT+license-file: LICENSE+build-type: Simple+extra-source-files:+ README.md+ LICENSE+ ChangeLog.md+data-files:+ COPYING+ plantuml.jar+data-dir: data++source-repository head+ type: git+ location: https://github.com/marcellussiegburg/call-plantuml++library+ exposed-modules:+ Language.PlantUML.Call+ other-modules:+ Paths_call_plantuml+ hs-source-dirs:+ src+ ghc-options: -Wall -Wincomplete-uni-patterns -Wincomplete-record-updates -Widentities -Wredundant-constraints+ build-depends:+ base >=4.12 && <5+ , bytestring >=0.10.4 && <0.12+ , filepath ==1.4.*+ , process ==1.6.*+ if os(windows)+ cpp-options: -DWINDOWS+ else+ default-language: Haskell2010++test-suite call-plantuml-test+ type: exitcode-stdio-1.0+ main-is: Spec.hs+ other-modules:+ Language.PlantUML.CallSpec+ Paths_call_plantuml+ hs-source-dirs:+ test+ ghc-options: -Wall -Wincomplete-uni-patterns -Wincomplete-record-updates -Widentities -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N+ build-depends:+ base >=4.12 && <5+ , bytestring >=0.10.4 && <0.12+ , call-plantuml+ , filepath ==1.4.*+ , hspec+ , process ==1.6.*+ if os(windows)+ cpp-options: -DWINDOWS+ else+ default-language: Haskell2010
+ data/COPYING view
@@ -0,0 +1,27 @@+======================================================================== +PlantUML : a free UML diagram generator +======================================================================== + +(C) Copyright 2009-2017, Arnaud Roques + +PlantUML is free software; you can redistribute it and/or modify it under the +terms of the MIT License. + +See http://opensource.org/licenses/MIT + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ data/plantuml.jar view
file too large to diff
+ src/Language/PlantUML/Call.hs view
@@ -0,0 +1,132 @@+{-# LANGUAGE CPP #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE ScopedTypeVariables #-}+{-|+Module : Language.PlantUML.Call+Description : A simple library to call PlantUML given a diagram specification+Copyright : (c) Marcellus Siegburg, 2022+License : MIT+Maintainer : marcellus.siegburg@uni-due.de++This module provides the basic functionality to call PlantUML.+-}+module Language.PlantUML.Call (+ DiagramType (..),+ drawPlantUMLDiagram,+ ) where++import Paths_call_plantuml (getDataDir)++import qualified Data.ByteString.Char8 as BS (+ dropWhile,+ head,+ null,+ putStrLn,+ tail,+ )++import Control.Concurrent (+ forkIO, killThread, newEmptyMVar, putMVar, takeMVar,+ )+import Control.Monad (unless, when)+import Data.ByteString (ByteString, hGetContents, hPutStr)+import Data.ByteString.Char8 (unpack)+import System.Exit (ExitCode (..))+import System.FilePath+ ((</>), (<.>))+import System.IO (+ hClose,+ hFlush,+#ifndef mingw32_HOST_OS+ BufferMode (NoBuffering),+ hSetBuffering,+#endif+ )+import System.Process (+ CreateProcess (..), StdStream (..),+ createProcess, proc, waitForProcess,+ )++{-|+An output format for PlantUML.+-}+data DiagramType =+ ASCIIArt |+ ASCIIArtUnicode |+ EPS |+ LaTeX |+ LaTeXFull |+ PNG |+ SVG |+ VDX+ deriving (Bounded, Enum, Read, Show)++typeShortName :: DiagramType -> String+typeShortName x = case x of+ ASCIIArt -> "txt"+ ASCIIArtUnicode -> "utxt"+ EPS -> "eps"+ LaTeX -> "latex"+ LaTeXFull -> "latex:nopreamble"+ PNG -> "png"+ SVG -> "svg"+ VDX -> "vdx"++{-|+This function may be used to draw a PlantUML diagram given a valid+specification and a return type.+It calls PlantUML via Java.+-}+drawPlantUMLDiagram+ :: DiagramType+ -- ^ The return type of diagram to return+ -> ByteString+ -- ^ The PlantUML diagram specification which should be loaded+ -> IO ByteString+drawPlantUMLDiagram what content = do+ dataDir <- getDataDir+ let callPlantUML = proc "java" [+ "-jar", dataDir </> "plantuml" <.> "jar",+ "-p", "-t" ++ typeShortName what, "-nometadata", "-noerror"+ ]+ (Just hin, Just hout, Just herr, ph) <-+ createProcess callPlantUML {+ std_out = CreatePipe,+ std_in = CreatePipe,+ std_err = CreatePipe+ }+ pout <- listenForOutput hout+ perr <- listenForOutput herr+#ifndef mingw32_HOST_OS+ hSetBuffering hin NoBuffering+#endif+ hPutStr hin content+ hFlush hin+ hClose hin+ out <- getOutput pout+ err <- getOutput perr+ printContentOnError ph out+ unless (BS.null err) $ fail $ unpack err+ return out+ where+ printContentOnError ph out = do+ code <- waitForProcess ph+ when (code == ExitFailure 1 || isError out)+ $ BS.putStrLn $ "Error on calling PlantUML with:\n" <> content+ listenForOutput h = do+ mvar <- newEmptyMVar+ pid <- forkIO $ hGetContents h >>= putMVar mvar+ return (pid, mvar)+ getOutput (pid, mvar) = do+ output <- takeMVar mvar+ killThread pid+ return output++isError :: ByteString -> Bool+isError xs =+ let ys = BS.dropWhile (== ' ') xs+ zs = BS.dropWhile (== ' ') $ BS.tail ys+ in not (BS.null ys)+ && BS.head ys == '\n'+ && not (BS.null zs)+ && BS.head zs == '\n'
+ test/Language/PlantUML/CallSpec.hs view
@@ -0,0 +1,24 @@+{-# LANGUAGE OverloadedStrings #-}+module Language.PlantUML.CallSpec (spec) where++import qualified Data.ByteString.Char8 as BS (filter, length)++import Test.Hspec++import Control.Monad (forM_)+import Data.ByteString.Char8 (ByteString)+import Language.PlantUML.Call (DiagramType (..), drawPlantUMLDiagram)++spec :: Spec+spec =+ describe "existsInstance" $ do+ it "an empty specification cannot be drawn" $+ drawPlantUMLDiagram SVG "" `shouldThrow` anyIOException+ it "generates the correct solution for hello world and ASCIIArt" $+ (BS.filter (/= '\r') <$> drawPlantUMLDiagram ASCIIArt helloWorld) `shouldReturn`+ " ,-. ,-----.\n |I| |World|\n `+' `--+--'\n | hello | \n |------------->| \n ,+. ,--+--.\n |I| |World|\n `-' `-----'\n"+ forM_ [minBound ..] $ \what -> it ("hello world is working on " ++ show what) $+ ((> 200) . BS.length <$> drawPlantUMLDiagram what helloWorld) `shouldReturn` True++helloWorld :: ByteString+helloWorld = "@startuml\nI -> World : hello\n@enduml"
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}