Skip to content

Commit

Permalink
Provide a default INSTALLFOLDER.
Browse files Browse the repository at this point in the history
If INSTALLFOLDER is referenced and not defined, define one with
reasonable default values.

Implements WIP wixtoolset/issues#7588.
  • Loading branch information
barnson committed Dec 23, 2023
1 parent f02ef4a commit c6b70db
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 9 deletions.
63 changes: 63 additions & 0 deletions src/wix/WixToolset.Core/Link/AddDefaultSymbolsCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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.Link
{
using System.Collections.Generic;
using System.Linq;
using WixToolset.Data;
using WixToolset.Data.Symbols;

internal class AddDefaultSymbolsCommand
{
public static readonly string WixStandardInstallFolder = "INSTALLFOLDER";
public static readonly string WixStandardInstallFolderParent = "ProgramFiles6432Folder";
public static readonly string WixStandardInstallFolderReference = "Directory:INSTALLFOLDER";

public AddDefaultSymbolsCommand(FindEntrySectionAndLoadSymbolsCommand find, List<IntermediateSection> sections)
{
this.Find = find;
this.Sections = sections;
}

public IntermediateSection EntrySection { get; }

public IEnumerable<IntermediateSection> Sections { get; }

public FindEntrySectionAndLoadSymbolsCommand Find { get; }

public void Execute()
{
var symbols = this.Sections.SelectMany(s => s.Symbols);
var directorySymbols = symbols.OfType<DirectorySymbol>();
var referenceSymbols = symbols.OfType<WixSimpleReferenceSymbol>();

if (referenceSymbols.Any(s => s.SymbolicName == WixStandardInstallFolderReference)
&& !directorySymbols.Any(d => d.Id.Id == WixStandardInstallFolder))
{
// If there are any INSTALLFOLDER references, add a default one, using the
// first reference as the "canonical" reference for source line numbers.
this.AddSymbol(new DirectorySymbol(null, new Identifier(AccessModifier.Global, WixStandardInstallFolder))
{
ParentDirectoryRef = WixStandardInstallFolderParent,
Name = "!(bind.Property.Manufacturer) !(bind.Property.ProductName)",
SourceName = ".",
});

this.AddSymbol(new WixSimpleReferenceSymbol(null, new Identifier(AccessModifier.Global, WixStandardInstallFolder))
{
Table = "Directory",
PrimaryKeys = WixStandardInstallFolderParent,
});
}
}

private void AddSymbol(IntermediateSymbol symbol)
{
this.Find.EntrySection.AddSymbol(symbol);

var symbolWithSection = new SymbolWithSection(this.Find.EntrySection, symbol);
var fullName = symbolWithSection.GetFullName();
this.Find.SymbolsByName.Add(fullName, symbolWithSection);
}
}
}
7 changes: 7 additions & 0 deletions src/wix/WixToolset.Core/Linker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ public Intermediate Link(ILinkContext context)
}
}

// Add default symbols that need a bit more intelligence than just being
// included in the standard library.
{
var command = new AddDefaultSymbolsCommand(find, sections);
command.Execute();
}

// Resolve the symbol references to find the set of sections we care about for linking.
// Of course, we start with the entry section (that's how it got its name after all).
var resolve = new ResolveReferencesCommand(this.Messaging, find.EntrySection, find.SymbolsByName);
Expand Down
38 changes: 38 additions & 0 deletions src/wix/test/WixToolsetTest.CoreIntegration/DirectoryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,44 @@ namespace WixToolsetTest.CoreIntegration

public class DirectoryFixture
{
[Fact]
public void CanGetDefaultInstallFolder()
{
var folder = TestData.Get(@"TestData\SingleFile");

using (var fs = new DisposableFileSystem())
{
var baseFolder = fs.GetFolder();
var intermediateFolder = Path.Combine(baseFolder, "obj");
var msiPath = Path.Combine(baseFolder, @"bin\test.msi");

var result = WixRunner.Execute(new[]
{
"build",
Path.Combine(folder, "Package.wxs"),
Path.Combine(folder, "PackageComponents.wxs"),
"-loc", Path.Combine(folder, "Package.en-us.wxl"),
"-bindpath", Path.Combine(folder, "data"),
"-intermediateFolder", intermediateFolder,
"-o", msiPath
});

result.AssertSuccess();

var intermediate = Intermediate.Load(Path.Combine(baseFolder, @"bin\test.wixpdb"));
var section = intermediate.Sections.Single();

var dirSymbols = section.Symbols.OfType<WixToolset.Data.Symbols.DirectorySymbol>().ToList();
WixAssert.CompareLineByLine(new[]
{
"INSTALLFOLDER:ProgramFiles6432Folder:Example Corporation MsiPackage",
"ProgramFiles6432Folder:ProgramFilesFolder:.",
"ProgramFilesFolder:TARGETDIR:PFiles",
"TARGETDIR::SourceDir"
}, dirSymbols.OrderBy(d => d.Id.Id).Select(d => d.Id.Id + ":" + d.ParentDirectoryRef + ":" + d.Name).ToArray());
}
}

[Fact]
public void CanGet32bitProgramFiles6432Folder()
{
Expand Down
2 changes: 1 addition & 1 deletion src/wix/test/WixToolsetTest.CoreIntegration/MsiFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void CanBuildSingleFile()

Assert.True(File.Exists(Path.Combine(baseFolder, @"bin\test.msi")));
Assert.True(File.Exists(Path.Combine(baseFolder, @"bin\test.wixpdb")));
Assert.True(File.Exists(Path.Combine(baseFolder, @"bin\PFiles\MsiPackage\test.txt")));
Assert.True(File.Exists(Path.Combine(baseFolder, @"bin\PFiles\Example Corporation MsiPackage\test.txt")));

var intermediate = Intermediate.Load(Path.Combine(baseFolder, @"bin\test.wixpdb"));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs">
<Package Name="MsiPackage" Codepage="1252" Language="1033" Version="1.0.0.0" Manufacturer="Example Corporation" UpgradeCode="047730a5-30fe-4a62-a520-da9381b8226a" Compressed="no" InstallerVersion="200" Scope="perMachine">


<MajorUpgrade DowngradeErrorMessage="!(loc.DowngradeError)" />

<Feature Id="ProductFeature" Title="!(loc.FeatureTitle)">
<ComponentGroupRef Id="ProductComponents" />
</Feature>
</Package>

<Fragment>
<StandardDirectory Id="ProgramFilesFolder">
<Directory Id="INSTALLFOLDER" Name="MsiPackage" SourceName="." />
</StandardDirectory>
</Fragment>
</Wix>

0 comments on commit c6b70db

Please sign in to comment.