2016-08-22 12:24:10 -07:00
using System.Collections.Generic ;
using Microsoft.Build.Construction ;
2016-08-23 13:50:05 -07:00
using Microsoft.DotNet.ProjectJsonMigration.Rules ;
2016-10-27 18:46:43 -07:00
using Microsoft.DotNet.Internal.ProjectModel ;
2016-08-22 12:24:10 -07:00
using NuGet.Frameworks ;
namespace Microsoft.DotNet.ProjectJsonMigration.Tests
{
2016-10-27 18:46:43 -07:00
internal class TemporaryProjectFileRuleRunner
2016-08-22 12:24:10 -07:00
{
public static ProjectRootElement RunRules ( IEnumerable < IMigrationRule > rules , string projectJson ,
2016-09-21 17:27:02 -07:00
string testDirectory , ProjectRootElement xproj = null )
2016-08-22 12:24:10 -07:00
{
var projectContext = GenerateProjectContextFromString ( testDirectory , projectJson ) ;
2016-09-21 17:27:02 -07:00
return RunMigrationRulesOnGeneratedProject ( rules , projectContext , testDirectory , xproj ) ;
2016-08-22 12:24:10 -07:00
}
private static ProjectContext GenerateProjectContextFromString ( string projectDirectory , string json )
{
var testPj = new ProjectJsonBuilder ( null )
. FromStringBase ( json )
. SaveToDisk ( projectDirectory ) ;
return ProjectContext . Create ( testPj , FrameworkConstants . CommonFrameworks . NetCoreApp10 ) ;
}
private static ProjectRootElement RunMigrationRulesOnGeneratedProject ( IEnumerable < IMigrationRule > rules ,
2016-09-21 17:27:02 -07:00
ProjectContext projectContext , string testDirectory , ProjectRootElement xproj )
2016-08-22 12:24:10 -07:00
{
var project = ProjectRootElement . Create ( ) ;
2016-12-07 11:49:15 -10:00
var testSettings = MigrationSettings . CreateMigrationSettingsTestHook ( testDirectory , testDirectory , project ) ;
2016-08-22 12:24:10 -07:00
var testInputs = new MigrationRuleInputs ( new [ ] { projectContext } , project ,
project . AddItemGroup ( ) ,
2016-09-21 17:27:02 -07:00
project . AddPropertyGroup ( ) ,
xproj ) ;
2016-08-22 12:24:10 -07:00
foreach ( var rule in rules )
{
rule . Apply ( testSettings , testInputs ) ;
}
return project ;
}
}
}