|
| 1 | +#!/bin/env dart |
| 2 | +// ignore_for_file: avoid_print |
| 3 | + |
| 4 | +import "dart:io"; |
| 5 | +import "package:path/path.dart"; |
| 6 | +import "package:yaml/yaml.dart"; |
| 7 | + |
| 8 | +Future<int> main() async { |
| 9 | + final exampleDir = |
| 10 | + canonicalize(join(dirname(Platform.script.toFilePath()), "../example")); |
| 11 | + |
| 12 | + print("building project"); |
| 13 | + final process = await Process.start( |
| 14 | + "flutter", |
| 15 | + [ |
| 16 | + "build", |
| 17 | + "web", |
| 18 | + "--base-href", |
| 19 | + "/flutter_web_bluetooth/", |
| 20 | + "--release", |
| 21 | + "--source-maps", |
| 22 | + "--dart-define=redirectToHttps=true" |
| 23 | + ], |
| 24 | + workingDirectory: exampleDir); |
| 25 | + process.stdout.pipe(stdout); |
| 26 | + process.stderr.pipe(stderr); |
| 27 | + final exitCode = await process.exitCode; |
| 28 | + if (exitCode != 0) { |
| 29 | + return exitCode; |
| 30 | + } |
| 31 | + |
| 32 | + final buildFolder = join(exampleDir, "build/web"); |
| 33 | + |
| 34 | + print("updating service working"); |
| 35 | + final serviceWorkerPath = join(buildFolder, "flutter_service_worker.js"); |
| 36 | + final serviceWorkerFile = File(serviceWorkerPath); |
| 37 | + final serviceWorker = await serviceWorkerFile.readAsString(); |
| 38 | + await serviceWorkerFile.writeAsString( |
| 39 | + serviceWorker.replaceFirst( |
| 40 | + RegExp("\"/\":"), "\"/flutter_web_bluetooth/\""), |
| 41 | + flush: true); |
| 42 | + |
| 43 | + print("Updating provided pubspec"); |
| 44 | + final pubspecFilePath = join(buildFolder, "assets/pubspec.lock"); |
| 45 | + final pubspecFile = File(pubspecFilePath); |
| 46 | + final pubspecContent = await pubspecFile.readAsString(); |
| 47 | + |
| 48 | + final parsed = loadYaml(pubspecContent); |
| 49 | + final packages = parsed["packages"]; |
| 50 | + if (packages == null) { |
| 51 | + throw ArgumentError(); |
| 52 | + } |
| 53 | + final library = packages["flutter_web_bluetooth"]; |
| 54 | + if (library == null) { |
| 55 | + throw ArgumentError(); |
| 56 | + } |
| 57 | + final version = library["version"]?.toString(); |
| 58 | + if (version == null) { |
| 59 | + throw ArgumentError("Version was not set"); |
| 60 | + } |
| 61 | + final newPubspecContent = """ |
| 62 | + packages: |
| 63 | + flutter_web_bluetooth: |
| 64 | + version: "$version" |
| 65 | + """; |
| 66 | + |
| 67 | + await pubspecFile.writeAsString(newPubspecContent); |
| 68 | + |
| 69 | + return 0; |
| 70 | +} |
0 commit comments