dotnet-installer/src/dotnet/commands/dotnet-migrate/MigrateCommand.cs

98 lines
3.6 KiB
C#
Raw Normal View History

// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Build.Construction;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.ProjectJsonMigration;
2016-08-23 20:50:05 +00:00
using Microsoft.DotNet.ProjectModel;
namespace Microsoft.DotNet.Tools.Migrate
{
public partial class MigrateCommand
{
2016-08-23 20:50:05 +00:00
private readonly string _templateFile;
private readonly string _projectArg;
2016-08-23 20:50:05 +00:00
private readonly string _sdkVersion;
2016-09-22 00:27:02 +00:00
private readonly string _xprojFilePath;
private readonly bool _skipProjectReferences;
2016-08-23 20:50:05 +00:00
private readonly TemporaryDotnetNewTemplateProject _temporaryDotnetNewProject;
public MigrateCommand(string templateFile, string projectArg, string sdkVersion, string xprojFilePath, bool skipProjectReferences)
{
_templateFile = templateFile;
_projectArg = projectArg ?? Directory.GetCurrentDirectory();
_sdkVersion = sdkVersion;
2016-09-22 00:27:02 +00:00
_xprojFilePath = xprojFilePath;
_skipProjectReferences = skipProjectReferences;
_temporaryDotnetNewProject = new TemporaryDotnetNewTemplateProject();
}
2016-08-23 20:50:05 +00:00
public int Execute()
{
var projectsToMigrate = GetProjectsToMigrate(_projectArg);
2016-08-23 20:50:05 +00:00
var msBuildTemplate = _templateFile != null ?
ProjectRootElement.TryOpen(_templateFile) : _temporaryDotnetNewProject.MSBuildProject;
var sdkVersion = _sdkVersion ?? new ProjectJsonParser(_temporaryDotnetNewProject.ProjectJson).SdkPackageVersion;
EnsureNotNull(sdkVersion, "Null Sdk Version");
foreach (var project in projectsToMigrate)
{
2016-09-26 22:30:51 +00:00
Console.WriteLine($"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);
}
2016-08-23 20:50:05 +00:00
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
{
2016-09-26 22:30:51 +00:00
throw new Exception($"Invalid project argument - '{projectArg}' is not a project.json file and a directory named '{projectArg}' doesn't exist.");
}
}
private void EnsureNotNull(string variable, string message)
{
if (variable == null)
{
throw new Exception(message);
}
}
private string GetProjectJsonPath(string projectJson)
{
2016-08-23 20:50:05 +00:00
projectJson = ProjectPathHelper.NormalizeProjectFilePath(projectJson);
if (File.Exists(projectJson))
{
return projectJson;
}
throw new Exception($"Unable to find project file at {projectJson}");
}
}
}