Add support to migrate a directory.
- Given a directory migrate all the project.jsons in it recursively. - Remove '-p/--project' option. Instead directly pass a directory or project.json as argument. - Add tests.
This commit is contained in:
parent
5434c7f392
commit
7ee2bd1f26
3 changed files with 78 additions and 29 deletions
|
@ -2,6 +2,7 @@
|
||||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using Microsoft.Build.Construction;
|
using Microsoft.Build.Construction;
|
||||||
using Microsoft.DotNet.Cli;
|
using Microsoft.DotNet.Cli;
|
||||||
|
@ -13,17 +14,17 @@ namespace Microsoft.DotNet.Tools.Migrate
|
||||||
public partial class MigrateCommand
|
public partial class MigrateCommand
|
||||||
{
|
{
|
||||||
private readonly string _templateFile;
|
private readonly string _templateFile;
|
||||||
private readonly string _projectJson;
|
private readonly string _projectArg;
|
||||||
private readonly string _sdkVersion;
|
private readonly string _sdkVersion;
|
||||||
private readonly string _xprojFilePath;
|
private readonly string _xprojFilePath;
|
||||||
private readonly bool _skipProjectReferences;
|
private readonly bool _skipProjectReferences;
|
||||||
|
|
||||||
private readonly TemporaryDotnetNewTemplateProject _temporaryDotnetNewProject;
|
private readonly TemporaryDotnetNewTemplateProject _temporaryDotnetNewProject;
|
||||||
|
|
||||||
public MigrateCommand(string templateFile, string projectJson, string sdkVersion, string xprojFilePath, bool skipProjectReferences)
|
public MigrateCommand(string templateFile, string projectArg, string sdkVersion, string xprojFilePath, bool skipProjectReferences)
|
||||||
{
|
{
|
||||||
_templateFile = templateFile;
|
_templateFile = templateFile;
|
||||||
_projectJson = projectJson;
|
_projectArg = projectArg ?? Directory.GetCurrentDirectory();
|
||||||
_sdkVersion = sdkVersion;
|
_sdkVersion = sdkVersion;
|
||||||
_xprojFilePath = xprojFilePath;
|
_xprojFilePath = xprojFilePath;
|
||||||
_skipProjectReferences = skipProjectReferences;
|
_skipProjectReferences = skipProjectReferences;
|
||||||
|
@ -32,24 +33,48 @@ namespace Microsoft.DotNet.Tools.Migrate
|
||||||
|
|
||||||
public int Execute()
|
public int Execute()
|
||||||
{
|
{
|
||||||
var project = GetProjectJsonPath(_projectJson) ?? GetProjectJsonPath(Directory.GetCurrentDirectory());
|
var projectsToMigrate = GetProjectsToMigrate(_projectArg);
|
||||||
EnsureNotNull(project, "Unable to find project.json");
|
|
||||||
var projectDirectory = Path.GetDirectoryName(project);
|
|
||||||
|
|
||||||
var msBuildTemplate = _templateFile != null ?
|
var msBuildTemplate = _templateFile != null ?
|
||||||
ProjectRootElement.TryOpen(_templateFile) : _temporaryDotnetNewProject.MSBuildProject;
|
ProjectRootElement.TryOpen(_templateFile) : _temporaryDotnetNewProject.MSBuildProject;
|
||||||
|
|
||||||
var outputDirectory = projectDirectory;
|
|
||||||
|
|
||||||
var sdkVersion = _sdkVersion ?? new ProjectJsonParser(_temporaryDotnetNewProject.ProjectJson).SdkPackageVersion;
|
var sdkVersion = _sdkVersion ?? new ProjectJsonParser(_temporaryDotnetNewProject.ProjectJson).SdkPackageVersion;
|
||||||
EnsureNotNull(sdkVersion, "Null Sdk Version");
|
EnsureNotNull(sdkVersion, "Null Sdk Version");
|
||||||
|
|
||||||
var migrationSettings = new MigrationSettings(projectDirectory, outputDirectory, sdkVersion, msBuildTemplate, _xprojFilePath);
|
foreach (var project in projectsToMigrate)
|
||||||
new ProjectMigrator().Migrate(migrationSettings, _skipProjectReferences);
|
{
|
||||||
|
Console.WriteLine($"START migrating project {project}..");
|
||||||
|
var projectDirectory = Path.GetDirectoryName(project);
|
||||||
|
var outputDirectory = projectDirectory;
|
||||||
|
var migrationSettings = new MigrationSettings(projectDirectory, outputDirectory, sdkVersion, msBuildTemplate, _xprojFilePath);
|
||||||
|
new ProjectMigrator().Migrate(migrationSettings, _skipProjectReferences);
|
||||||
|
Console.WriteLine($"END migrating project {project}.");
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private IEnumerable<string> GetProjectsToMigrate(string projectArg)
|
||||||
|
{
|
||||||
|
if (projectArg.EndsWith(Project.FileName))
|
||||||
|
{
|
||||||
|
yield return GetProjectJsonPath(projectArg);
|
||||||
|
}
|
||||||
|
else if (Directory.Exists(projectArg))
|
||||||
|
{
|
||||||
|
var projects = Directory.EnumerateFiles(projectArg, Project.FileName, SearchOption.AllDirectories);
|
||||||
|
|
||||||
|
foreach(var project in projects)
|
||||||
|
{
|
||||||
|
yield return GetProjectJsonPath(project);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new Exception($"Invalid project argument - {projectArg}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void EnsureNotNull(string variable, string message)
|
private void EnsureNotNull(string variable, string message)
|
||||||
{
|
{
|
||||||
if (variable == null)
|
if (variable == null)
|
||||||
|
@ -60,11 +85,6 @@ namespace Microsoft.DotNet.Tools.Migrate
|
||||||
|
|
||||||
private string GetProjectJsonPath(string projectJson)
|
private string GetProjectJsonPath(string projectJson)
|
||||||
{
|
{
|
||||||
if (projectJson == null)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
projectJson = ProjectPathHelper.NormalizeProjectFilePath(projectJson);
|
projectJson = ProjectPathHelper.NormalizeProjectFilePath(projectJson);
|
||||||
|
|
||||||
if (File.Exists(projectJson))
|
if (File.Exists(projectJson))
|
||||||
|
|
|
@ -20,8 +20,12 @@ namespace Microsoft.DotNet.Tools.Migrate
|
||||||
app.HandleResponseFiles = true;
|
app.HandleResponseFiles = true;
|
||||||
app.HelpOption("-h|--help");
|
app.HelpOption("-h|--help");
|
||||||
|
|
||||||
|
CommandArgument projectArgument = app.Argument("<PROJECT_JSON/PROJECT_DIR>",
|
||||||
|
"The path to project.json file or a directory to migrate." +
|
||||||
|
" If a directory is specified, then it will recursively search for project.json files to migrate." +
|
||||||
|
" Defaults to current directory if nothing is specified.");
|
||||||
|
|
||||||
CommandOption template = app.Option("-t|--template-file", "Base MSBuild template to use for migrated app. The default is the project included in dotnet new -t msbuild", CommandOptionType.SingleValue);
|
CommandOption template = app.Option("-t|--template-file", "Base MSBuild template to use for migrated app. The default is the project included in dotnet new -t msbuild", CommandOptionType.SingleValue);
|
||||||
CommandOption project = app.Option("-p|--project", "The path to the project to run (defaults to the current directory). Can be a path to a project.json or a project directory", CommandOptionType.SingleValue);
|
|
||||||
CommandOption sdkVersion = app.Option("-v|--sdk-package-version", "The version of the sdk package that will be referenced in the migrated app. The default is the version of the sdk in dotnet new -t msbuild", CommandOptionType.SingleValue);
|
CommandOption sdkVersion = app.Option("-v|--sdk-package-version", "The version of the sdk package that will be referenced in the migrated app. The default is the version of the sdk in dotnet new -t msbuild", CommandOptionType.SingleValue);
|
||||||
CommandOption xprojFile = app.Option("-x|--xproj-file", "The path to the xproj file to use. Required when there is more than one xproj in a project directory.", CommandOptionType.SingleValue);
|
CommandOption xprojFile = app.Option("-x|--xproj-file", "The path to the xproj file to use. Required when there is more than one xproj in a project directory.", CommandOptionType.SingleValue);
|
||||||
CommandOption skipProjectReferences = app.Option("-s|--skip-project-references", "Skip migrating project references. By default project references are migrated recursively", CommandOptionType.BoolValue);
|
CommandOption skipProjectReferences = app.Option("-s|--skip-project-references", "Skip migrating project references. By default project references are migrated recursively", CommandOptionType.BoolValue);
|
||||||
|
@ -30,7 +34,7 @@ namespace Microsoft.DotNet.Tools.Migrate
|
||||||
{
|
{
|
||||||
MigrateCommand migrateCommand = new MigrateCommand(
|
MigrateCommand migrateCommand = new MigrateCommand(
|
||||||
template.Value(),
|
template.Value(),
|
||||||
project.Value(),
|
projectArgument.Value,
|
||||||
sdkVersion.Value(),
|
sdkVersion.Value(),
|
||||||
xprojFile.Value(),
|
xprojFile.Value(),
|
||||||
skipProjectReferences.BoolValue.HasValue ? skipProjectReferences.BoolValue.Value : false);
|
skipProjectReferences.BoolValue.HasValue ? skipProjectReferences.BoolValue.Value : false);
|
||||||
|
|
|
@ -129,12 +129,7 @@ namespace Microsoft.DotNet.Migration.Tests
|
||||||
MigrateProject(Path.Combine(projectDirectory, projectName));
|
MigrateProject(Path.Combine(projectDirectory, projectName));
|
||||||
|
|
||||||
string[] migratedProjects = expectedProjects.Split(new char[] { ',' });
|
string[] migratedProjects = expectedProjects.Split(new char[] { ',' });
|
||||||
foreach(var migratedProject in migratedProjects)
|
VerifyMigration(migratedProjects, projectDirectory);
|
||||||
{
|
|
||||||
var dirInfo = new DirectoryInfo(Path.Combine(projectDirectory, migratedProject));
|
|
||||||
var csproj = $"{migratedProject}.csproj";
|
|
||||||
dirInfo.Should().HaveFile(csproj);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
|
@ -150,13 +145,36 @@ namespace Microsoft.DotNet.Migration.Tests
|
||||||
|
|
||||||
FixUpProjectJsons(projectDirectory);
|
FixUpProjectJsons(projectDirectory);
|
||||||
|
|
||||||
MigrateCommand.Run(new [] { "-p", Path.Combine(projectDirectory, projectName), "--skip-project-references" }).Should().Be(0);
|
MigrateCommand.Run(new [] { Path.Combine(projectDirectory, projectName), "--skip-project-references" }).Should().Be(0);
|
||||||
|
|
||||||
var migratedProjects = Directory.EnumerateFiles(projectDirectory, "*.csproj", SearchOption.AllDirectories);
|
VerifyMigration(Enumerable.Repeat(projectName, 1), projectDirectory);
|
||||||
migratedProjects.Count().Should().Be(1, "Only the root project must be migrated");
|
}
|
||||||
|
|
||||||
var migratedProject = Path.GetFileName(migratedProjects.First());
|
[Fact]
|
||||||
migratedProject.Should().Be($"{projectName}.csproj");
|
public void It_migrates_all_projects_in_given_directory()
|
||||||
|
{
|
||||||
|
var projectDirectory = TestAssetsManager.CreateTestInstance("TestAppDependencyGraph").Path;
|
||||||
|
|
||||||
|
FixUpProjectJsons(projectDirectory);
|
||||||
|
|
||||||
|
MigrateCommand.Run(new [] { projectDirectory, "--skip-project-references" }).Should().Be(0);
|
||||||
|
|
||||||
|
string[] migratedProjects = new string[] { "ProjectA", "ProjectB", "ProjectC", "ProjectD", "ProjectE" };
|
||||||
|
VerifyMigration(migratedProjects, projectDirectory);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void It_migrates_given_project_json()
|
||||||
|
{
|
||||||
|
var projectDirectory = TestAssetsManager.CreateTestInstance("TestAppDependencyGraph").Path;
|
||||||
|
|
||||||
|
FixUpProjectJsons(projectDirectory);
|
||||||
|
|
||||||
|
var project = Path.Combine(projectDirectory, "ProjectA", "project.json");
|
||||||
|
MigrateCommand.Run(new [] { project }).Should().Be(0);
|
||||||
|
|
||||||
|
string[] migratedProjects = new string[] { "ProjectA", "ProjectB", "ProjectC", "ProjectD", "ProjectE" };
|
||||||
|
VerifyMigration(migratedProjects, projectDirectory);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void FixUpProjectJsons(string projectDirectory)
|
private void FixUpProjectJsons(string projectDirectory)
|
||||||
|
@ -170,6 +188,13 @@ namespace Microsoft.DotNet.Migration.Tests
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void VerifyMigration(IEnumerable<string> expectedProjects, string rootDir)
|
||||||
|
{
|
||||||
|
var migratedProjects = Directory.EnumerateFiles(rootDir, "*.csproj", SearchOption.AllDirectories)
|
||||||
|
.Select(s => Path.GetFileNameWithoutExtension(s));
|
||||||
|
migratedProjects.Should().BeEquivalentTo(expectedProjects);
|
||||||
|
}
|
||||||
|
|
||||||
private MigratedBuildComparisonData GetDotnetNewComparisonData(string projectDirectory, string dotnetNewType)
|
private MigratedBuildComparisonData GetDotnetNewComparisonData(string projectDirectory, string dotnetNewType)
|
||||||
{
|
{
|
||||||
DotnetNew(projectDirectory, dotnetNewType);
|
DotnetNew(projectDirectory, dotnetNewType);
|
||||||
|
@ -241,7 +266,7 @@ namespace Microsoft.DotNet.Migration.Tests
|
||||||
private void MigrateProject(string projectDirectory)
|
private void MigrateProject(string projectDirectory)
|
||||||
{
|
{
|
||||||
var result =
|
var result =
|
||||||
MigrateCommand.Run(new [] { "-p", projectDirectory });
|
MigrateCommand.Run(new [] { projectDirectory });
|
||||||
|
|
||||||
result.Should().Be(0);
|
result.Should().Be(0);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue