Пример #1
0
        public static async Task <AppxPackage> ToAppxPackage(this InstalledPackage pkg, CancellationToken cancellationToken = default)
        {
            if (pkg.InstallLocation == null)
            {
                return(null);
            }

            var manifestReader = new AppxManifestReader();

            IAppxFileReader reader;

            if (pkg.ManifestLocation == null || !File.Exists(pkg.ManifestLocation))
            {
                reader = new PackageIdentityFileReaderAdapter(PackageContext.CurrentUser, pkg.PackageId);
            }
            else
            {
                reader = new FileInfoFileReaderAdapter(pkg.ManifestLocation);
            }

            using (reader)
            {
                return(await manifestReader.Read(reader, cancellationToken).ConfigureAwait(false));
            }
        }
        public async Task RunToolInContext(InstalledPackage package, string toolPath, string arguments, CancellationToken cancellationToken = default, IProgress <ProgressData> progress = default)
        {
            if (package == null)
            {
                throw new ArgumentNullException(nameof(package));
            }

            if (toolPath == null)
            {
                throw new ArgumentNullException(nameof(toolPath));
            }

            using (IAppxFileReader reader = new FileInfoFileReaderAdapter(package.ManifestLocation))
            {
                var maniReader = new AppxManifestReader();
                var manifest   = await maniReader.Read(reader, cancellationToken).ConfigureAwait(false);

                if (!manifest.Applications.Any())
                {
                    throw new InvalidOperationException("Cannot execute a command in this package context. The package does not have any applications defined.");
                }

                await RunToolInContext(package.PackageFamilyName, manifest.Applications[0].Id, toolPath, arguments, cancellationToken, progress).ConfigureAwait(false);
            }
        }
Пример #3
0
        public async Task <AppxPackage> GetByManifestPath(string manifestPath, PackageFindMode mode = PackageFindMode.CurrentUser, CancellationToken cancellationToken = default, IProgress <ProgressData> progress = default)
        {
            using IAppxFileReader reader = new FileInfoFileReaderAdapter(manifestPath);
            var manifestReader = new AppxManifestReader();
            // ReSharper disable once AccessToDisposedClosure
            var package = await Task.Run(() => manifestReader.Read(reader, true, cancellationToken), cancellationToken).ConfigureAwait(false);

            return(package);
        }
Пример #4
0
        private void SourcePathOnValueChanged(object sender, ValueChangedEventArgs e)
        {
            try
            {
                var ext = Path.GetExtension((string)e.NewValue);
                if (string.Equals(".msix", ext))
                {
                    using (IAppxFileReader reader = new ZipArchiveFileReaderAdapter((string)e.NewValue))
                    {
                        var mr   = new AppxManifestReader();
                        var read = mr.Read(reader).GetAwaiter().GetResult();
                        if (string.IsNullOrWhiteSpace(this.DisplayName.CurrentValue))
                        {
                            this.DisplayName.CurrentValue = read.DisplayName + " - Modification package";
                        }

                        this.ParentName.CurrentValue      = read.Name;
                        this.ParentPublisher.CurrentValue = read.Publisher;
                    }
                }
                else
                {
                    using (IAppxFileReader reader = new FileInfoFileReaderAdapter((string)e.NewValue))
                    {
                        var mr   = new AppxManifestReader();
                        var read = mr.Read(reader).GetAwaiter().GetResult();
                        if (string.IsNullOrWhiteSpace(this.DisplayName.CurrentValue))
                        {
                            this.DisplayName.CurrentValue = read.DisplayName + " - Modification package";
                        }

                        this.ParentName.CurrentValue      = read.Name;
                        this.ParentPublisher.CurrentValue = read.Publisher;
                    }
                }

                this.OnPropertyChanged(nameof(IsIncludeVfsFoldersEnabled));
            }
            catch (Exception exception)
            {
                Logger.Error(exception);
                this.interactionService.ShowError("Could not read the properties from the package.", exception);
            }
        }
Пример #5
0
        public async Task RunToolInContext(InstalledPackage package, string toolPath, string arguments = null, CancellationToken cancellationToken = default, IProgress <ProgressData> progress = default)
        {
            using IAppxFileReader reader = new FileInfoFileReaderAdapter(package.ManifestLocation);

            var maniReader = new AppxManifestReader();
            var manifest   = await maniReader.Read(reader, cancellationToken).ConfigureAwait(false);

            if (!manifest.Applications.Any())
            {
                throw new InvalidOperationException("Cannot start tool on a package without applications.");
            }

            var proxyObject = new RunToolInContextDto
            {
                PackageFamilyName = package.PackageFamilyName,
                AppId             = manifest.Applications[0].Id,
                Arguments         = arguments,
                ToolPath          = toolPath
            };

            await this.client.Invoke(proxyObject, cancellationToken, progress).ConfigureAwait(false);
        }
        private static async Task <string[]> GetEntryPoints(string manifestLocation)
        {
            if (!File.Exists(manifestLocation))
            {
                return(new string[0]);
            }

            var reader = new AppxManifestReader();

            using (IAppxFileReader appxSource = new FileInfoFileReaderAdapter(manifestLocation))
            {
                var appxPackage = await reader.Read(appxSource).ConfigureAwait(false);

                return(appxPackage.Applications.Select(app =>
                {
                    if (string.IsNullOrEmpty(app.Id))
                    {
                        return @"shell:appsFolder\" + appxPackage.FamilyName;
                    }

                    return @"shell:appsFolder\" + appxPackage.FamilyName + "!" + app.Id;
                }).ToArray());
            }
        }
Пример #7
0
        private async Task PrepareModificationPackage(XDocument template, ModificationPackageConfig config)
        {
            XNamespace nsUap4           = "http://schemas.microsoft.com/appx/manifest/uap/windows10/4";
            XNamespace nsUap6           = "http://schemas.microsoft.com/appx/manifest/uap/windows10/6";
            XNamespace nsRescap         = "http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities";
            XNamespace nsRescap6        = "http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities/6";
            XNamespace nsBuild          = "http://schemas.microsoft.com/developer/appx/2015/build";
            XNamespace defaultNamespace = "http://schemas.microsoft.com/appx/manifest/foundation/windows10";

            var root = template.Root;

            if (root == null)
            {
                root = new XElement(defaultNamespace + "Package");
                template.Add(root);
            }
            else
            {
                defaultNamespace = root.GetDefaultNamespace();
            }

            if (root.GetPrefixOfNamespace(nsUap4) == null)
            {
                root.Add(new XAttribute(XNamespace.Xmlns + "uap4", nsUap6.NamespaceName));
            }

            if (root.GetPrefixOfNamespace(nsUap6) == null)
            {
                root.Add(new XAttribute(XNamespace.Xmlns + "uap6", nsUap6.NamespaceName));
            }

            if (root.GetPrefixOfNamespace(nsRescap) == null)
            {
                root.Add(new XAttribute(XNamespace.Xmlns + "rescap", nsRescap.NamespaceName));
            }

            if (root.GetPrefixOfNamespace(nsRescap6) == null)
            {
                root.Add(new XAttribute(XNamespace.Xmlns + "rescap6", nsRescap6.NamespaceName));
            }

            if (root.GetPrefixOfNamespace(nsBuild) == null)
            {
                root.Add(new XAttribute(XNamespace.Xmlns + "build", nsBuild.NamespaceName));
            }

            var package      = GetOrCreateNode(template, "Package", defaultNamespace);
            var dependencies = GetOrCreateNode(package, "Dependencies", defaultNamespace);

            var dependency = new XElement(nsUap4 + "MainPackageDependency");

            dependencies.Add(dependency);

            var parentName      = config.ParentName;
            var parentPublisher = config.ParentPublisher;

            if (string.IsNullOrEmpty(parentPublisher) || string.IsNullOrEmpty(parentName))
            {
                IAppxFileReader reader = null;
                try
                {
                    if (string.Equals(FileConstants.AppxManifestFile, Path.GetFileName(config.ParentPackagePath), StringComparison.OrdinalIgnoreCase))
                    {
                        reader = new FileInfoFileReaderAdapter(config.ParentPackagePath);
                    }
                    else
                    {
                        reader = new ZipArchiveFileReaderAdapter(config.ParentPackagePath);
                    }

                    var manifestReader = new AppxManifestReader();
                    var read           = await manifestReader.Read(reader).ConfigureAwait(false);

                    if (string.IsNullOrEmpty(parentPublisher))
                    {
                        parentPublisher = read.Publisher;
                    }

                    if (string.IsNullOrEmpty(parentName))
                    {
                        parentName = read.Name;
                    }
                }
                finally
                {
                    reader?.Dispose();
                }
            }

            dependency.SetAttributeValue("Name", parentName);
            dependency.SetAttributeValue("Publisher", parentPublisher);

            var identity = GetOrCreateNode(package, "Identity", defaultNamespace);

            identity.SetAttributeValue("Name", config.Name);
            identity.SetAttributeValue("Publisher", config.Publisher);

            var fixVersion = Version.Parse(config.Version);

            var major    = fixVersion.Major;
            var minor    = fixVersion.Minor;
            var build    = fixVersion.Build;
            var revision = fixVersion.Revision;

            if (major < 0)
            {
                throw new FormatException("Invalid version format, major version is required.");
            }

            if (minor < 0)
            {
                throw new FormatException("Invalid version format, major version is required.");
            }

            if (revision < 0)
            {
                revision = 0;
            }

            if (build < 0)
            {
                build = 0;
            }

            identity.SetAttributeValue("Version", new Version(major, minor, build, revision).ToString(4));

            var properties = GetOrCreateNode(package, "Properties", defaultNamespace);

            GetOrCreateNode(properties, "DisplayName", defaultNamespace).Value          = config.DisplayName ?? "Modification Package Name";
            GetOrCreateNode(properties, "PublisherDisplayName", defaultNamespace).Value = config.DisplayPublisher ?? "Modification Package Publisher Name";
            GetOrCreateNode(properties, "Description", defaultNamespace).Value          = "Modification Package for " + parentName;
            GetOrCreateNode(properties, "Logo", defaultNamespace).Value         = "Assets\\Logo.png";
            GetOrCreateNode(properties, "ModificationPackage", nsRescap6).Value = "true";

            var branding = new MsixHeroBrandingInjector();

            branding.Inject(template);
        }
Пример #8
0
        private async Task <YamlDefinition> CreateFromMsix(string filePath, CancellationToken cancellationToken = default)
        {
            var yamlDefinition = new YamlDefinition()
            {
                Installers = new List <YamlInstaller>
                {
                    new YamlInstaller
                    {
                        Scope         = YamlScope.user,
                        InstallerType = YamlInstallerType.msix
                    }
                }
            };

            IAppxFileReader reader;

            if (filePath.EndsWith(".xml", StringComparison.OrdinalIgnoreCase))
            {
                reader = new FileInfoFileReaderAdapter(filePath);
            }
            else
            {
                reader = new ZipArchiveFileReaderAdapter(filePath);

                try
                {
                    yamlDefinition.Installers[0].SignatureSha256 = await this.CalculateSignatureHashAsync(new FileInfo(filePath), cancellationToken).ConfigureAwait(false);
                }
                catch (ArgumentException)
                {
                }
            }

            using (reader)
            {
                var manifestReader = new AppxManifestReader();
                var details        = await manifestReader.Read(reader, cancellationToken).ConfigureAwait(false);

                yamlDefinition.Name        = details.DisplayName;
                yamlDefinition.Publisher   = details.PublisherDisplayName;
                yamlDefinition.Version     = details.Version;
                yamlDefinition.Description = details.Description;

                if (details.Applications?.Any() == true)
                {
                    // Exclude some unrelated PSF stuff - they are not the right choice for the app moniker.
                    var candidateForAppMoniker = details.Applications.Select(a => a.Executable)
                                                 .FirstOrDefault(a =>
                                                                 !string.IsNullOrEmpty(a) && !a.StartsWith("psf", StringComparison.OrdinalIgnoreCase) &&
                                                                 !a.StartsWith("AI_stubs", StringComparison.OrdinalIgnoreCase) &&
                                                                 a.EndsWith(".exe", StringComparison.OrdinalIgnoreCase));

                    if (!string.IsNullOrEmpty(candidateForAppMoniker))
                    {
                        yamlDefinition.AppMoniker = candidateForAppMoniker.Substring(0, candidateForAppMoniker.Length - ".exe".Length).Split('\\', '/').Last();
                    }
                }

                switch (details.ProcessorArchitecture)
                {
                case AppxPackageArchitecture.Arm:
                    yamlDefinition.Installers[0].Arch = YamlArchitecture.arm;
                    break;

                case AppxPackageArchitecture.Neutral:
                    yamlDefinition.Installers[0].Arch = YamlArchitecture.Neutral;
                    break;

                case AppxPackageArchitecture.Arm64:
                    yamlDefinition.Installers[0].Arch = YamlArchitecture.arm64;
                    break;

                case AppxPackageArchitecture.x86:
                    yamlDefinition.Installers[0].Arch = YamlArchitecture.x86;
                    break;

                case AppxPackageArchitecture.x64:
                    yamlDefinition.Installers[0].Arch = YamlArchitecture.x64;
                    break;
                }
            }

            return(yamlDefinition);
        }