Add support for Migration defaults (#4242)

* Add support for sdk props/targets defaults to Migration.

* fix the transform applicator

* remove transform applicator dependency on insertion order for item merges

* defaults constructor msbuild version change
This commit is contained in:
Bryan Thornbury 2016-10-20 11:00:41 -07:00 committed by GitHub
parent 6373cdde60
commit ecbc45098d
28 changed files with 1251 additions and 302 deletions

View file

@ -0,0 +1,193 @@
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Execution;
using Newtonsoft.Json;
using System.Threading;
using Microsoft.Build.Construction;
using Microsoft.DotNet.ProjectJsonMigration.Models;
namespace MigrationDefaultsConstructor
{
public class Program
{
private const string c_temporaryDotnetNewMSBuildProjectName = "p";
static void Main(string[] args)
{
var sdkRootPath=args[0];
var beforeCommonSdkTargetsFilePath = Path.Combine(sdkRootPath, "src", "Tasks", "Microsoft.NET.Build.Tasks", "build", "Microsoft.NET.Sdk.BeforeCommon.targets");
var commonSdkTargetsFilePath = Path.Combine(sdkRootPath, "src", "Tasks", "Microsoft.NET.Build.Tasks", "build", "Microsoft.NET.Sdk.Common.targets");
var sdkTargetsFilePath = Path.Combine(sdkRootPath, "src", "Tasks", "Microsoft.NET.Build.Tasks", "build", "Microsoft.NET.Sdk.targets");
var sdkPropsFilePath = Path.Combine(sdkRootPath, "src", "Tasks", "Microsoft.NET.Build.Tasks", "build", "Microsoft.NET.Sdk.props");
var csharpTargetsFilePath = Path.Combine(sdkRootPath, "src", "Tasks", "Microsoft.NET.Build.Tasks", "build", "Microsoft.NET.Sdk.CSharp.targets");
var csharpPropsFilePath = Path.Combine(sdkRootPath, "src", "Tasks", "Microsoft.NET.Build.Tasks", "build", "Microsoft.NET.Sdk.CSharp.props");
var beforeCommonSdkTargetsFile = ProjectRootElement.Open(beforeCommonSdkTargetsFilePath);
var commonSdkTargetsFile = ProjectRootElement.Open(commonSdkTargetsFilePath);
var sdkTargetsFile = ProjectRootElement.Open(sdkTargetsFilePath);
var sdkPropsFile = ProjectRootElement.Open(sdkPropsFilePath);
var csharpPropsFile = ProjectRootElement.Open(csharpPropsFilePath);
var csharpTargetsFile = ProjectRootElement.Open(csharpTargetsFilePath);
var allProperties = new List<DefaultProjectPropertyInfo>();
var allItems = new List<DefaultProjectItemInfo>();
AddPropertyDefault(allProperties, sdkPropsFile, "OutputType");
AddPropertyDefault(allProperties, sdkPropsFile, "Configuration", ignoreConditions: true);
AddPropertyDefault(allProperties, sdkPropsFile, "Platform");
AddPropertyDefault(allProperties, sdkPropsFile, "FileAlignment");
AddPropertyDefault(allProperties, sdkPropsFile, "PlatformTarget");
AddPropertyDefault(allProperties, sdkPropsFile, "ErrorReport");
AddPropertyDefault(allProperties, sdkPropsFile, "AssemblyName");
AddPropertyDefault(allProperties, sdkPropsFile, "RootNamespace");
AddPropertyDefault(allProperties, sdkPropsFile, "Deterministic");
AddPropertyDefault(allProperties, csharpPropsFile, "WarningLevel");
AddPropertyDefault(allProperties, csharpPropsFile, "NoWarn");
AddHardcodedPropertyDefault(allProperties, "PackageRequireLicenseAcceptance", "false");
AddConfigurationPropertyDefaults(allProperties, sdkPropsFile, "Debug");
AddConfigurationPropertyDefaults(allProperties, sdkPropsFile, "Release");
AddConfigurationPropertyDefaults(allProperties, csharpPropsFile, "Debug");
AddConfigurationPropertyDefaults(allProperties, csharpPropsFile, "Release");
AddPropertyDefault(allProperties, commonSdkTargetsFile, "VersionPrefix", ignoreConditions: true);
AddPropertyDefault(allProperties, commonSdkTargetsFile, "AssemblyTitle", ignoreConditions: true);
AddPropertyDefault(allProperties, commonSdkTargetsFile, "Product", ignoreConditions: true);
AddPropertyDefault(allProperties, commonSdkTargetsFile, "NeutralLanguage", ignoreConditions: true);
AddPropertyDefault(allProperties, beforeCommonSdkTargetsFile, "AutoUnifyAssemblyReferences", ignoreConditions: true);
AddPropertyDefault(allProperties, beforeCommonSdkTargetsFile, "DesignTimeAutoUnify", ignoreConditions: true);
AddPropertyDefault(allProperties, beforeCommonSdkTargetsFile, "TargetExt", ignoreConditions: true);
AddCompileAndEmbeddedResourceDefaults(allItems, sdkTargetsFile);
var wrapper = new SerializableMigrationDefaultsInfo()
{
Items = allItems,
Properties = allProperties
};
var output = Path.Combine(Directory.GetCurrentDirectory(), "sdkdefaults.json");
string json = JsonConvert.SerializeObject(wrapper, Formatting.Indented);
File.WriteAllText(output, json);
}
private static void AddHardcodedPropertyDefault(List<DefaultProjectPropertyInfo> allProperties,
string name,
string value,
string condition="",
string parentCondition="")
{
var propertyInfo = new DefaultProjectPropertyInfo
{
Name = name,
Value = value,
Condition = condition,
ParentCondition = parentCondition
};
allProperties.Add(propertyInfo);
}
private static void AddCompileAndEmbeddedResourceDefaults(List<DefaultProjectItemInfo> allItems, ProjectRootElement msbuild)
{
var exclude = msbuild.Properties.Where(p=>p.Name == "DefaultExcludes").First().Value;
var compileInclude = msbuild.Items.Where(i => i.ItemType == "Compile").First().Include;
if (string.IsNullOrEmpty(compileInclude))
{
compileInclude = "**\\*.cs";
}
var embedInclude = msbuild.Items.Where(i => i.ItemType == "EmbeddedResource").First().Include;
if (string.IsNullOrEmpty(embedInclude))
{
embedInclude = "**\\*.resx";
}
allItems.Add(new DefaultProjectItemInfo
{
ItemType = "Compile",
Include=compileInclude,
Exclude=exclude
});
allItems.Add(new DefaultProjectItemInfo
{
ItemType = "EmbeddedResource",
Include=embedInclude,
Exclude=exclude
});
}
private static void AddConfigurationPropertyDefaults(List<DefaultProjectPropertyInfo> allProperties, ProjectRootElement msbuild, string config)
{
var configPropertyGroup = msbuild.PropertyGroups.Where(p => p.Condition.Contains("$(Configuration)") && p.Condition.Contains(config)).First();
configPropertyGroup.Condition = $" '$(Configuration)' == '{config}' ";
foreach (var property in configPropertyGroup.Properties)
{
var propertyInfo = new DefaultProjectPropertyInfo
{
Name = property.Name,
Value = property.Value,
Condition = property.Condition,
ParentCondition = property.Parent.Condition
};
allProperties.Add(propertyInfo);
}
}
private static void AddPropertyDefault(List<DefaultProjectPropertyInfo> allProperties, ProjectRootElement msbuild, string propertyName, int? index=null, bool ignoreConditions=false)
{
var properties = msbuild.Properties.Where(p => p.Name == propertyName).ToList();
if (!properties.Any())
{
throw new Exception("property not found:" + propertyName);
}
if (properties.Count() > 1 && index == null)
{
throw new Exception("More than one property found but index is null:" + propertyName);
}
var property = properties[index ?? 0];
if (ignoreConditions)
{
var propertyInfo = new DefaultProjectPropertyInfo
{
Name = property.Name,
Value = property.Value,
Condition = null,
ParentCondition = null
};
allProperties.Add(propertyInfo);
}
else
{
var propertyInfo = new DefaultProjectPropertyInfo
{
Name = property.Name,
Value = property.Value,
Condition = property.Condition,
ParentCondition = property.Parent.Condition
};
allProperties.Add(propertyInfo);
}
}
}
}

View file

@ -0,0 +1,7 @@
# Migration Defaults Constructor
This pulls the migration property and item defaults from a clone of the dotnet/sdk repo.
Run `./run.sh` to generate an sdkdefaults.json
Move it to the Microsoft.DotNet.ProjectJsonMigration project under `src` to override the defaults in dotnet (it's embedded as a resource).

View file

@ -0,0 +1,23 @@
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.1"
},
"dotnet": {
"target": "project"
},
"Microsoft.Build.Runtime": "15.1.319-preview5"
},
"imports": ["dnxcore50", "portable-net45+win8"]
}
}
}

View file

@ -0,0 +1,22 @@
#!/usr/bin/env bash
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ "$SOURCE" != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
rm -rf bin obj
dotnet publish -o bin -f netcoreapp1.0
cp -a "$DIR/bin/runtimes/any/native/." "$DIR/bin"
sdkRevision="cc1fc023e3375b3944dbedfdd4ba2b5d2cbd01f0"
sdkRoot="$DIR/bin/sdk"
(cd bin && \
git clone https://github.com/dotnet/sdk.git && \
cd sdk && \
git reset --hard $sdkRevision)
dotnet "$DIR/bin/MigrationDefaultsConstructor.dll" "$sdkRoot"