PR Feedback

This commit is contained in:
Bryan Thornbury 2016-08-23 13:50:05 -07:00
parent 903764aa7d
commit b0554d3ff3
72 changed files with 769 additions and 680 deletions

View file

@ -9,6 +9,8 @@ using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.Cli;
using System.Linq;
using System.IO;
using Microsoft.DotNet.ProjectJsonMigration.Rules;
using Microsoft.DotNet.Tools.Common;
using Newtonsoft.Json.Linq;
namespace Microsoft.DotNet.ProjectJsonMigration
@ -24,30 +26,34 @@ namespace Microsoft.DotNet.ProjectJsonMigration
// - Migrating Deprecated project.jsons
// - Configuration dependent source exclusion
public void Migrate(MigrationSettings migrationSettings)
{
var projectDirectory = migrationSettings.ProjectDirectory;
EnsureDirectoryExists(migrationSettings.OutputDirectory);
private readonly IMigrationRule _ruleSet;
var migrationRuleInputs = ComputeMigrationRuleInputs(migrationSettings);
VerifyInputs(migrationRuleInputs);
new DefaultMigrationRuleSet().Apply(migrationSettings, migrationRuleInputs);
public ProjectMigrator() : this(new DefaultMigrationRuleSet()) { }
public ProjectMigrator(IMigrationRule ruleSet)
{
_ruleSet = ruleSet;
}
private void EnsureDirectoryExists(string outputDirectory)
public void Migrate(MigrationSettings migrationSettings)
{
if (!Directory.Exists(outputDirectory))
{
Directory.CreateDirectory(outputDirectory);
}
var migrationRuleInputs = ComputeMigrationRuleInputs(migrationSettings);
VerifyInputs(migrationRuleInputs, migrationSettings);
SetupOutputDirectory(migrationSettings.ProjectDirectory, migrationSettings.OutputDirectory);
_ruleSet.Apply(migrationSettings, migrationRuleInputs);
}
private MigrationRuleInputs ComputeMigrationRuleInputs(MigrationSettings migrationSettings)
{
var projectContexts = ProjectContext.CreateContextForEachFramework(migrationSettings.ProjectDirectory);
var templateMSBuildProject = migrationSettings.MSBuildProjectTemplate ?? ProjectRootElement.Create();
var templateMSBuildProject = migrationSettings.MSBuildProjectTemplate;
if (templateMSBuildProject == null)
{
throw new Exception("Expected non-null MSBuildProjectTemplate in MigrationSettings");
}
var propertyGroup = templateMSBuildProject.AddPropertyGroup();
var itemGroup = templateMSBuildProject.AddItemGroup();
@ -55,29 +61,80 @@ namespace Microsoft.DotNet.ProjectJsonMigration
return new MigrationRuleInputs(projectContexts, templateMSBuildProject, itemGroup, propertyGroup);
}
private void VerifyInputs(MigrationRuleInputs migrationRuleInputs)
private void VerifyInputs(MigrationRuleInputs migrationRuleInputs, MigrationSettings migrationSettings)
{
VerifyProject(migrationRuleInputs.ProjectContexts);
VerifyProject(migrationRuleInputs.ProjectContexts, migrationSettings.ProjectDirectory);
}
private void VerifyProject(IEnumerable<ProjectContext> projectContexts)
private void VerifyProject(IEnumerable<ProjectContext> projectContexts, string projectDirectory)
{
if (projectContexts.Count() > 1)
{
throw new Exception("MultiTFM projects currently not supported.");
MigrationErrorCodes.MIGRATE20011($"Multi-TFM projects currently not supported.").Throw();
}
if (projectContexts.Count() == 0)
if (!projectContexts.Any())
{
throw new Exception("No projects found");
MigrationErrorCodes.MIGRATE1013($"No projects found in {projectDirectory}").Throw();
}
if (projectContexts.First().LockFile == null)
var defaultProjectContext = projectContexts.First();
if (defaultProjectContext.LockFile == null)
{
throw new Exception("Restore must be run prior to project migration.");
MigrationErrorCodes.MIGRATE1012(
$"project.lock.json not found in {projectDirectory}, please run dotnet restore before doing migration").Throw();
}
var diagnostics = defaultProjectContext.ProjectFile.Diagnostics;
if (diagnostics.Any())
{
MigrationErrorCodes.MIGRATE1011(
$"{projectDirectory}{Environment.NewLine}{string.Join(Environment.NewLine, diagnostics.Select(d => d.Message))}")
.Throw();
}
var compilerName =
defaultProjectContext.ProjectFile.GetCompilerOptions(defaultProjectContext.TargetFramework, "_")
.CompilerName;
if (!compilerName.Equals("csc", StringComparison.OrdinalIgnoreCase))
{
MigrationErrorCodes.MIGRATE20013(
$"Cannot migrate project {defaultProjectContext.ProjectFile.ProjectFilePath} using compiler {compilerName}").Throw();
}
}
private void SetupOutputDirectory(string projectDirectory, string outputDirectory)
{
if (!Directory.Exists(outputDirectory))
{
Directory.CreateDirectory(outputDirectory);
}
if (projectDirectory != outputDirectory)
{
CopyProjectToOutputDirectory(projectDirectory, outputDirectory);
}
}
private void CopyProjectToOutputDirectory(string projectDirectory, string outputDirectory)
{
var sourceFilePaths = Directory.EnumerateFiles(projectDirectory, "*", SearchOption.AllDirectories);
foreach (var sourceFilePath in sourceFilePaths)
{
var relativeFilePath = PathUtility.GetRelativePath(projectDirectory, sourceFilePath);
var destinationFilePath = Path.Combine(outputDirectory, relativeFilePath);
var destinationDirectory = Path.GetDirectoryName(destinationFilePath);
if (!Directory.Exists(destinationDirectory))
{
Directory.CreateDirectory(destinationDirectory);
}
File.Copy(sourceFilePath, destinationFilePath);
}
}
}
}