aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core.WindowsInstaller/Bind/AddCreateFoldersCommand.cs
blob: 6cc4153fc6ede28825d097767a1cbafe675cdef6 (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
// 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.Collections.Generic;
    using System.Linq;
    using WixToolset.Data;
    using WixToolset.Data.Tuples;

    /// <summary>
    /// Add CreateFolder tuples, if not already present, for null-keypath components.
    /// </summary>
    internal class AddCreateFoldersCommand
    {
        internal AddCreateFoldersCommand(IntermediateSection section)
        {
            this.Section = section;
        }

        private IntermediateSection Section { get; }

        public void Execute()
        {
            var createFolderTuplesByComponentRef = new HashSet<string>(this.Section.Tuples.OfType<CreateFolderTuple>().Select(t => t.ComponentRef));
            foreach (var componentTuple in this.Section.Tuples.OfType<ComponentTuple>().Where(t => t.KeyPathType == ComponentKeyPathType.Directory).ToList())
            {
                if (!createFolderTuplesByComponentRef.Contains(componentTuple.Id.Id))
                {
                    var createFolderTuple = new CreateFolderTuple(componentTuple.SourceLineNumbers)
                    {
                        DirectoryRef = componentTuple.DirectoryRef,
                        ComponentRef = componentTuple.Id.Id,
                    };

                    this.Section.Tuples.Add(createFolderTuple);
                }
            }
        }
    }
}