From f18b96045088d6d989e70df19343a99092685e5e Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Mon, 22 Jun 2020 23:09:30 -0700 Subject: Move CustomAction script inner text to ScriptFile attribute --- .../Bind/UpdateFromTextFilesCommand.cs | 77 ++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/WixToolset.Core.WindowsInstaller/Bind/UpdateFromTextFilesCommand.cs (limited to 'src/WixToolset.Core.WindowsInstaller/Bind/UpdateFromTextFilesCommand.cs') diff --git a/src/WixToolset.Core.WindowsInstaller/Bind/UpdateFromTextFilesCommand.cs b/src/WixToolset.Core.WindowsInstaller/Bind/UpdateFromTextFilesCommand.cs new file mode 100644 index 00000000..4d09ff6b --- /dev/null +++ b/src/WixToolset.Core.WindowsInstaller/Bind/UpdateFromTextFilesCommand.cs @@ -0,0 +1,77 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. + +namespace WixToolset.Core.WindowsInstaller.Bind +{ + using System; + using System.IO; + using System.Linq; + using WixToolset.Data; + using WixToolset.Data.Tuples; + using WixToolset.Extensibility.Services; + + internal class UpdateFromTextFilesCommand + { + public UpdateFromTextFilesCommand(IMessaging messaging, IntermediateSection section) + { + this.Messaging = messaging; + this.Section = section; + } + + private IMessaging Messaging { get; } + + private IntermediateSection Section { get; } + + public void Execute() + { + foreach (var bbControl in this.Section.Tuples.OfType().Where(t => t.SourceFile != null)) + { + bbControl.Text = this.ReadTextFile(bbControl.SourceLineNumbers, bbControl.SourceFile.Path); + } + + foreach (var control in this.Section.Tuples.OfType().Where(t => t.SourceFile != null)) + { + control.Text = this.ReadTextFile(control.SourceLineNumbers, control.SourceFile.Path); + } + + foreach (var customAction in this.Section.Tuples.OfType().Where(c => c.ScriptFile != null)) + { + customAction.Target = this.ReadTextFile(customAction.SourceLineNumbers, customAction.ScriptFile.Path); + } + } + + /// + /// Reads a text file and returns the contents. + /// + /// Source line numbers for row from source. + /// Source path to file to read. + /// Text string read from file. + private string ReadTextFile(SourceLineNumber sourceLineNumbers, string source) + { + try + { + using (var reader = new StreamReader(source)) + { + return reader.ReadToEnd(); + } + } + catch (DirectoryNotFoundException e) + { + this.Messaging.Write(ErrorMessages.BinderFileManagerMissingFile(sourceLineNumbers, e.Message)); + } + catch (FileNotFoundException e) + { + this.Messaging.Write(ErrorMessages.BinderFileManagerMissingFile(sourceLineNumbers, e.Message)); + } + catch (IOException e) + { + this.Messaging.Write(ErrorMessages.BinderFileManagerMissingFile(sourceLineNumbers, e.Message)); + } + catch (NotSupportedException) + { + this.Messaging.Write(ErrorMessages.FileNotFound(sourceLineNumbers, source)); + } + + return null; + } + } +} -- cgit v1.2.3-55-g6feb