diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 92c45ff18..213257850 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -68,6 +68,8 @@ jobs: - 'third_party/discord/*.zip.gpg' - 'ios/**' - 'scripts/build_ios.sh' + - 'scripts/flutter_git_commit.sh' + - 'scripts/test_flutter_git_commit.py' - 'scripts/prepare_flutter_sdk.dart' - 'patches/flutter/**' - 'scripts/check_backend_ios.py' @@ -90,6 +92,8 @@ jobs: - 'android/**' - 'rust_backend/**' - 'scripts/build_android.sh' + - 'scripts/flutter_git_commit.sh' + - 'scripts/test_flutter_git_commit.py' - 'scripts/prepare_flutter_sdk.dart' - 'patches/flutter/**' - 'scripts/build_rust_backend.sh' @@ -238,6 +242,7 @@ jobs: - name: Build and audit Rust iOS release application run: | python3 -m unittest discover -s scripts -p 'test_check_backend_ios.py' + python3 -m unittest discover -s scripts -p 'test_flutter_git_commit.py' flutter build ios --release --no-codesign --target lib/main.dart test -s build/ios/iphoneos/Runner.app/Extensions/LinkNowPlaying.appex/LinkNowPlaying if [[ "$MORNYE_DISCORD_SDK" == "1" ]]; then @@ -298,6 +303,7 @@ jobs: run: | python3 -m unittest discover -s scripts -p 'test_prepare_discord_sdk.py' python3 -m unittest discover -s scripts -p 'test_build_android.py' + python3 -m unittest discover -s scripts -p 'test_flutter_git_commit.py' python3 -m unittest discover -s scripts -p 'test_check_backend_apk.py' # Fork PRs cannot access repository secrets and exercise the Lite build. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 928e72d47..9d6ebf34a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -72,9 +72,11 @@ exact Flutter version declared in `.fvmrc` and replace `fvm flutter` with ``` The About footer shows the short commit supplied through `GIT_COMMIT` at -compile time. The Android build script and iOS release workflow supply it -automatically. Include the same `--dart-define` when running Flutter build -commands directly; without it, the footer shows only the copyright. +compile time. Android Gradle and the iOS Flutter build phase supply it +automatically, including direct mobile Flutter builds and Xcode builds. +An explicit `--dart-define` takes precedence. Supply it when building from +a source archive without Git metadata or running desktop Flutter commands +directly; without a revision, the footer shows only the copyright. ## Building the App diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 7b55dfa2e..2dca9923b 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -8,6 +8,17 @@ plugins { id("dev.flutter.flutter-gradle-plugin") } +// Direct Flutter builds also need the revision displayed in About. +val buildDartDefines = providers.exec { + workingDir(rootProject.projectDir.parentFile) + commandLine( + "bash", + "scripts/flutter_git_commit.sh", + project.findProperty("dart-defines")?.toString().orEmpty(), + ) +}.standardOutput.asText.get().trim() +project.extensions.extraProperties["dart-defines"] = buildDartDefines + // Flutter reads this when configuring variant tasks, after this build script. // Keep release AOT debug data outside libapp.so; explicit paths and size analysis take precedence. if (!project.hasProperty("split-debug-info") && !project.hasProperty("code-size-directory")) { diff --git a/ios/Runner.xcodeproj/project.pbxproj b/ios/Runner.xcodeproj/project.pbxproj index d665be2f4..a8ba2f540 100644 --- a/ios/Runner.xcodeproj/project.pbxproj +++ b/ios/Runner.xcodeproj/project.pbxproj @@ -457,7 +457,7 @@ ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" build"; + shellScript = "set -e\nDART_DEFINES=\"$(/bin/bash \"$SRCROOT/../scripts/flutter_git_commit.sh\" \"${DART_DEFINES:-}\")\"\nexport DART_DEFINES\n/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" build"; }; A46BE908918B6EAA621AB35B /* [CP] Check Pods Manifest.lock */ = { isa = PBXShellScriptBuildPhase; diff --git a/scripts/flutter_git_commit.sh b/scripts/flutter_git_commit.sh new file mode 100644 index 000000000..8b8f3fd48 --- /dev/null +++ b/scripts/flutter_git_commit.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +# Supply About's build revision without replacing explicit Flutter defines. + +set -euo pipefail + +BUILD_DART_DEFINES="${1:-}" +IFS=',' read -r -a ENCODED_DEFINES <<< "$BUILD_DART_DEFINES" +for encoded_define in ${ENCODED_DEFINES[@]+"${ENCODED_DEFINES[@]}"}; do + decoded_define="$(printf '%s' "$encoded_define" | base64 --decode 2>/dev/null || true)" + if [[ "$decoded_define" == GIT_COMMIT=* ]]; then + printf '%s\n' "$BUILD_DART_DEFINES" + exit 0 + fi +done + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +BUILD_GIT_COMMIT="$(git -C "$SCRIPT_DIR/.." rev-parse --short=8 HEAD 2>/dev/null || true)" +if [[ "$BUILD_GIT_COMMIT" =~ ^[0-9a-f]{8,40}$ ]]; then + encoded_commit="$(printf 'GIT_COMMIT=%s' "$BUILD_GIT_COMMIT" | base64 | tr -d '\r\n')" + BUILD_DART_DEFINES="${BUILD_DART_DEFINES:+$BUILD_DART_DEFINES,}$encoded_commit" +fi +printf '%s\n' "$BUILD_DART_DEFINES" diff --git a/scripts/test_flutter_git_commit.py b/scripts/test_flutter_git_commit.py new file mode 100644 index 000000000..62a72bc99 --- /dev/null +++ b/scripts/test_flutter_git_commit.py @@ -0,0 +1,109 @@ +"""Check mobile build revisions without building the native applications.""" + +import base64 +import json +import os +import shutil +import subprocess +import tempfile +import unittest +from pathlib import Path + + +class FlutterGitCommitTest(unittest.TestCase): + def setUp(self): + directory = tempfile.TemporaryDirectory() + self.addCleanup(directory.cleanup) + self.project = Path(directory.name) + scripts = self.project / "scripts" + scripts.mkdir() + self.script = scripts / "flutter_git_commit.sh" + shutil.copyfile(Path(__file__).with_name(self.script.name), self.script) + tools = self.project / "tools" + tools.mkdir() + git = tools / "git" + git.write_text('#!/bin/sh\nprintf "%s\\n" "${BUILD_TEST_COMMIT:-}"\n') + git.chmod(0o755) + self.env = os.environ.copy() + self.env.update( + PATH=str(tools) + os.pathsep + os.defpath, + BUILD_TEST_COMMIT="1234abcd", + ) + + @staticmethod + def encode(value): + return base64.b64encode(value.encode()).decode() + + def defines(self, existing=""): + result = subprocess.run( + ["/bin/bash", str(self.script), existing], + cwd=self.project, env=self.env, text=True, capture_output=True, + ) + self.assertEqual(result.returncode, 0, result.stderr) + return result.stdout.strip() + + def test_direct_build_supplies_current_revision(self): + self.assertEqual(self.defines(), self.encode("GIT_COMMIT=1234abcd")) + + def test_other_defines_are_preserved(self): + existing = self.encode("FEATURE=true") + "," + self.encode("LABEL=hello, world") + self.assertEqual( + self.defines(existing), existing + "," + self.encode("GIT_COMMIT=1234abcd"), + ) + + def test_explicit_revision_takes_precedence_without_duplicates(self): + existing = self.encode("GIT_COMMIT=87654321") + "," + self.encode("FEATURE=true") + self.assertEqual(self.defines(existing), existing) + + def test_explicit_empty_revision_is_preserved(self): + existing = self.encode("GIT_COMMIT=") + self.assertEqual(self.defines(existing), existing) + + def test_source_archive_preserves_existing_defines(self): + self.env["BUILD_TEST_COMMIT"] = "" + existing = self.encode("FEATURE=true") + self.assertEqual(self.defines(existing), existing) + self.assertEqual(self.defines(), "") + + def test_non_revision_git_output_is_ignored(self): + self.env["BUILD_TEST_COMMIT"] = "invalid revision" + self.assertEqual(self.defines(), "") + + def test_ios_build_phase_exports_revision_to_flutter(self): + project_file = ( + Path(__file__).resolve().parent.parent + / "ios/Runner.xcodeproj/project.pbxproj" + ) + phase = next( + line.strip() for line in project_file.read_text().splitlines() + if "shellScript =" in line and "flutter_git_commit.sh" in line + ) + command = json.loads(phase.removeprefix("shellScript = ").removesuffix(";")) + flutter_root = self.project / "flutter" + backend = flutter_root / "packages/flutter_tools/bin/xcode_backend.sh" + backend.parent.mkdir(parents=True) + backend.write_text( + '#!/bin/sh\n[ "$1" = build ] || exit 1\n' + 'printf "%s\\n" "$DART_DEFINES"\n' + ) + (self.project / "ios").mkdir() + self.env.update( + SRCROOT=str(self.project / "ios"), FLUTTER_ROOT=str(flutter_root), + ) + for revision in (None, "87654321"): + with self.subTest(revision=revision): + existing = self.encode("FEATURE=true") + expected = existing + "," + self.encode("GIT_COMMIT=1234abcd") + if revision is not None: + existing += "," + self.encode("GIT_COMMIT=" + revision) + expected = existing + self.env["DART_DEFINES"] = existing + result = subprocess.run( + ["/bin/sh", "-c", command], env=self.env, text=True, capture_output=True, + ) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(result.stdout.strip(), expected) + + +if __name__ == "__main__": + unittest.main()