public override async Task <int> Execute() { var msixSdkWrapper = new MakeAppxWrapper(); Logger.Info($"Unpacking [{this.Verb.Package}] to [{this.Verb.Directory}]..."); try { await this.Console.WriteInfo($"Unpacking [{this.Verb.Package}] to [{this.Verb.Directory}]...").ConfigureAwait(false); await msixSdkWrapper.UnpackPackage(this.Verb.Package, this.Verb.Directory, !this.Verb.NoValidation).ConfigureAwait(false); await this.Console.WriteSuccess($"The package has been unpacked to [{this.Verb.Directory}]."); if (this.Verb.RemovePackageAfterExtraction) { await this.Console.WriteInfo($"Removing source package {this.Verb.Package}..."); ExceptionGuard.Guard(() => File.Delete(this.Verb.Package)); } return(StandardExitCodes.ErrorSuccess); } catch (SdkException e) { Logger.Error(e); await this.Console.WriteError(e.Message); return(e.ExitCode); } catch (Exception e) { Logger.Error(e); await this.Console.WriteError(e.Message); return(StandardExitCodes.ErrorGeneric); } }
public override async Task <int> Execute() { var msixSdkWrapper = new MakeAppxWrapper(); Logger.Info($"Packing [{this.Verb.Directory}] to [{this.Verb.Package}]..."); try { await this.Console.WriteInfo($"Packing [{this.Verb.Directory}] to [{this.Verb.Package}]...").ConfigureAwait(false); await msixSdkWrapper.PackPackageDirectory(this.Verb.Directory, this.Verb.Package, !this.Verb.NoCompression, !this.Verb.NoValidation).ConfigureAwait(false); await this.Console.WriteSuccess($"Package [{this.Verb.Package}] has been created."); if (this.Verb.RemoveDirectoryAfterPacking) { await this.Console.WriteInfo($"Removing source directory {this.Verb.Directory}..."); ExceptionGuard.Guard(() => Directory.Delete(this.Verb.Directory, true)); } return(StandardExitCodes.ErrorSuccess); } catch (SdkException e) { Logger.Error(e); await this.Console.WriteError(e.Message); return(e.ExitCode); } catch (Exception e) { Logger.Error(e); await this.Console.WriteError(e.Message); return(StandardExitCodes.ErrorGeneric); } }
protected override async Task <bool> Save(CancellationToken cancellationToken, IProgress <ProgressData> progress) { var temporaryFiles = new List <string>(); try { var fileListBuilder = new PackageFileListBuilder(); fileListBuilder.AddDirectory(this.InputPath.CurrentValue, true, null); if (this.PrePackOptions != null && !this.PrePackOptions.ManifestPresent) { if (!this.PrePackOptions.CanConvert) { throw new InvalidOperationException("The selected folder does not contain a manifest file and any executable files. It cannot be packed to MSIX format."); } if (!string.IsNullOrEmpty(this.InputPath.CurrentValue)) { progress.Report(new ProgressData(0, "Creating manifest file...")); var options = new AppxManifestCreatorOptions { CreateLogo = this.PrePackOptions.CreateLogo, EntryPoints = this.PrePackOptions.EntryPoints.Where(e => e.IsChecked).Select(e => e.Value).ToArray(), PackageDisplayName = Path.GetFileName(this.InputPath.CurrentValue), RegistryFile = this.PrePackOptions.SelectedRegistry?.FilePath == null ? null : new FileInfo(this.PrePackOptions.SelectedRegistry.FilePath) }; // ReSharper disable once AssignNullToNotNullAttribute await foreach (var result in this.manifestCreator.CreateManifestForDirectory(new DirectoryInfo(this.InputPath.CurrentValue), options, cancellationToken).ConfigureAwait(false)) { temporaryFiles.Add(result.SourcePath); if (result.PackageRelativePath == null) { continue; } fileListBuilder.AddFile(result.SourcePath, result.PackageRelativePath); } } } using var progressWrapper = new WrappedProgress(progress); var progress1 = progressWrapper.GetChildProgress(50); var progress2 = this.Sign.CurrentValue ? progressWrapper.GetChildProgress(30) : null; var tempFileList = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N") + ".list"); temporaryFiles.Add(tempFileList); var tempManifestPath = Path.Combine(Path.GetTempPath(), "AppxManifest-" + Guid.NewGuid().ToString("N") + ".xml"); temporaryFiles.Add(tempManifestPath); var srcManifest = fileListBuilder.GetManifestSourcePath(); if (srcManifest == null || !File.Exists(srcManifest)) { throw new InvalidOperationException("The selected folder cannot be packed because it has no manifest, and MSIX Hero was unable to create one. A manifest can be only created if the selected folder contains any executable file."); } // Copy manifest to a temporary file var injector = new MsixHeroBrandingInjector(); await using (var manifestStream = File.OpenRead(fileListBuilder.GetManifestSourcePath())) { var xml = await XDocument.LoadAsync(manifestStream, LoadOptions.None, cancellationToken).ConfigureAwait(false); await injector.Inject(xml).ConfigureAwait(false); await File.WriteAllTextAsync(tempManifestPath, xml.ToString(SaveOptions.None), cancellationToken); fileListBuilder.AddManifest(tempManifestPath); } await File.WriteAllTextAsync(tempFileList, fileListBuilder.ToString(), cancellationToken).ConfigureAwait(false); var sdk = new MakeAppxWrapper(); await sdk.PackPackageFiles(tempFileList, this.OutputPath.CurrentValue, this.Compress.CurrentValue, this.Validate.CurrentValue, cancellationToken, progress1).ConfigureAwait(false); if (this.Sign.CurrentValue) { var manager = await this.signingManagerFactory.GetProxyFor(SelfElevationLevel.HighestAvailable, cancellationToken).ConfigureAwait(false); string timeStampUrl; switch (this.SelectedCertificate.TimeStampSelectionMode.CurrentValue) { case TimeStampSelectionMode.None: timeStampUrl = null; break; case TimeStampSelectionMode.Auto: timeStampUrl = "auto"; break; case TimeStampSelectionMode.Url: timeStampUrl = this.SelectedCertificate.TimeStamp.CurrentValue; break; default: throw new ArgumentOutOfRangeException(); } switch (this.SelectedCertificate.Store.CurrentValue) { case CertificateSource.Personal: await manager.SignPackageWithInstalled(this.OutputPath.CurrentValue, this.OverrideSubject.CurrentValue, this.SelectedCertificate.SelectedPersonalCertificate.CurrentValue?.Model, timeStampUrl, IncreaseVersionMethod.None, cancellationToken, progress2).ConfigureAwait(false); break; case CertificateSource.Pfx: await manager.SignPackageWithPfx(this.OutputPath.CurrentValue, this.OverrideSubject.CurrentValue, this.SelectedCertificate.PfxPath.CurrentValue, this.SelectedCertificate.Password.CurrentValue, timeStampUrl, IncreaseVersionMethod.None, cancellationToken, progress2).ConfigureAwait(false); break; case CertificateSource.DeviceGuard: await manager.SignPackageWithDeviceGuardFromUi(this.OutputPath.CurrentValue, this.SelectedCertificate.DeviceGuard.CurrentValue, timeStampUrl, IncreaseVersionMethod.None, cancellationToken, progress2).ConfigureAwait(false); break; } } if (this.RemoveDirectory.CurrentValue) { ExceptionGuard.Guard(() => Directory.Delete(this.InputPath.CurrentValue, true)); } return(true); } finally { foreach (var tempFile in temporaryFiles) { ExceptionGuard.Guard(() => File.Delete(tempFile)); } } }
public override async Task <int> Execute() { try { await this.OnBegin().ConfigureAwait(false); if (!File.Exists(this.package) && !Directory.Exists(this.package)) { await this.Console.WriteError($"The path {this.package} does not exist."); return(10); } var validation = await this.Validate().ConfigureAwait(false); if (validation != 0) { return(validation); } if (File.Exists(this.package)) { // This is a file... if (string.Equals(Path.GetFileName(this.package), FileConstants.AppxManifestFile, StringComparison.OrdinalIgnoreCase)) { // .. a manifest file var result = await this.ExecuteOnExtractedPackage(Path.GetDirectoryName(this.package)).ConfigureAwait(false); await this.OnFinished().ConfigureAwait(false); return(result); } if (string.Equals(".msix", Path.GetExtension(this.package))) { // .. an MSIX package var msixMgr = new MakeAppxWrapper(); var tempFolder = Path.Combine(Path.GetTempPath(), "msixhero-" + Guid.NewGuid().ToString("N").Substring(0, 8)); try { await this.Console.WriteInfo($"Opening {Path.GetFileName(this.package)}...").ConfigureAwait(false); // 1) Unpack first await msixMgr.UnpackPackage(this.package, tempFolder, false).ConfigureAwait(false); // 2) Make edit var result = await this.ExecuteOnExtractedPackage(tempFolder).ConfigureAwait(false); if (result != StandardExitCodes.ErrorSuccess) { await this.Console.WriteWarning($"The package has not been updated due to previous errors.").ConfigureAwait(false); return(result); } // 3) Add branding XDocument document; await using (var fs = File.OpenRead(Path.Combine(tempFolder, "AppxManifest.xml"))) { document = await XDocument.LoadAsync(fs, LoadOptions.None, CancellationToken.None).ConfigureAwait(false); } var inject = new MsixHeroBrandingInjector(); await inject.Inject(document).ConfigureAwait(false); var writer = new AppxDocumentWriter(document); await writer.WriteAsync(Path.Combine(tempFolder, "AppxManifest.xml")).ConfigureAwait(false); if (result == StandardExitCodes.ErrorSuccess) { await this.Console.WriteInfo($"Saving {Path.GetFileName(this.package)}...").ConfigureAwait(false); // 3) Pack again await msixMgr.PackPackageDirectory(tempFolder, this.package, false, false); await this.OnFinished().ConfigureAwait(false); } return(result); } finally { if (Directory.Exists(tempFolder)) { ExceptionGuard.Guard(() => Directory.Delete(tempFolder, true)); } } } } else if (Directory.Exists(this.package)) { // this is extracted directory var manifestPath = Path.Combine(this.package, FileConstants.AppxManifestFile); if (File.Exists(manifestPath)) { var result = await this.ExecuteOnExtractedPackage(this.package).ConfigureAwait(false); XDocument document; await using (var fs = File.OpenRead(manifestPath)) { document = await XDocument.LoadAsync(fs, LoadOptions.None, CancellationToken.None).ConfigureAwait(false); } var inject = new MsixHeroBrandingInjector(); await inject.Inject(document).ConfigureAwait(false); var writer = new AppxDocumentWriter(document); await writer.WriteAsync(manifestPath).ConfigureAwait(false); await this.OnFinished().ConfigureAwait(false); return(result); } } await this.Console.WriteError($"The path {this.package} is neither a directory with extracted MSIX, an .MSIX package or a manifest file.").ConfigureAwait(false); return(StandardExitCodes.ErrorParameter); } catch (Exception e) { await this.Console.WriteError(e.Message).ConfigureAwait(false); return(StandardExitCodes.ErrorGeneric); } }