2018-12-14 13:44:01 -05:00
// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.
2017-03-14 15:48:33 -04:00
using System ;
using System.Collections.Generic ;
using System.Text ;
using System.IO ;
using AutomationTool ;
using UnrealBuildTool ;
using System.Text.RegularExpressions ;
2017-08-31 12:08:38 -04:00
using Tools.DotNETCommon ;
2019-01-14 12:15:37 -05:00
using System.Linq ;
2017-03-14 15:48:33 -04:00
class CheckCsprojDotNetVersion : BuildCommand
{
public override void ExecuteBuild ( )
{
// just calling this DesiredTargetVersion because TargetVersion and TargetedVersion get super confusing.
2019-01-14 12:15:37 -05:00
string DesiredTargetVersionParam = this . ParseParamValue ( "TargetVersion" , null ) ;
if ( string . IsNullOrEmpty ( DesiredTargetVersionParam ) )
2017-03-14 15:48:33 -04:00
{
throw new AutomationException ( "-TargetVersion was not specified." ) ;
}
2019-01-14 12:15:37 -05:00
string [ ] DesiredTargetVersions = DesiredTargetVersionParam . Split ( '+' ) ;
2018-09-04 14:32:41 -04:00
CommandUtils . LogInformation ( "Scanning for all csproj's..." ) ;
2017-03-14 15:48:33 -04:00
// Check for all csproj's in the engine dir
DirectoryReference EngineDir = CommandUtils . EngineDirectory ;
// grab the targeted version.,
Regex FrameworkRegex = new Regex ( "<TargetFrameworkVersion>v(\\d\\.\\d\\.?\\d?)<\\/TargetFrameworkVersion>" ) ;
Regex PossibleAppConfigRegex = new Regex ( "<TargetFrameworkProfile>(.+)<\\/TargetFrameworkProfile>" ) ;
Regex AppConfigRegex = new Regex ( "<supportedRuntime version=\"v(\\d\\.\\d\\.?\\d?)\" sku=\"\\.NETFramework,Version=v(\\d\\.\\d\\.?\\d?),Profile=(.+)\"\\/>" ) ;
2017-09-25 12:29:01 -04:00
Regex DotNetCoreRegex = new Regex ( "<TargetFramework>(netcoreapp2.0|netstandard2.0)<\\/TargetFramework>" ) ;
foreach ( FileReference CsProj in DirectoryReference . EnumerateFiles ( EngineDir , "*.csproj" , SearchOption . AllDirectories ) )
{
2018-09-04 14:32:41 -04:00
if ( CsProj . ContainsName ( "ThirdParty" , EngineDir ) | |
( CsProj . ContainsName ( "UE4TemplateProject" , EngineDir ) & & CsProj . GetFileName ( ) . Equals ( "ProjectTemplate.csproj" ) ) | |
2017-09-25 12:29:01 -04:00
CsProj . GetFileNameWithoutExtension ( ) . ToLower ( ) . Contains ( "_mono" ) | |
CsProj . GetFileNameWithoutExtension ( ) . ToLower ( ) . Contains ( "unrealvs" ) )
2017-03-14 15:48:33 -04:00
2017-09-25 12:29:01 -04:00
{
continue ;
}
// read in the file
string Contents = File . ReadAllText ( CsProj . FullName ) ;
Match Match = DotNetCoreRegex . Match ( Contents ) ;
// Check if we're a _NETCore app, ignore these.
if ( Match . Success )
{
continue ;
}
Match = FrameworkRegex . Match ( Contents ) ;
if ( Match . Success )
2017-03-14 15:48:33 -04:00
{
2017-09-25 12:29:01 -04:00
string TargetedVersion = Match . Groups [ 1 ] . Value ;
2017-03-14 15:48:33 -04:00
// make sure we match, throw warning otherwise
2019-01-14 12:15:37 -05:00
if ( ! DesiredTargetVersions . Any ( DesiredTargetVersion = > DesiredTargetVersion . Equals ( TargetedVersion , StringComparison . InvariantCultureIgnoreCase ) ) )
2017-03-14 15:48:33 -04:00
{
2019-01-14 12:15:37 -05:00
CommandUtils . LogWarning ( "Targeted Framework version for project: {0} was not {1}! Targeted Version: {2}" , CsProj , String . Join ( "/" , DesiredTargetVersions ) , TargetedVersion ) ;
2017-03-14 15:48:33 -04:00
}
}
// if we don't have a TargetFrameworkVersion, check for the existence of TargetFrameworkProfile.
else
{
2017-09-25 12:29:01 -04:00
Match = PossibleAppConfigRegex . Match ( Contents ) ;
if ( ! Match . Success )
2017-03-14 15:48:33 -04:00
{
2018-09-04 14:32:41 -04:00
CommandUtils . LogInformation ( "No TargetFrameworkVersion or TargetFrameworkProfile found for project {0}, is it a mono project? If not, does it compile properly?" , CsProj ) ;
2017-03-14 15:48:33 -04:00
continue ;
}
// look for the app config
FileReference AppConfigFile = FileReference . Combine ( CsProj . Directory , "app.config" ) ;
2017-09-25 12:29:01 -04:00
string Profile = Match . Groups [ 1 ] . Value ;
2017-03-14 15:48:33 -04:00
if ( ! FileReference . Exists ( AppConfigFile ) )
{
2018-09-04 14:32:41 -04:00
CommandUtils . LogInformation ( "Found TargetFrameworkProfile but no associated app.config containing the version for project {0}." , CsProj ) ;
2017-03-14 15:48:33 -04:00
continue ;
}
// read in the app config
Contents = File . ReadAllText ( AppConfigFile . FullName ) ;
2017-09-25 12:29:01 -04:00
Match = AppConfigRegex . Match ( Contents ) ;
if ( ! Match . Success )
2017-03-14 15:48:33 -04:00
{
2018-09-04 14:32:41 -04:00
CommandUtils . LogInformation ( "Couldn't find a supportedRuntime match for the version in the app.config for project {0}." , CsProj ) ;
2017-03-14 15:48:33 -04:00
continue ;
}
// Version1 is the one that appears right after supportedRuntime
// Version2 is the one in the sku
// ProfileString should match the TargetFrameworkProfile from the csproj
2017-09-25 12:29:01 -04:00
string Version1String = Match . Groups [ 1 ] . Value ;
string Version2String = Match . Groups [ 2 ] . Value ;
string ProfileString = Match . Groups [ 3 ] . Value ;
2017-03-14 15:48:33 -04:00
// not sure how this is possible, but check for it anyway
if ( ! ProfileString . Equals ( Profile , StringComparison . InvariantCultureIgnoreCase ) )
{
CommandUtils . LogWarning ( "The TargetFrameworkProfile in csproj {0} ({1}) doesn't match the sku in it's app.config ({2})." , CsProj , Profile , ProfileString ) ;
continue ;
}
// if the version numbers don't match the app.config is probably corrupt.
if ( ! Version1String . Equals ( Version2String , StringComparison . InvariantCultureIgnoreCase ) )
{
CommandUtils . LogWarning ( "The supportedRunTimeVersion ({0}) and the sku version ({1}) in the app.config for project {2} don't match." , Version1String , Version2String , CsProj ) ;
continue ;
}
// make sure the versions match
2019-01-14 12:15:37 -05:00
if ( ! ( DesiredTargetVersions . Any ( DesiredTargetVersion = > DesiredTargetVersion . Equals ( Version1String , StringComparison . InvariantCultureIgnoreCase ) ) ) )
2017-03-14 15:48:33 -04:00
{
2019-01-14 12:15:37 -05:00
CommandUtils . LogWarning ( "Targeted Framework version for project: {0} was not {1}! Targeted Version: {2}" , CsProj , String . Join ( "/" , DesiredTargetVersions ) , Version1String ) ;
2017-03-14 15:48:33 -04:00
}
}
}
}
}