Moving existing csproj files with same name as migration output to backup
This commit is contained in:
parent
56c3010f72
commit
717d0a45fa
4 changed files with 72 additions and 26 deletions
|
@ -11,6 +11,8 @@ namespace Microsoft.DotNet.ProjectJsonMigration
|
|||
{
|
||||
internal class MigrationBackupPlan
|
||||
{
|
||||
private const string TempCsprojExtention = ".migration_in_place_backup";
|
||||
|
||||
private readonly FileInfo globalJson;
|
||||
|
||||
public MigrationBackupPlan(
|
||||
|
@ -57,7 +59,8 @@ namespace Microsoft.DotNet.ProjectJsonMigration
|
|||
.Where(f => f.Name == "project.json"
|
||||
|| f.Extension == ".xproj"
|
||||
|| f.FullName.EndsWith(".xproj.user")
|
||||
|| f.FullName.EndsWith(".lock.json"));
|
||||
|| f.FullName.EndsWith(".lock.json")
|
||||
|| f.FullName.EndsWith(TempCsprojExtention));
|
||||
}
|
||||
|
||||
public DirectoryInfo ProjectBackupDirectory { get; }
|
||||
|
@ -81,10 +84,23 @@ namespace Microsoft.DotNet.ProjectJsonMigration
|
|||
|
||||
foreach (var file in FilesToMove)
|
||||
{
|
||||
var fileName = file.Name.EndsWith(TempCsprojExtention)
|
||||
? Path.GetFileNameWithoutExtension(file.Name)
|
||||
: file.Name;
|
||||
|
||||
file.MoveTo(
|
||||
Path.Combine(
|
||||
ProjectBackupDirectory.FullName, file.Name));
|
||||
Path.Combine(ProjectBackupDirectory.FullName, fileName));
|
||||
}
|
||||
}
|
||||
|
||||
public static void RenameCsprojFromMigrationOutputNameToTempName(string outputProject)
|
||||
{
|
||||
var backupFileName = $"{outputProject}{TempCsprojExtention}";
|
||||
if (File.Exists(backupFileName))
|
||||
{
|
||||
File.Delete(backupFileName);
|
||||
}
|
||||
File.Move(outputProject, backupFileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration
|
|||
{
|
||||
private readonly IMigrationRule _ruleSet;
|
||||
private readonly ProjectDependencyFinder _projectDependencyFinder = new ProjectDependencyFinder();
|
||||
private HashSet<string> _migratedProjects = new HashSet<string>();
|
||||
|
||||
public ProjectMigrator() : this(new DefaultMigrationRuleSet()) { }
|
||||
|
||||
|
@ -76,7 +77,6 @@ namespace Microsoft.DotNet.ProjectJsonMigration
|
|||
var settings = new MigrationSettings(projectDir,
|
||||
projectDir,
|
||||
rootSettings.MSBuildProjectTemplatePath);
|
||||
MigrateProject(settings);
|
||||
projectMigrationReports.Add(MigrateProject(settings));
|
||||
}
|
||||
|
||||
|
@ -141,13 +141,28 @@ namespace Microsoft.DotNet.ProjectJsonMigration
|
|||
{
|
||||
var migrationRuleInputs = ComputeMigrationRuleInputs(migrationSettings);
|
||||
var projectName = migrationRuleInputs.DefaultProjectContext.GetProjectName();
|
||||
var outputProject = Path.Combine(migrationSettings.OutputDirectory, projectName + ".csproj");
|
||||
|
||||
try
|
||||
{
|
||||
if (IsMigrated(migrationSettings, migrationRuleInputs))
|
||||
if (File.Exists(outputProject))
|
||||
{
|
||||
MigrationTrace.Instance.WriteLine(String.Format(LocalizableStrings.SkipMigrationAlreadyMigrated, nameof(ProjectMigrator), migrationSettings.ProjectDirectory));
|
||||
return new ProjectMigrationReport(migrationSettings.ProjectDirectory, projectName, skipped: true);
|
||||
if (_migratedProjects.Contains(outputProject))
|
||||
{
|
||||
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);
|
||||
|
@ -185,7 +200,8 @@ namespace Microsoft.DotNet.ProjectJsonMigration
|
|||
}
|
||||
}
|
||||
|
||||
var outputProject = Path.Combine(migrationSettings.OutputDirectory, projectName + ".csproj");
|
||||
_migratedProjects.Add(outputProject);
|
||||
|
||||
return new ProjectMigrationReport(
|
||||
migrationSettings.ProjectDirectory,
|
||||
projectName,
|
||||
|
@ -286,14 +302,5 @@ namespace Microsoft.DotNet.ProjectJsonMigration
|
|||
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -119,17 +119,17 @@ namespace Microsoft.DotNet.Tools.Migrate
|
|||
foreach (var report in migrationReport.ProjectMigrationReports)
|
||||
{
|
||||
var reportPathWithTrailingSlash = PathUtility.EnsureTrailingSlash(report.ProjectDirectory);
|
||||
var relReportPath = PathUtility.GetRelativePath(
|
||||
var relativeReportPath = PathUtility.GetRelativePath(
|
||||
slnPathWithTrailingSlash,
|
||||
reportPathWithTrailingSlash);
|
||||
|
||||
var xprojPath = Path.Combine(relReportPath, report.ProjectName + ".xproj");
|
||||
var projects = _slnFile.Projects.Where(p => p.FilePath == xprojPath);
|
||||
var xprojPath = Path.Combine(relativeReportPath, report.ProjectName + ".xproj");
|
||||
var xprojProjectsReferencedBySolution = _slnFile.Projects.Where(p => p.FilePath == xprojPath);
|
||||
|
||||
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(
|
||||
Path.GetDirectoryName(slnProject.FilePath),
|
||||
migratedProjectName);
|
||||
|
@ -137,12 +137,12 @@ namespace Microsoft.DotNet.Tools.Migrate
|
|||
}
|
||||
else
|
||||
{
|
||||
var csprojPath = Path.Combine(relReportPath, migratedProjectName);
|
||||
var slnAlreadyContainsMigratedCsproj = _slnFile.Projects
|
||||
var csprojPath = Path.Combine(relativeReportPath, migratedProjectName);
|
||||
var solutionContainsCsprojPriorToMigration = _slnFile.Projects
|
||||
.Where(p => p.FilePath == csprojPath)
|
||||
.Any();
|
||||
|
||||
if (!slnAlreadyContainsMigratedCsproj)
|
||||
if (!solutionContainsCsprojPriorToMigration)
|
||||
{
|
||||
csprojFilesToAdd.Add(Path.Combine(report.ProjectDirectory, migratedProjectName));
|
||||
}
|
||||
|
@ -299,7 +299,6 @@ namespace Microsoft.DotNet.Tools.Migrate
|
|||
|
||||
if (projectMigrationReport.Errors.Any())
|
||||
{
|
||||
|
||||
sb.AppendLine(RedIfColored($"Project {projectMigrationReport.ProjectName} migration failed ({projectMigrationReport.ProjectDirectory})"));
|
||||
|
||||
foreach (var error in projectMigrationReport.Errors.Select(e => e.GetFormattedErrorMessage()))
|
||||
|
|
|
@ -67,6 +67,30 @@ namespace Microsoft.DotNet.Migration.Tests
|
|||
"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]
|
||||
public void WhenSolutionContainsACsprojFileItDoesNotTryToAddItAgain()
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue