Skip to content
This repository has been archived by the owner on Dec 10, 2020. It is now read-only.

Commit

Permalink
Enable C# 8, NRT annotations and RollForward=Major (#38)
Browse files Browse the repository at this point in the history
Enable C# 8 and add nullable reference types (NRT) annotations.

Enable [`RollForward=Major`](https://docs.microsoft.com/en-us/dotnet/core/whats-new/dotnet-core-3-0#major-version-roll-forward) to allow running the tool on .NET Core 3.x and later (fixes #42).

Build with .NET Core 3.1 SDK and SourceLink enabled.
Retarget tests to .NET Core 3.1 and .NET 4.8.
Upgrade NuGet packages used for tests.
  • Loading branch information
nil4 authored Jan 7, 2020
1 parent ad49457 commit 52d4784
Show file tree
Hide file tree
Showing 27 changed files with 290 additions and 282 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ is that you can use the same command, for both installation and usage, across al
Install `dotnet-xdt` as a global tool (only once):

```cmd
dotnet tool install --global dotnet-xdt --version 2.1.1
dotnet tool install --global dotnet-xdt --version 2.2.0
```

And then you can apply XDT transforms, from the command-line, anywhere on your PC, e.g.:
Expand Down Expand Up @@ -58,7 +58,7 @@ Download the latest build of `dotnet-xdt.exe` from the [releases page](https://g
For complete flexibility, reference the cross-platform `DotNet.Xdt` NuGet package in your application:

```cmd
dotnet add package DotNet.Xdt --version 2.1.1
dotnet add package DotNet.Xdt --version 2.2.0
```

You can apply XDT transforms to any XML file, or other XML sources that can be read from
Expand Down
12 changes: 6 additions & 6 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# https://www.appveyor.com/docs/appveyor-yml/

image: Visual Studio 2017
image: Visual Studio 2019 Preview

version: 2.1.{build}

Expand All @@ -11,7 +11,7 @@ clone_depth: 1
environment:
DOTNET_CLI_TELEMETRY_OPTOUT: 1
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
DOTNET_SDK_URL: 'https://dotnetcli.azureedge.net/dotnet/Sdk/2.2.204/dotnet-sdk-2.2.204-win-x64.zip'
DOTNET_SDK_URL: 'https://dotnetcli.azureedge.net/dotnet/Sdk/3.1.100/dotnet-sdk-3.1.100-win-x64.zip'

cache:
- '%LocalAppData%\NuGet\v3-cache' # NuGet v3
Expand Down Expand Up @@ -40,9 +40,9 @@ artifacts:
- path: dotnet-xdt/bin/$(configuration)/*.nupkg
- path: dotnet-xdt/bin/$(configuration)/net461/dotnet-xdt.exe
name: dotnet-xdt.exe
- path: dotnet-xdt.tests/bin/$(configuration)/net461/test-results.xml
name: test-results/net461.xml
- path: dotnet-xdt.tests/bin/$(configuration)/netcoreapp2.1/test-results.xml
name: test-results/netcoreapp21.xml
- path: dotnet-xdt.tests/bin/$(configuration)/net48/test-results.xml
name: test-results/net48.xml
- path: dotnet-xdt.tests/bin/$(configuration)/netcoreapp3.1/test-results.xml
name: test-results/netcoreapp31.xml

deploy: off
9 changes: 7 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ pool:

steps:
- task: UseDotNet@2
displayName: 'Install SDK 2.2.204'
displayName: 'Install SDK 2.1.607'
inputs:
version: 2.2.204
version: 2.1.607

- task: UseDotNet@2
displayName: 'Install SDK 3.1.100'
inputs:
version: 3.1.100

- task: BatchScript@1
displayName: 'Run build.cmd'
Expand Down
10 changes: 5 additions & 5 deletions dotnet-xdt.tests/TestTransformationLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,30 @@ public void LogMessage(MessageType type, string message, params object[] message
public void LogWarning(string message, params object[] messageArgs)
=> LogWarning("", message, messageArgs);

public void LogWarning(string file, string message, params object[] messageArgs)
public void LogWarning(string? file, string message, params object[] messageArgs)
=> LogWarning(file, 0, 0, message, messageArgs);

// we will format like: transform.xml (30, 10) warning: Argument 'snap' did not match any attributes
const string WarningFormat = "{0} ({1}, {2}) warning: {3}";
public void LogWarning(string file, int lineNumber, int linePosition, string message, params object[] messageArgs)
public void LogWarning(string? file, int lineNumber, int linePosition, string message, params object[] messageArgs)
=> _log.AppendLine(string.Format(WarningFormat, Path.GetFileName(file), lineNumber, linePosition, string.Format(message,messageArgs)));

public void LogError(string message, params object[] messageArgs)
=> LogError("", message, messageArgs);

public void LogError(string file, string message, params object[] messageArgs)
public void LogError(string? file, string message, params object[] messageArgs)
=> LogError(file, 0, 0, message, messageArgs);

//transform.xml(33, 10) error: Could not resolve 'ThrowException' as a type of Transform
const string ErrorFormat = "{0} ({1}, {2}) error: {3}";
public void LogError(string file, int lineNumber, int linePosition, string message, params object[] messageArgs)
public void LogError(string? file, int lineNumber, int linePosition, string message, params object[] messageArgs)
=> _log.AppendLine(string.Format(ErrorFormat, Path.GetFileName(file), lineNumber, linePosition, string.Format(message,messageArgs)));

public void LogErrorFromException(Exception ex) {}

public void LogErrorFromException(Exception ex, string file) {}

public void LogErrorFromException(Exception ex, string file, int lineNumber, int linePosition)
public void LogErrorFromException(Exception ex, string? file, int lineNumber, int linePosition)
{
string message = ex.Message;
LogError(file, lineNumber, linePosition, message);
Expand Down
8 changes: 5 additions & 3 deletions dotnet-xdt.tests/dotnet-xdt.tests.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;net461</TargetFrameworks>
<TargetFrameworks>netcoreapp3.1;net48</TargetFrameworks>
<IsPackable>false</IsPackable>
<IsPublishable>false</IsPublishable>
<LangVersion>latest</LangVersion>
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
<NullableContextOptions>$(Nullable)</NullableContextOptions>
<Features>strict</Features>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
Expand All @@ -16,7 +18,7 @@
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Fixie" Version="2.0.4" />
<PackageReference Include="Fixie" Version="2.2.0" />
<PackageReference Include="Shouldly" Version="3.0.2" />
<DotNetCliToolReference Include="Fixie.Console" Version="2.0.4" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion dotnet-xdt/IXmlOriginalDocumentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace DotNet.Xdt
{
interface IXmlOriginalDocumentService
{
XmlNodeList SelectNodes(string path, XmlNamespaceManager nsmgr);
XmlNodeList? SelectNodes(string path, XmlNamespaceManager nsmgr);
}
}
10 changes: 5 additions & 5 deletions dotnet-xdt/IXmlTransformationLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ public interface IXmlTransformationLogger
void LogMessage(string message, params object[] messageArgs);
void LogMessage(MessageType type, string message, params object[] messageArgs);
void LogWarning(string message, params object[] messageArgs);
void LogWarning(string file, string message, params object[] messageArgs);
void LogWarning(string file, int lineNumber, int linePosition, string message, params object[] messageArgs);
void LogWarning(string? file, string message, params object[] messageArgs);
void LogWarning(string? file, int lineNumber, int linePosition, string message, params object[] messageArgs);
void LogError(string message, params object[] messageArgs);
void LogError(string file, string message, params object[] messageArgs);
void LogError(string file, int lineNumber, int linePosition, string message, params object[] messageArgs);
void LogError(string? file, string message, params object[] messageArgs);
void LogError(string? file, int lineNumber, int linePosition, string message, params object[] messageArgs);
void LogErrorFromException(Exception ex);
void LogErrorFromException(Exception ex, string file);
void LogErrorFromException(Exception ex, string file, int lineNumber, int linePosition);
void LogErrorFromException(Exception ex, string? file, int lineNumber, int linePosition);
void StartSection(string message, params object[] messageArgs);
void StartSection(MessageType type, string message, params object[] messageArgs);
void EndSection(string message, params object[] messageArgs);
Expand Down
8 changes: 4 additions & 4 deletions dotnet-xdt/NamedTypeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ internal void AddPathRegistration(string path, string nameSpace)
_registrations.Add(new PathRegistration(path, nameSpace));
}

internal TObjectType Construct<TObjectType>(string typeName) where TObjectType : class
internal TObjectType? Construct<TObjectType>(string typeName) where TObjectType : class
{
if (string.IsNullOrEmpty(typeName)) return null;

Type type = GetType(typeName);
Type? type = GetType(typeName);

if (type == null)
throw new XmlTransformationException(string.Format(System.Globalization.CultureInfo.CurrentCulture, SR.XMLTRANSFORMATION_UnknownTypeName, typeName, typeof(TObjectType).Name));
Expand All @@ -56,9 +56,9 @@ internal TObjectType Construct<TObjectType>(string typeName) where TObjectType :
return constructor.Invoke(Array.Empty<object>()) as TObjectType;
}

Type GetType(string typeName)
Type? GetType(string typeName)
{
Type foundType = null;
Type? foundType = null;
foreach (Registration registration in _registrations)
{
if (!registration.IsValid) continue;
Expand Down
18 changes: 9 additions & 9 deletions dotnet-xdt/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static class Program

static int Main(string[] args)
{
string sourceFilePath = null, outputFilePath = null, transformFilePath = null;
string? sourceFilePath = null, outputFilePath = null, transformFilePath = null;
bool verbose = false, printUsage = false;

if (!ParseArguments(args, ref sourceFilePath, ref outputFilePath, ref transformFilePath, ref verbose, ref printUsage))
Expand Down Expand Up @@ -105,8 +105,8 @@ static void PrintUsage(TextWriter writer)
writer.WriteLine($"Example: {ToolName} --source original.xml --transform delta.xml --output final.xml --verbose");
}

static bool ParseArguments(IReadOnlyList<string> args, ref string sourceFilePath, ref string outputFilePath,
ref string transformFilePath, ref bool verbose, ref bool showHelp)
static bool ParseArguments(IReadOnlyList<string> args, ref string? sourceFilePath, ref string? outputFilePath,
ref string? transformFilePath, ref bool verbose, ref bool showHelp)
{
for (var i = 0; i < args.Count; i++)
{
Expand Down Expand Up @@ -150,7 +150,7 @@ static bool ParseArguments(IReadOnlyList<string> args, ref string sourceFilePath
&& !string.IsNullOrWhiteSpace(outputFilePath)
&& !string.IsNullOrWhiteSpace(transformFilePath);

bool TryRead(ref int index, ref string value)
bool TryRead(ref int index, ref string? value)
{
++index;
if (index >= args.Count || value != null) return false;
Expand Down Expand Up @@ -180,19 +180,19 @@ public void LogMessage(MessageType type, string message, params object[] message
public void LogWarning(string message, params object[] messageArgs)
=> Console.WriteLine($"{Prefix}WARN: {message}", messageArgs);

public void LogWarning(string file, string message, params object[] messageArgs)
public void LogWarning(string? file, string message, params object[] messageArgs)
=> Console.WriteLine($"{Prefix}WARN '{file}': {message}", messageArgs);

public void LogWarning(string file, int lineNumber, int linePosition, string message, params object[] messageArgs)
public void LogWarning(string? file, int lineNumber, int linePosition, string message, params object[] messageArgs)
=> Console.WriteLine($"{Prefix}WARN '{file}':{lineNumber}:{linePosition}: {message}", messageArgs);

public void LogError(string message, params object[] messageArgs)
=> Console.Error.WriteLine($"{Prefix}ERROR: {message}", messageArgs);

public void LogError(string file, string message, params object[] messageArgs)
public void LogError(string? file, string message, params object[] messageArgs)
=> Console.Error.WriteLine($"{Prefix}ERROR '{file}': {message}", messageArgs);

public void LogError(string file, int lineNumber, int linePosition, string message, params object[] messageArgs)
public void LogError(string? file, int lineNumber, int linePosition, string message, params object[] messageArgs)
=> Console.Error.WriteLine($"{Prefix}ERROR '{file}':{lineNumber}:{linePosition}: {message}", messageArgs);

public void LogErrorFromException(Exception ex)
Expand All @@ -201,7 +201,7 @@ public void LogErrorFromException(Exception ex)
public void LogErrorFromException(Exception ex, string file)
=> Console.Error.WriteLine($"{Prefix}ERROR '{file}': {ex}");

public void LogErrorFromException(Exception ex, string file, int lineNumber, int linePosition)
public void LogErrorFromException(Exception ex, string? file, int lineNumber, int linePosition)
=> Console.Error.WriteLine($"{Prefix}ERROR '{file}':{lineNumber}:{linePosition}: {ex}");

public void StartSection(string message, params object[] messageArgs)
Expand Down
2 changes: 1 addition & 1 deletion dotnet-xdt/XmlArgumentUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal static IList<string> SplitArguments(string argumentString)
static IList<string> RecombineArguments(IList<string> arguments, char separator)
{
var combinedArguments = new List<string>();
string combinedArgument = null;
string? combinedArgument = null;
var parenCount = 0;

foreach (string argument in arguments)
Expand Down
16 changes: 8 additions & 8 deletions dotnet-xdt/XmlAttributePreservationDict.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class XmlAttributePreservationDict
readonly List<string> _orderedAttributes = new List<string>();
readonly Dictionary<string, string> _leadingSpaces = new Dictionary<string, string>();

string _attributeNewLineString;
string? _attributeNewLineString;
bool? _oneAttributePerLine;

bool OneAttributePerLine => _oneAttributePerLine
Expand Down Expand Up @@ -66,7 +66,7 @@ static int EnumerateAttributes(string elementStartTag, Action<int, int, string>

internal void WritePreservedAttributes(XmlAttributePreservingWriter writer, XmlAttributeCollection attributes)
{
string oldNewLineString = null;
string? oldNewLineString = null;
if (_attributeNewLineString != null)
oldNewLineString = writer.SetAttributeNewLineString(_attributeNewLineString);

Expand Down Expand Up @@ -119,7 +119,7 @@ internal void UpdatePreservationInfo(XmlAttributeCollection updatedAttributes, X
}

var firstAttribute = true;
string keepLeadingWhitespace = null;
string? keepLeadingWhitespace = null;
foreach (string key in _orderedAttributes)
{
bool exists = attributeExists[key];
Expand Down Expand Up @@ -173,7 +173,7 @@ internal void UpdatePreservationInfo(XmlAttributeCollection updatedAttributes, X
_leadingSpaces[key] = " ";
else if (OneAttributePerLine)
// Add the indent space between each attribute
_leadingSpaces[key] = GetAttributeNewLineString(formatter);
_leadingSpaces[key] = GetAttributeNewLineString(formatter)!;
else
// Don't add any hard-coded spaces. All new attributes
// should be at the end, so they'll be formatted while
Expand Down Expand Up @@ -220,13 +220,13 @@ bool ComputeOneAttributePerLine()
static bool ContainsNewLine(string space)
=> space.IndexOf("\n", StringComparison.Ordinal) >= 0;

public string GetAttributeNewLineString(XmlFormatter formatter)
=> _attributeNewLineString ?? (_attributeNewLineString = ComputeAttributeNewLineString(formatter));
public string? GetAttributeNewLineString(XmlFormatter? formatter)
=> _attributeNewLineString ??= ComputeAttributeNewLineString(formatter);

string ComputeAttributeNewLineString(XmlFormatter formatter)
string? ComputeAttributeNewLineString(XmlFormatter? formatter)
=> LookAheadForNewLineString() ?? formatter?.CurrentAttributeIndent;

string LookAheadForNewLineString()
string? LookAheadForNewLineString()
{
foreach (string space in _leadingSpaces.Values)
if (ContainsNewLine(space)) return space;
Expand Down
2 changes: 1 addition & 1 deletion dotnet-xdt/XmlAttributePreservationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public XmlAttributePreservationProvider(string fileName)
_reader = new PositionTrackingTextReader(_streamReader);
}

public XmlAttributePreservationDict GetDictAtPosition(int lineNumber, int linePosition)
public XmlAttributePreservationDict? GetDictAtPosition(int lineNumber, int linePosition)
{
if (_reader.ReadToPosition(lineNumber, linePosition))
{
Expand Down
18 changes: 9 additions & 9 deletions dotnet-xdt/XmlAttributePreservingWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class XmlAttributePreservingWriter : XmlWriter
readonly XmlTextWriter _xmlWriter;
readonly AttributeTextWriter _textWriter;

public XmlAttributePreservingWriter(string fileName, Encoding encoding)
public XmlAttributePreservingWriter(string fileName, Encoding? encoding)
: this(encoding == null ? new StreamWriter(fileName) : new StreamWriter(fileName, false, encoding))
{ }

public XmlAttributePreservingWriter(Stream w, Encoding encoding)
public XmlAttributePreservingWriter(Stream w, Encoding? encoding)
: this(encoding == null ? new StreamWriter(w) : new StreamWriter(w, encoding))
{ }

Expand Down Expand Up @@ -86,7 +86,7 @@ enum State
}

State _state = State.Writing;
StringBuilder _writeBuffer;
StringBuilder? _writeBuffer;

readonly TextWriter _baseWriter;

Expand All @@ -97,7 +97,7 @@ public AttributeTextWriter(TextWriter baseWriter)
: base(CultureInfo.InvariantCulture)
=> _baseWriter = baseWriter;

public string AttributeLeadingWhitespace { get; set; }
public string? AttributeLeadingWhitespace { get; set; }

public string AttributeNewLineString { get; set; } = "\r\n";

Expand Down Expand Up @@ -136,7 +136,7 @@ public override void Write(char value)
break;
case State.ReadingAttribute:
case State.Buffering:
_writeBuffer.Append(value);
_writeBuffer?.Append(value);
break;
}
}
Expand All @@ -156,12 +156,12 @@ void UpdateState(char value)
case '>':
if (_state == State.Buffering)
{
string currentBuffer = _writeBuffer.ToString();
string currentBuffer = _writeBuffer?.ToString() ?? "";
if (currentBuffer.EndsWith(" /", StringComparison.Ordinal))
{
// We've identified the string " />" at the
// end of the buffer, so remove the space
_writeBuffer.Remove(currentBuffer.LastIndexOf(' '), 1);
_writeBuffer?.Remove(currentBuffer.LastIndexOf(' '), 1);
}
ChangeState(State.Writing);
}
Expand Down Expand Up @@ -234,12 +234,12 @@ void WriteQueuedAttribute()
// Write leading whitespace
if (AttributeLeadingWhitespace != null)
{
_writeBuffer.Insert(0, AttributeLeadingWhitespace);
_writeBuffer?.Insert(0, AttributeLeadingWhitespace);
AttributeLeadingWhitespace = null;
}
else
{
int lineLength = _linePosition + _writeBuffer.Length + 1;
int lineLength = _linePosition + _writeBuffer!.Length + 1;
if (lineLength > MaxLineLength)
_writeBuffer.Insert(0, AttributeNewLineString);
else
Expand Down
Loading

0 comments on commit 52d4784

Please sign in to comment.