19
19
# ##########
20
20
# Options #
21
21
# ##########
22
- # TODO should this opt be removed: ("name", :cmd, :temp => false)
23
22
struct OptionSpec
24
23
name:: String
25
24
short_name:: Union{Nothing,String}
@@ -101,15 +100,15 @@ struct ArgSpec
101
100
end
102
101
const CommandDeclaration = Tuple{CommandKind,
103
102
Vector{String}, # names
104
- Function, # handler
103
+ Union{Nothing, Function} , # handler
105
104
Tuple{ArgClass, Vector{Int}}, # argument count
106
105
Vector{OptionDeclaration}, # options
107
106
Union{Nothing, Markdown. MD}, # help
108
107
}
109
108
struct CommandSpec
110
109
kind:: CommandKind
111
110
names:: Vector{String}
112
- handler:: Function
111
+ handler:: Union{Nothing, Function}
113
112
argument_spec:: ArgSpec # note: just use range operator for max/min
114
113
option_specs:: Dict{String, OptionSpec}
115
114
help:: Union{Nothing, Markdown.MD}
311
310
# #############
312
311
const Token = Union{String, VersionRange, Rev}
313
312
const PkgArguments = Union{Vector{String}, Vector{PackageSpec}}
314
- # TODO embed spec in PkgCommand?
315
313
struct PkgCommand
316
314
meta_options:: Vector{Option}
317
315
spec:: CommandSpec
@@ -321,14 +319,14 @@ struct PkgCommand
321
319
PkgCommand (meta_opts, cmd_name, opts, args) = new (meta_opts, cmd_name, opts, args)
322
320
end
323
321
324
- const APIOption = Pair {Symbol, Any}
325
- APIOptions (command:: PkgCommand ):: Vector{APIOption } =
322
+ const APIOptions = Dict {Symbol, Any}
323
+ APIOptions (command:: PkgCommand ):: Dict{Symbol, Any } =
326
324
APIOptions (command. options, command. spec. option_specs)
327
325
328
326
function APIOptions (options:: Vector{Option} ,
329
327
specs:: Dict{String, OptionSpec} ,
330
- ):: Vector{APIOption }
331
- return map (options) do opt
328
+ ):: Dict{Symbol, Any }
329
+ keyword_vec = map (options) do opt
332
330
spec = specs[opt. val]
333
331
# opt is switch
334
332
spec. is_switch && return spec. api
@@ -337,18 +335,9 @@ function APIOptions(options::Vector{Option},
337
335
# given opt wrapper
338
336
return spec. api. first => spec. api. second (opt. argument)
339
337
end
338
+ return Dict (keyword_vec)
340
339
end
341
340
342
- function key_api (key:: Symbol , api_opts:: Vector{APIOption} )
343
- index = findfirst (x-> x. first == key, api_opts)
344
- if index != = nothing
345
- return api_opts[index]. second
346
- end
347
- end
348
-
349
- set_default! (opt, api_opts:: Vector{APIOption} ) =
350
- key_api (opt. first, api_opts) === nothing && push! (api_opts, opt)
351
-
352
341
function enforce_argument_order (args:: Vector{Token} )
353
342
prev_arg = nothing
354
343
function check_prev_arg (valid_type:: DataType , error_message:: AbstractString )
@@ -496,6 +485,8 @@ function PkgCommand(statement::Statement)::PkgCommand
496
485
return PkgCommand (meta_opts, statement. command, opts, args)
497
486
end
498
487
488
+ Context! (ctx:: APIOptions ):: Context = Types. Context! (collect (ctx))
489
+
499
490
# ############
500
491
# Execution #
501
492
# ############
@@ -519,15 +510,14 @@ function do_cmd(repl::REPL.AbstractREPL, input::String; do_rethrow=false)
519
510
end
520
511
521
512
function do_cmd! (command:: PkgCommand , repl)
522
- meta_opts = APIOptions (command. meta_options, meta_option_specs)
523
- ctx = Context (meta_opts... )
513
+ context = APIOptions (command. meta_options, meta_option_specs)
524
514
spec = command. spec
525
515
526
516
# REPL specific commands
527
517
if spec. kind == CMD_HELP
528
- return Base. invokelatest (do_help!, ctx, command, repl)
518
+ return Base. invokelatest (do_help!, command, repl)
529
519
elseif spec. kind == CMD_PREVIEW
530
- ctx . preview = true
520
+ context[ : preview] = true
531
521
cmd = command. arguments[1 ]
532
522
cmd_spec = get (command_specs, cmd, nothing )
533
523
cmd_spec === nothing &&
@@ -538,10 +528,15 @@ function do_cmd!(command::PkgCommand, repl)
538
528
539
529
# API commands
540
530
# TODO is invokelatest still needed?
541
- Base. invokelatest (spec. handler, ctx, command. arguments, APIOptions (command))
531
+ api_opts = APIOptions (command)
532
+ if applicable (spec. handler, context, command. arguments, api_opts)
533
+ Base. invokelatest (spec. handler, context, command. arguments, api_opts)
534
+ else
535
+ Base. invokelatest (spec. handler, command. arguments, api_opts)
536
+ end
542
537
end
543
538
544
- function do_help! (ctk :: Context , command:: PkgCommand , repl:: REPL.AbstractREPL )
539
+ function do_help! (command:: PkgCommand , repl:: REPL.AbstractREPL )
545
540
disp = REPL. REPLDisplay (repl)
546
541
if isempty (command. arguments)
547
542
Base. display (disp, help)
@@ -562,82 +557,76 @@ function do_help!(ctk::Context, command::PkgCommand, repl::REPL.AbstractREPL)
562
557
end
563
558
564
559
# TODO set default Display.status keyword: mode = PKGMODE_COMBINED
565
- function do_status! (ctx:: Context , args:: PkgArguments , api_opts:: Vector{APIOption} )
566
- set_default! (:mode => PKGMODE_COMBINED, api_opts)
567
- Display. status (ctx, key_api (:mode , api_opts))
568
- end
569
-
570
- # TODO remove the need to specify a handler function (not needed for REPL commands)
571
- do_preview! (ctx:: Context , args:: PkgArguments , api_opts:: Vector{APIOption} ) = nothing
560
+ do_status! (ctx:: APIOptions , args:: PkgArguments , api_opts:: APIOptions ) =
561
+ Display. status (Context! (ctx), get (api_opts, :mode , PKGMODE_COMBINED))
572
562
573
563
# TODO , test recursive dependencies as on option.
574
- function do_test! (ctx:: Context , args:: PkgArguments , api_opts:: Vector{APIOption} )
564
+ function do_test! (ctx:: APIOptions , args:: PkgArguments , api_opts:: APIOptions )
575
565
foreach (arg -> arg. mode = PKGMODE_MANIFEST, args)
576
- API. test (ctx, args; api_opts... )
566
+ API. test (Context! ( ctx) , args; collect ( api_opts) ... )
577
567
end
578
568
579
- function do_registry_add! (ctx:: Context , args:: PkgArguments , api_opts:: Vector{APIOption} )
569
+ function do_registry_add! (ctx:: APIOptions , args:: PkgArguments , api_opts:: APIOptions )
580
570
println (" This is a dummy function for now" )
581
571
println (" My args are:" )
582
572
for arg in args
583
573
println (" - $arg " )
584
574
end
585
575
end
586
576
587
- do_precompile! (ctx:: Context , args:: PkgArguments , api_opts:: Vector{APIOption} ) =
588
- API. precompile (ctx)
577
+ do_precompile! (ctx:: APIOptions , args:: PkgArguments , api_opts:: APIOptions ) =
578
+ API. precompile (Context! ( ctx) )
589
579
590
- do_resolve! (ctx:: Context , args:: PkgArguments , api_opts:: Vector{APIOption} ) =
591
- API. resolve (ctx)
580
+ do_resolve! (ctx:: APIOptions , args:: PkgArguments , api_opts:: APIOptions ) =
581
+ API. resolve (Context! ( ctx) )
592
582
593
- do_gc! (ctx:: Context , args:: PkgArguments , api_opts:: Vector{APIOption} ) =
594
- API. gc (ctx; api_opts... )
583
+ do_gc! (ctx:: APIOptions , args:: PkgArguments , api_opts:: APIOptions ) =
584
+ API. gc (Context! ( ctx); collect ( api_opts) ... )
595
585
596
- do_instantiate! (ctx:: Context , args:: PkgArguments , api_opts:: Vector{APIOption} ) =
597
- API. instantiate (ctx; api_opts... )
586
+ do_instantiate! (ctx:: APIOptions , args:: PkgArguments , api_opts:: APIOptions ) =
587
+ API. instantiate (Context! ( ctx); collect ( api_opts) ... )
598
588
599
- do_generate! (ctx:: Context , args:: PkgArguments , api_opts:: Vector{APIOption} ) =
600
- API. generate (ctx, args[1 ])
589
+ do_generate! (ctx:: APIOptions , args:: PkgArguments , api_opts:: APIOptions ) =
590
+ API. generate (Context! ( ctx) , args[1 ])
601
591
602
- do_build! (ctx:: Context , args:: PkgArguments , api_opts:: Vector{APIOption} ) =
603
- API. build (ctx, args; api_opts... )
592
+ do_build! (ctx:: APIOptions , args:: PkgArguments , api_opts:: APIOptions ) =
593
+ API. build (Context! ( ctx) , args; collect ( api_opts) ... )
604
594
605
- do_rm! (ctx:: Context , args:: PkgArguments , api_opts:: Vector{APIOption} ) =
606
- API. rm (ctx, args; api_opts... )
595
+ do_rm! (ctx:: APIOptions , args:: PkgArguments , api_opts:: APIOptions ) =
596
+ API. rm (Context! ( ctx) , args; collect ( api_opts) ... )
607
597
608
- do_free! (ctx:: Context , args:: PkgArguments , api_opts:: Vector{APIOption} ) =
609
- API. free (ctx, args; api_opts... )
598
+ do_free! (ctx:: APIOptions , args:: PkgArguments , api_opts:: APIOptions ) =
599
+ API. free (Context! ( ctx) , args; collect ( api_opts) ... )
610
600
611
- do_up! (ctx:: Context , args:: PkgArguments , api_opts:: Vector{APIOption} ) =
612
- API. up (ctx, args; api_opts... )
601
+ do_up! (ctx:: APIOptions , args:: PkgArguments , api_opts:: APIOptions ) =
602
+ API. up (Context! ( ctx) , args; collect ( api_opts) ... )
613
603
614
- function do_activate! (ctx:: Context , args:: PkgArguments , api_opts:: Vector{APIOption} )
615
- # TODO : Remove the ctx argument to this function.
604
+ function do_activate! (args:: PkgArguments , api_opts:: APIOptions )
616
605
if isempty (args)
617
606
return API. activate (nothing )
618
607
else
619
608
return API. activate (args[1 ])
620
609
end
621
610
end
622
611
623
- function do_pin! (ctx:: Context , args:: PkgArguments , api_opts:: Vector{APIOption} )
612
+ function do_pin! (ctx:: APIOptions , args:: PkgArguments , api_opts:: APIOptions )
624
613
for arg in args
625
614
# TODO not sure this is correct
626
615
if arg. version. ranges[1 ]. lower != arg. version. ranges[1 ]. upper
627
616
cmderror (" pinning a package requires a single version, not a versionrange" )
628
617
end
629
618
end
630
- API. pin (ctx, args; api_opts... )
619
+ API. pin (Context! ( ctx) , args; collect ( api_opts) ... )
631
620
end
632
621
633
- function do_add! (ctx:: Context , args:: PkgArguments , api_opts:: Vector{APIOption} )
634
- push! ( api_opts, :mode => :add )
635
- API. add_or_develop (ctx, args; api_opts... )
622
+ function do_add! (ctx:: APIOptions , args:: PkgArguments , api_opts:: APIOptions )
623
+ api_opts[ :mode ] = :add
624
+ API. add_or_develop (Context! ( ctx) , args; collect ( api_opts) ... )
636
625
end
637
626
638
- function do_develop! (ctx:: Context , args:: PkgArguments , api_opts:: Vector{APIOption} )
639
- push! ( api_opts, :mode => :develop )
640
- API. add_or_develop (ctx, args; api_opts... )
627
+ function do_develop! (ctx:: APIOptions , args:: PkgArguments , api_opts:: APIOptions )
628
+ api_opts[ :mode ] = :develop
629
+ API. add_or_develop (Context! ( ctx) , args; collect ( api_opts) ... )
641
630
end
642
631
643
632
# #####################
@@ -950,7 +939,7 @@ julia is started with `--startup-file=yes`.
950
939
""" ,
951
940
),( CMD_HELP,
952
941
[" help" , " ?" ],
953
- do_help! ,
942
+ nothing ,
954
943
(ARG_RAW, []),
955
944
[],
956
945
md """
@@ -1201,7 +1190,7 @@ Deletes packages that cannot be reached from any existing environment.
1201
1190
""" ,
1202
1191
),( CMD_PREVIEW,
1203
1192
[" preview" ],
1204
- do_preview! ,
1193
+ nothing ,
1205
1194
(ARG_RAW, [1 ]),
1206
1195
[],
1207
1196
md """
0 commit comments