// Copyright 2026 TesterArmy.
// Modified by Dario Kondratiuk.
// SPDX-License-Identifier: Apache-2.0
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
using E2E.Internal;
namespace E2E.Cli;
/// Where a service's pipeline goes, relative to the repository root (upstream's CiFile).
/// The pipeline's path.
/// Whether e2e already runs there, so init writes nothing.
/// For a file the main pipeline must include: the lines to add to it.
/// Something else the main pipeline needs before the included jobs can run.
/// Appended to every job name when the pipeline already has a job of one of those names.
internal sealed record CiFile(
string File,
bool Existing,
string? Include = null,
string? Note = null,
string? JobSuffix = null
);
///
/// How the iOS job builds the app for the simulator (upstream's IosBuild): Expo generates ios/ and builds it, a bare
/// React Native app builds its workspace with xcodebuild, and anything else gets a step that fails until it is replaced.
///
internal abstract record IosBuild
{
private IosBuild() { }
/// An Expo app.
internal sealed record Expo : IosBuild;
/// A bare React Native app.
/// The name of the .xcworkspace under ios/, without its extension.
/// Whether the app has a Gemfile, so the pods install through Bundler.
internal sealed record ReactNative(string Workspace, bool Bundler) : IosBuild;
/// Any other app.
internal sealed record Custom : IosBuild;
}
/// How the Android job builds a release APK (upstream's AndroidBuild).
internal abstract record AndroidBuild
{
private AndroidBuild() { }
/// An Expo app.
/// Whether Expo still has to generate android/.
internal sealed record Expo(bool Prebuild) : AndroidBuild;
/// A bare React Native app.
internal sealed record ReactNative : AndroidBuild;
/// Any other app.
internal sealed record Custom : AndroidBuild;
}
/// A phone job: the targets it runs and how it builds the app (the ios and android members of upstream's CiJobs).
/// The build kind of the platform.
/// The names of the targets the job runs.
/// How the app is built.
internal sealed record CiPhoneJob(IReadOnlyList Targets, TBuild Build);
///
/// The jobs a pipeline holds, one per platform (upstream's CiJobs). Web runs every target that is not on a phone: its
/// names when the config has phone targets too, or null for every target of the config (upstream's 'all'). A platform
/// with no targets gets no job.
///
/// The web job: its target names, or null for all of them; Web itself is null for no job.
/// The iOS job.
/// The Android job.
internal sealed record CiJobs(
CiWebJob? Web = null,
CiPhoneJob? Ios = null,
CiPhoneJob? Android = null
)
{
/// Whether the pipeline has any job.
public bool Any => Web is not null || Ios is not null || Android is not null;
}
/// The web job of .
/// The names of the targets it runs; null for every target of the config.
internal sealed record CiWebJob(IReadOnlyList? Targets = null);
/// A target as the pipeline sees it: its name and the platform its engine drives (upstream's CiTarget).
/// The target's name.
/// The target's platform.
internal sealed record CiTarget(string Name, string Platform);
/// What the workflow is rendered from (upstream's WorkflowContext); every path is POSIX and relative to the repository root.
/// The test project's file.
/// The SDK version to set up.
/// Environment variables the run reads from secrets of the same name.
/// The jobs to write.
/// Appended to every GitLab job name.
/// The folder of the Expo or React Native app, '' for the repository root (upstream's projectDir, which is where its package.json is).
/// The app's package manager: npm, pnpm, yarn, or bun (upstream's manager).
/// Yarn 2 or later, whose frozen install is --immutable (upstream's yarnBerry).
/// Whether the test project writes a JUnit report with the junit logger (upstream's --reporter list,junit).
internal sealed record WorkflowContext(
string ProjectFile,
string DotnetVersion,
IReadOnlyList Secrets,
CiJobs Jobs,
string? JobSuffix = null,
string AppDir = "",
string Manager = "npm",
bool YarnBerry = false,
bool Junit = true
);
/// One CI service e2e init --ci writes a pipeline for (upstream's CiService).
/// The --ci value.
/// The service's name.
/// When the pipeline runs.
/// The docs page for the service's mobile jobs and everything init does not write.
/// Where the pipeline goes.
/// The pipeline's text.
/// How to give the run a model key it reads from the environment.
internal sealed record CiService(
string Id,
string Label,
string Hint,
string Docs,
Func Locate,
Func Render,
Func SecretHint
);
///
/// Upstream's cli/init/ci.ts: the CI pipeline e2e init writes, GitHub Actions or GitLab CI/CD, one job per
/// platform the config's targets run on. The port runs dotnet test where upstream runs e2e run, installs
/// Chromium with the project's playwright.ps1 where upstream runs e2e-web install, and has no Node, package
/// manager, or Expo/React Native build to detect: the iOS and Android jobs always get a build step to replace. The pipelines
/// are written as text, not edited from the YAML templates of upstream's templates/ci/, since the port has no YAML library.
///
internal static partial class Ci
{
/// Where the GitHub Actions workflow goes, relative to the repository root.
private const string GithubWorkflow = ".github/workflows/e2e.yml";
/// GitLab's pipeline file, and the file init writes beside one that already exists.
public const string GitlabPipeline = ".gitlab-ci.yml";
private const string GitlabIncluded = ".gitlab/e2e.gitlab-ci.yml";
/// Where the Android job installs the APK from.
private const string Apk = "android/app/build/outputs/apk/release/app-release.apk";
/// .NET-only: the package that gives dotnet test the junit logger, which stands for upstream's --reporter junit.
public const string JunitLogger = "JunitXml.TestLogger";
/// .NET-only: the version of init adds to a project.
public const string JunitLoggerVersion = "8.1.0";
/// Words YAML reads as a boolean or null, which a name must not be left bare as.
private static readonly string[] YamlReserved = ["true", "false", "yes", "no", "on", "off", "null", "y", "n"];
/// Phone platforms run in their own job, on a runner with a simulator or an emulator.
private static readonly string[] Phones = ["ios", "android"];
private static readonly TimeSpan RegexTimeout = TimeSpan.FromSeconds(1);
/// The SDK version a pipeline sets up when the project names none.
private const string DefaultDotnet = "10.0";
/// The jobs the GitLab pipeline defines, which an existing pipeline's own jobs must not share names with.
private static readonly string[] GitlabJobs = ["e2e-web", ".e2e-mobile", "e2e-ios", "e2e-android"];
/// The keys whose values a service runs as shell: a GitHub step's run, an action's script, and a GitLab job's scripts.
private static readonly string[] CommandKeys = ["run", "script", "before_script", "after_script"];
/// The services, in the order the flag lists them.
public static readonly IReadOnlyList Services =
[
new(
"github-actions",
"GitHub Actions",
"on every pull request and push to main",
"/integrations/github",
LocateGithubWorkflow,
RenderGithubWorkflow,
name => $"The workflow reads {name} from the repository secrets; add it with gh secret set {name}"
),
new(
"gitlab",
"GitLab CI/CD",
"in every merge request and on the default branch",
"/integrations/gitlab",
LocateGitlabPipeline,
RenderGitlabPipeline,
name =>
$"The pipeline reads {name} from the project's CI/CD variables; add it masked and hidden with glab variable set {name} --masked --hidden --raw"
),
];
/// The service with this id.
public static CiService GetCiService(string id) =>
Services.FirstOrDefault(service => service.Id == id)
?? throw new ArgumentException("unknown CI service: " + id, nameof(id));
///
/// Whether a pipeline file runs dotnet test in one of its commands (upstream's runsE2E, which looks for
/// e2e run). The words in a YAML or shell comment, or in a step's name, run nothing.
///
internal static bool RunsE2E(string pipeline)
{
foreach (var raw in pipeline.Split('\n'))
{
var line = CommentPattern().Replace(raw.TrimEnd('\r'), "").Trim();
line = line.StartsWith("- ", StringComparison.Ordinal) ? line[2..].TrimStart() : line;
var key = KeyPattern().Match(line);
if (key.Success && !CommandKeys.Contains(key.Groups[1].Value))
{
continue;
}
var command = key.Success ? line[key.Length..] : line;
if (RunPattern().IsMatch(command))
{
return true;
}
}
return false;
}
///
/// Where the GitHub workflow goes: .github/workflows/e2e.yml, unless that file or another workflow already runs
/// the tests, in which case e2e is set up.
///
internal static CiFile LocateGithubWorkflow(string root)
{
var workflows = Path.Combine(root, ".github", "workflows");
var running = Directory.Exists(workflows)
? Directory
.GetFiles(workflows)
.Select(Path.GetFileName)
.Where(name =>
name!.EndsWith(".yml", StringComparison.Ordinal) || name.EndsWith(".yaml", StringComparison.Ordinal)
)
.Order(StringComparer.Ordinal)
.FirstOrDefault(name => RunsE2E(System.IO.File.ReadAllText(Path.Combine(workflows, name!))))
: null;
if (running is not null)
{
return new CiFile(".github/workflows/" + running, true);
}
return new CiFile(GithubWorkflow, System.IO.File.Exists(Path.Combine(root, GithubWorkflow)));
}
///
/// Where the GitLab job goes: .gitlab-ci.yml when the project has none, else a file of its own that
/// .gitlab-ci.yml includes, so init never edits the project's pipeline. A pipeline file that already runs the tests
/// counts as set up.
///
internal static CiFile LocateGitlabPipeline(string root)
{
var main = Path.Combine(root, GitlabPipeline);
if (!System.IO.File.Exists(main))
{
return new CiFile(GitlabPipeline, false);
}
var pipeline = System.IO.File.ReadAllText(main);
if (RunsE2E(pipeline))
{
return new CiFile(GitlabPipeline, true);
}
// The jobs run in GitLab's default `test` stage, which a pipeline that lists its own stages may leave out.
var stages = GitlabStages(pipeline);
// GitLab merges jobs of the same name across included files, so the jobs step aside for ones the pipeline already has.
var taken = GitlabJobs.Any(name =>
Regex.IsMatch(pipeline, "^" + Regex.Escape(name) + @"\s*:", RegexOptions.Multiline, RegexTimeout)
);
return new CiFile(
GitlabIncluded,
System.IO.File.Exists(Path.Combine(root, GitlabIncluded)),
"include:\n - local: " + GitlabIncluded,
stages is not null && !stages.Contains("test")
? "The e2e jobs run in the test stage; add test to the stages in " + GitlabPipeline
: null,
taken ? "-run" : null
);
}
/// The names a pipeline's top-level stages: lists, however the list is laid out; null without one.
internal static IReadOnlyList? GitlabStages(string pipeline)
{
foreach (var document in DocumentSeparator().Split(pipeline))
{
var lines = document.Split('\n').Select(line => line.TrimEnd('\r')).ToList();
var at = lines.FindIndex(line => TopLevelStages().IsMatch(line));
if (at < 0)
{
continue;
}
var rest = NodeProperties().Replace(TopLevelStages().Match(lines[at]).Groups[1].Value.Trim(), "");
if (rest.StartsWith('*'))
{
// An alias to an anchored list.
var anchor = "&" + rest[1..].Trim();
var anchored = lines.FindIndex(line => AnchorLine(line, anchor));
if (anchored < 0)
{
continue;
}
(at, rest) = (
anchored,
NodeProperties()
.Replace(CommentPattern().Replace(lines[anchored], "").Split(anchor, 2)[1].Trim(), "")
);
}
return ReadList(lines, at, rest);
}
return null;
}
/// The names of a command's job and the settings the CI pipelines need, read from a config.
internal static IReadOnlyList ConfigTargets(JsonObject config)
{
if (config["targets"] is not JsonArray array)
{
return [new CiTarget("web", "web")];
}
var targets = new List();
foreach (var node in array)
{
if (node is not JsonObject target)
{
continue;
}
var platform =
target["platform"] is JsonValue p && p.TryGetValue(out var declared) ? declared : "web";
var name = target["name"] is JsonValue n && n.TryGetValue(out var named) ? named : platform;
targets.Add(new CiTarget(name, platform));
}
return targets;
}
///
/// The jobs for a config's targets (upstream's ciJobs). iOS and Android targets get a job each; the rest, web
/// and HTTP-only alike, run in the web job, which names them only when the config has phone targets too.
///
internal static CiJobs GetCiJobs(IReadOnlyList targets, IosBuild ios, AndroidBuild android)
{
List Named(string platform) =>
[.. targets.Where(target => target.Platform == platform).Select(target => target.Name)];
List others =
[
.. targets.Where(target => !Phones.Contains(target.Platform)).Select(target => target.Name),
];
var phones = targets.Any(target => Phones.Contains(target.Platform));
var (iosTargets, androidTargets) = (Named("ios"), Named("android"));
return new CiJobs(
others.Count == 0 ? null : new CiWebJob(phones ? others : null),
iosTargets.Count == 0 ? null : new CiPhoneJob(iosTargets, ios),
androidTargets.Count == 0 ? null : new CiPhoneJob(androidTargets, android)
);
}
///
/// The app build for each phone platform, from the app's package.json and native folders (upstream's
/// detectMobileBuilds): Expo when it depends on expo, bare React Native when it depends on
/// react-native and has the native project, else custom.
///
internal static (IosBuild Ios, AndroidBuild Android) DetectMobileBuilds(string appDir, Func dependsOn)
{
var ios = Path.Combine(appDir, "ios");
var workspace = Directory.Exists(ios)
? Directory
.GetFileSystemEntries(ios)
.Select(Path.GetFileName)
.Order(StringComparer.Ordinal)
.FirstOrDefault(name =>
name!.EndsWith(".xcworkspace", StringComparison.Ordinal) && name != "Pods.xcworkspace"
)
: null;
var gradle = System.IO.File.Exists(Path.Combine(appDir, "android", "gradlew"));
if (dependsOn("expo"))
{
return (new IosBuild.Expo(), new AndroidBuild.Expo(!gradle));
}
if (dependsOn("react-native"))
{
return (
workspace is null
? new IosBuild.Custom()
: new IosBuild.ReactNative(
workspace[..^".xcworkspace".Length],
System.IO.File.Exists(Path.Combine(appDir, "Gemfile"))
),
gradle ? new AndroidBuild.ReactNative() : new AndroidBuild.Custom()
);
}
return (new IosBuild.Custom(), new AndroidBuild.Custom());
}
///
/// .NET-only: the folder of the JavaScript app the phone jobs build, which upstream finds as its project's own folder: the
/// nearest directory at or above , up to , with a
/// package.json. Null when there is none.
///
internal static string? FindAppDir(string projectDir, string repositoryRoot)
{
var root = Path.TrimEndingDirectorySeparator(Path.GetFullPath(repositoryRoot));
for (
var current = Path.TrimEndingDirectorySeparator(Path.GetFullPath(projectDir));
;
current = Path.GetDirectoryName(current)!
)
{
if (System.IO.File.Exists(Path.Combine(current, "package.json")))
{
return current;
}
if (
string.Equals(current, root, StringComparison.OrdinalIgnoreCase)
|| Path.GetDirectoryName(current) is null
)
{
return null;
}
}
}
/// .NET-only: whether the package.json in lists among its dependencies.
internal static bool DependsOn(string appDir, string name)
{
try
{
var document = JsonNode.Parse(
System.IO.File.ReadAllText(Path.Combine(appDir, "package.json")),
documentOptions: new JsonDocumentOptions
{
CommentHandling = JsonCommentHandling.Skip,
AllowTrailingCommas = true,
}
);
return new[] { "dependencies", "devDependencies" }.Any(section =>
(document?[section] as JsonObject)?.ContainsKey(name) == true
);
}
catch (Exception cause)
when (cause is JsonException or IOException or InvalidOperationException or UnauthorizedAccessException)
{
return false;
}
}
/// .NET-only: the package manager of the app in , from its lockfile (upstream reads packageManager and the lockfile); npm when there is none.
internal static (string Manager, bool YarnBerry) DetectManager(string appDir, string? repositoryRoot = null)
{
// A workspace app keeps its lockfile at the workspace root: the nearest folder up to the repository with one.
var stop = repositoryRoot is null ? null : Path.TrimEndingDirectorySeparator(Path.GetFullPath(repositoryRoot));
var found = appDir;
for (
var current = Path.TrimEndingDirectorySeparator(Path.GetFullPath(appDir));
;
current = Path.GetDirectoryName(current)!
)
{
if (
new[] { "pnpm-lock.yaml", "yarn.lock", "bun.lock", "bun.lockb", "package-lock.json" }.Any(file =>
System.IO.File.Exists(Path.Combine(current, file))
)
)
{
found = current;
break;
}
if (
stop is null
|| string.Equals(current, stop, StringComparison.OrdinalIgnoreCase)
|| Path.GetDirectoryName(current) is null
)
{
break;
}
}
bool Has(string file) => System.IO.File.Exists(Path.Combine(found, file));
if (Has("pnpm-lock.yaml"))
{
return ("pnpm", false);
}
if (Has("yarn.lock"))
{
return ("yarn", Has(".yarnrc.yml"));
}
return Has("bun.lock") || Has("bun.lockb") ? ("bun", false) : ("npm", false);
}
/// The SDK version a project file targets, such as 10.0 for net10.0; the default when it names none.
internal static string DotnetVersionOf(string? project)
{
var match = project is null ? Match.Empty : TargetFramework().Match(project);
return match.Success ? match.Groups[1].Value : DefaultDotnet;
}
/// The repository the project is in: the nearest directory at or above with a .git entry, else (upstream's findRepositoryRoot).
public static string FindRepositoryRoot(string dir, string fallback)
{
for (var current = Path.GetFullPath(dir); ; current = Path.GetDirectoryName(current)!)
{
if (System.IO.File.Exists(Path.Combine(current, ".git")) || Directory.Exists(Path.Combine(current, ".git")))
{
return current;
}
if (Path.GetDirectoryName(current) is null)
{
return Path.GetFullPath(fallback);
}
}
}
///
/// The service the repository looks hosted on: GitLab for a .gitlab-ci.yml or a remote on a gitlab. host,
/// GitHub for a .github/ directory or a github.com remote. Null when nothing points either way (upstream's detectCiService).
///
public static string? DetectCiService(string repositoryRoot)
{
var remotes = "";
var gitConfig = Path.Combine(repositoryRoot, ".git", "config");
if (System.IO.File.Exists(gitConfig))
{
remotes = System.IO.File.ReadAllText(gitConfig);
}
if (
System.IO.File.Exists(Path.Combine(repositoryRoot, GitlabPipeline))
|| Regex.IsMatch(remotes, @"url\s*=.*\bgitlab\.", RegexOptions.None, RegexTimeout)
)
{
return "gitlab";
}
if (
Directory.Exists(Path.Combine(repositoryRoot, ".github"))
|| Regex.IsMatch(remotes, @"url\s*=.*github\.com[:/]", RegexOptions.None, RegexTimeout)
)
{
return "github-actions";
}
return null;
}
/// relative to with forward slashes; empty for the root itself (upstream's posixRelative).
public static string PosixRelative(string root, string target) =>
Path.GetRelativePath(root, target).Replace('\\', '/') is var relative && relative == "."
? ""
: relative.Replace('\\', '/');
/// The gateways an existing config uses, by its agents.default.provider (upstream's gatewaysInConfig, which reads the import lines).
internal static IReadOnlyList GatewaysInConfig(JsonObject config)
{
var provider =
config["agents"]?["default"]?["provider"] is JsonValue value && value.TryGetValue(out var text)
? text
: null;
return [.. Gateways.All.Where(gateway => gateway.Provider == provider)];
}
/// Whether a config points an OpenAI-compatible model at a loopback baseUrl, which a CI runner cannot reach (upstream's configUsesLocalModel).
internal static bool ConfigUsesLocalModel(JsonObject config) =>
config["agents"] is JsonObject agents
&& agents.Any(agent =>
agent.Value?["baseUrl"] is JsonValue url
&& url.TryGetValue(out var text)
&& Uri.TryCreate(text, UriKind.Absolute, out var parsed)
&& Urls.IsLoopbackHost(parsed.Host)
);
/// Whether a config sets an app.command, so the runner can start the app on a fresh CI machine (upstream's configStartsApp).
internal static bool ConfigStartsApp(JsonObject config) =>
(config["app"] as JsonObject)?.ContainsKey("command") == true
|| (config["targets"] as JsonArray)?.Any(target =>
(target?["app"] as JsonObject)?.ContainsKey("command") == true
) == true;
/// Reads an existing config as the runner does, with comments and trailing commas allowed; null when it is not a JSON object.
internal static JsonObject? ReadConfig(string text)
{
try
{
return JsonNode.Parse(
text,
documentOptions: new JsonDocumentOptions
{
CommentHandling = JsonCommentHandling.Skip,
AllowTrailingCommas = true,
}
) as JsonObject;
}
catch (JsonException)
{
return null;
}
}
///
/// The GitHub Actions workflow: one job per platform in Jobs, on pull requests and on pushes to main. The
/// actions are pinned to the commits upstream's template pins.
///
internal static string RenderGithubWorkflow(WorkflowContext context)
{
var jobs = new List();
if (context.Jobs.Web is { } web)
{
jobs.Add(GithubJob("web", "ubuntu-latest", 30, context, GithubWebSteps, web.Targets));
}
if (context.Jobs.Ios is { } ios)
{
jobs.Add(
GithubJob(
"ios",
"macos-26",
90,
context,
GithubIosSteps.Replace(
"@APPBUILD@",
GithubBuildSteps(context, IosBuildOf(context, ios.Build), ios.Build is IosBuild.Custom),
StringComparison.Ordinal
),
ios.Targets
)
);
}
if (context.Jobs.Android is { } android)
{
jobs.Add(
GithubJob(
"android",
"ubuntu-latest",
60,
context,
GithubAndroidSteps.Replace(
"@APPBUILD@",
GithubBuildSteps(
context,
AndroidBuildOf(context, android.Build),
android.Build is AndroidBuild.Custom
),
StringComparison.Ordinal
),
android.Targets
)
);
}
const string Header = """
# Written by `e2e init --ci github-actions`: the e2e tests on every pull request
# and every push to main, one job per platform. See https://github.com/hardkoded/e2e-dotnet
name: e2e
on:
pull_request:
push:
branches: [main]
permissions:
contents: read
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
""";
return Header + string.Join("\n\n", jobs) + "\n";
}
private static string GithubJob(
string name,
string runner,
int minutes,
WorkflowContext context,
string steps,
IReadOnlyList? targets
)
{
const string Template = """
@NAME@:
runs-on: @RUNNER@
timeout-minutes: @MINUTES@
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: actions/setup-dotnet@v4
with:
dotnet-version: @DOTNET@.x
cache: true
cache-dependency-path: "**/*.csproj"
@STEPS@
- name: Upload the report
if: ${{ !cancelled() }}
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: e2e-@NAME@
path: |
.e2e/report.json
.e2e/junit.xml@J@
.e2e/results
# .e2e is a hidden directory, which upload-artifact skips when it expands a folder.
include-hidden-files: true
if-no-files-found: warn
retention-days: 7
""";
var secrets = string.Join(
"\n",
[
.. context.Secrets.Select(secret => " " + secret + ": ${{ secrets." + secret + " }}"),
.. TargetEnv(targets).Select(line => " " + line),
]
);
var expanded = steps
.Replace("@ENV@", secrets.Length == 0 ? "" : "\n env:\n" + secrets, StringComparison.Ordinal)
.Replace("@SECRETS@\n", secrets.Length == 0 ? "" : secrets + "\n", StringComparison.Ordinal)
.Replace("@BUILD@", BuildCommand(context), StringComparison.Ordinal)
.Replace("@TEST@", GithubTestCommand(context), StringComparison.Ordinal)
.Replace("@PLAYWRIGHT@", PlaywrightScript(context), StringComparison.Ordinal)
.Replace("@APK@", ApkPath(context), StringComparison.Ordinal)
.Replace("@BUILDDIR@", Join(context.AppDir, "build"), StringComparison.Ordinal);
return Junit(
Template
.Replace("@STEPS@", Indent(expanded, 6), StringComparison.Ordinal)
.Replace("@NAME@", name, StringComparison.Ordinal)
.Replace("@RUNNER@", runner, StringComparison.Ordinal)
.Replace(
"@MINUTES@",
minutes.ToString(System.Globalization.CultureInfo.InvariantCulture),
StringComparison.Ordinal
)
.Replace("@DOTNET@", context.DotnetVersion, StringComparison.Ordinal),
context.Junit
);
}
private const string GithubRunStep = """
- name: Run e2e
run: @TEST@@ENV@
""";
private const string GithubWebSteps =
"""
- name: Build
run: @BUILD@
- uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ~/.cache/ms-playwright
key: playwright-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('**/*.csproj', '**/Directory.Packages.props', '**/Directory.Build.props') }}
- name: Install browsers
run: pwsh @PLAYWRIGHT@ install --with-deps chromium
"""
+ "\n"
+ GithubRunStep;
private const string GithubIosSteps =
"""
@APPBUILD@
- name: Build the tests
run: @BUILD@
# Boots the image's own iPhone 17 Pro on a pinned runtime: a simulator made with `simctl create`
# can spend minutes on its first boot. Bump the runtime on purpose, after a local run on it,
# since the accessibility tree changes between iOS releases.
- name: Boot a simulator and install the app
env:
IOS_RUNTIME: com.apple.CoreSimulator.SimRuntime.iOS-26-5
run: |
set -euo pipefail
UDID=$(xcrun simctl list devices available -j | jq -r --arg rt "$IOS_RUNTIME" '.devices[$rt][]? | select(.name == "iPhone 17 Pro") | .udid' | head -n 1)
if [ -z "$UDID" ]; then
echo "::error::No iPhone 17 Pro on $IOS_RUNTIME"
xcrun simctl list runtimes
exit 1
fi
xcrun simctl boot "$UDID"
xcrun simctl bootstatus "$UDID"
APP=$(find @BUILDDIR@ -maxdepth 5 -name '*.app' -type d | head -n 1)
if [ -z "$APP" ]; then
echo "::error::No .app under @BUILDDIR@/"
exit 1
fi
xcrun simctl install "$UDID" "$APP"
# A target with device set from the E2E_DEVICE variable drives this simulator, whatever else the image has.
echo "E2E_DEVICE=$UDID" >> "$GITHUB_ENV"
"""
+ "\n"
+ GithubRunStep;
private const string GithubAndroidSteps = """
- uses: actions/setup-java@b843ebf9b913146b38da2eaa7aeddf76074ae1ad # v6.1.0
with:
distribution: temurin
java-version: 17
- name: Enable KVM
run: |
echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
sudo udevadm control --reload-rules
sudo udevadm trigger --name-match=kvm
@APPBUILD@
- name: Build the tests
run: @BUILD@
- name: Run e2e
uses: reactivecircus/android-emulator-runner@a421e43855164a8197daf9d8d40fe71c6996bb0d # v2.38.0
with:
api-level: 36
arch: x86_64
target: google_apis
profile: pixel_7
emulator-options: -no-window -gpu swiftshader_indirect -no-snapshot -noaudio -no-boot-anim
disable-animations: true
script: |
adb install -r @APK@
@TEST@
env:
@SECRETS@
# The emulator the action boots, for a target with device set from the E2E_DEVICE variable.
E2E_DEVICE: emulator-5554
""";
private static string GithubTestCommand(WorkflowContext context) =>
$"dotnet test -c Release --no-build {Quote(context.ProjectFile)}"
+ (context.Junit ? " --logger \"junit;LogFilePath=${{ github.workspace }}/.e2e/junit.xml\"" : "");
///
/// The env lines that point a job's run at its one target: E2E_TARGET, the port's counterpart of upstream's
/// --target (one name). A job with several targets sets none and every test uses the config's own choice.
///
private static string[] TargetEnv(IReadOnlyList? targets) =>
targets is { Count: 1 } ? ["E2E_TARGET: " + YamlScalar(targets[0])] : [];
/// A target name as a YAML value: bare when it is plain, else double-quoted.
private static string YamlScalar(string value) =>
value.Length > 0
&& char.IsAsciiLetter(value[0])
&& value.All(c => char.IsAsciiLetterOrDigit(c) || c is '_' or '-' or '.')
&& !YamlReserved.Contains(value, StringComparer.OrdinalIgnoreCase)
? value
: System.Text.Json.JsonSerializer.Serialize(value);
/// The lines of that end in the @J@ marker: kept without it when the JUnit report is on, else dropped.
private static string Junit(string text, bool on) =>
string.Join(
"\n",
text.Split('\n')
.Where(line => on || !line.EndsWith("@J@", StringComparison.Ordinal))
.Select(line => line.EndsWith("@J@", StringComparison.Ordinal) ? line[..^3] : line)
);
/// `dir/file`, or `file` at the root (upstream's join).
private static string Join(string dir, string file) => dir.Length == 0 ? file : dir + "/" + file;
/// Where every build writes the APK the Android job installs.
private static string ApkPath(WorkflowContext context) => Join(context.AppDir, Apk);
private static string BuildCommand(WorkflowContext context) =>
$"dotnet build -c Release {Quote(context.ProjectFile)}";
/// The comment lines above an app build and its commands (upstream's { comment, lines }).
private sealed record AppBuild(string[] Comment, string[] Lines);
/// The package manager's bin runner in a pipeline file (upstream's exec): the short forms the CI docs use.
private static string Exec(WorkflowContext context, string command) =>
context.Manager switch
{
"pnpm" => "pnpm exec " + command,
"yarn" => "yarn " + command,
"bun" => "bunx " + command,
_ => "npx " + command,
};
/// The commands that set up the package manager and install the app's dependencies without touching the lockfile.
private static string[] InstallLines(WorkflowContext context) =>
context.Manager switch
{
"pnpm" =>
[
"command -v corepack || npm install --global corepack",
"corepack enable",
"pnpm install --frozen-lockfile",
],
"yarn" when context.YarnBerry =>
[
"command -v corepack || npm install --global corepack",
"corepack enable",
"yarn install --immutable",
],
"yarn" => ["command -v yarn || npm install --global yarn", "yarn install --frozen-lockfile"],
"bun" => ["npm install --global bun", "bun install --frozen-lockfile"],
_ => ["npm ci"],
};
/// The iOS build's commands and the comment above them (upstream's iosBuild); every build leaves the .app under build/.
private static AppBuild IosBuildOf(WorkflowContext context, IosBuild build) =>
build switch
{
IosBuild.Expo => new(
[
"Expo generates ios/ (CNG): expo run:ios prebuilds it, installs the pods, and builds without a simulator.",
],
[Exec(context, "expo run:ios --configuration Release --device generic --no-bundler --output build/ios")]
),
IosBuild.ReactNative native => new(
["Installs the pods, then builds the workspace for the simulator into build/."],
[
.. native.Bundler
? new[] { "bundle install", "(cd ios && bundle exec pod install)" }
: ["(cd ios && pod install)"],
$"xcodebuild -workspace ios/{native.Workspace}.xcworkspace -scheme {native.Workspace} -configuration Release -sdk iphonesimulator -destination 'generic/platform=iOS Simulator' -derivedDataPath build",
]
),
_ => new(
[
$"Replace this step with your app's Release build for the iOS simulator, with the .app under {Join(context.AppDir, "build")}/.",
],
[
$"echo \"Replace this step with your app's build for the simulator, with the .app under {Join(context.AppDir, "build")}/\" >&2",
"exit 1",
]
),
};
/// The Android build's commands and the comment above them (upstream's androidBuild); every build writes the APK at .
private static AppBuild AndroidBuildOf(WorkflowContext context, AndroidBuild build)
{
const string Gradle = "(cd android && ./gradlew :app:assembleRelease -PreactNativeArchitectures=x86_64)";
string[] abi = ["Only the emulator's ABI: building all four takes about 25 minutes longer."];
return build switch
{
AndroidBuild.Expo expo => new(
["Expo generates android/ (CNG) and signs release builds with the debug keystore.", .. abi],
[
.. expo.Prebuild ? new[] { Exec(context, "expo prebuild --platform android --no-install") } : [],
Gradle,
]
),
AndroidBuild.ReactNative => new(abi, [Gradle]),
_ => new(
[$"Replace this step with your app's release build, an APK at {ApkPath(context)}."],
[$"echo \"Replace this step with your app's build, an APK at {ApkPath(context)}\" >&2", "exit 1"]
),
};
}
///
/// A phone job's app steps for GitHub: for an Expo or React Native app, Node and the install, then the build in the app's
/// folder; for any other app, the build step that fails until it is replaced.
///
private static string GithubBuildSteps(WorkflowContext context, AppBuild build, bool custom)
{
var steps = new List();
var directory = context.AppDir.Length == 0 ? "" : " working-directory: " + Quote(context.AppDir) + "\n";
if (!custom)
{
steps.Add(
"- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0\n with:\n node-version: 24\n"
+ "- name: Install JavaScript dependencies\n"
+ directory
+ " run: "
+ RunValue(InstallLines(context))
);
}
steps.Add(
string.Concat(build.Comment.Select(line => "# " + line + "\n"))
+ "- name: Build the app\n"
+ (custom ? "" : directory)
+ " run: "
+ RunValue(build.Lines)
);
return string.Join("\n", steps);
}
/// The value of a run: key at the step's own indentation: one command inline, several as a block.
private static string RunValue(string[] lines) =>
lines.Length == 1 ? lines[0] : "|\n" + string.Join("\n", lines.Select(line => " " + line));
/// A path as one shell word: quoted when it holds a space or a character the shell or YAML reads.
private static string Quote(string path) =>
path.All(c => char.IsLetterOrDigit(c) || c is '/' or '.' or '_' or '-')
? path
: "'" + path.Replace("'", "'\\''", StringComparison.Ordinal) + "'";
/// The path of playwright.ps1 the project's build writes.
private static string PlaywrightScript(WorkflowContext context)
{
var directory = context.ProjectFile.Contains('/', StringComparison.Ordinal)
? context.ProjectFile[..context.ProjectFile.LastIndexOf('/')] + "/"
: "";
return Quote($"{directory}bin/Release/net{context.DotnetVersion}/playwright.ps1");
}
///
/// The GitLab CI/CD pipeline: one job per platform in Jobs, in merge request pipelines and on the default branch.
/// The web job runs in a dotnet/sdk image on GitLab's runners; the mobile jobs need self-managed ones. The rules sit
/// on the jobs, not in a workflow: block, so the same file works as .gitlab-ci.yml and as a file another pipeline includes.
///
internal static string RenderGitlabPipeline(WorkflowContext context)
{
var parts = new List();
if (context.Jobs.Web is { } web)
{
parts.Add(
GitlabWeb.Replace(
" @TARGETVAR@\n",
string.Concat(TargetEnv(web.Targets).Select(line => " " + line + "\n")),
StringComparison.Ordinal
)
);
}
if (context.Jobs.Ios is not null || context.Jobs.Android is not null)
{
parts.Add(GitlabMobile);
}
if (context.Jobs.Ios is { } ios)
{
parts.Add(
GitlabPhone(
GitlabIos,
ios.Targets,
GitlabBuildItems(context, IosBuildOf(context, ios.Build), ios.Build is IosBuild.Custom)
)
);
}
if (context.Jobs.Android is { } android)
{
parts.Add(
GitlabPhone(
GitlabAndroid,
android.Targets,
GitlabBuildItems(
context,
AndroidBuildOf(context, android.Build),
android.Build is AndroidBuild.Custom
)
)
);
}
const string Header = """
# Written by `e2e init --ci gitlab`: the e2e tests in every merge request and on
# the default branch, one job per platform. See https://github.com/hardkoded/e2e-dotnet
""";
return Header
+ "\n"
+ Junit(
string.Join("\n\n", parts)
.Replace("@SUFFIX@", context.JobSuffix ?? "", StringComparison.Ordinal)
.Replace("@DOTNET@", context.DotnetVersion, StringComparison.Ordinal)
.Replace("@BUILD@", BuildCommand(context), StringComparison.Ordinal)
.Replace("@PLAYWRIGHT@", PlaywrightScript(context), StringComparison.Ordinal)
.Replace(
"@TEST@",
$"dotnet test -c Release --no-build {Quote(context.ProjectFile)}"
+ (context.Junit ? " --logger \"junit;LogFilePath=$CI_PROJECT_DIR/.e2e/junit.xml\"" : ""),
StringComparison.Ordinal
)
.Replace("@APK@", ApkPath(context), StringComparison.Ordinal)
.Replace("@BUILDDIR@", Join(context.AppDir, "build"), StringComparison.Ordinal),
context.Junit
)
+ "\n";
}
/// A phone job: its E2E_TARGET variable when it runs one target, and the app's build items.
private static string GitlabPhone(string job, IReadOnlyList targets, string items) =>
job.Replace(
" @TARGETVARS@\n",
TargetEnv(targets) is [var line] ? " variables:\n " + line + "\n" : "",
StringComparison.Ordinal
)
.Replace(" @APPBUILD@\n", items, StringComparison.Ordinal);
///
/// A phone job's app build as script items on a self-managed shell runner: for an Expo or React Native app the install and the
/// build in the app's folder, then back to the project; for any other app the step that fails until it is replaced.
///
private static string GitlabBuildItems(WorkflowContext context, AppBuild build, bool custom)
{
var comment = string.Concat(build.Comment.Select(line => " # " + line + "\n"));
if (custom)
{
return comment + " - " + string.Join(" && ", build.Lines) + "\n";
}
var enter = context.AppDir.Length == 0 ? [] : new[] { $"cd \"$CI_PROJECT_DIR/{context.AppDir}\"" };
string[] leave = context.AppDir.Length == 0 ? [] : ["cd \"$CI_PROJECT_DIR\""];
return string.Concat(enter.Concat(InstallLines(context)).Select(line => " - " + line + "\n"))
+ comment
+ string.Concat(build.Lines.Concat(leave).Select(line => " - " + line + "\n"));
}
private const string GitlabWeb = """
e2e-web@SUFFIX@:
image: mcr.microsoft.com/dotnet/sdk:@DOTNET@
interruptible: true
timeout: 30 minutes
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
variables:
# GitLab caches only paths inside the project.
NUGET_PACKAGES: $CI_PROJECT_DIR/.nuget
PLAYWRIGHT_BROWSERS_PATH: $CI_PROJECT_DIR/.cache/ms-playwright
@TARGETVAR@
cache:
key:
files:
- "**/*.csproj"
paths:
- .nuget/
- .cache/ms-playwright/
script:
- @BUILD@
- pwsh @PLAYWRIGHT@ install --with-deps chromium
- @TEST@
artifacts:
when: always
expire_in: 7 days
paths:
- .e2e/report.json
- .e2e/junit.xml@J@
- .e2e/results/
reports:@J@
junit: .e2e/junit.xml@J@
""";
private const string GitlabMobile = """
# GitLab's hosted runners can't run a simulator or an emulator, so the mobile jobs run on
# self-managed runners with the shell executor: a Mac with Xcode, and Linux with KVM, a JDK,
# the Android SDK, and the .NET SDK. Replace the tags with your runners' tags.
.e2e-mobile@SUFFIX@:
interruptible: true
timeout: 90 minutes
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
artifacts:
when: always
expire_in: 7 days
paths:
- .e2e/report.json
- .e2e/junit.xml@J@
- .e2e/results/
# The Android job's emulator output, for a boot that fails.
- .e2e/emulator.log
reports:@J@
junit: .e2e/junit.xml@J@
""";
private const string GitlabIos = """
e2e-ios@SUFFIX@:
extends: .e2e-mobile@SUFFIX@
tags: [macos]
@TARGETVARS@
script:
@APPBUILD@
- @BUILD@
# A simulator of the job's own on a pinned runtime, so the run never drives a device another job left booted.
- export E2E_DEVICE=$(xcrun simctl create "e2e-$CI_JOB_ID" "iPhone 17 Pro" com.apple.CoreSimulator.SimRuntime.iOS-26-5)
- echo "$E2E_DEVICE" > "$CI_PROJECT_DIR/.simulator-udid"
- xcrun simctl bootstatus "$E2E_DEVICE" -b
- xcrun simctl install "$E2E_DEVICE" "$(find @BUILDDIR@ -maxdepth 5 -name '*.app' -type d | head -n 1)"
- @TEST@
after_script:
# after_script starts a new shell in the project directory, after a failed script too, but not after a timeout.
- xcrun simctl delete "$(cat "$CI_PROJECT_DIR/.simulator-udid")" || true
""";
private const string GitlabAndroid = """
e2e-android@SUFFIX@:
extends: .e2e-mobile@SUFFIX@
tags: [linux-kvm]
@TARGETVARS@
script:
@APPBUILD@
- @BUILD@
# An emulator of the job's own: the port comes from CI_CONCURRENT_ID, so parallel jobs on one runner don't share one.
# It boots an AVD named e2e that you create on the runner.
- mkdir -p .e2e
- PORT=$((5554 + 2 * CI_CONCURRENT_ID))
- export E2E_DEVICE=emulator-$PORT
- adb -s $E2E_DEVICE emu kill || true
- $ANDROID_HOME/emulator/emulator -avd e2e -read-only -port $PORT -no-window -no-audio -no-boot-anim -no-snapshot -gpu swiftshader_indirect > .e2e/emulator.log 2>&1 &
- timeout 300 adb -s $E2E_DEVICE wait-for-device
- timeout 300 sh -c 'until [ "$(adb -s $E2E_DEVICE shell getprop sys.boot_completed | tr -d "\r")" = 1 ]; do sleep 2; done'
- adb -s $E2E_DEVICE install -r @APK@
- @TEST@
after_script:
- adb -s emulator-$((5554 + 2 * CI_CONCURRENT_ID)) emu kill || true
""";
private static string Indent(string text, int spaces)
{
var pad = new string(' ', spaces);
return string.Join("\n", text.Split('\n').Select(line => line.Length == 0 ? line : pad + line));
}
private static bool AnchorLine(string line, string anchor) =>
Regex.IsMatch(
CommentPattern().Replace(line, ""),
Regex.Escape(anchor) + @"(?![\w-])",
RegexOptions.None,
RegexTimeout
);
/// The items of the list that starts at : a flow list over any number of lines, or the block items below the key.
private static List ReadList(List lines, int at, string rest)
{
var items = new List();
if (rest.StartsWith('['))
{
var text = new StringBuilder(rest);
for (var i = at + 1; i < lines.Count && !text.ToString().Contains(']', StringComparison.Ordinal); i++)
{
text.Append(' ').Append(CommentPattern().Replace(lines[i], ""));
}
var flow = text.ToString();
var end = flow.IndexOf(']', StringComparison.Ordinal);
flow = flow[1..(end < 0 ? flow.Length : end)];
items.AddRange(flow.Split(',').Select(Unquote).Where(item => item != ""));
return items;
}
for (var i = at + 1; i < lines.Count; i++)
{
var line = CommentPattern().Replace(lines[i], "");
if (line.Trim() == "")
{
continue;
}
var trimmed = line.TrimStart();
if (
!trimmed.StartsWith('-')
|| (line.Length == trimmed.Length && !trimmed.StartsWith("- ", StringComparison.Ordinal))
)
{
break;
}
items.Add(Unquote(trimmed[1..]));
}
return items;
}
private static string Unquote(string item) => item.Trim().Trim('\'', '"').Trim();
[GeneratedRegex(@"(?:^|\s)#.*$", RegexOptions.None, 1000)]
private static partial Regex CommentPattern();
[GeneratedRegex(@"^([A-Za-z_][\w-]*):(?:\s|$)", RegexOptions.None, 1000)]
private static partial Regex KeyPattern();
/// dotnet test as words of their own, which dotnet testing and x-dotnet test are not.
[GeneratedRegex(@"(?:^|[\s;&|()\[\],'""`])dotnet\s+test(?=$|[\s;&|()\[\],'""`])", RegexOptions.None, 1000)]
private static partial Regex RunPattern();
/// The anchors and tags that may come before a YAML value: &stages, !!seq.
[GeneratedRegex(@"^(?:[&!][^\s]*\s*)+", RegexOptions.None, 1000)]
private static partial Regex NodeProperties();
[GeneratedRegex(@"^stages:[ \t]*(.*)$", RegexOptions.None, 1000)]
private static partial Regex TopLevelStages();
[GeneratedRegex(@"^---.*$", RegexOptions.Multiline, 1000)]
private static partial Regex DocumentSeparator();
[GeneratedRegex(@"\s*(?:[^<;]*;)*?net(\d+\.\d+)", RegexOptions.None, 1000)]
private static partial Regex TargetFramework();
}