-
Notifications
You must be signed in to change notification settings - Fork 374
/
Package.idr
1079 lines (977 loc) · 41.5 KB
/
Package.idr
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module Idris.Package
import Compiler.Common
import Core.Context
import Core.Context.Log
import Core.Core
import Core.Directory
import Core.Metadata
import Core.Name.Namespace
import Core.Options
import Core.Unify
import Data.List
import Data.Maybe
import Data.SnocList
import Data.String
import Data.These
import Parser.Package
import System
import System.Directory
import Libraries.System.Directory.Tree
import System.File
import Libraries.Data.SortedMap
import Libraries.Data.StringMap
import Libraries.Data.StringTrie
import Libraries.Data.WithDefault
import Libraries.Text.Parser
import Libraries.Utils.Path
import Libraries.Text.PrettyPrint.Prettyprinter.Render.String
import Idris.CommandLine
import Idris.Doc.HTML
import Idris.Doc.String
import Idris.Error
import Idris.ModTree
import Idris.Pretty
import Idris.ProcessIdr
import Idris.REPL
import Idris.REPL.Common
import Idris.REPL.Opts
import Idris.SetOptions
import Idris.Syntax
import Idris.Version
import public Idris.Package.Types
import Idris.Package.Init
import Idris.Package.ToJson
%default covering
installDir : PkgDesc -> String
installDir p = name p
++ "-"
++ show (fromMaybe (MkPkgVersion (0 ::: [])) (version p))
data DescField : Type where
PVersion : FC -> PkgVersion -> DescField
PLangVersions : FC -> PkgVersionBounds -> DescField
PVersionDep : FC -> String -> DescField
PAuthors : FC -> String -> DescField
PMaintainers : FC -> String -> DescField
PLicense : FC -> String -> DescField
PBrief : FC -> String -> DescField
PReadMe : FC -> String -> DescField
PHomePage : FC -> String -> DescField
PSourceLoc : FC -> String -> DescField
PBugTracker : FC -> String -> DescField
PDepends : List Depends -> DescField
PModules : List (FC, ModuleIdent) -> DescField
PMainMod : FC -> ModuleIdent -> DescField
PExec : String -> DescField
POpts : FC -> String -> DescField
PSourceDir : FC -> String -> DescField
PBuildDir : FC -> String -> DescField
POutputDir : FC -> String -> DescField
PPrebuild : FC -> String -> DescField
PPostbuild : FC -> String -> DescField
PPreinstall : FC -> String -> DescField
PPostinstall : FC -> String -> DescField
PPreclean : FC -> String -> DescField
PPostclean : FC -> String -> DescField
field : String -> Rule DescField
field fname
= strField PAuthors "authors"
<|> strField PMaintainers "maintainers"
<|> strField PLicense "license"
<|> strField PBrief "brief"
<|> strField PReadMe "readme"
<|> strField PHomePage "homepage"
<|> strField PSourceLoc "sourceloc"
<|> strField PBugTracker "bugtracker"
<|> strField POpts "options"
<|> strField POpts "opts"
<|> strField PSourceDir "sourcedir"
<|> strField PBuildDir "builddir"
<|> strField POutputDir "outputdir"
<|> strField PPrebuild "prebuild"
<|> strField PPostbuild "postbuild"
<|> strField PPreinstall "preinstall"
<|> strField PPostinstall "postinstall"
<|> strField PPreclean "preclean"
<|> strField PPostclean "postclean"
<|> do start <- location
ignore $ exactProperty "version"
mustWork $ do
equals
vs <- choose stringLit (sepBy1 dot' integerLit)
end <- location
the (EmptyRule _) $ case vs of
Left v => pure (PVersionDep (MkFC (PhysicalPkgSrc fname) start end) v)
Right vs => pure (PVersion (MkFC (PhysicalPkgSrc fname) start end)
(MkPkgVersion (fromInteger <$> vs)))
<|> do start <- location
ignore $ exactProperty "langversion"
mustWork $ do
lvs <- langversions
end <- location
pure (PLangVersions (MkFC (PhysicalPkgSrc fname) start end) lvs)
<|> do start <- location
ignore $ exactProperty "version"
mustWork $ do
equals
v <- stringLit
end <- location
pure (PVersionDep (MkFC (PhysicalPkgSrc fname) start end) v)
<|> do ignore $ exactProperty "depends"
mustWork $ do
equals
ds <- sep depends
pure (PDepends ds)
<|> do ignore $ exactProperty "modules"
mustWork $ do
equals
ms <- sep (do start <- location
m <- moduleIdent
end <- location
pure (MkFC (PhysicalPkgSrc fname) start end, m))
pure (PModules ms)
<|> do ignore $ exactProperty "main"
mustWork $ do
equals
start <- location
m <- moduleIdent
end <- location
pure (PMainMod (MkFC (PhysicalPkgSrc fname) start end) m)
<|> do ignore $ exactProperty "executable"
mustWork $ do
equals
e <- (stringLit <|> packageName)
pure (PExec e)
where
data Bound = LT PkgVersion Bool | GT PkgVersion Bool
bound : Rule (List Bound)
bound
= do lte
vs <- sepBy1 dot' integerLit
pure [LT (MkPkgVersion (fromInteger <$> vs)) True]
<|> do gte
vs <- sepBy1 dot' integerLit
pure [GT (MkPkgVersion (fromInteger <$> vs)) True]
<|> do lt
vs <- sepBy1 dot' integerLit
pure [LT (MkPkgVersion (fromInteger <$> vs)) False]
<|> do gt
vs <- sepBy1 dot' integerLit
pure [GT (MkPkgVersion (fromInteger <$> vs)) False]
<|> do eqop
vs <- sepBy1 dot' integerLit
pure [LT (MkPkgVersion (fromInteger <$> vs)) True,
GT (MkPkgVersion (fromInteger <$> vs)) True]
mkBound : List Bound -> PkgVersionBounds -> EmptyRule PkgVersionBounds
mkBound (LT b i :: bs) pkgbs
= maybe (mkBound bs ({ upperBound := Just b,
upperInclusive := i } pkgbs))
(\_ => fail "Dependency already has an upper bound")
pkgbs.upperBound
mkBound (GT b i :: bs) pkgbs
= maybe (mkBound bs ({ lowerBound := Just b,
lowerInclusive := i } pkgbs))
(\_ => fail "Dependency already has a lower bound")
pkgbs.lowerBound
mkBound [] pkgbs = pure pkgbs
langversions : EmptyRule PkgVersionBounds
langversions
= do bs <- sepBy andop bound
mkBound (concat bs) anyBounds
depends : Rule Depends
depends
= do name <- packageName
bs <- sepBy andop bound
pure (MkDepends name !(mkBound (concat bs) anyBounds))
strField : (FC -> String -> DescField) -> String -> Rule DescField
strField fieldConstructor fieldName
= do start <- location
ignore $ exactProperty fieldName
mustWork $ do
equals
str <- stringLit
end <- location
pure $ fieldConstructor (MkFC (PhysicalPkgSrc fname) start end) str
parsePkgDesc : String -> Rule (String, List DescField)
parsePkgDesc fname
= do ignore $ exactProperty "package"
name <- packageName
fields <- many (field fname)
EndOfInput <- peek
| DotSepIdent _ name => fail "Unrecognised property \{show name}"
| tok => fail "Expected end of file"
pure (name, fields)
data ParsedMods : Type where
data MainMod : Type where
addField : {auto c : Ref Ctxt Defs} ->
{auto s : Ref Syn SyntaxInfo} ->
{auto o : Ref ROpts REPLOpts} ->
{auto p : Ref ParsedMods (List (FC, ModuleIdent))} ->
{auto m : Ref MainMod (Maybe (FC, ModuleIdent))} ->
DescField -> PkgDesc -> Core PkgDesc
addField (PVersion fc n) pkg = pure $ { version := Just n } pkg
addField (PLangVersions fc bs) pkg = pure $ { langversion := Just bs } pkg
addField (PVersionDep fc n) pkg
= do emitWarning (Deprecated fc "version numbers must now be of the form x.y.z" Nothing)
pure pkg
addField (PAuthors fc a) pkg = pure $ { authors := Just a } pkg
addField (PMaintainers fc a) pkg = pure $ { maintainers := Just a } pkg
addField (PLicense fc a) pkg = pure $ { license := Just a } pkg
addField (PBrief fc a) pkg = pure $ { brief := Just a } pkg
addField (PReadMe fc a) pkg = pure $ { readme := Just a } pkg
addField (PHomePage fc a) pkg = pure $ { homepage := Just a } pkg
addField (PSourceLoc fc a) pkg = pure $ { sourceloc := Just a } pkg
addField (PBugTracker fc a) pkg = pure $ { bugtracker := Just a } pkg
addField (PDepends ds) pkg = pure $ { depends := ds } pkg
-- we can't resolve source files for modules without knowing the source directory,
-- so we save them for the second pass
addField (PModules ms) pkg = do put ParsedMods ms
pure pkg
addField (PMainMod loc n) pkg = do put MainMod (Just (loc, n))
pure pkg
addField (PExec e) pkg = pure $ { executable := Just e } pkg
addField (POpts fc e) pkg = pure $ { options := Just (fc, e) } pkg
addField (PSourceDir fc a) pkg = pure $ { sourcedir := Just a } pkg
addField (PBuildDir fc a) pkg = pure $ { builddir := Just a } pkg
addField (POutputDir fc a) pkg = pure $ { outputdir := Just a } pkg
addField (PPrebuild fc e) pkg = pure $ { prebuild := Just (fc, e) } pkg
addField (PPostbuild fc e) pkg = pure $ { postbuild := Just (fc, e) } pkg
addField (PPreinstall fc e) pkg = pure $ { preinstall := Just (fc, e) } pkg
addField (PPostinstall fc e) pkg = pure $ { postinstall := Just (fc, e) } pkg
addField (PPreclean fc e) pkg = pure $ { preclean := Just (fc, e) } pkg
addField (PPostclean fc e) pkg = pure $ { postclean := Just (fc, e) } pkg
addFields : {auto c : Ref Ctxt Defs} ->
{auto s : Ref Syn SyntaxInfo} ->
{auto o : Ref ROpts REPLOpts} ->
(setSrc : Bool) ->
List DescField -> PkgDesc -> Core PkgDesc
addFields setSrc xs desc = do
p <- newRef ParsedMods []
m <- newRef MainMod Nothing
added <- go {p} {m} xs desc
when setSrc $ setSourceDir (sourcedir added)
ms <- get ParsedMods
mmod <- get MainMod
pure $
{ modules := !(traverse toSource ms)
, mainmod := !(traverseOpt toSource mmod)
} added
where
toSource : (FC, ModuleIdent) -> Core (ModuleIdent, String)
toSource (loc, ns) = pure (ns, !(nsToSource loc ns))
go : {auto p : Ref ParsedMods (List (FC, ModuleIdent))} ->
{auto m : Ref MainMod (Maybe (FC, ModuleIdent))} ->
List DescField -> PkgDesc -> Core PkgDesc
go [] dsc = pure dsc
go (x :: xs) dsc = go xs !(addField x dsc)
runScript : Maybe (FC, String) -> Core ()
runScript Nothing = pure ()
runScript (Just (fc, s))
= do res <- coreLift $ system s
when (res /= 0) $
throw (GenericMsg fc "Script failed")
export
parsePkgFile : {auto c : Ref Ctxt Defs} ->
{auto s : Ref Syn SyntaxInfo} ->
{auto o : Ref ROpts REPLOpts} ->
(setSrc : Bool) -> -- parse package file as a dependency
String -> Core PkgDesc
parsePkgFile setSrc file = do
Right (pname, fs) <- coreLift $ parseFile file $ parsePkgDesc file
| Left err => do
Right res <- coreLift (readFile file)
| _ => throw err
setCurrentElabSource res
doc <- perror err
msg <- render doc
throw (UserError msg)
addFields setSrc fs (initPkgDesc pname)
--------------------------------------------------------------------------------
-- Dependency Resolution
--------------------------------------------------------------------------------
record Candidate where
constructor MkCandidate
name : String
version : Maybe PkgVersion
directory : String
toCandidate : (name : String) -> (String,Maybe PkgVersion) -> Candidate
toCandidate name (dir,v) = MkCandidate name v dir
record ResolutionError where
constructor MkRE
decisions : List Candidate
depends : Depends
version : Maybe PkgVersion
prepend : Candidate -> ResolutionError -> ResolutionError
prepend p = { decisions $= (p ::)}
reason : Maybe PkgVersion -> String
reason Nothing = "no matching version is installed"
reason (Just x) = "assigned version \{show x} which is out of bounds"
printResolutionError : ResolutionError -> String
printResolutionError (MkRE ds d v) = go [<] ds
where go : SnocList String -> List Candidate -> String
go ss [] =
let pre := "required \{d.pkgname} \{show d.pkgbounds} but"
in fastConcat . intersperse "; " $ ss <>> ["\{pre} \{reason v}"]
go ss (c :: cs) =
let v := fromMaybe defaultVersion c.version
in go (ss :< "\{c.name}-\{show v}") cs
data ResolutionRes : Type where
Resolved : List String -> ResolutionRes
Failed : List ResolutionError -> ResolutionRes
printErrs : PkgDesc -> List ResolutionError -> String
printErrs x es =
unlines $ "Failed to resolve the dependencies for \{x.name}:"
:: map (indent 2 . printResolutionError) es
-- try all possible resolution paths, keeping the first
-- that works
tryAll : List Candidate
-> (Candidate -> Core ResolutionRes)
-> Core ResolutionRes
tryAll ps f = go [<] ps
where go : SnocList ResolutionError
-> List Candidate
-> Core ResolutionRes
go se [] = pure (Failed $ se <>> [])
go se (x :: xs) = do
Failed errs <- f x | Resolved res => pure (Resolved res)
go (se <>< map (prepend x) errs) xs
addDeps :
{auto c : Ref Ctxt Defs} ->
{auto s : Ref Syn SyntaxInfo} ->
{auto o : Ref ROpts REPLOpts} ->
PkgDesc ->
Core ()
addDeps pkg = do
Resolved allPkgs <- getTransitiveDeps pkg.depends empty
| Failed errs => throw $ GenericMsg EmptyFC (printErrs pkg errs)
log "package.depends" 10 $ "all depends: \{show allPkgs}"
traverse_ addPackageDir allPkgs
traverse_ addDataDir ((</> "data") <$> allPkgs)
where
-- Note: findPkgDir throws an error if a package is not found
-- *unless* --ignore-missing-ipkg is enabled
-- therefore, findPkgDir returns Nothing, skip the package
--
-- We use a backtracking algorithm here: If several versions of
-- a package are installed, we must try all, which are are
-- potentially in bounds, because their set of dependencies
-- might be different across versions and not all of them
-- might lead to a resolvable situation.
getTransitiveDeps :
List Depends ->
(done : StringMap (Maybe PkgVersion)) ->
Core ResolutionRes
getTransitiveDeps [] done = do
ms <- for (StringMap.toList done) $
\(pkg, mv) => findPkgDir pkg (exactBounds mv)
pure . Resolved $ catMaybes ms
getTransitiveDeps (dep :: deps) done =
case lookup dep.pkgname done of
Just mv =>
if inBounds mv dep.pkgbounds
-- already resolved dependency is in bounds
-- so we keep it and resolve remaining deps
then getTransitiveDeps deps done
-- the resolved dependency does not satisfy the
-- current bounds. we return an error and backtrack
else pure (Failed [MkRE [] dep $ mv <|> Just defaultVersion])
Nothing => do
log "package.depends" 50 "adding new dependency: \{dep.pkgname} (\{show dep.pkgbounds})"
pkgDirs <- findPkgDirs dep.pkgname dep.pkgbounds
let candidates := toCandidate dep.pkgname <$> pkgDirs
case candidates of
[] => do
defs <- get Ctxt
if defs.options.session.ignoreMissingPkg
-- this corresponds to what `findPkgDir` does in
-- case of `ignoreMissingPkg` being set to `True`
then getTransitiveDeps deps done
else pure (Failed [MkRE [] dep Nothing])
_ => tryAll candidates $ \(MkCandidate name mv pkgDir) => do
let pkgFile = pkgDir </> name <.> "ipkg"
True <- coreLift $ exists pkgFile
| False => getTransitiveDeps deps (insert name mv done)
pkg <- parsePkgFile False pkgFile
getTransitiveDeps
(pkg.depends ++ deps)
(insert pkg.name pkg.version done)
--------------------------------------------------------------------------------
-- Processing Options
--------------------------------------------------------------------------------
processOptions : {auto c : Ref Ctxt Defs} ->
{auto o : Ref ROpts REPLOpts} ->
Maybe (FC, String) -> Core ()
processOptions Nothing = pure ()
processOptions (Just (fc, opts))
= do let Right clopts = getOpts (words opts)
| Left err => throw (GenericMsg fc err)
ignore $ preOptions clopts
compileMain : {auto c : Ref Ctxt Defs} ->
{auto s : Ref Syn SyntaxInfo} ->
{auto o : Ref ROpts REPLOpts} ->
Name -> String -> String -> Core ()
compileMain mainn mfilename exec
= do modIdent <- ctxtPathToNS mfilename
m <- newRef MD (initMetadata (PhysicalIdrSrc modIdent))
u <- newRef UST initUState
ignore $ loadMainFile mfilename
ignore $ compileExp (PRef replFC mainn) exec
prepareCompilation : {auto c : Ref Ctxt Defs} ->
{auto s : Ref Syn SyntaxInfo} ->
{auto o : Ref ROpts REPLOpts} ->
PkgDesc ->
List CLOpt ->
Core (List Error)
prepareCompilation pkg opts =
do
processOptions (options pkg)
addDeps pkg
ignore $ preOptions opts
runScript (prebuild pkg)
let toBuild = maybe (map snd (modules pkg))
(\m => snd m :: map snd (modules pkg))
(mainmod pkg)
buildAll toBuild
assertIdrisCompatibility : PkgDesc -> Core ()
assertIdrisCompatibility pkg
= do let Just requiredBounds = pkg.langversion
| Nothing => pure ()
unless (inBounds version requiredBounds) $
throw (GenericMsg emptyFC "\{pkg.name} requires Idris2 \{show requiredBounds} but the installed version of Idris2 is \{show Version.version}.")
build : {auto c : Ref Ctxt Defs} ->
{auto s : Ref Syn SyntaxInfo} ->
{auto o : Ref ROpts REPLOpts} ->
PkgDesc ->
List CLOpt ->
Core (List Error)
build pkg opts
= do assertIdrisCompatibility pkg
[] <- prepareCompilation pkg opts
| errs => pure errs
whenJust (executable pkg) $ \ exec =>
do let Just (mainNS, mainFile) = mainmod pkg
| Nothing => throw (GenericMsg emptyFC "No main module given")
let mainName = NS (miAsNamespace mainNS) (UN $ Basic "main")
coreLift $ putStrLn "Now compiling the executable: \{exec}"
compileMain mainName mainFile exec
runScript (postbuild pkg)
pure []
installBuildArtifactFrom : {auto o : Ref ROpts REPLOpts} ->
{auto c : Ref Ctxt Defs} ->
String ->
String -> String -> ModuleIdent -> Core ()
installBuildArtifactFrom file_extension builddir destdir ns
= do let filename_trunk = ModuleIdent.toPath ns
let filename = builddir </> filename_trunk <.> file_extension
let modPath = reverse $ fromMaybe [] $ tail' $ unsafeUnfoldModuleIdent ns
let destNest = joinPath modPath
let destPath = destdir </> destNest
let destFile = destdir </> filename_trunk <.> file_extension
Right _ <- coreLift $ mkdirAll $ destPath
| Left err => throw $ InternalError $ unlines
[ "Can't make directories " ++ show modPath
, show err ]
coreLift $ putStrLn $ "Installing " ++ filename ++ " to " ++ destPath
Right _ <- coreLift $ copyFile filename destFile
| Left err => throw $ InternalError $ unlines
[ "Can't copy file " ++ filename ++ " to " ++ destPath
, show err ]
pure ()
installFrom : {auto o : Ref ROpts REPLOpts} ->
{auto c : Ref Ctxt Defs} ->
String -> String -> ModuleIdent -> Core ()
installFrom builddir destdir ns = do
installBuildArtifactFrom "ttc" builddir destdir ns
installBuildArtifactFrom "ttm" builddir destdir ns
let filename_trunk = ModuleIdent.toPath ns
let modPath = reverse $ fromMaybe [] $ tail' $ unsafeUnfoldModuleIdent ns
let destNest = joinPath modPath
let destPath = destdir </> destNest
let installCodeGenFiles = \cg => do
Just cgdata <- getCG cg
| Nothing => pure Nothing
let Just ext = incExt cgdata
| Nothing => pure Nothing
let srcFile = builddir </> filename_trunk <.> ext
let destFile = destdir </> filename_trunk <.> ext
let Just (dir, _) = splitParent destFile
| Nothing => pure Nothing
ensureDirectoryExists dir
pure $ Just (srcFile, destFile)
objPaths_in <- traverse
installCodeGenFiles
(incrementalCGs !getSession)
let objPaths = mapMaybe id objPaths_in
-- Copy object files, if any. They don't necessarily get created,
-- since some modules don't generate any code themselves.
traverse_
(\ (obj, dest) => do
coreLift $ putStrLn $ "Installing " ++ obj ++ " to " ++ destPath
ignore $ coreLift $ copyFile obj dest)
objPaths
installSrcFrom : {auto c : Ref Ctxt Defs} ->
String -> String -> (ModuleIdent, FileName) -> Core ()
installSrcFrom wdir destdir (ns, srcRelPath)
= do let srcfile = ModuleIdent.toPath ns
let srcPath = wdir </> srcRelPath
let Just ext = extension srcPath
| _ => throw (InternalError $
"Unexpected failure when installing source file:\n"
++ srcPath
++ "\n"
++ "Can't extract file extension.")
let modPath = reverse $ fromMaybe [] $ tail' $ unsafeUnfoldModuleIdent ns
let destNest = joinPath modPath
let destPath = destdir </> destNest
let destFile = destdir </> srcfile <.> ext
Right _ <- coreLift $ mkdirAll $ destPath
| Left err => throw $ InternalError $ unlines
[ "Can't make directories " ++ show modPath
, show err ]
coreLift $ putStrLn $ "Installing " ++ srcPath ++ " to " ++ destPath
when !(coreLift $ exists destFile) $ do
-- Grant read/write access to the file we are about to overwrite.
Right _ <- coreLift $ chmod destFile
(MkPermissions [Read, Write] [Read, Write] [Read, Write])
| Left err => throw $ UserError (show err)
pure ()
Right _ <- coreLift $ copyFile srcPath destFile
| Left err => throw $ InternalError $ unlines
[ "Can't copy file " ++ srcPath ++ " to " ++ destPath
, show err ]
-- Make the source read-only
Right _ <- coreLift $ chmod destFile (MkPermissions [Read] [Read] [Read])
| Left err => throw $ UserError (show err)
pure ()
absoluteInstallDir : (relativeInstallDir : String) -> Core String
absoluteInstallDir relativeInstallDir = do
mdestdir <- coreLift $ getEnv "DESTDIR"
let destdir = fromMaybe "" mdestdir
pure $ destdir ++ relativeInstallDir
-- Install all the built modules in prefix/package/
-- We've already built and checked for success, so if any don't exist that's
-- an internal error.
install : {auto c : Ref Ctxt Defs} ->
{auto o : Ref ROpts REPLOpts} ->
PkgDesc ->
List CLOpt ->
(installSrc : Bool) ->
Core ()
install pkg opts installSrc -- not used but might be in the future
= do defs <- get Ctxt
build <- ttcBuildDirectory
let lib = installDir pkg
libTargetDir <- absoluteInstallDir =<< libInstallDirectory lib
ttcTargetDir <- absoluteInstallDir =<< ttcInstallDirectory lib
srcTargetDir <- absoluteInstallDir =<< srcInstallDirectory lib
let src = source_dir (dirs (options defs))
runScript (preinstall pkg)
let toInstall = maybe (modules pkg)
(:: modules pkg)
(mainmod pkg)
wdir <- getWorkingDir
-- Make the package installation directory
Right _ <- coreLift $ mkdirAll libTargetDir
| Left err => throw $ InternalError $ unlines
[ "Can't make directory " ++ libTargetDir
, show err ]
traverse_ (installFrom (wdir </> build) ttcTargetDir . fst) toInstall
when installSrc $ do
traverse_ (installSrcFrom wdir srcTargetDir) toInstall
-- install package file
let pkgFile = libTargetDir </> pkg.name <.> "ipkg"
coreLift $ putStrLn $ "Installing package file for \{pkg.name} to \{libTargetDir}"
let pkgStr = String.renderString $ layoutUnbounded $ pretty $ savePkgMetadata pkg
log "package.depends" 25 $ " package file:\n\{pkgStr}"
coreLift_ $ writeFile pkgFile pkgStr
runScript (postinstall pkg)
where
savePkgMetadata : PkgDesc -> PkgDesc
savePkgMetadata pkg =
the (PkgDesc -> PkgDesc)
{ depends := pkg.depends
, version := pkg.version
} $ initPkgDesc pkg.name
-- Check package without compiling anything.
check : {auto c : Ref Ctxt Defs} ->
{auto s : Ref Syn SyntaxInfo} ->
{auto o : Ref ROpts REPLOpts} ->
PkgDesc ->
List CLOpt ->
Core (List Error)
check pkg opts =
do assertIdrisCompatibility pkg
[] <- prepareCompilation pkg opts
| errs => pure errs
runScript (postbuild pkg)
pure []
makeDoc : {auto c : Ref Ctxt Defs} ->
{auto s : Ref Syn SyntaxInfo} ->
{auto o : Ref ROpts REPLOpts} ->
PkgDesc ->
List CLOpt ->
Core (List Error)
makeDoc pkg opts =
do [] <- prepareCompilation pkg opts
| errs => pure errs
defs <- get Ctxt
let build = build_dir (dirs (options defs))
let docBase = build </> "docs"
let docDir = docBase </> "docs"
Right () <- coreLift $ mkdirAll docDir
| Left err => fileError docDir err
u <- newRef UST initUState
setPPrint (MkPPOpts False True False False)
[] <- concat <$> for (modules pkg) (\(mod, filename) => do
-- load dependencies
let ns = miAsNamespace mod
addImport (MkImport emptyFC False mod ns)
-- generate docs for all visible names
defs <- get Ctxt
let ctxt = gamma defs
visibleDefs <- map catMaybes $ for [1..nextEntry ctxt - 1] $ \ i =>
do -- Select the entries that are from `mod` and visible
Just gdef <- lookupCtxtExact (Resolved i) ctxt
| _ => pure Nothing
let Just nfc = isNonEmptyFC $ location gdef
| _ => do log "doc.module.definitions" 70 $ unwords
[ show mod ++ ":"
, show (fullname gdef)
, "has an empty FC"
]
pure Nothing
let PhysicalIdrSrc mod' = origin nfc
| _ => pure Nothing
let True = mod == mod'
| _ => do log "doc.module.definitions" 60 $ unwords
[ show mod ++ ":"
, show (fullname gdef)
, "was defined in"
, show mod'
]
pure Nothing
let True = visible gdef
| _ => pure Nothing
pure (Just gdef)
let outputFilePath = docDir </> (show mod ++ ".html")
allDocs <- for (sortBy (compare `on` startPos . toNonEmptyFC . location) visibleDefs) $ \ def =>
getDocsForName emptyFC (fullname def) shortNamesConfig
let allDecls = annotate Declarations $ vcat allDocs
-- grab module header doc
syn <- get Syn
let modDoc = lookup mod (modDocstrings syn)
log "doc.module" 10 $ unwords
[ "Looked up doc for"
, show mod
, "and got:"
, show modDoc
]
log "doc.module" 100 $ "from: " ++ show (modDocstrings syn)
-- grab publically re-exported modules
let mreexports = do docs <- lookup mod $ modDocexports syn
guard (not $ null docs)
pure docs
whenJust mreexports $ \ reexports =>
log "doc.module" 15 $ unwords
[ "All imported:", show reexports]
let modExports = map (map (reAnnotate Syntax . prettyImport)) mreexports
Right () <- do doc <- renderModuleDoc mod modDoc modExports
(allDecls <$ guard (not $ null allDocs))
coreLift $ writeFile outputFilePath doc
| Left err => fileError (docBase </> "index.html") err
pure $ the (List Error) []
)
| errs => pure errs
Right () <- do syn <- get Syn
coreLift $ writeFile (docBase </> "index.html") $ renderDocIndex pkg (modDocstrings syn)
| Left err => fileError (docBase </> "index.html") err
errs <- for cssFiles $ \ cssFile => do
let fn = cssFile.filename ++ ".css"
css <- readDataFile ("docs/" ++ fn)
Right () <- coreLift $ writeFile (docBase </> fn) css
| Left err => fileError (docBase </> fn) err
pure (the (List Error) [])
let [] = concat errs
| err => pure err
runScript (postbuild pkg)
pure []
where
visible : GlobalDef -> Bool
visible def = case definition def of
(DCon _ _ _) => False
_ => (collapseDefault (visibility def) /= Private)
fileError : String -> FileError -> Core (List Error)
fileError filename err = pure [FileErr filename err]
-- Data.These.bitraverse hand specialised for Core
bitraverseC : (a -> Core c) -> (b -> Core d) -> These a b -> Core (These c d)
bitraverseC f g (This a) = [| This (f a) |]
bitraverseC f g (That b) = [| That (g b) |]
bitraverseC f g (Both a b) = [| Both (f a) (g b) |]
-- Data.StringTrie.foldWithKeysM hand specialised for Core
foldWithKeysC : Monoid b => (List String -> Core b) -> (List String -> a -> Core b) -> StringTrie a -> Core b
foldWithKeysC {a} {b} fk fv = go []
where
go : List String -> StringTrie a -> Core b
go ks (MkStringTrie nd) =
map bifold $ bitraverseC
(fv ks)
(\sm => foldlC
(\x, (k, vs) =>
do let ks' = ks++[k]
y <- assert_total $ go ks' vs
z <- fk ks'
pure $ x <+> y <+> z)
neutral
(StringMap.toList sm))
nd
clean : {auto c : Ref Ctxt Defs} ->
{auto o : Ref ROpts REPLOpts} ->
PkgDesc ->
List CLOpt ->
Core ()
clean pkg opts -- `opts` is not used but might be in the future
= do defs <- get Ctxt
runScript (preclean pkg)
let pkgmods = maybe
(map fst (modules pkg))
(\m => fst m :: map fst (modules pkg))
(mainmod pkg)
let toClean : List (List String, String)
= mapMaybe (\ mod => case unsafeUnfoldModuleIdent mod of
[] => Nothing
(x :: xs) => Just(xs, x))
pkgmods
srcdir <- getWorkingDir
let d = dirs (options defs)
bdir <- ttcBuildDirectory
let builddir = srcdir </> bdir </> "ttc"
let outputdir = srcdir </> outputDirWithDefault d
-- the usual pair syntax breaks with `No such variable a` here for some reason
let pkgTrie : StringTrie (List String)
= foldl (\trie, ksv =>
let ks = Builtin.fst ksv
v = Builtin.snd ksv
in
insertWith (reverse ks) (maybe [v] (v::)) trie) empty toClean
foldWithKeysC (deleteFolder builddir)
(\ks => map concat . traverse (deleteBin builddir ks))
pkgTrie
deleteFolder builddir []
maybe (pure ()) (\e => delete (outputdir </> e))
(executable pkg)
-- clean out the generated docs
let build = build_dir (dirs (options defs))
deleteDocsFolder $ build </> "docs" </> "docs"
deleteDocsFolder $ build </> "docs"
runScript (postclean pkg)
where
delete : String -> Core ()
delete path = do Right () <- coreLift $ removeFile path
| Left err => pure ()
coreLift $ putStrLn $ "Removed: " ++ path
deleteFolder : String -> List String -> Core ()
deleteFolder builddir ns = delete $ builddir </> joinPath ns
deleteBin : String -> List String -> String -> Core ()
deleteBin builddir ns mod
= do let ttFile = builddir </> joinPath ns </> mod
delete $ ttFile <.> "ttc"
delete $ ttFile <.> "ttm"
deleteDocsFolder : String -> Core ()
deleteDocsFolder dir
= do Right docbasefiles <- coreLift $ listDir dir
| Left err => pure ()
traverse_ (\x => delete $ dir </> x)
docbasefiles
deleteFolder dir []
-- Just load the given module, if it exists, which will involve building
-- it if necessary
runRepl : {auto c : Ref Ctxt Defs} ->
{auto s : Ref Syn SyntaxInfo} ->
{auto o : Ref ROpts REPLOpts} ->
Maybe String ->
Core ()
runRepl fname = do
u <- newRef UST initUState
origin <- maybe
(pure $ Virtual Interactive) (\fname => do
modIdent <- ctxtPathToNS fname
pure (PhysicalIdrSrc modIdent)
) fname
m <- newRef MD (initMetadata origin)
case fname of
Nothing => pure ()
Just fn => do
errs <- loadMainFile fn
displayErrors errs
repl {u} {s}
||| If the user did not provide a package file we can look in the working
||| directory. If there is exactly one `.ipkg` file then use that!
localPackageFile : Maybe String -> Core String
localPackageFile (Just fp) = pure fp
localPackageFile Nothing
= do wdir <- getWorkingDir
tree <- coreLift (explore $ parse wdir)
let candidates = map fileName tree.files
case filter (".ipkg" `isSuffixOf`) candidates of
[fp] => pure fp
[] => throw $ UserError "No .ipkg file supplied and none could be found in the working directory."
_ => throw $ UserError "No .ipkg file supplied and the working directory contains more than one."
processPackage : {auto c : Ref Ctxt Defs} ->
{auto s : Ref Syn SyntaxInfo} ->
{auto o : Ref ROpts REPLOpts} ->
List CLOpt ->
(PkgCommand, Maybe String) ->
Core ()
processPackage opts (cmd, mfile)
= withCtxt . withSyn . withROpts $ case cmd of
Init =>
do Just pkg <- coreLift interactive
| Nothing => coreLift (exitWith (ExitFailure 1))
let fp = fromMaybe (pkg.name ++ ".ipkg") mfile
False <- coreLift (exists fp)
| _ => throw (GenericMsg emptyFC ("File " ++ fp ++ " already exists"))
Right () <- coreLift $ writeFile fp (show $ pretty pkg)
| Left err => throw (FileErr fp err)
pure ()
_ =>
do file <- localPackageFile mfile
let Just (dir, filename) = splitParent file
| _ => throw $ InternalError "Tried to split empty string"
let True = isSuffixOf ".ipkg" filename
| _ => do coreLift $ putStrLn ("Packages must have an '.ipkg' extension: " ++ show file ++ ".")
coreLift (exitWith (ExitFailure 1))
setWorkingDir dir
pkg <- parsePkgFile True filename
whenJust (builddir pkg) setBuildDir
setOutputDir (outputdir pkg)
case cmd of
Build => do [] <- build pkg opts
| errs => coreLift (exitWith (ExitFailure 1))
pure ()
MkDoc => do [] <- makeDoc pkg opts
| errs => coreLift (exitWith (ExitFailure 1))
pure ()
Install => do [] <- build pkg opts
| errs => coreLift (exitWith (ExitFailure 1))
install pkg opts {installSrc = False}
InstallWithSrc =>
do [] <- build pkg opts
| errs => coreLift (exitWith (ExitFailure 1))
install pkg opts {installSrc = True}
Typecheck => do
[] <- check pkg opts
| errs => coreLift (exitWith (ExitFailure 1))
pure ()
Clean => clean pkg opts
REPL => do
[] <- build pkg opts
| errs => coreLift (exitWith (ExitFailure 1))
runRepl (map snd $ mainmod pkg)
Init => pure () -- already handled earlier
DumpJson => coreLift . putStrLn $ toJson pkg
DumpInstallDir => do
libInstallDir <- libInstallDirectory (installDir pkg)
dir <- absoluteInstallDir libInstallDir
coreLift (putStrLn dir)
record PackageOpts where
constructor MkPFR
pkgDetails : List (PkgCommand, Maybe String)
oopts : List CLOpt
hasError : Bool
partitionOpts : List CLOpt -> PackageOpts
partitionOpts opts = foldr pOptUpdate (MkPFR [] [] False) opts
where
data OptType : Type where
PPackage : PkgCommand -> Maybe String -> OptType
POpt : OptType
PIgnore : OptType
PErr : OptType
optType : CLOpt -> OptType
optType (Package cmd f) = PPackage cmd f
optType Quiet = POpt
optType Verbose = POpt
optType (Timing l) = POpt
optType (Logging l) = POpt
optType CaseTreeHeuristics = POpt
optType (DumpANF f) = POpt
optType (DumpCases f) = POpt
optType (DumpLifted f) = POpt
optType (DumpVMCode f) = POpt
optType DebugElabCheck = POpt
optType (SetCG f) = POpt
optType (IncrementalCG _) = POpt
optType (Directive d) = POpt
optType (BuildDir f) = POpt