2016-08-22 19:21:52 +00:00
|
|
|
|
// 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.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;
|
2016-08-22 19:21:52 +00:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Migrate
|
|
|
|
|
{
|
|
|
|
|
public partial class MigrateCommand
|
|
|
|
|
{
|
2016-08-23 20:50:05 +00:00
|
|
|
|
private readonly string _templateFile;
|
|
|
|
|
private readonly string _outputDirectory;
|
|
|
|
|
private readonly string _projectJson;
|
|
|
|
|
private readonly string _sdkVersion;
|
2016-09-22 00:27:02 +00:00
|
|
|
|
private readonly string _xprojFilePath;
|
2016-08-22 19:21:52 +00:00
|
|
|
|
|
2016-08-23 20:50:05 +00:00
|
|
|
|
private readonly TemporaryDotnetNewTemplateProject _temporaryDotnetNewProject;
|
2016-08-22 19:21:52 +00:00
|
|
|
|
|
2016-09-22 00:27:02 +00:00
|
|
|
|
public MigrateCommand(string templateFile, string outputDirectory, string projectJson, string sdkVersion, string xprojFilePath)
|
2016-08-22 19:21:52 +00:00
|
|
|
|
{
|
|
|
|
|
_templateFile = templateFile;
|
|
|
|
|
_outputDirectory = outputDirectory;
|
|
|
|
|
_projectJson = projectJson;
|
|
|
|
|
_sdkVersion = sdkVersion;
|
2016-09-22 00:27:02 +00:00
|
|
|
|
_xprojFilePath = xprojFilePath;
|
2016-08-22 19:21:52 +00:00
|
|
|
|
|
|
|
|
|
_temporaryDotnetNewProject = new TemporaryDotnetNewTemplateProject();
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-23 20:50:05 +00:00
|
|
|
|
public int Execute()
|
2016-08-22 19:21:52 +00:00
|
|
|
|
{
|
2016-08-23 20:50:05 +00:00
|
|
|
|
var project = GetProjectJsonPath(_projectJson) ?? GetProjectJsonPath(Directory.GetCurrentDirectory());
|
2016-08-22 19:21:52 +00:00
|
|
|
|
EnsureNotNull(project, "Unable to find project.json");
|
|
|
|
|
var projectDirectory = Path.GetDirectoryName(project);
|
|
|
|
|
|
2016-08-23 20:50:05 +00:00
|
|
|
|
var msBuildTemplate = _templateFile != null ?
|
|
|
|
|
ProjectRootElement.TryOpen(_templateFile) : _temporaryDotnetNewProject.MSBuildProject;
|
2016-08-22 19:21:52 +00:00
|
|
|
|
|
2016-08-23 20:50:05 +00:00
|
|
|
|
var outputDirectory = _outputDirectory ?? projectDirectory;
|
2016-08-22 19:21:52 +00:00
|
|
|
|
EnsureNotNull(outputDirectory, "Null output directory");
|
|
|
|
|
|
|
|
|
|
var sdkVersion = _sdkVersion ?? new ProjectJsonParser(_temporaryDotnetNewProject.ProjectJson).SdkPackageVersion;
|
|
|
|
|
EnsureNotNull(sdkVersion, "Null Sdk Version");
|
|
|
|
|
|
2016-09-22 00:27:02 +00:00
|
|
|
|
var migrationSettings = new MigrationSettings(projectDirectory, outputDirectory, sdkVersion, msBuildTemplate, _xprojFilePath);
|
2016-08-22 19:21:52 +00:00
|
|
|
|
new ProjectMigrator().Migrate(migrationSettings);
|
2016-08-23 20:50:05 +00:00
|
|
|
|
|
2016-08-22 19:21:52 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void EnsureNotNull(string variable, string message)
|
|
|
|
|
{
|
|
|
|
|
if (variable == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetProjectJsonPath(string projectJson)
|
|
|
|
|
{
|
|
|
|
|
if (projectJson == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-23 20:50:05 +00:00
|
|
|
|
projectJson = ProjectPathHelper.NormalizeProjectFilePath(projectJson);
|
|
|
|
|
|
2016-08-22 19:21:52 +00:00
|
|
|
|
if (File.Exists(projectJson))
|
|
|
|
|
{
|
|
|
|
|
return projectJson;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new Exception($"Unable to find project file at {projectJson}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|