Moving existing csproj files with same name as migration output to backup

This commit is contained in:
Justin Goshi 2017-01-20 12:21:04 -08:00
parent 56c3010f72
commit 717d0a45fa
4 changed files with 72 additions and 26 deletions

View file

@ -11,6 +11,8 @@ namespace Microsoft.DotNet.ProjectJsonMigration
{ {
internal class MigrationBackupPlan internal class MigrationBackupPlan
{ {
private const string TempCsprojExtention = ".migration_in_place_backup";
private readonly FileInfo globalJson; private readonly FileInfo globalJson;
public MigrationBackupPlan( public MigrationBackupPlan(
@ -57,7 +59,8 @@ namespace Microsoft.DotNet.ProjectJsonMigration
.Where(f => f.Name == "project.json" .Where(f => f.Name == "project.json"
|| f.Extension == ".xproj" || f.Extension == ".xproj"
|| f.FullName.EndsWith(".xproj.user") || f.FullName.EndsWith(".xproj.user")
|| f.FullName.EndsWith(".lock.json")); || f.FullName.EndsWith(".lock.json")
|| f.FullName.EndsWith(TempCsprojExtention));
} }
public DirectoryInfo ProjectBackupDirectory { get; } public DirectoryInfo ProjectBackupDirectory { get; }
@ -81,10 +84,23 @@ namespace Microsoft.DotNet.ProjectJsonMigration
foreach (var file in FilesToMove) foreach (var file in FilesToMove)
{ {
var fileName = file.Name.EndsWith(TempCsprojExtention)
? Path.GetFileNameWithoutExtension(file.Name)
: file.Name;
file.MoveTo( file.MoveTo(
Path.Combine( Path.Combine(ProjectBackupDirectory.FullName, fileName));
ProjectBackupDirectory.FullName, file.Name));
} }
} }
public static void RenameCsprojFromMigrationOutputNameToTempName(string outputProject)
{
var backupFileName = $"{outputProject}{TempCsprojExtention}";
if (File.Exists(backupFileName))
{
File.Delete(backupFileName);
}
File.Move(outputProject, backupFileName);
}
} }
} }

View file

@ -20,6 +20,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration
{ {
private readonly IMigrationRule _ruleSet; private readonly IMigrationRule _ruleSet;
private readonly ProjectDependencyFinder _projectDependencyFinder = new ProjectDependencyFinder(); private readonly ProjectDependencyFinder _projectDependencyFinder = new ProjectDependencyFinder();
private HashSet<string> _migratedProjects = new HashSet<string>();
public ProjectMigrator() : this(new DefaultMigrationRuleSet()) { } public ProjectMigrator() : this(new DefaultMigrationRuleSet()) { }
@ -76,7 +77,6 @@ namespace Microsoft.DotNet.ProjectJsonMigration
var settings = new MigrationSettings(projectDir, var settings = new MigrationSettings(projectDir,
projectDir, projectDir,
rootSettings.MSBuildProjectTemplatePath); rootSettings.MSBuildProjectTemplatePath);
MigrateProject(settings);
projectMigrationReports.Add(MigrateProject(settings)); projectMigrationReports.Add(MigrateProject(settings));
} }
@ -141,13 +141,28 @@ namespace Microsoft.DotNet.ProjectJsonMigration
{ {
var migrationRuleInputs = ComputeMigrationRuleInputs(migrationSettings); var migrationRuleInputs = ComputeMigrationRuleInputs(migrationSettings);
var projectName = migrationRuleInputs.DefaultProjectContext.GetProjectName(); var projectName = migrationRuleInputs.DefaultProjectContext.GetProjectName();
var outputProject = Path.Combine(migrationSettings.OutputDirectory, projectName + ".csproj");
try try
{ {
if (IsMigrated(migrationSettings, migrationRuleInputs)) if (File.Exists(outputProject))
{ {
MigrationTrace.Instance.WriteLine(String.Format(LocalizableStrings.SkipMigrationAlreadyMigrated, nameof(ProjectMigrator), migrationSettings.ProjectDirectory)); if (_migratedProjects.Contains(outputProject))
return new ProjectMigrationReport(migrationSettings.ProjectDirectory, projectName, skipped: true); {
MigrationTrace.Instance.WriteLine(String.Format(
LocalizableStrings.SkipMigrationAlreadyMigrated,
nameof(ProjectMigrator),
migrationSettings.ProjectDirectory));
return new ProjectMigrationReport(
migrationSettings.ProjectDirectory,
projectName,
skipped: true);
}
else
{
MigrationBackupPlan.RenameCsprojFromMigrationOutputNameToTempName(outputProject);
}
} }
VerifyInputs(migrationRuleInputs, migrationSettings); VerifyInputs(migrationRuleInputs, migrationSettings);
@ -185,7 +200,8 @@ namespace Microsoft.DotNet.ProjectJsonMigration
} }
} }
var outputProject = Path.Combine(migrationSettings.OutputDirectory, projectName + ".csproj"); _migratedProjects.Add(outputProject);
return new ProjectMigrationReport( return new ProjectMigrationReport(
migrationSettings.ProjectDirectory, migrationSettings.ProjectDirectory,
projectName, projectName,
@ -286,14 +302,5 @@ namespace Microsoft.DotNet.ProjectJsonMigration
File.Copy(sourceFilePath, destinationFilePath); File.Copy(sourceFilePath, destinationFilePath);
} }
} }
public bool IsMigrated(MigrationSettings migrationSettings, MigrationRuleInputs migrationRuleInputs)
{
var outputName = migrationRuleInputs.DefaultProjectContext.GetProjectName();
var outputProject = Path.Combine(migrationSettings.OutputDirectory, outputName + ".csproj");
return File.Exists(outputProject);
}
} }
} }

View file

@ -119,17 +119,17 @@ namespace Microsoft.DotNet.Tools.Migrate
foreach (var report in migrationReport.ProjectMigrationReports) foreach (var report in migrationReport.ProjectMigrationReports)
{ {
var reportPathWithTrailingSlash = PathUtility.EnsureTrailingSlash(report.ProjectDirectory); var reportPathWithTrailingSlash = PathUtility.EnsureTrailingSlash(report.ProjectDirectory);
var relReportPath = PathUtility.GetRelativePath( var relativeReportPath = PathUtility.GetRelativePath(
slnPathWithTrailingSlash, slnPathWithTrailingSlash,
reportPathWithTrailingSlash); reportPathWithTrailingSlash);
var xprojPath = Path.Combine(relReportPath, report.ProjectName + ".xproj"); var xprojPath = Path.Combine(relativeReportPath, report.ProjectName + ".xproj");
var projects = _slnFile.Projects.Where(p => p.FilePath == xprojPath); var xprojProjectsReferencedBySolution = _slnFile.Projects.Where(p => p.FilePath == xprojPath);
var migratedProjectName = report.ProjectName + ".csproj"; var migratedProjectName = report.ProjectName + ".csproj";
if (projects.Count() == 1) if (xprojProjectsReferencedBySolution.Count() == 1)
{ {
var slnProject = projects.Single(); var slnProject = xprojProjectsReferencedBySolution.Single();
slnProject.FilePath = Path.Combine( slnProject.FilePath = Path.Combine(
Path.GetDirectoryName(slnProject.FilePath), Path.GetDirectoryName(slnProject.FilePath),
migratedProjectName); migratedProjectName);
@ -137,12 +137,12 @@ namespace Microsoft.DotNet.Tools.Migrate
} }
else else
{ {
var csprojPath = Path.Combine(relReportPath, migratedProjectName); var csprojPath = Path.Combine(relativeReportPath, migratedProjectName);
var slnAlreadyContainsMigratedCsproj = _slnFile.Projects var solutionContainsCsprojPriorToMigration = _slnFile.Projects
.Where(p => p.FilePath == csprojPath) .Where(p => p.FilePath == csprojPath)
.Any(); .Any();
if (!slnAlreadyContainsMigratedCsproj) if (!solutionContainsCsprojPriorToMigration)
{ {
csprojFilesToAdd.Add(Path.Combine(report.ProjectDirectory, migratedProjectName)); csprojFilesToAdd.Add(Path.Combine(report.ProjectDirectory, migratedProjectName));
} }
@ -299,7 +299,6 @@ namespace Microsoft.DotNet.Tools.Migrate
if (projectMigrationReport.Errors.Any()) if (projectMigrationReport.Errors.Any())
{ {
sb.AppendLine(RedIfColored($"Project {projectMigrationReport.ProjectName} migration failed ({projectMigrationReport.ProjectDirectory})")); sb.AppendLine(RedIfColored($"Project {projectMigrationReport.ProjectName} migration failed ({projectMigrationReport.ProjectDirectory})"));
foreach (var error in projectMigrationReport.Errors.Select(e => e.GetFormattedErrorMessage())) foreach (var error in projectMigrationReport.Errors.Select(e => e.GetFormattedErrorMessage()))

View file

@ -67,6 +67,30 @@ namespace Microsoft.DotNet.Migration.Tests
"PJAppWithSlnAndXprojRefThatRefsCsprojWhereSlnDoesNotRefCsproj"); "PJAppWithSlnAndXprojRefThatRefsCsprojWhereSlnDoesNotRefCsproj");
} }
[Fact]
public void WhenSolutionContainsACsprojFileItGetsMovedToBackup()
{
var projectDirectory = TestAssets
.Get("NonRestoredTestProjects", "PJAppWithSlnAndOneAlreadyMigratedCsproj")
.CreateInstance()
.WithSourceFiles()
.Root
.FullName;
var solutionRelPath = Path.Combine("TestApp", "TestApp.sln");
var cmd = new DotnetCommand()
.WithWorkingDirectory(projectDirectory)
.ExecuteWithCapturedOutput($"migrate \"{solutionRelPath}\"");
cmd.Should().Pass();
File.Exists(Path.Combine(projectDirectory, "TestLibrary", "TestLibrary.csproj"))
.Should().BeTrue();
File.Exists(Path.Combine(projectDirectory, "TestLibrary", "TestLibrary.csproj.migration_in_place_backup"))
.Should().BeFalse();
File.Exists(Path.Combine(projectDirectory, "backup", "TestLibrary", "TestLibrary.csproj"))
.Should().BeTrue();
}
[Fact] [Fact]
public void WhenSolutionContainsACsprojFileItDoesNotTryToAddItAgain() public void WhenSolutionContainsACsprojFileItDoesNotTryToAddItAgain()
{ {