2016-08-22 19:24:10 +00:00
|
|
|
|
using System.Collections.Generic;
|
2017-01-04 01:16:05 +00:00
|
|
|
|
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;
|
2016-10-28 01:46:43 +00:00
|
|
|
|
using Microsoft.DotNet.Internal.ProjectModel;
|
2017-01-26 07:48:31 +00:00
|
|
|
|
using Microsoft.DotNet.TestFramework;
|
2016-08-22 19:24:10 +00:00
|
|
|
|
using NuGet.Frameworks;
|
2017-01-26 07:48:31 +00:00
|
|
|
|
using System.IO;
|
2016-08-22 19:24:10 +00:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.ProjectJsonMigration.Tests
|
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
internal class TemporaryProjectFileRuleRunner
|
2016-08-22 19:24:10 +00:00
|
|
|
|
{
|
2017-01-04 01:16:05 +00:00
|
|
|
|
public static ProjectRootElement RunRules(
|
|
|
|
|
IEnumerable<IMigrationRule> rules,
|
|
|
|
|
string projectJson,
|
|
|
|
|
string testDirectory,
|
|
|
|
|
ProjectRootElement xproj=null)
|
2016-08-22 19:24:10 +00:00
|
|
|
|
{
|
2017-01-04 01:16:05 +00:00
|
|
|
|
var projectContexts = GenerateProjectContextsFromString(testDirectory, projectJson);
|
|
|
|
|
return RunMigrationRulesOnGeneratedProject(rules, projectContexts, testDirectory, xproj);
|
2016-08-22 19:24:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-04 01:16:05 +00:00
|
|
|
|
private static IEnumerable<ProjectContext> GenerateProjectContextsFromString(
|
|
|
|
|
string projectDirectory,
|
|
|
|
|
string json)
|
2016-08-22 19:24:10 +00:00
|
|
|
|
{
|
2017-01-26 07:48:31 +00:00
|
|
|
|
|
|
|
|
|
var globalJson = Path.Combine(new DirectoryInfo(projectDirectory).Parent.FullName, "global.json");
|
|
|
|
|
if (!File.Exists(globalJson))
|
|
|
|
|
{
|
|
|
|
|
var file = new FileInfo(globalJson);
|
|
|
|
|
File.WriteAllText(file.FullName, @"{}");
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-22 19:24:10 +00:00
|
|
|
|
var testPj = new ProjectJsonBuilder(null)
|
|
|
|
|
.FromStringBase(json)
|
|
|
|
|
.SaveToDisk(projectDirectory);
|
|
|
|
|
|
2017-01-04 01:16:05 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2017-01-04 01:16:05 +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();
|
2016-12-07 21:49:15 +00:00
|
|
|
|
var testSettings = MigrationSettings.CreateMigrationSettingsTestHook(testDirectory, testDirectory, project);
|
2017-01-04 01:16:05 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|