2017-11-21 20:10:06 -08:00
|
|
|
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Xml;
|
|
|
|
|
using System.Xml.Serialization;
|
2017-12-04 14:13:24 -08:00
|
|
|
|
using Microsoft.DotNet.ToolPackage.ToolConfigurationDeserialization;
|
2018-01-13 09:40:48 -08:00
|
|
|
|
using Microsoft.DotNet.Tools;
|
2017-11-21 20:10:06 -08:00
|
|
|
|
|
2017-12-04 14:13:24 -08:00
|
|
|
|
namespace Microsoft.DotNet.ToolPackage
|
2017-11-21 20:10:06 -08:00
|
|
|
|
{
|
|
|
|
|
internal static class ToolConfigurationDeserializer
|
|
|
|
|
{
|
|
|
|
|
public static ToolConfiguration Deserialize(string pathToXml)
|
|
|
|
|
{
|
|
|
|
|
var serializer = new XmlSerializer(typeof(DotNetCliTool));
|
|
|
|
|
|
|
|
|
|
DotNetCliTool dotNetCliTool;
|
|
|
|
|
|
|
|
|
|
using (var fs = new FileStream(pathToXml, FileMode.Open))
|
|
|
|
|
{
|
|
|
|
|
var reader = XmlReader.Create(fs);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
dotNetCliTool = (DotNetCliTool)serializer.Deserialize(reader);
|
|
|
|
|
}
|
|
|
|
|
catch (InvalidOperationException e) when (e.InnerException is XmlException)
|
|
|
|
|
{
|
|
|
|
|
throw new ToolConfigurationException(
|
2018-01-13 09:40:48 -08:00
|
|
|
|
string.Format(CommonLocalizableStrings.ToolSettingsInvalidXml, e.InnerException.Message));
|
2017-11-21 20:10:06 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dotNetCliTool.Commands.Length != 1)
|
|
|
|
|
{
|
|
|
|
|
throw new ToolConfigurationException(
|
2018-01-13 09:40:48 -08:00
|
|
|
|
CommonLocalizableStrings.ToolSettingMoreThanOneCommand);
|
2017-11-21 20:10:06 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dotNetCliTool.Commands[0].Runner != "dotnet")
|
|
|
|
|
{
|
|
|
|
|
throw new ToolConfigurationException(
|
2018-01-13 09:40:48 -08:00
|
|
|
|
CommonLocalizableStrings.ToolSettingInvalidRunner);
|
2017-11-21 20:10:06 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var commandName = dotNetCliTool.Commands[0].Name;
|
|
|
|
|
var toolAssemblyEntryPoint = dotNetCliTool.Commands[0].EntryPoint;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return new ToolConfiguration(commandName, toolAssemblyEntryPoint);
|
|
|
|
|
}
|
|
|
|
|
catch (ArgumentException e)
|
|
|
|
|
{
|
2018-01-13 09:40:48 -08:00
|
|
|
|
throw new ToolConfigurationException(
|
|
|
|
|
string.Format(CommonLocalizableStrings.ToolSettingsContainError,
|
|
|
|
|
e.Message));
|
2017-11-21 20:10:06 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|