aboutsummaryrefslogtreecommitdiff
path: root/clpm.icl
blob: dab9e83c19a318434374ab3fbc832c456d0125fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
module clpm

from StdFunc import flip, o
import StdFile
import StdList
import StdOverloaded
import StdString
import StdTuple

import Control.Applicative
import Control.Monad

import Data.Error
from Data.Func import $
import Data.Functor
import Data.Maybe
import Data.Tuple

from Text import instance + String
import Text.JSON

import System.CommandLine
import System.Directory
import System.File

import CLPM.Package

:: Action
	= NoAction
	| Install
	| Make

:: Arguments
	= { package_file :: String
	  , action       :: Action
	  }

instance zero Arguments
where
	zero
		= { package_file = PACKAGE_FILE
		  , action = NoAction
		  }

Start w
# (io,w) = stdio w
# ((missed, args), w) = appFst (parseArgs [] zero o tl) $ getCommandLine w
| not (isEmpty missed)
	# io = foldr (flip (<<<)) io ["Unknown option: " + m + "\n" \\ m <- missed]
	# (_,w) = fclose io w
	= w
# (pkg,w) = readPackage args.package_file w
| isNothing pkg
	# io = io <<< "Could not parse " <<< args.package_file <<< '\n'
	# (_,w) = fclose io w
	= w
# pkg = fromJust pkg
# (io,w) = do args pkg io w
# (_,w) = fclose io w
= w

do :: Arguments Package *File *World -> *(*File, *World)
do args pkg io w = case args.action of
	NoAction = (io,w)
	Install  = install pkg io w
	Make     = make pkg io w

install :: Package *File *World -> *(*File, *World)
install pkg io w
# (e,w) = fileExists MODULE_DIR w
# w = if e w (snd $ createDirectory MODULE_DIR w)
# (r,w) = syscall "echo hello" w
# io = io <<< r <<< "\n"
= (io,w)

make :: Package *File *World -> *(*File, *World)
make pkg io w
# (ps,w) = getRecursivePaths pkg w
| isError ps
	= (io <<< fromError ps <<< "\n", w)
# ps = fromOk ps
# (r,w) = syscall (foldl (+) "clm" [" -I " + p \\ p <- ps] +
	foldl (+) "" [" " + f \\ f <- optionsToFlags pkg.options] +
	" " + pkg.main + " -o " + pkg.main) w
= (io,w)

parseArgs :: [String] Arguments [String] -> ([String], Arguments)
parseArgs miss args []
	= (miss, args)
parseArgs miss args ["-p":name:rest]
	= parseArgs miss {args & package_file = name} rest
parseArgs miss args ["install":rest]
	= parseArgs miss {args & action = Install} rest
parseArgs miss args ["make":rest]
	= parseArgs miss {args & action = Make} rest
parseArgs miss args [a:rest]
	= parseArgs [a:miss] args rest

syscall :: !String !*World -> !*(!Int, !*World)
syscall cmd w = code {
	ccall system "s:I:A"
}