From 95f2f4425b900374c7d7b583ae810b096121b3c4 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Sat, 2 Dec 2017 00:46:11 -0800 Subject: Implement support for IExtensionCommandLine and IPreprocessorExtension --- .../Example.Extension/ExampleExtensionFactory.cs | 11 ++++- .../ExamplePreprocessorExtension.cs | 55 ---------------------- .../ExamplePreprocessorExtensionAndCommandLine.cs | 46 ++++++++++++++++++ .../ExtensionFixture.cs | 43 +++++++++++++++++ .../TestData/ExampleExtension/Package.wxs | 2 + 5 files changed, 100 insertions(+), 57 deletions(-) delete mode 100644 src/test/Example.Extension/ExamplePreprocessorExtension.cs create mode 100644 src/test/Example.Extension/ExamplePreprocessorExtensionAndCommandLine.cs (limited to 'src/test') diff --git a/src/test/Example.Extension/ExampleExtensionFactory.cs b/src/test/Example.Extension/ExampleExtensionFactory.cs index 9539ee85..b91d06e9 100644 --- a/src/test/Example.Extension/ExampleExtensionFactory.cs +++ b/src/test/Example.Extension/ExampleExtensionFactory.cs @@ -7,11 +7,18 @@ namespace Example.Extension public class ExampleExtensionFactory : IExtensionFactory { + private ExamplePreprocessorExtensionAndCommandLine preprocessorExtension; + public bool TryCreateExtension(Type extensionType, out object extension) { - if (extensionType == typeof(IPreprocessorExtension)) + if (extensionType == typeof(IExtensionCommandLine) || extensionType == typeof(IPreprocessorExtension)) { - extension = new ExamplePreprocessorExtension(); + if (preprocessorExtension == null) + { + preprocessorExtension = new ExamplePreprocessorExtensionAndCommandLine(); + } + + extension = preprocessorExtension; } else if (extensionType == typeof(ICompilerExtension)) { diff --git a/src/test/Example.Extension/ExamplePreprocessorExtension.cs b/src/test/Example.Extension/ExamplePreprocessorExtension.cs deleted file mode 100644 index c16c8b5a..00000000 --- a/src/test/Example.Extension/ExamplePreprocessorExtension.cs +++ /dev/null @@ -1,55 +0,0 @@ -// 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 Example.Extension -{ - using System; - using System.Xml.Linq; - using WixToolset.Data; - using WixToolset.Extensibility; - - internal class ExamplePreprocessorExtension : IPreprocessorExtension - { - public ExamplePreprocessorExtension() - { - } - - public IPreprocessorCore Core { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - - public string[] Prefixes => throw new NotImplementedException(); - - public string EvaluateFunction(string prefix, string function, string[] args) - { - throw new NotImplementedException(); - } - - public void Finish() - { - throw new NotImplementedException(); - } - - public string GetVariableValue(string prefix, string name) - { - throw new NotImplementedException(); - } - - public void Initialize() - { - throw new NotImplementedException(); - } - - public void PreprocessDocument(XDocument document) - { - throw new NotImplementedException(); - } - - public string PreprocessParameter(string name) - { - throw new NotImplementedException(); - } - - public bool ProcessPragma(SourceLineNumber sourceLineNumbers, string prefix, string pragma, string args, XContainer parent) - { - throw new NotImplementedException(); - } - } -} \ No newline at end of file diff --git a/src/test/Example.Extension/ExamplePreprocessorExtensionAndCommandLine.cs b/src/test/Example.Extension/ExamplePreprocessorExtensionAndCommandLine.cs new file mode 100644 index 00000000..53394ea3 --- /dev/null +++ b/src/test/Example.Extension/ExamplePreprocessorExtensionAndCommandLine.cs @@ -0,0 +1,46 @@ +// 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 Example.Extension +{ + using System; + using System.Collections.Generic; + using WixToolset.Extensibility; + using WixToolset.Extensibility.Services; + + internal class ExamplePreprocessorExtensionAndCommandLine : BasePreprocessorExtension, IExtensionCommandLine + { + private string exampleValueFromCommandLine; + + public IEnumerable CommandLineSwitches => throw new NotImplementedException(); + + public ExamplePreprocessorExtensionAndCommandLine() + { + this.Prefixes = new[] { "ex" }; + } + + public void PreParse(ICommandLineContext context) + { + } + + public bool TryParseArgument(IParseCommandLine parseCommandLine, string arg) + { + if (parseCommandLine.IsSwitch(arg) && arg.Substring(1).Equals("example", StringComparison.OrdinalIgnoreCase)) + { + parseCommandLine.GetNextArgumentOrError(ref this.exampleValueFromCommandLine); + return true; + } + + return false; + } + + public override string GetVariableValue(string prefix, string name) + { + if (prefix == "ex" && "test".Equals(name, StringComparison.OrdinalIgnoreCase)) + { + return String.IsNullOrWhiteSpace(this.exampleValueFromCommandLine) ? "(null)" : this.exampleValueFromCommandLine; + } + + return null; + } + } +} \ No newline at end of file diff --git a/src/test/WixToolsetTest.CoreIntegration/ExtensionFixture.cs b/src/test/WixToolsetTest.CoreIntegration/ExtensionFixture.cs index 5181c748..6acf3472 100644 --- a/src/test/WixToolsetTest.CoreIntegration/ExtensionFixture.cs +++ b/src/test/WixToolsetTest.CoreIntegration/ExtensionFixture.cs @@ -56,5 +56,48 @@ namespace WixToolsetTest.CoreIntegration Assert.Equal("Bar", example[1].AsString()); } } + + [Fact] + public void CanParseCommandLineWithExtension() + { + var folder = TestData.Get(@"TestData\ExampleExtension"); + var extensionPath = Path.GetFullPath(new Uri(typeof(ExampleExtensionFactory).Assembly.CodeBase).LocalPath); + + using (var fs = new DisposableFileSystem()) + { + var intermediateFolder = fs.GetFolder(); + + var program = new Program(); + var result = program.Run(new WixToolsetServiceProvider(), new[] + { + "build", + Path.Combine(folder, "Package.wxs"), + Path.Combine(folder, "PackageComponents.wxs"), + "-loc", Path.Combine(folder, "Package.en-us.wxl"), + "-ext", extensionPath, + "-bindpath", Path.Combine(folder, "data"), + "-intermediateFolder", intermediateFolder, + "-example", "test", + "-o", Path.Combine(intermediateFolder, @"bin\extest.msi") + }); + + Assert.Equal(0, result); + + Assert.True(File.Exists(Path.Combine(intermediateFolder, @"bin\extest.msi"))); + Assert.True(File.Exists(Path.Combine(intermediateFolder, @"bin\extest.wixpdb"))); + Assert.True(File.Exists(Path.Combine(intermediateFolder, @"bin\MsiPackage\example.txt"))); + + var intermediate = Intermediate.Load(Path.Combine(intermediateFolder, @"bin\extest.wir")); + var section = intermediate.Sections.Single(); + + var wixFile = section.Tuples.OfType().Single(); + Assert.Equal(Path.Combine(folder, @"data\example.txt"), wixFile[WixFileTupleFields.Source].AsPath().Path); + Assert.Equal(@"example.txt", wixFile[WixFileTupleFields.Source].PreviousValue.AsPath().Path); + + var property = section.Tuples.OfType().Where(p => p.Id.Id == "ExampleProperty").Single(); + Assert.Equal("ExampleProperty", property.Property); + Assert.Equal("test", property.Value); + } + } } } diff --git a/src/test/WixToolsetTest.CoreIntegration/TestData/ExampleExtension/Package.wxs b/src/test/WixToolsetTest.CoreIntegration/TestData/ExampleExtension/Package.wxs index cdc323ec..9fd42214 100644 --- a/src/test/WixToolsetTest.CoreIntegration/TestData/ExampleExtension/Package.wxs +++ b/src/test/WixToolsetTest.CoreIntegration/TestData/ExampleExtension/Package.wxs @@ -6,6 +6,8 @@ + + -- cgit v1.2.3-55-g6feb