From 5ba862bfa618c89a563d555e8ce7b44a904df406 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Wed, 6 Dec 2017 11:39:26 -0800 Subject: Add support for loading Intermediates from extensions --- .../TestData/Example.Extension/Data/example.txt | 1 + .../TestData/Example.Extension/Data/example.wir | Bin 0 -> 534 bytes .../TestData/Example.Extension/Data/example.wxs | 8 ++ .../Example.Extension/Example.Extension.csproj | 22 ++++++ .../Example.Extension/ExampleCompilerExtension.cs | 84 +++++++++++++++++++++ .../Example.Extension/ExampleExtensionData.cs | 33 ++++++++ .../Example.Extension/ExampleExtensionFactory.cs | 39 ++++++++++ .../ExamplePreprocessorExtensionAndCommandLine.cs | 46 +++++++++++ .../TestData/Example.Extension/ExampleTuple.cs | 31 ++++++++ .../TestData/Example.Extension/TupleDefinitions.cs | 18 +++++ 10 files changed, 282 insertions(+) create mode 100644 src/test/TestData/Example.Extension/Data/example.txt create mode 100644 src/test/TestData/Example.Extension/Data/example.wir create mode 100644 src/test/TestData/Example.Extension/Data/example.wxs create mode 100644 src/test/TestData/Example.Extension/Example.Extension.csproj create mode 100644 src/test/TestData/Example.Extension/ExampleCompilerExtension.cs create mode 100644 src/test/TestData/Example.Extension/ExampleExtensionData.cs create mode 100644 src/test/TestData/Example.Extension/ExampleExtensionFactory.cs create mode 100644 src/test/TestData/Example.Extension/ExamplePreprocessorExtensionAndCommandLine.cs create mode 100644 src/test/TestData/Example.Extension/ExampleTuple.cs create mode 100644 src/test/TestData/Example.Extension/TupleDefinitions.cs (limited to 'src/test/TestData') diff --git a/src/test/TestData/Example.Extension/Data/example.txt b/src/test/TestData/Example.Extension/Data/example.txt new file mode 100644 index 00000000..1b4ffe8a --- /dev/null +++ b/src/test/TestData/Example.Extension/Data/example.txt @@ -0,0 +1 @@ +This is example.txt. \ No newline at end of file diff --git a/src/test/TestData/Example.Extension/Data/example.wir b/src/test/TestData/Example.Extension/Data/example.wir new file mode 100644 index 00000000..674f63fc Binary files /dev/null and b/src/test/TestData/Example.Extension/Data/example.wir differ diff --git a/src/test/TestData/Example.Extension/Data/example.wxs b/src/test/TestData/Example.Extension/Data/example.wxs new file mode 100644 index 00000000..53531e99 --- /dev/null +++ b/src/test/TestData/Example.Extension/Data/example.wxs @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/src/test/TestData/Example.Extension/Example.Extension.csproj b/src/test/TestData/Example.Extension/Example.Extension.csproj new file mode 100644 index 00000000..d04ce553 --- /dev/null +++ b/src/test/TestData/Example.Extension/Example.Extension.csproj @@ -0,0 +1,22 @@ + + + + + + netstandard2.0 + false + + + + + + + + + + + + + + + diff --git a/src/test/TestData/Example.Extension/ExampleCompilerExtension.cs b/src/test/TestData/Example.Extension/ExampleCompilerExtension.cs new file mode 100644 index 00000000..5b20e48f --- /dev/null +++ b/src/test/TestData/Example.Extension/ExampleCompilerExtension.cs @@ -0,0 +1,84 @@ +// 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 System.Xml.Linq; + using WixToolset.Data; + using WixToolset.Extensibility; + + internal class ExampleCompilerExtension : BaseCompilerExtension + { + public ExampleCompilerExtension() + { + this.Namespace = "http://www.example.com/scheams/v1/wxs"; + } + + public override void ParseElement(Intermediate intermediate, IntermediateSection section, XElement parentElement, XElement element, IDictionary context) + { + var processed = false; + + switch (parentElement.Name.LocalName) + { + case "Component": + switch (element.Name.LocalName) + { + case "Example": + this.ParseExampleElement(intermediate, section, element); + processed = true; + break; + } + break; + } + + if (!processed) + { + base.ParseElement(intermediate, section, parentElement, element, context); + } + } + + private void ParseExampleElement(Intermediate intermediate, IntermediateSection section, XElement element) + { + var sourceLineNumbers = this.ParseHelper.GetSourceLineNumbers(element); + Identifier id = null; + string value = null; + + foreach (var attrib in element.Attributes()) + { + if (String.IsNullOrEmpty(attrib.Name.NamespaceName) || this.Namespace == attrib.Name.Namespace) + { + switch (attrib.Name.LocalName) + { + case "Id": + id = this.ParseHelper.GetAttributeIdentifier(sourceLineNumbers, attrib); + break; + + case "Value": + value = this.ParseHelper.GetAttributeValue(sourceLineNumbers, attrib); + break; + + default: + this.ParseHelper.UnexpectedAttribute(element, attrib); + break; + } + } + else + { + this.ParseAttribute(intermediate, section, element, attrib, null); + } + } + + if (null == id) + { + //this.Messaging(WixErrors.ExpectedAttribute(sourceLineNumbers, element.Name.LocalName, "Id")); + } + + if (!this.Messaging.EncounteredError) + { + var tuple = this.ParseHelper.CreateRow(section, sourceLineNumbers, "Example", id); + tuple.Set(1, value); + } + } + } +} diff --git a/src/test/TestData/Example.Extension/ExampleExtensionData.cs b/src/test/TestData/Example.Extension/ExampleExtensionData.cs new file mode 100644 index 00000000..6b179ea6 --- /dev/null +++ b/src/test/TestData/Example.Extension/ExampleExtensionData.cs @@ -0,0 +1,33 @@ +// 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 WixToolset.Data; + using WixToolset.Extensibility; + + internal class ExampleExtensionData : IExtensionData + { + public string DefaultCulture => null; + + public Intermediate GetLibrary(ITupleDefinitionCreator tupleDefinitions) + { + return Intermediate.Load(typeof(ExampleExtensionData).Assembly, "Example.Extension.Data.Example.wir", tupleDefinitions); + } + + public bool TryGetTupleDefinitionByName(string name, out IntermediateTupleDefinition tupleDefinition) + { + switch (name) + { + case "Example": + tupleDefinition = TupleDefinitions.Example; + break; + + default: + tupleDefinition = null; + break; + } + + return tupleDefinition != null; + } + } +} \ No newline at end of file diff --git a/src/test/TestData/Example.Extension/ExampleExtensionFactory.cs b/src/test/TestData/Example.Extension/ExampleExtensionFactory.cs new file mode 100644 index 00000000..b91d06e9 --- /dev/null +++ b/src/test/TestData/Example.Extension/ExampleExtensionFactory.cs @@ -0,0 +1,39 @@ +// 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 WixToolset.Extensibility; + + public class ExampleExtensionFactory : IExtensionFactory + { + private ExamplePreprocessorExtensionAndCommandLine preprocessorExtension; + + public bool TryCreateExtension(Type extensionType, out object extension) + { + if (extensionType == typeof(IExtensionCommandLine) || extensionType == typeof(IPreprocessorExtension)) + { + if (preprocessorExtension == null) + { + preprocessorExtension = new ExamplePreprocessorExtensionAndCommandLine(); + } + + extension = preprocessorExtension; + } + else if (extensionType == typeof(ICompilerExtension)) + { + extension = new ExampleCompilerExtension(); + } + else if (extensionType == typeof(IExtensionData)) + { + extension = new ExampleExtensionData(); + } + else + { + extension = null; + } + + return extension != null; + } + } +} diff --git a/src/test/TestData/Example.Extension/ExamplePreprocessorExtensionAndCommandLine.cs b/src/test/TestData/Example.Extension/ExamplePreprocessorExtensionAndCommandLine.cs new file mode 100644 index 00000000..53394ea3 --- /dev/null +++ b/src/test/TestData/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/TestData/Example.Extension/ExampleTuple.cs b/src/test/TestData/Example.Extension/ExampleTuple.cs new file mode 100644 index 00000000..f280a5c8 --- /dev/null +++ b/src/test/TestData/Example.Extension/ExampleTuple.cs @@ -0,0 +1,31 @@ +// 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 WixToolset.Data; + + public enum ExampleTupleFields + { + Example, + Value, + } + + public class ExampleTuple : IntermediateTuple + { + public ExampleTuple() : base(TupleDefinitions.Example, null, null) + { + } + + public ExampleTuple(SourceLineNumber sourceLineNumber, Identifier id = null) : base(TupleDefinitions.Example, sourceLineNumber, id) + { + } + + public IntermediateField this[ExampleTupleFields index] => this.Fields[(int)index]; + + public string Value + { + get => this.Fields[(int)ExampleTupleFields.Value]?.AsString(); + set => this.Set((int)ExampleTupleFields.Value, value); + } + } +} diff --git a/src/test/TestData/Example.Extension/TupleDefinitions.cs b/src/test/TestData/Example.Extension/TupleDefinitions.cs new file mode 100644 index 00000000..2c320fbc --- /dev/null +++ b/src/test/TestData/Example.Extension/TupleDefinitions.cs @@ -0,0 +1,18 @@ +// 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 WixToolset.Data; + + public static class TupleDefinitions + { + public static readonly IntermediateTupleDefinition Example = new IntermediateTupleDefinition( + "Example", + new[] + { + new IntermediateFieldDefinition(nameof(ExampleTupleFields.Example), IntermediateFieldType.String), + new IntermediateFieldDefinition(nameof(ExampleTupleFields.Value), IntermediateFieldType.String), + }, + typeof(ExampleTuple)); + } +} -- cgit v1.2.3-55-g6feb