2016-08-22 12:24:10 -07:00
using System.Collections.Generic ;
2017-01-03 17:16:05 -08:00
using System.Linq ;
2016-08-22 12:24:10 -07:00
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 ;
2017-01-25 23:48:31 -08:00
using Microsoft.DotNet.TestFramework ;
2016-08-22 12:24:10 -07:00
using NuGet.Frameworks ;
2017-01-25 23:48:31 -08:00
using System.IO ;
2016-08-22 12:24:10 -07:00
namespace Microsoft.DotNet.ProjectJsonMigration.Tests
{
2016-10-27 18:46:43 -07:00
internal class TemporaryProjectFileRuleRunner
2016-08-22 12:24:10 -07:00
{
2017-01-03 17:16:05 -08:00
public static ProjectRootElement RunRules (
IEnumerable < IMigrationRule > rules ,
string projectJson ,
string testDirectory ,
ProjectRootElement xproj = null )
2016-08-22 12:24:10 -07:00
{
2017-01-03 17:16:05 -08:00
var projectContexts = GenerateProjectContextsFromString ( testDirectory , projectJson ) ;
return RunMigrationRulesOnGeneratedProject ( rules , projectContexts , testDirectory , xproj ) ;
2016-08-22 12:24:10 -07:00
}
2017-01-03 17:16:05 -08:00
private static IEnumerable < ProjectContext > GenerateProjectContextsFromString (
string projectDirectory ,
string json )
2016-08-22 12:24:10 -07:00
{
2017-01-25 23:48:31 -08:00
var globalJson = Path . Combine ( new DirectoryInfo ( projectDirectory ) . Parent . FullName , "global.json" ) ;
if ( ! File . Exists ( globalJson ) )
{
var file = new FileInfo ( globalJson ) ;
2017-01-26 12:40:29 -08:00
try
{
File . WriteAllText ( file . FullName , @"{}" ) ;
}
catch ( IOException )
{
//this means there is someone else writing to the file already. So, just ignore it.
}
2017-01-25 23:48:31 -08:00
}
2016-08-22 12:24:10 -07:00
var testPj = new ProjectJsonBuilder ( null )
. FromStringBase ( json )
. SaveToDisk ( projectDirectory ) ;
2017-01-03 17:16:05 -08:00
var projectContexts = ProjectContext . CreateContextForEachFramework ( projectDirectory ) ;
if ( projectContexts . Count ( ) = = 0 )
{
projectContexts = new [ ]
2017-03-02 20:35:20 -08:00
{
2017-01-03 17:16:05 -08:00
ProjectContext . Create ( testPj , FrameworkConstants . CommonFrameworks . NetCoreApp10 )
} ;
}
return projectContexts ;
2016-08-22 12:24:10 -07:00
}
2017-01-03 17:16:05 -08:00
private static ProjectRootElement RunMigrationRulesOnGeneratedProject (
IEnumerable < IMigrationRule > rules ,
IEnumerable < ProjectContext > projectContexts ,
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 ) ;
2017-01-03 17:16:05 -08:00
var testInputs = new MigrationRuleInputs (
projectContexts ,
project ,
2016-08-22 12:24:10 -07:00
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 ;
}
}
2017-03-02 20:35:20 -08:00
}