dotnet-installer/test/Microsoft.DotNet.ProjectJsonMigration.Tests/TemporaryProjectFileRuleRunner.cs

87 lines
3 KiB
C#
Raw Normal View History

// 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.Collections.Generic;
using System.Linq;
2016-08-22 19:24:10 +00:00
using Microsoft.Build.Construction;
2016-08-23 20:50:05 +00:00
using Microsoft.DotNet.ProjectJsonMigration.Rules;
using Microsoft.DotNet.Internal.ProjectModel;
using Microsoft.DotNet.TestFramework;
2016-08-22 19:24:10 +00:00
using NuGet.Frameworks;
using System.IO;
2016-08-22 19:24:10 +00:00
namespace Microsoft.DotNet.ProjectJsonMigration.Tests
{
internal class TemporaryProjectFileRuleRunner
2016-08-22 19:24:10 +00:00
{
public static ProjectRootElement RunRules(
IEnumerable<IMigrationRule> rules,
string projectJson,
string testDirectory,
ProjectRootElement xproj=null)
2016-08-22 19:24:10 +00:00
{
var projectContexts = GenerateProjectContextsFromString(testDirectory, projectJson);
return RunMigrationRulesOnGeneratedProject(rules, projectContexts, testDirectory, xproj);
2016-08-22 19:24:10 +00:00
}
private static IEnumerable<ProjectContext> GenerateProjectContextsFromString(
string projectDirectory,
string json)
2016-08-22 19:24:10 +00:00
{
var globalJson = Path.Combine(new DirectoryInfo(projectDirectory).Parent.FullName, "global.json");
if (!File.Exists(globalJson))
{
var file = new FileInfo(globalJson);
try
{
File.WriteAllText(file.FullName, @"{}");
}
catch (IOException)
{
//this means there is someone else writing to the file already. So, just ignore it.
}
}
2016-08-22 19:24:10 +00:00
var testPj = new ProjectJsonBuilder(null)
.FromStringBase(json)
.SaveToDisk(projectDirectory);
var projectContexts = ProjectContext.CreateContextForEachFramework(projectDirectory);
if (projectContexts.Count() == 0)
{
projectContexts = new []
{
ProjectContext.Create(testPj, FrameworkConstants.CommonFrameworks.NetCoreApp10)
};
}
return projectContexts;
2016-08-22 19:24:10 +00:00
}
private static ProjectRootElement RunMigrationRulesOnGeneratedProject(
IEnumerable<IMigrationRule> rules,
IEnumerable<ProjectContext> projectContexts,
string testDirectory,
ProjectRootElement xproj)
2016-08-22 19:24:10 +00:00
{
var project = ProjectRootElement.Create();
var testSettings = MigrationSettings.CreateMigrationSettingsTestHook(testDirectory, testDirectory, project);
var testInputs = new MigrationRuleInputs(
projectContexts,
project,
2016-08-22 19:24:10 +00:00
project.AddItemGroup(),
2016-09-22 00:27:02 +00:00
project.AddPropertyGroup(),
xproj);
2016-08-22 19:24:10 +00:00
foreach (var rule in rules)
{
rule.Apply(testSettings, testInputs);
}
return project;
}
}
}