Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

feat(web): About reads the version from the host's release.json

Dev jacoba9367991df7e
diff --git a/desktop/lib/core/app_version.dart b/desktop/lib/core/app_version.dart
index e6b0113d9..5606efee0 100644
--- a/desktop/lib/core/app_version.dart
+++ b/desktop/lib/core/app_version.dart
@@ -1,8 +1,10 @@
import 'dart:io';
+import 'package:flutter/foundation.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'runtime_platform.dart';
+import 'web_release.dart';
/// The version of the build currently running.
///
@@ -14,10 +16,17 @@ import 'runtime_platform.dart';
/// `version.txt` next to the built executable; this reads that back when it
/// exists and falls through to [PackageInfo] everywhere else — macOS,
/// Windows, or a Linux dev build with no packaged `version.txt`.
+///
+/// A browser reads its host's `release.json` instead, the version a deploy is
+/// checked against; `flutter run` serves none and falls through as well.
Future<String> runningAppVersion({
String? executablePath,
Future<String> Function()? packageInfoVersion,
}) async {
+ if (kIsWeb) {
+ final released = await webReleaseVersion();
+ if (released != null) return released;
+ }
if (RuntimePlatform.isLinux) {
final exe = File(executablePath ?? RuntimePlatform.resolvedExecutable);
final versionFile = File('${exe.parent.path}/version.txt');
diff --git a/desktop/lib/core/page_base_native.dart b/desktop/lib/core/page_base_native.dart
new file mode 100644
index 000000000..04f3121aa
--- /dev/null
+++ b/desktop/lib/core/page_base_native.dart
@@ -0,0 +1,2 @@
+/// A native build has no page, so nothing sits beside one.
+Uri? pageBaseUri() => null;
diff --git a/desktop/lib/core/page_base_web.dart b/desktop/lib/core/page_base_web.dart
new file mode 100644
index 000000000..5365d86a7
--- /dev/null
+++ b/desktop/lib/core/page_base_web.dart
@@ -0,0 +1,5 @@
+import 'package:web/web.dart' as web;
+
+/// The page's `<base href>`: `/harness-web/` in a release, whatever the visible
+/// URL is (`/`, `/s/:id`), since the host rewrites those onto its index.html.
+Uri? pageBaseUri() => Uri.tryParse(web.document.baseURI);
diff --git a/desktop/lib/core/web_release.dart b/desktop/lib/core/web_release.dart
new file mode 100644
index 000000000..5f9c05185
--- /dev/null
+++ b/desktop/lib/core/web_release.dart
@@ -0,0 +1,40 @@
+import 'package:dio/dio.dart';
+
+import 'page_base_native.dart'
+ if (dart.library.js_interop) 'page_base_web.dart'
+ as impl;
+
+/// The manifest a web release writes beside its index.html
+/// (`scripts/build-web-release.sh`); the host serves it `no-store`.
+const kWebReleaseManifest = 'release.json';
+
+const _readTimeout = Duration(seconds: 10);
+
+/// The web release this host serves, read from its [kWebReleaseManifest] —
+/// the same file `make release-web` checks after a deploy. Null natively, under
+/// `flutter run` (which serves no manifest), or when it cannot be read.
+Future<String?> webReleaseVersion({Dio? dio}) async {
+ final base = impl.pageBaseUri();
+ if (base == null) return null;
+ try {
+ final response = await (dio ?? Dio()).getUri<Object?>(
+ base.resolve(kWebReleaseManifest),
+ options: Options(
+ responseType: ResponseType.json,
+ sendTimeout: _readTimeout,
+ receiveTimeout: _readTimeout,
+ ),
+ );
+ return webReleaseVersionFrom(response.data);
+ } on DioException {
+ return null;
+ }
+}
+
+/// The `version` a release manifest names — pure, so it is tested without a
+/// browser.
+String? webReleaseVersionFrom(Object? manifest) => switch (manifest) {
+ {'version': final String version} when version.trim().isNotEmpty =>
+ version.trim(),
+ _ => null,
+};
diff --git a/desktop/test/web_release_test.dart b/desktop/test/web_release_test.dart
new file mode 100644
index 000000000..947725dbb
--- /dev/null
+++ b/desktop/test/web_release_test.dart
@@ -0,0 +1,33 @@
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness/core/web_release.dart';
+
+void main() {
+ test('a release manifest names its version', () {
+ expect(
+ webReleaseVersionFrom({
+ 'version': '1.3.19',
+ 'sourceCommit': '54fc5b06',
+ 'baseHref': '/harness-web/',
+ }),
+ '1.3.19',
+ );
+ expect(webReleaseVersionFrom({'version': ' 1.3.20 \n'}), '1.3.20');
+ });
+
+ test('anything else names none', () {
+ for (final manifest in <Object?>[
+ null,
+ '1.3.19',
+ <String, Object?>{},
+ {'version': ''},
+ {'version': 1319},
+ ['1.3.19'],
+ ]) {
+ expect(webReleaseVersionFrom(manifest), isNull, reason: '$manifest');
+ }
+ });
+
+ test('no manifest beside the page (native, or flutter run) names none', () async {
+ expect(await webReleaseVersion(), isNull);
+ });
+}