flutter.buildFlutterApplication: Allow building for the Web

This commit is contained in:
hacker1024
2024-01-18 10:16:55 +11:00
parent b43ee05f4e
commit f51a0b053d
3 changed files with 151 additions and 127 deletions

View File

@@ -103,6 +103,9 @@ flutter.buildFlutterApplication {
pname = "firmware-updater"; pname = "firmware-updater";
version = "unstable-2023-04-30"; version = "unstable-2023-04-30";
# To build for the Web, use the flutterHostPlatform argument.
# flutterHostPlatform = "web";
src = fetchFromGitHub { src = fetchFromGitHub {
owner = "canonical"; owner = "canonical";
repo = "firmware-updater"; repo = "firmware-updater";

View File

@@ -17,13 +17,14 @@
{ pubGetScript ? "flutter pub get" { pubGetScript ? "flutter pub get"
, flutterBuildFlags ? [ ] , flutterBuildFlags ? [ ]
, flutterHostPlatform ? "linux"
, extraWrapProgramArgs ? "" , extraWrapProgramArgs ? ""
, ... , ...
}@args: }@args:
(buildDartApplication.override { let
dart = flutter; hostPlatforms = rec {
}) (args // { universal = args // {
sdkSetupScript = '' sdkSetupScript = ''
# Pub needs SSL certificates. Dart normally looks in a hardcoded path. # Pub needs SSL certificates. Dart normally looks in a hardcoded path.
# https://github.com/dart-lang/sdk/blob/3.1.0/runtime/bin/security_context_linux.cc#L48 # https://github.com/dart-lang/sdk/blob/3.1.0/runtime/bin/security_context_linux.cc#L48
@@ -79,8 +80,12 @@
}]' "$out" | '${moreutils}/bin/sponge' "$out" }]' "$out" | '${moreutils}/bin/sponge' "$out"
fi fi
''; '';
};
nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [ linux = universal // {
outputs = universal.outputs or [ ] ++ [ "debug" ];
nativeBuildInputs = (universal.nativeBuildInputs or [ ]) ++ [
wrapGAppsHook wrapGAppsHook
# Flutter requires pkg-config for Linux desktop support, and many plugins # Flutter requires pkg-config for Linux desktop support, and many plugins
@@ -92,10 +97,10 @@
pkg-config pkg-config
]; ];
buildInputs = (args.buildInputs or [ ]) ++ [ glib ]; buildInputs = (universal.buildInputs or [ ]) ++ [ glib ];
dontDartBuild = true; dontDartBuild = true;
buildPhase = args.buildPhase or '' buildPhase = universal.buildPhase or ''
runHook preBuild runHook preBuild
mkdir -p build/flutter_assets/fonts mkdir -p build/flutter_assets/fonts
@@ -106,7 +111,7 @@
''; '';
dontDartInstall = true; dontDartInstall = true;
installPhase = args.installPhase or '' installPhase = universal.installPhase or ''
runHook preInstall runHook preInstall
built=build/linux/*/release/bundle built=build/linux/*/release/bundle
@@ -134,11 +139,6 @@
fi fi
done done
# Install the package_config.json file.
# This is normally done by dartInstallHook, but we disable it.
mkdir -p "$pubcache"
cp .dart_tool/package_config.json "$pubcache/package_config.json"
runHook postInstall runHook postInstall
''; '';
@@ -147,4 +147,30 @@
''${gappsWrapperArgs[@]} \ ''${gappsWrapperArgs[@]} \
${extraWrapProgramArgs} ${extraWrapProgramArgs}
''; '';
}) };
web = universal // {
dontDartBuild = true;
buildPhase = universal.buildPhase or ''
runHook preBuild
mkdir -p build/flutter_assets/fonts
flutter build web -v --release ${builtins.concatStringsSep " " (map (flag: "\"${flag}\"") flutterBuildFlags)}
runHook postBuild
'';
dontDartInstall = true;
installPhase = universal.installPhase or ''
runHook preInstall
cp -r build/web "$out"
runHook postInstall
'';
};
};
in
(buildDartApplication.override { dart = flutter.override { supportedTargetFlutterPlatforms = [ "universal" flutterHostPlatform ]; }; })
hostPlatforms.${flutterHostPlatform} or "Unsupported Flutter host platform: ${flutterHostPlatform}"

View File

@@ -51,12 +51,7 @@ let
(mkCustomFlutter args).overrideAttrs (prev: next: { (mkCustomFlutter args).overrideAttrs (prev: next: {
passthru = next.passthru // rec { passthru = next.passthru // rec {
inherit wrapFlutter mkCustomFlutter mkFlutter; inherit wrapFlutter mkCustomFlutter mkFlutter;
buildFlutterApplication = callPackage ../../../build-support/flutter { buildFlutterApplication = callPackage ../../../build-support/flutter { flutter = wrapFlutter (mkCustomFlutter args); };
# Package a minimal version of Flutter that only uses Linux desktop release artifacts.
flutter = (wrapFlutter (mkCustomFlutter args)).override {
supportedTargetFlutterPlatforms = [ "universal" "linux" ];
};
};
}; };
}); });