2017-03-02 21:04:03 -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.
|
|
|
|
|
2016-11-22 13:56:13 -08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using Microsoft.DotNet.Tools.Common;
|
|
|
|
using NuGet.Packaging;
|
|
|
|
using NuGet.ProjectModel;
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Cli.Utils
|
|
|
|
{
|
|
|
|
public class PackagedCommandSpecFactoryWithCliRuntime : PackagedCommandSpecFactory
|
|
|
|
{
|
2017-08-13 17:48:27 -07:00
|
|
|
public PackagedCommandSpecFactoryWithCliRuntime() : base(AddAdditionalParameters)
|
2016-11-22 13:56:13 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-08-13 17:48:27 -07:00
|
|
|
private static void AddAdditionalParameters(string commandPath, IList<string> arguments)
|
2016-11-22 13:56:13 -08:00
|
|
|
{
|
|
|
|
if(PrefersCliRuntime(commandPath))
|
|
|
|
{
|
2017-04-24 23:09:45 -07:00
|
|
|
var runtimeConfigFile = Path.ChangeExtension(commandPath, FileNameSuffixes.RuntimeConfigJson);
|
2017-04-26 17:59:08 -07:00
|
|
|
|
|
|
|
if (!File.Exists(runtimeConfigFile))
|
|
|
|
{
|
|
|
|
throw new GracefulException(string.Format(LocalizableStrings.CouldNotFindToolRuntimeConfigFile,
|
|
|
|
nameof(PackagedCommandSpecFactory),
|
|
|
|
Path.GetFileName(commandPath)));
|
|
|
|
}
|
|
|
|
|
2017-04-24 23:09:45 -07:00
|
|
|
var runtimeConfig = new RuntimeConfig(runtimeConfigFile);
|
|
|
|
|
|
|
|
var muxer = new Muxer();
|
|
|
|
|
|
|
|
Version currentFrameworkSimpleVersion = GetVersionWithoutPrerelease(muxer.SharedFxVersion);
|
|
|
|
Version toolFrameworkSimpleVersion = GetVersionWithoutPrerelease(runtimeConfig.Framework.Version);
|
|
|
|
|
|
|
|
if (currentFrameworkSimpleVersion.Major != toolFrameworkSimpleVersion.Major)
|
|
|
|
{
|
|
|
|
Reporter.Verbose.WriteLine(
|
|
|
|
string.Format(
|
|
|
|
LocalizableStrings.IgnoringPreferCLIRuntimeFile,
|
|
|
|
nameof(PackagedCommandSpecFactory),
|
|
|
|
runtimeConfig.Framework.Version,
|
|
|
|
muxer.SharedFxVersion));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
arguments.Add("--fx-version");
|
2017-04-26 14:57:18 -07:00
|
|
|
arguments.Add(muxer.SharedFxVersion);
|
2017-04-24 23:09:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Version GetVersionWithoutPrerelease(string version)
|
|
|
|
{
|
|
|
|
int dashOrPlusIndex = version.IndexOfAny(new char[] { '-', '+' });
|
|
|
|
|
|
|
|
if (dashOrPlusIndex >= 0)
|
|
|
|
{
|
|
|
|
version = version.Substring(0, dashOrPlusIndex);
|
2016-11-22 13:56:13 -08:00
|
|
|
}
|
2017-04-24 23:09:45 -07:00
|
|
|
|
|
|
|
return new Version(version);
|
2016-11-22 13:56:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static bool PrefersCliRuntime(string commandPath)
|
|
|
|
{
|
|
|
|
var libTFMPackageDirectory = Path.GetDirectoryName(commandPath);
|
|
|
|
var packageDirectory = Path.Combine(libTFMPackageDirectory, "..", "..");
|
|
|
|
var preferCliRuntimePath = Path.Combine(packageDirectory, "prefercliruntime");
|
|
|
|
|
|
|
|
Reporter.Verbose.WriteLine(
|
2016-12-16 22:41:06 -08:00
|
|
|
string.Format(
|
|
|
|
LocalizableStrings.LookingForPreferCliRuntimeFile,
|
2017-04-24 23:09:45 -07:00
|
|
|
nameof(PackagedCommandSpecFactory),
|
2016-12-16 22:41:06 -08:00
|
|
|
preferCliRuntimePath));
|
2016-11-22 13:56:13 -08:00
|
|
|
|
|
|
|
return File.Exists(preferCliRuntimePath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|