public async Task<AppxManifestCreatorAdviser> AnalyzeDirectory(DirectoryInfo directoryInfo, CancellationToken cancellationToken = default)
        {
            var result = new AppxManifestCreatorAdviser();

            var manifest = new FileInfo(Path.Combine(directoryInfo.FullName, FileConstants.AppxManifestFile));
            if (manifest.Exists)
            {
                result.Manifest = manifest;
            }

            result.RegistryFiles = directoryInfo.EnumerateFiles("*.reg", SearchOption.AllDirectories).ToArray();
            result.Directory = directoryInfo;
            result.EntryPoints = (await this.GetEntryPointCandidates(directoryInfo, cancellationToken).ConfigureAwait(false)).ToArray();

            if (manifest.Exists)
            {
                var manifestSummary = await AppxManifestSummaryReader.FromManifest(manifest.FullName, AppxManifestSummaryReader.ReadMode.Properties);
                if (manifestSummary.Logo != null)
                {
                    result.Logo = new FileInfo(Path.Combine(directoryInfo.FullName, manifestSummary.Logo.Replace("/", "\\")));
                    if (!result.Logo.Exists)
                    {
                        result.Logo = null;
                    }
                }
            }

            return result;
        }
Beispiel #2
0
        private static async Task <MsixPackageVisuals> GetVisualsFromManifest(string installLocation, CancellationToken cancellationToken = default)
        {
            try
            {
                cancellationToken.ThrowIfCancellationRequested();
                var reader = await AppxManifestSummaryReader.FromInstallLocation(installLocation).ConfigureAwait(false);

                var logo = Path.Combine(installLocation, reader.Logo);

                if (File.Exists(Path.Combine(installLocation, logo)))
                {
                    return(new MsixPackageVisuals(
                               reader.DisplayName,
                               reader.DisplayPublisher,
                               logo,
                               reader.Description,
                               reader.AccentColor,
                               reader.PackageType));
                }

                var extension  = Path.GetExtension(logo);
                var baseName   = Path.GetFileNameWithoutExtension(logo);
                var baseFolder = Path.GetDirectoryName(logo);

                logo = null;

                // ReSharper disable once AssignNullToNotNullAttribute
                var dirInfo = new DirectoryInfo(Path.Combine(installLocation, baseFolder));
                if (dirInfo.Exists)
                {
                    var found = dirInfo.EnumerateFiles(baseName + "*" + extension).FirstOrDefault();
                    if (found != null)
                    {
                        logo = found.FullName;
                    }
                }

                cancellationToken.ThrowIfCancellationRequested();
                return(new MsixPackageVisuals(reader.DisplayName, reader.DisplayPublisher, logo, reader.Description, reader.AccentColor, reader.PackageType));
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception)
            {
                return(new MsixPackageVisuals());
            }
        }
        public async Task Add(string filePath, AddAppxPackageOptions options = 0, CancellationToken cancellationToken = default, IProgress <ProgressData> progress = default)
        {
            this.SideloadingConfigurator.AssertSideloadingEnabled();

            Logger.Info("Installing package {0}", filePath);
            if (filePath == null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }

            if (string.Equals(Path.GetFileName(filePath), FileConstants.AppxManifestFile, StringComparison.OrdinalIgnoreCase))
            {
                if (options.HasFlag(AddAppxPackageOptions.AllBundleResources))
                {
                    throw new ArgumentException("Cannot use the flag AllBundleResources with non-bundle packages.", nameof(options));
                }

                var reader = await AppxManifestSummaryReader.FromManifest(filePath).ConfigureAwait(false);

                DeploymentOptions deploymentOptions = 0;

                if (options.HasFlag(AddAppxPackageOptions.AllowDowngrade))
                {
                    deploymentOptions |= DeploymentOptions.ForceUpdateFromAnyVersion;
                }

                if (options.HasFlag(AddAppxPackageOptions.KillRunningApps))
                {
                    deploymentOptions |= DeploymentOptions.ForceApplicationShutdown;
                    deploymentOptions |= DeploymentOptions.ForceTargetApplicationShutdown;
                }

                deploymentOptions |= DeploymentOptions.DevelopmentMode;

                await AsyncOperationHelper.ConvertToTask(
                    PackageManagerWrapper.Instance.RegisterPackageAsync(new Uri(filePath), Enumerable.Empty <Uri>(), deploymentOptions),
                    $"Installing {reader.DisplayName} {reader.Version}...",
                    cancellationToken,
                    progress).ConfigureAwait(false);
            }
            else if (string.Equals(FileConstants.AppInstallerExtension, Path.GetExtension(filePath), StringComparison.OrdinalIgnoreCase))
            {
                if (options.HasFlag(AddAppxPackageOptions.AllUsers))
                {
                    throw new ArgumentException("Cannot install a package from .appinstaller for all users.", nameof(options));
                }

                if (options.HasFlag(AddAppxPackageOptions.AllBundleResources))
                {
                    throw new ArgumentException("Cannot use the flag AllBundleResources with non-bundle packages.", nameof(options));
                }

                if (options.HasFlag(AddAppxPackageOptions.AllowDowngrade))
                {
                    throw new ArgumentException("Cannot force a downgrade with .appinstaller. The .appinstaller defines on its own whether the downgrade is allowed.", nameof(options));
                }

                AddPackageByAppInstallerOptions deploymentOptions = 0;

                if (options.HasFlag(AddAppxPackageOptions.KillRunningApps))
                {
                    deploymentOptions |= AddPackageByAppInstallerOptions.ForceTargetAppShutdown;
                }

                var volume = PackageManagerWrapper.Instance.GetDefaultPackageVolume();
                await AsyncOperationHelper.ConvertToTask(
                    PackageManagerWrapper.Instance.AddPackageByAppInstallerFileAsync(new Uri(filePath, UriKind.Absolute), deploymentOptions, volume),
                    "Installing from " + Path.GetFileName(filePath) + "...",
                    cancellationToken,
                    progress).ConfigureAwait(false);
            }
            else
            {
                string name, version, publisher;

                DeploymentOptions deploymentOptions = 0;

                switch (Path.GetExtension(filePath))
                {
                case FileConstants.AppxBundleExtension:
                case FileConstants.MsixBundleExtension:
                {
                    IAppxIdentityReader reader = new AppxIdentityReader();
                    var identity = await reader.GetIdentity(filePath, cancellationToken).ConfigureAwait(false);

                    name      = identity.Name;
                    publisher = identity.Publisher;
                    version   = identity.Version;

                    if (options.HasFlag(AddAppxPackageOptions.AllBundleResources))
                    {
                        deploymentOptions |= DeploymentOptions.InstallAllResources;
                    }

                    break;
                }

                default:
                {
                    if (options.HasFlag(AddAppxPackageOptions.AllBundleResources))
                    {
                        throw new ArgumentException("Cannot use the flag AllBundleResources with non-bundle packages.", nameof(options));
                    }

                    var reader = await AppxManifestSummaryReader.FromMsix(filePath).ConfigureAwait(false);

                    name      = reader.DisplayName;
                    version   = reader.Version;
                    publisher = reader.Publisher;
                    break;
                }
                }

                if (options.HasFlag(AddAppxPackageOptions.AllowDowngrade))
                {
                    deploymentOptions |= DeploymentOptions.ForceUpdateFromAnyVersion;
                }

                if (options.HasFlag(AddAppxPackageOptions.KillRunningApps))
                {
                    deploymentOptions |= DeploymentOptions.ForceApplicationShutdown;
                    deploymentOptions |= DeploymentOptions.ForceTargetApplicationShutdown;
                }

                if (options.HasFlag(AddAppxPackageOptions.AllUsers))
                {
                    var deploymentResult = await AsyncOperationHelper.ConvertToTask(
                        PackageManagerWrapper.Instance.AddPackageAsync(new Uri(filePath, UriKind.Absolute), Enumerable.Empty <Uri>(), deploymentOptions),
                        $"Installing {name} {version}...",
                        cancellationToken,
                        progress).ConfigureAwait(false);

                    if (!deploymentResult.IsRegistered)
                    {
                        throw new InvalidOperationException("The package could not be registered.");
                    }

                    var findInstalled = PackageManagerWrapper.Instance.FindPackages(name, publisher).FirstOrDefault();
                    if (findInstalled == null)
                    {
                        throw new InvalidOperationException("The package could not be registered.");
                    }

                    var familyName = findInstalled.Id.FamilyName;

                    await AsyncOperationHelper.ConvertToTask(
                        PackageManagerWrapper.Instance.ProvisionPackageForAllUsersAsync(familyName),
                        $"Provisioning {name} {version}...",
                        cancellationToken,
                        progress).ConfigureAwait(false);
                }
                else
                {
                    var deploymentResult = await AsyncOperationHelper.ConvertToTask(
                        PackageManagerWrapper.Instance.AddPackageAsync(new Uri(filePath, UriKind.Absolute), Enumerable.Empty <Uri>(), deploymentOptions),
                        "Installing " + name + "...",
                        cancellationToken,
                        progress).ConfigureAwait(false);

                    if (!deploymentResult.IsRegistered)
                    {
                        var message = "Could not install " + name + " " + version + ".";
                        if (!string.IsNullOrEmpty(deploymentResult.ErrorText))
                        {
                            message += " " + deploymentResult.ErrorText;
                        }

                        if (deploymentResult.ExtendedErrorCode != null)
                        {
                            throw new InvalidOperationException(message, deploymentResult.ExtendedErrorCode);
                        }

                        throw new InvalidOperationException(message);
                    }
                }
            }
        }