aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core/Bind/ExtractEmbeddedFilesCommand.cs
blob: bb9ab844aeb44d1d645f33947b862480a87b2d9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// 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.Bind
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using WixToolset.Data;
    using WixToolset.Extensibility.Data;
    using WixToolset.Extensibility.Services;

    public class ExtractEmbeddedFilesCommand
    {
        public ExtractEmbeddedFilesCommand(IBackendHelper backendHelper, IEnumerable<IExpectedExtractFile> embeddedFiles)
        {
            this.BackendHelper = backendHelper;
            this.FilesWithEmbeddedFiles = embeddedFiles;
        }

        public IEnumerable<ITrackedFile> TrackedFiles { get; private set; }

        private IBackendHelper BackendHelper { get; }

        private IEnumerable<IExpectedExtractFile> FilesWithEmbeddedFiles { get; }

        public void Execute()
        {
            var trackedFiles = new List<ITrackedFile>();
            var group = this.FilesWithEmbeddedFiles.GroupBy(e => e.Uri);

            foreach (var expectedEmbeddedFileByUri in group)
            {
                var baseUri = expectedEmbeddedFileByUri.Key;

                using (var wixout = WixOutput.Read(baseUri))
                {
                    var uniqueIds = new SortedSet<string>(StringComparer.OrdinalIgnoreCase);

                    foreach (var embeddedFile in expectedEmbeddedFileByUri)
                    {
                        if (uniqueIds.Add(embeddedFile.EmbeddedFileId))
                        {
                            wixout.ExtractEmbeddedFile(embeddedFile.EmbeddedFileId, embeddedFile.OutputPath);
                            trackedFiles.Add(this.BackendHelper.TrackFile(embeddedFile.OutputPath, TrackedFileType.Temporary));
                        }
                    }
                }
            }

            this.TrackedFiles = trackedFiles;
        }
    }
}