CreateBasicAPIExamples.icl 3.75 KB
Newer Older
Mart Lubbers's avatar
Mart Lubbers committed
1 2
module CreateBasicAPIExamples

3
import StdEnv
Mart Lubbers's avatar
Mart Lubbers committed
4 5 6 7 8 9 10 11 12 13 14
import Data.Functor
import Data.Func
import Data.Error
import Data.Maybe
import Data.Tuple
import System.FilePath
import System.File
import System.Directory
import System.OSError
import Text

15 16
(>>=) infixl 1
(>>=) :== seqErrorsSt
Mart Lubbers's avatar
Mart Lubbers committed
17 18 19 20 21 22

filterDirs :: FilePath -> Bool
filterDirs "." = False
filterDirs ".." = False
filterDirs _ = True

23 24 25 26
seq [] = tuple (Ok [])
seq [e:es] = e >>= \a->seq es >>= \as->tuple (Ok [a:as])

recurse :: FilePath -> .(*World -> *(MaybeError OSError [FilePath], !*World))
Mart Lubbers's avatar
Mart Lubbers committed
27
recurse root
28 29 30 31
	| endsWith ".dcl" root = tuple (Ok [root])
	= getFileInfo root >>= \fi->if fi.directory
			(readDirectory root >>= ((o) (appFst (fmap flatten))) o seq o map (recurse o (</>)root) o filter filterDirs)
			(tuple (Ok []))
Mart Lubbers's avatar
Mart Lubbers committed
32 33

makeExs :: [FilePath] -> [String]
34
makeExs i =
35 36
	[ "module BasicAPIExamples\n"
	, "\n"
37
	, "import iTasks\n"
38
	, "import Text.HTML\n"
39
	, "import qualified iTasks.Extensions.Admin.UserAdmin\n"
40 41 42 43
	, "\n"
	, join "\n" ["import qualified " +++ toDots i\\i<-i]
	, "\n\n"
	, "Start :: *World -> *World\n"
44
	, "Start world = doTasks {WorkflowCollection|name=name,loginMessage=Just loginMessage,welcomeMessage=Just welcomeMessage,allowGuests=False,workflows=basicAPIExamples} world\n"
45
	, "where\n"
46
	, "\tname = \"iTasks Example Collection\"\n"
47 48 49 50 51 52 53
	, "\tloginMessage = DivTag []\n"
	, "\t\t[Text \"iTasks is a framework to create information systems from task specifications.\",BrTag []\n"
	, "\t\t,Text \"Although useful for support of individual tasks, information systems add even more value when a task \"\n"
	, "\t\t,Text \"requires multiple people to work together to accomplish it.\"\n"
	, "\t\t,Text \"Therefore this example application is a multi-user demonstration of tasks expressed in iTasks.\",BrTag [],BrTag[]\n"

	, "\t\t,Text \"You can log in with a demonstration user account:\",BrTag []\n"
54
	, "\t\t\t,UlTag []\n"
55
	, "\t\t\t\t[LiTag [] [Text \"Alice (username: alice, password: alice)\"]\n"
56 57
	, "\t\t\t\t,LiTag [] [Text \"Bob (username: bob, password: bob)\"]\n"
	, "\t\t\t\t,LiTag [] [Text \"Carol (username: carol, password: carol)\"]\n"
58
	, "\t\t\t\t,LiTag [] [Text \"An administrator with full access (username: root, password: root)\"]\n"
59 60
	, "\t\t\t\t]\n"
	, "\t\t\t]\n"
61
	, "\n"
62 63 64 65 66 67 68 69 70
  	, "\twelcomeMessage = DivTag []\n"
    , "\t\t[H1Tag [] [Text \"Welcome\"],PTag []\n"
    , "\t\t\t[Text \"In this generic application you can work on multiple tasks concurrently.\", BrTag []\n"
    , "\t\t\t,Text \"In the list above you can see the set of ongoing tasks that you can choose to work on.\", BrTag []\n"
    , "\t\t\t,Text \"Additionally you can add tasks to this list with the 'New' button. This will open a window with a collection of predefined tasks.\", BrTag []\n"
    , "\t\t\t,Text \"These tasks range from simple TODO items, to complex multi-user workflows.\", BrTag []\n"
    , "\t\t\t]\n"
	, "\t\t]\n"
	, "\n"
71
	, "basicAPIExamples :: [Workflow]\n"
Mart Lubbers's avatar
Mart Lubbers committed
72
	, "basicAPIExamples =\n"
73
	, "\t[",join "\n\t," (defaultWfs ++ exampleWfs), "\n\t]\n"]
74
where
75 76
	defaultWfs = ["restrictedTransientWorkflow \"Users\" \"User management\" [\"admin\"] 'iTasks.Extensions.Admin.UserAdmin'.manageUsers"]
	exampleWfs = map (\i->concat ["'", toDots i, "'.wf \"", toString (insertSpaces 0 (dropExtension i)), "\""]) i
77 78 79 80 81 82
	toDots = join "." o split (toString pathSeparator) o dropExtension
	insertSpaces i s
		| i == size s = []
		| s % (i, i+2) == "API" = [' ','A','P','I':insertSpaces (i+3) s]
		| isUpper s.[i] && (i == 0  || s.[i-1] <> '/') = [' ',s.[i]:insertSpaces (i+1) s]
		= [s.[i]:insertSpaces (i+1) s]
Mart Lubbers's avatar
Mart Lubbers committed
83 84 85

Start w
# (io, w) = stdio w
86
# (mcwd, w) = recurse "BasicAPIExamples" w
Mart Lubbers's avatar
Mart Lubbers committed
87 88 89 90
| isError mcwd = abort ("Error in getting the files: " +++ toString (snd (fromError mcwd)) +++ "\n")
# io = foldl (<<<) io (makeExs (fromOk mcwd))
# (ok, w) = fclose io w
| not ok = abort "Couldn't close stdio\n"
91
= w