Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Msix.hasPackageIdentity() to check if an app was installed with an MSIX #294

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 3.17.0

- Add `Msix.assetUri()` to get an `ms-appx:///` URI out of any Flutter asset name.
- Add `Msix.hasPackageIdentity()` to check if the currently running program was installed with an MSIX.

## 3.16.8

- update VCLibs files [#273](https://github.com/YehudaKremer/msix/pull/273)
Expand Down
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ it on a website.

## 📋 Installation

In your `pubspec.yaml`, add the `msix` package as a new [dev dependency] with
In your `pubspec.yaml`, add the `msix` package as a new dependency with
the following command:

```console
PS c:\src\flutter_project> flutter pub add --dev msix
PS c:\src\flutter_project> dart pub add msix
```

## 📦 Creating an MSIX installer
Expand Down Expand Up @@ -135,6 +135,32 @@ the `certificate_path` and `certificate_password` fields.
machine. You can disable this by using the `--install-certificate false` option, or the YAML
option `install_certificate: false`.

## Using this package at runtime

### Get a URI to Flutter assets

If you need to get an `ms-appx:///` URI from a Flutter asset, use `Msix.assetUrI()`:

```dart
// Flutter
final logoPath = 'assets/logo.png';
Image.asset(logoPath);

// Windows APIs
final logoUri = Msix.assetUri('assets/logo.png');
someWindowsApi(logoUri);
```

### Check if an app was installed with an MSIX

Using an MSIX grants your application [package identity](https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/package-identity-overview), which, among other things, allows it to use [certain APIs](https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/modernize-packaged-apps). You might need to check at runtime if your application has package identity.

```dart
if (Msix.hasPackageIdentity()) {
showNotifications();
}
```

## ![microsoft store icon][] Publishing to the Microsoft Store

To generate an MSIX file for publishing to the Microsoft Store, use the
Expand Down
13 changes: 13 additions & 0 deletions lib/msix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:io';
import 'package:path/path.dart' as p;
import 'package:cli_util/cli_logging.dart';
import 'package:get_it/get_it.dart';

import 'src/app_installer.dart';
import 'src/windows_build.dart';
import 'src/configuration.dart';
Expand All @@ -11,9 +12,21 @@ import 'src/appx_manifest.dart';
import 'src/makeappx.dart';
import 'src/sign_tool.dart';
import 'src/method_extensions.dart';
import 'src/msix_check.dart';

/// Main class that handles all the msix package functionality
class Msix {
/// Gets an `ms-appx:///` URI from a [Flutter asset](https://docs.flutter.dev/ui/assets/assets-and-images).
static Uri assetUri(String path) => Uri.parse("ms-appx:///data/flutter_assets/$path");

/// Returns whether the current app was installed with an MSIX installer.
///
/// Using an MSIX grants your application [package identity](https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/package-identity-overview),
/// which allows it to use [certain APIs](https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/modernize-packaged-apps).
static bool hasPackageIdentity() {
return checkPackageIdentity();
}

late Logger _logger;
late Configuration _config;

Expand Down
2 changes: 2 additions & 0 deletions lib/src/msix_check.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'msix_check/stub.dart'
if (dart.library.ffi) 'msix_check/ffi.dart';
14 changes: 14 additions & 0 deletions lib/src/msix_check/ffi.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import "dart:io";
import "dart:ffi";
import "package:ffi/ffi.dart";
import "package:win32/win32.dart";

bool isWindows8OrGreater() => IsWindows8OrGreater() == 1;

bool checkPackageIdentity() => using((arena) {
if (!Platform.isWindows) return false;
if (!isWindows8OrGreater()) return false;
final length = arena<Uint32>();
final error = GetCurrentPackageFullName(length, nullptr);
return error != WIN32_ERROR.APPMODEL_ERROR_NO_PACKAGE;
});
1 change: 1 addition & 0 deletions lib/src/msix_check/stub.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bool checkPackageIdentity() => false;
6 changes: 4 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: msix
description: A command-line tool that create Msix installer from your flutter windows-build files.
version: 3.16.8
version: 3.17.0
maintainer: Yehuda Kremer ([email protected])
homepage: https://github.com/YehudaKremer/msix
issue_tracker: https://github.com/YehudaKremer/msix/issues
Expand All @@ -15,7 +15,7 @@ platforms:
windows:

environment:
sdk: ">=2.19.0 <4.0.0"
sdk: ">=3.1.0 <4.0.0"

dependencies:
args: ^2.3.0
Expand All @@ -27,6 +27,8 @@ dependencies:
pub_semver: ^2.1.0
console: ^4.1.0
cli_util: ^0.4.0
ffi: ^2.1.3
win32: ^5.10.0

dev_dependencies:
test: ^1.23.0
Expand Down
10 changes: 10 additions & 0 deletions test/misc_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:msix/msix.dart';
import 'package:test/test.dart';

void main() => test(
'Asset URIs are valid',
() => expect(
Msix.assetUri('assets/image.jpg'),
Uri.parse('ms-appx:///data/flutter_assets/assets/image.jpg'),
),
);