/// <summary> /// Creates the appropriate backend compiler based on the output hint path. /// </summary> /// <param name="type">Package type to generate.</param> /// <param name="outputPathHint">Hint for the output file name, used to determine which backend compiler to create.</param> /// <returns>Appropriate backend compiler for the output file name.</returns> public static BackendCompiler Create(PackageType type, string outputPathHint) { BackendCompiler backend = null; switch (type) { case PackageType.Appx: backend = new AppxBackendCompiler(CompilerOutputType.AppxPackage); break; case PackageType.Msi: //backend = new MsiBackendCompiler(CompilerOutputType.MsiPackage); break; case PackageType.Vsix: backend = new VsixBackendCompiler(); break; case PackageType.Wixlib: backend = new WixBackendCompiler(CompilerOutputType.WixLibrary); break; } return(backend); }
/// <summary> /// Removes the destination for a file transfer. /// </summary> /// <param name="backend">Compiler backend that generated the transfer.</param> /// <param name="transfers">File transfers destination to remove.</param> private static void RemoveDestination(BackendCompiler backend, FileTransfer transfer) { if (File.Exists(transfer.Destination)) { backend.OnMessage(new CompilerMessageEventArgs(CompilerMessage.RemovingDestinationFile(transfer.Destination), transfer.LineNumber)); // try to ensure the file is not read-only FileAttributes attributes = File.GetAttributes(transfer.Destination); try { File.SetAttributes(transfer.Destination, attributes & ~FileAttributes.ReadOnly); } catch (ArgumentException ae) // thrown for unauthorized access errors { throw new CompilerException(new CompilerMessageEventArgs(CompilerMessage.CannotTransferFile(transfer.Source, transfer.Destination), transfer.LineNumber), ae); } // try to delete the file try { File.Delete(transfer.Destination); } catch (IOException ioe) { throw new CompilerException(new CompilerMessageEventArgs(CompilerMessage.CannotTransferFile(transfer.Source, transfer.Destination), transfer.LineNumber), ioe); } } else // no idea what just happened, bail { //throw; } }
/// <summary> /// Process a set of transfers. /// </summary> /// <param name="backend">Compiler backend that generated the transfers.</param> /// <param name="transfers">Set of file transfers to execute.</param> public static void ExecuteTransfers(BackendCompiler backend, IEnumerable <FileTransfer> transfers) { foreach (FileTransfer transfer in transfers) { FileTransfer.ExecuteTransfer(backend, transfer); } }
private static string ConvertToPartUri(BackendCompiler backend, string path) { string[] idPath = path.Split(new char[] { ':' }, 2); if (!idPath[0].Equals("ApplicationFolder") && !idPath[0].Equals("InstallFolder")) { // TOOD: send warning that we are ignoring all other roots and we always put files in ApplicationFolder } return(idPath[1].Replace('\\', '/')); }
public static bool TryCreate(BackendCompiler backend, File file, out PackageFile packageFile) { packageFile = null; string path; if (backend.FileManager.TryResolvePath(file.Source, out path)) { string mimeType = "application/x-msdownload"; // TODO: pick appropriate MIME type. string partUri = PackageFile.ConvertToPartUri(backend, file.Path); packageFile = new PackageFile() { File = file, MimeType = mimeType, PartUri = partUri, SourcePath = path }; } else { backend.OnMessage(new CompilerMessageEventArgs(CompilerMessage.FileNotFound(file.Source), file)); } return(packageFile != null); }
/// <summary> /// Copy or move a single transfer. /// </summary> /// <param name="backend">Compiler backend that generated the transfers.</param> /// <param name="transfer">File transfers to execute.</param> public static void ExecuteTransfer(BackendCompiler backend, FileTransfer transfer) { if (transfer.Redundant) { // TODO: log that we tried to transfer a redundant file? return; } bool retry = false; do { try { if (transfer.Move) { backend.OnMessage(new CompilerMessageEventArgs(CompilerMessage.MoveFile(transfer.Source, transfer.Destination), transfer.LineNumber)); File.Move(transfer.Source, transfer.Destination); } else { backend.OnMessage(new CompilerMessageEventArgs(CompilerMessage.CopyFile(transfer.Source, transfer.Destination), transfer.LineNumber)); File.Copy(transfer.Source, transfer.Destination, true); } retry = false; } catch (FileNotFoundException e) { throw new CompilerException(new CompilerMessageEventArgs(CompilerMessage.FileNotFound(transfer.Source), transfer.LineNumber), e); } catch (DirectoryNotFoundException dnfe) { // if we already retried, give up if (retry) { throw new CompilerException(new CompilerMessageEventArgs(CompilerMessage.CannotTransferFile(transfer.Source, transfer.Destination), transfer.LineNumber), dnfe); } string directory = Path.GetDirectoryName(transfer.Destination); Directory.CreateDirectory(directory); retry = true; } catch (UnauthorizedAccessException uae) { // if we already retried, give up if (retry) { throw new CompilerException(new CompilerMessageEventArgs(CompilerMessage.CannotTransferFile(transfer.Source, transfer.Destination), transfer.LineNumber), uae); } FileTransfer.RemoveDestination(backend, transfer); retry = true; } catch (IOException e) { // if we already retried, give up if (retry) { throw new CompilerException(new CompilerMessageEventArgs(CompilerMessage.CannotTransferFile(transfer.Source, transfer.Destination), transfer.LineNumber), e); } FileTransfer.RemoveDestination(backend, transfer); retry = true; } } while (retry); }