1+ // Licensed to Elasticsearch B.V under one or more agreements.
2+ // Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+ // See the LICENSE file in the project root for more information
4+
5+ module CommandLine
6+
7+ open Argu
8+ open Microsoft.FSharp .Reflection
9+ open System
10+ open Bullseye
11+
12+ type Build =
13+ | [<CliPrefix( CliPrefix.None); SubCommand>] Clean
14+ | [<CliPrefix( CliPrefix.None); SubCommand>] Version
15+ | [<CliPrefix( CliPrefix.None); Hidden; SubCommand>] Compile
16+ | [<CliPrefix( CliPrefix.None); SubCommand>] Build
17+ | [<CliPrefix( CliPrefix.None); SubCommand>] Test
18+
19+ | [<CliPrefix( CliPrefix.None); SubCommand>] Format
20+
21+ | [<CliPrefix( CliPrefix.None); Hidden; SubCommand>] Lint
22+ | [<CliPrefix( CliPrefix.None); Hidden; SubCommand>] PristineCheck
23+ | [<CliPrefix( CliPrefix.None); Hidden; SubCommand>] ValidateLicenses
24+
25+ | [<CliPrefix( CliPrefix.None); SubCommand>] Publish
26+ | [<CliPrefix( CliPrefix.None); Hidden; SubCommand>] PublishBinaries
27+ | [<CliPrefix( CliPrefix.None); Hidden; SubCommand>] PublishContainers
28+
29+ | [<CliPrefix( CliPrefix.None); SubCommand>] ReleaseNotes
30+ | [<CliPrefix( CliPrefix.None); SubCommand>] Release
31+
32+ | [<Inherit; AltCommandLine( " -s" ) >] Single_ Target
33+ | [<Inherit>] Token of string
34+ | [<Inherit; AltCommandLine( " -c" ) >] Skip_ Dirty_ Check
35+ with
36+ interface IArgParserTemplate with
37+ member this.Usage =
38+ match this with
39+ // commands
40+ | Clean -> " clean known output locations"
41+ | Version -> " print version information"
42+ | Build -> " Run build"
43+
44+ | Test -> " runs a clean build and then runs all the tests "
45+ | Release -> " runs build, tests, and create and validates the packages shy of publishing them"
46+ | Publish -> " Publishes artifacts"
47+ | Format -> " runs dotnet format"
48+
49+ // steps
50+ | Lint
51+ | PristineCheck
52+ | PublishBinaries
53+ | PublishContainers
54+ | ValidateLicenses
55+ | ReleaseNotes
56+ | Compile
57+
58+ // flags
59+ | Single_ Target -> " Runs the provided sub command without running their dependencies"
60+ | Token _ -> " Token to be used to authenticate with github"
61+ | Skip_ Dirty_ Check -> " Skip the clean checkout check that guards the release/publish targets"
62+
63+ member this.StepName =
64+ match FSharpValue.GetUnionFields( this, typeof< Build>) with
65+ | case, _ -> case.Name.ToLowerInvariant()
66+
67+ static member Targets =
68+ let cases = FSharpType.GetUnionCases( typeof< Build>)
69+ seq {
70+ for c in cases do
71+ if c.GetFields() .Length = 0 then
72+ FSharpValue.MakeUnion( c, [| |]) :?> Build
73+ }
74+
75+ static member Ignore ( _ : Build ) _ = ()
76+
77+ static member Step action ( target : Build ) parsed =
78+ Targets.Target( target.StepName, Action( fun _ -> action( parsed)))
79+
80+ static member Cmd ( dependsOn : Build list ) ( composedOf : Build list ) action ( target : Build ) ( parsed : ParseResults < Build >) =
81+ let singleTarget = parsed.TryGetResult Single_ Target |> Option.isSome
82+ let dependsOn = if singleTarget then [] else dependsOn
83+
84+ let steps = dependsOn @ composedOf |> List.map _. StepName
85+ Targets.Target( target.StepName, steps, Action( fun _ -> action parsed))
0 commit comments