Skip to content

Commit 61ed066

Browse files
authored
Merge pull request #89 from slaveOftime/feature/run-multiple-pipelines
Support run multiple pipelines in one execution
2 parents ad2abf8 + 8e6bb7b commit 61ed066

4 files changed

Lines changed: 90 additions & 31 deletions

File tree

Fun.Build.Tests/PipelineBuilderTests.fs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ open Xunit
44
open Fun.Build
55
open System.Diagnostics
66
open System.Threading.Tasks
7-
open Xunit
87

98

109
[<Fact>]
@@ -197,6 +196,36 @@ let ``runIfOnlySpecified should work`` () =
197196
}
198197
)
199198

199+
[<Fact>]
200+
let ``runIfOnlySpecified should work for multiple -p`` () =
201+
shouldBeCalled (fun call ->
202+
let p1 = "demo1"
203+
let p2 = "demo2"
204+
let args = [ "-p"; p1; "d1"; "--"; "d1-1"; "-p"; p2; "d2"; "--"; "d2-2" ]
205+
pipeline p1 {
206+
cmdArgs args
207+
stage "" {
208+
run (fun ctx ->
209+
call ctx
210+
Assert.Equal([| "-p"; p1; "d1" |], ctx.GetAllCmdArgs())
211+
Assert.Equal([| "d1-1" |], ctx.GetRemainingCmdArgs())
212+
)
213+
}
214+
runIfOnlySpecified
215+
}
216+
pipeline p2 {
217+
cmdArgs args
218+
stage "" {
219+
run (fun ctx ->
220+
call ctx
221+
Assert.Equal([| "-p"; p2; "d2" |], ctx.GetAllCmdArgs())
222+
Assert.Equal([| "d2-2" |], ctx.GetRemainingCmdArgs())
223+
)
224+
}
225+
runIfOnlySpecified
226+
}
227+
)
228+
200229

201230
[<Fact>]
202231
let ``parallel should work`` () =

Fun.Build/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
## [Unreleased]
44

5+
## [1.1.17] - 2025-09-23
6+
7+
Support run multiple pipelines in one execution
8+
9+
```bash
10+
dotnet fsi build.fsx -- -p pipeline1 ... -p pipeline2 ...
11+
```
12+
513
## [1.1.16] - 2025-01-13
614

715
- Add optional argument disablePrintCommand

Fun.Build/PipelineBuilder.fs

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ type private IsSpecified = bool
1111
/// Used to keep registered data for later usage
1212
let private runIfOnlySpecifiedPipelines = System.Collections.Generic.List<struct (IsSpecified * PipelineContext)>()
1313

14+
let private getPipelineIndexes args =
15+
args |> Seq.indexed |> Seq.filter (fun (_, arg) -> arg = "-p" || arg = "--pipeline") |> Seq.map fst |> Seq.toList
16+
1417

1518
type PipelineBuilder(name: string) =
1619

@@ -244,10 +247,15 @@ type PipelineBuilder(name: string) =
244247
let specified = defaultArg specified true
245248
let ctx = build.Invoke(PipelineContext.Create name)
246249

247-
let args = ctx.CmdArgs @ Array.toList (Environment.GetCommandLineArgs())
250+
let args =
251+
if ctx.RemainingCmdArgs.IsEmpty then
252+
ctx.CmdArgs
253+
else
254+
[ yield! ctx.CmdArgs; "--"; yield! ctx.RemainingCmdArgs ]
255+
248256
let isHelp = args |> Seq.exists (fun arg -> arg = "-h" || arg = "--help")
249257
let verbose = args |> Seq.exists (fun arg -> arg = "-v" || arg = "--verbose")
250-
let pipelineIndex = args |> Seq.tryFindIndex (fun arg -> arg = "-p" || arg = "--pipeline")
258+
let pipelineIndexes = getPipelineIndexes args
251259

252260
runIfOnlySpecifiedPipelines.Add(specified, ctx)
253261

@@ -258,18 +266,24 @@ type PipelineBuilder(name: string) =
258266
)
259267

260268
try
261-
if isHelp then
262-
match pipelineIndex with
263-
| Some index when List.length args > index + 1 && ctx.Name.Equals(args[index + 1], StringComparison.OrdinalIgnoreCase) ->
264-
ctx.RunCommandHelp(verbose)
265-
| _ -> ()
266-
else
267-
match pipelineIndex with
268-
| Some index when List.length args > index + 1 ->
269-
if ctx.Name.Equals(args[index + 1], StringComparison.OrdinalIgnoreCase) then
270-
ctx.Run()
271-
| None when not specified -> ctx.Run()
272-
| _ -> ()
269+
match pipelineIndexes with
270+
| [] when not specified -> ctx.Run()
271+
| [] -> ()
272+
| _ :: _ ->
273+
for i, index in Seq.indexed pipelineIndexes do
274+
if List.length args > index + 1 && ctx.Name.Equals(args[index + 1], StringComparison.OrdinalIgnoreCase) then
275+
let args =
276+
if i = Seq.length pipelineIndexes - 1 then
277+
args[index..]
278+
else
279+
args[index .. pipelineIndexes[i + 1] - 1]
280+
let argInfo = resolveCmdArgsAndRemainings args
281+
let ctx =
282+
{ ctx with
283+
CmdArgs = argInfo.CmdArgs
284+
RemainingCmdArgs = argInfo.RemainingArgs
285+
}
286+
if isHelp then ctx.RunCommandHelp(verbose) else ctx.Run()
273287
with
274288
| :? PipelineFailedException
275289
| :? PipelineCancelledException ->
@@ -287,17 +301,18 @@ let inline pipeline name = PipelineBuilder name
287301
/// If you only have one specified pipeline, it will try to print its command only help information.
288302
let tryPrintPipelineCommandHelp () =
289303
let args = Environment.GetCommandLineArgs()
290-
let pipelineIndex = args |> Seq.tryFindIndex (fun arg -> arg = "-p" || arg = "--pipeline")
291-
292-
match pipelineIndex with
293-
| Some index ->
294-
let pipelineName = args[index + 1]
295-
let isPipelineRegistered =
296-
runIfOnlySpecifiedPipelines
297-
|> Seq.exists (fun struct (_, x) -> x.Name.Equals(pipelineName, StringComparison.OrdinalIgnoreCase))
298-
if not isPipelineRegistered then
299-
AnsiConsole.MarkupLineInterpolated $"Pipeline [red]{pipelineName}[/] is not found."
300-
AnsiConsole.MarkupLine "You can use [green]runIfOnlySpecified[/] for your pipline, or check if the name is correct."
304+
let pipelineIndexes = getPipelineIndexes args
305+
306+
match pipelineIndexes with
307+
| _ :: _ ->
308+
for index in pipelineIndexes do
309+
let pipelineName = args[index + 1]
310+
let isPipelineRegistered =
311+
runIfOnlySpecifiedPipelines
312+
|> Seq.exists (fun struct (_, x) -> x.Name.Equals(pipelineName, StringComparison.OrdinalIgnoreCase))
313+
if not isPipelineRegistered then
314+
AnsiConsole.MarkupLineInterpolated $"Pipeline [red]{pipelineName}[/] is not found."
315+
AnsiConsole.MarkupLine "You can use [green]runIfOnlySpecified[/] for your pipline, or check if the name is correct."
301316

302317
| _ ->
303318
let isHelp = args |> Seq.exists (fun arg -> arg = "-h" || arg = "--help")

demo-cmd.fsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ module Apps =
1212

1313
let all = [ app1; app2; app3 ]
1414

15-
let args = {|
16-
app = fun apps -> CmdArg.Create(shortName = "-a", longName = "--app", values = apps, description = "specify the app you want to dev")
17-
path = CmdArg.Create("-f", "--file", "publish directory for the app")
18-
watch = CmdArg.Create(shortName = "-w", description = "if is in watch mode")
19-
|}
15+
let args =
16+
{| app = fun apps -> CmdArg.Create(shortName = "-a", longName = "--app", values = apps, description = "specify the app you want to dev")
17+
path = CmdArg.Create("-f", "--file", "publish directory for the app")
18+
watch = CmdArg.Create(shortName = "-w", description = "if is in watch mode") |}
2019

2120

2221
pipeline "demo" {
@@ -64,4 +63,12 @@ pipeline "demo" {
6463
runIfOnlySpecified
6564
}
6665

66+
67+
pipeline "demo2" {
68+
description "another demo"
69+
stage "build" { echo "build something" }
70+
runIfOnlySpecified
71+
}
72+
73+
6774
tryPrintPipelineCommandHelp ()

0 commit comments

Comments
 (0)