| 1 | // 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. |
| 2 | |
| 3 | namespace WixToolset.Core.WindowsInstaller.Bind |
| 4 | { |
| 5 | using System; |
| 6 | using System.IO; |
| 7 | using System.Linq; |
| 8 | using WixToolset.Data; |
| 9 | using WixToolset.Data.Symbols; |
| 10 | using WixToolset.Extensibility.Services; |
| 11 | |
| 12 | internal class UpdateFromTextFilesCommand |
| 13 | { |
| 14 | public UpdateFromTextFilesCommand(IMessaging messaging, IntermediateSection section) |
| 15 | { |
| 16 | this.Messaging = messaging; |
| 17 | this.Section = section; |
| 18 | } |
| 19 | |
| 20 | private IMessaging Messaging { get; } |
| 21 | |
| 22 | private IntermediateSection Section { get; } |
| 23 | |
| 24 | public void Execute() |
| 25 | { |
| 26 | foreach (var bbControl in this.Section.Symbols.OfType<BBControlSymbol>().Where(t => t.SourceFile != null)) |
| 27 | { |
| 28 | bbControl.Text = this.ReadTextFile(bbControl.SourceLineNumbers, bbControl.SourceFile.Path); |
| 29 | } |
| 30 | |
| 31 | foreach (var control in this.Section.Symbols.OfType<ControlSymbol>().Where(t => t.SourceFile != null)) |
| 32 | { |
| 33 | control.Text = this.ReadTextFile(control.SourceLineNumbers, control.SourceFile.Path); |
| 34 | } |
| 35 | |
| 36 | foreach (var customAction in this.Section.Symbols.OfType<CustomActionSymbol>().Where(c => c.ScriptFile != null)) |
| 37 | { |
| 38 | customAction.Target = this.ReadTextFile(customAction.SourceLineNumbers, customAction.ScriptFile.Path); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /// <summary> |
| 43 | /// Reads a text file and returns the contents. |
| 44 | /// </summary> |
| 45 | /// <param name="sourceLineNumbers">Source line numbers for row from source.</param> |
| 46 | /// <param name="source">Source path to file to read.</param> |
| 47 | /// <returns>Text string read from file.</returns> |
| 48 | private string ReadTextFile(SourceLineNumber sourceLineNumbers, string source) |
| 49 | { |
| 50 | try |
| 51 | { |
| 52 | using (var reader = new StreamReader(source)) |
| 53 | { |
| 54 | return reader.ReadToEnd(); |
| 55 | } |
| 56 | } |
| 57 | catch (DirectoryNotFoundException e) |
| 58 | { |
| 59 | this.Messaging.Write(ErrorMessages.BinderFileManagerMissingFile(sourceLineNumbers, e.Message)); |
| 60 | } |
| 61 | catch (FileNotFoundException e) |
| 62 | { |
| 63 | this.Messaging.Write(ErrorMessages.BinderFileManagerMissingFile(sourceLineNumbers, e.Message)); |
| 64 | } |
| 65 | catch (IOException e) |
| 66 | { |
| 67 | this.Messaging.Write(ErrorMessages.BinderFileManagerMissingFile(sourceLineNumbers, e.Message)); |
| 68 | } |
| 69 | catch (NotSupportedException) |
| 70 | { |
| 71 | this.Messaging.Write(ErrorMessages.FileNotFound(sourceLineNumbers, source)); |
| 72 | } |
| 73 | |
| 74 | return null; |
| 75 | } |
| 76 | } |
| 77 | } |