Skip to content

feat: add support for remaining args #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions Fun.Build.Tests/PipelineBuilderTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -485,3 +485,61 @@ let ``runBeforeEachStage and runAfterEachStage should work`` () =
Assert.Equal(4, j)
Assert.Equal(3, ti)
Assert.Equal(3, tj)

[<Fact>]
let ``check GetAllCmdArgs and RemainingArgs works when remaining args is not empty`` () =
let mutable actualAllCmdArgs = []
let mutable actualRemainingArgs = []

pipeline "demo" {
cmdArgs [ "-p"; "demo"; "test1"; "v1"; "--"; "test2"; "v2" ]
stage "shows arguments" {
run (fun ctx ->
actualAllCmdArgs <- ctx.GetAllCmdArgs()
actualRemainingArgs <- ctx.GetRemainingCmdArgs()
)
}
runImmediate
}

Assert.Equal<string list>([ "-p"; "demo"; "test1"; "v1" ], actualAllCmdArgs)
Assert.Equal<string list>([ "test2"; "v2" ], actualRemainingArgs)

[<Fact>]
let ``check GetAllCmdArgs and RemainingArgs works when remaining args is provided but empty`` () =
let mutable actualAllCmdArgs = []
let mutable actualRemainingArgs = []

pipeline "demo" {
cmdArgs [ "-p"; "demo"; "test1"; "v1"; "--" ]
stage "shows arguments" {
run (fun ctx ->
actualAllCmdArgs <- ctx.GetAllCmdArgs()
actualRemainingArgs <- ctx.GetRemainingCmdArgs()
)
}
runImmediate
}

Assert.Equal<string list>([ "-p"; "demo"; "test1"; "v1" ], actualAllCmdArgs)
Assert.Equal<string list>([], actualRemainingArgs)


[<Fact>]
let ``check GetAllCmdArgs and RemainingArgs works when remaining args is not provided`` () =
let mutable actualAllCmdArgs = []
let mutable actualRemainingArgs = []

pipeline "demo" {
cmdArgs [ "-p"; "demo"; "test1"; "v1" ]
stage "shows arguments" {
run (fun ctx ->
actualAllCmdArgs <- ctx.GetAllCmdArgs()
actualRemainingArgs <- ctx.GetRemainingCmdArgs()
)
}
runImmediate
}

Assert.Equal<string list>([ "-p"; "demo"; "test1"; "v1" ], actualAllCmdArgs)
Assert.Equal<string list>([], actualRemainingArgs)
8 changes: 7 additions & 1 deletion Fun.Build/PipelineBuilder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,13 @@ type PipelineBuilder(name: string) =
member inline _.cmdArgs([<InlineIfLambda>] build: BuildPipeline, args: string list) =
BuildPipeline(fun ctx ->
let ctx = build.Invoke ctx
{ ctx with CmdArgs = args }

let argsInfo = resolveCmdArgsAndRemainings args

{ ctx with
CmdArgs = argsInfo.CmdArgs
RemainingCmdArgs = argsInfo.RemainingArgs
}
)

/// Set working dir for all steps under the stage.
Expand Down
5 changes: 4 additions & 1 deletion Fun.Build/PipelineContextExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ module PipelineContextExtensionsInternal =
with _ ->
envVars.Add(key, "")

let argsInfo = Environment.GetCommandLineArgs() |> Array.toList |> resolveCmdArgsAndRemainings

{
Name = name
Description = ValueNone
Mode = Mode.Execution
Verify = fun _ -> true
CmdArgs = Seq.toList (Environment.GetCommandLineArgs())
CmdArgs = argsInfo.CmdArgs
RemainingCmdArgs = argsInfo.RemainingArgs
EnvVars = envVars |> Seq.map (fun (KeyValue(k, v)) -> k, v) |> Map.ofSeq
AcceptableExitCodes = set [| 0 |]
Timeout = ValueNone
Expand Down
28 changes: 28 additions & 0 deletions Fun.Build/StageContextExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,40 @@ module StageContextExtensions =
// If not find then return ""
member inline ctx.GetEnvVar(key: string) = ctx.TryGetEnvVar key |> ValueOption.defaultValue ""

/// <summary>
/// Get the command arguments from the command line without the remaining arguments that are placed after <c>--</c>.
///
/// For example, if you run the following arguments:
///
/// <c> -p demo test1 v1 -- test2 v2</c>
///
/// will return
///
/// <c>[ "-p"; "demo"; "test1"; "v1" ]</c>
/// </summary>
member ctx.GetAllCmdArgs() =
match ctx.ParentContext with
| ValueSome(StageParent.Pipeline p) -> p.CmdArgs
| ValueSome(StageParent.Stage s) -> s.GetAllCmdArgs()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add some comments on the GetAllCmdArgs to identify it will not return the remaing ones?

| ValueNone -> []

/// <summary>
/// Return the remaining command arguments they are the arguments placed after <c>--</c> in the command line.
///
/// For example, if you run the following arguments:
///
/// <c> -p demo test1 v1 -- test2 v2</c>
///
/// will return
///
/// <c>[ "test2"; "v2" ]</c>
/// </summary>
member ctx.GetRemainingCmdArgs() =
match ctx.ParentContext with
| ValueSome(StageParent.Pipeline p) -> p.RemainingCmdArgs
| ValueSome(StageParent.Stage s) -> s.GetRemainingCmdArgs()
| ValueNone -> []

member ctx.TryGetCmdArg(key: string) =
let cmdArgs = ctx.GetAllCmdArgs()
match cmdArgs |> List.tryFindIndex ((=) key) with
Expand Down
1 change: 1 addition & 0 deletions Fun.Build/Types.Internal.fs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type PipelineContext = {
/// Verify before run pipeline, will throw PipelineFailedException if return false
Verify: PipelineContext -> bool
CmdArgs: string list
RemainingCmdArgs: string list
EnvVars: Map<string, string>
AcceptableExitCodes: Set<int>
Timeout: TimeSpan voption
Expand Down
13 changes: 13 additions & 0 deletions Fun.Build/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ let getFsiFileName () =
else
"your_script.fsx"

let resolveCmdArgsAndRemainings (args: string list) =

let cmdArgs = args |> List.takeWhile (fun x -> x <> "--")

let remainingArgs =
let remainingArgsStartIndex = cmdArgs.Length + 1

if remainingArgsStartIndex < args.Length then
args.[remainingArgsStartIndex..]
else
[]

{| CmdArgs = cmdArgs; RemainingArgs = remainingArgs |}

module ValueOption =

Expand Down
Loading