Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli add-package: allow to use on solution file #8199

Merged
merged 1 commit into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -39,6 +40,7 @@ public virtual async Task ExecuteAsync(CommandLineArgs commandLineArgs)
var addSourceCodeToSolutionFile = withSourceCode && commandLineArgs.Options.ContainsKey("add-to-solution-file");

await ProjectNugetPackageAdder.AddAsync(
GetSolutionFile(commandLineArgs),
GetProjectFile(commandLineArgs),
commandLineArgs.Target,
version,
Expand Down Expand Up @@ -94,30 +96,24 @@ protected virtual string GetProjectFile(CommandLineArgs commandLineArgs)
return providedProjectFile;
}

var foundProjectFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.csproj");
if (foundProjectFiles.Length == 1)
{
return foundProjectFiles[0];
}

if (foundProjectFiles.Length == 0)
{
throw new CliUsageException("'abp add-package' command should be used inside a folder contaning a .csproj file!");
}

//foundProjectFiles.Length > 1
return Directory.GetFiles(Directory.GetCurrentDirectory(), "*.csproj").FirstOrDefault();
}

var sb = new StringBuilder("There are multiple project (.csproj) files in the current directory. Please specify one of the files below:");
protected virtual string GetSolutionFile(CommandLineArgs commandLineArgs)
{
var providedSolutionFile = PathHelper.NormalizePath(
commandLineArgs.Options.GetOrNull(
Options.Solution.Short,
Options.Solution.Long
)
);

foreach (var foundProjectFile in foundProjectFiles)
if (!providedSolutionFile.IsNullOrWhiteSpace())
{
sb.AppendLine("* " + foundProjectFile);
return providedSolutionFile;
}

sb.AppendLine("Example:");
sb.AppendLine($"abp add-package {commandLineArgs.Target} -p {foundProjectFiles[0]}");

throw new CliUsageException(sb.ToString());
return Directory.GetFiles(Directory.GetCurrentDirectory(), "*.sln").FirstOrDefault();
}

public static class Options
Expand All @@ -128,6 +124,12 @@ public static class Project
public const string Long = "project";
}

public static class Solution
{
public const string Short = "s";
public const string Long = "solution";
}

public static class Version
{
public const string Short = "v";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class ProjectNugetPackageAdder : ITransientDependency
}

public async Task AddAsync(
string solutionFile,
string projectFile,
string packageName,
string version = null,
Expand All @@ -66,6 +67,7 @@ public class ProjectNugetPackageAdder : ITransientDependency
bool addSourceCodeToSolutionFile = false)
{
await AddAsync(
solutionFile,
projectFile,
await FindNugetPackageInfoAsync(packageName),
version,
Expand All @@ -76,13 +78,31 @@ public class ProjectNugetPackageAdder : ITransientDependency
}

public async Task AddAsync(
string solutionFile,
string projectFile,
NugetPackageInfo package,
string version = null,
bool useDotnetCliToInstall = true,
bool withSourceCode = false,
bool addSourceCodeToSolutionFile = false)
{
if (projectFile == null)
{
if (solutionFile == null)
{
throw new CliUsageException("Couldn't find any project/solution.");
}

projectFile = GetProjectFile(solutionFile, package);

if (projectFile == null)
{
throw new CliUsageException("Couldn't find any project/solution.");
}
}

solutionFile ??= FindSolutionFile(projectFile);

if (version == null)
{
version = GetAbpVersionOrNull(projectFile);
Expand All @@ -92,17 +112,30 @@ public class ProjectNugetPackageAdder : ITransientDependency

if (withSourceCode)
{
await AddSourceCode(projectFile, package, version);
await ConvertPackageReferenceToProjectReference(projectFile, package);
await AddSourceCode(projectFile, solutionFile, package, version);
await ConvertPackageReferenceToProjectReference(projectFile, solutionFile, package);

if (addSourceCodeToSolutionFile)
{
await SolutionFileModifier.AddPackageToSolutionFileAsync(package, FindSolutionFile(projectFile));
await SolutionFileModifier.AddPackageToSolutionFileAsync(package, solutionFile);
}
}
}

private async Task ConvertPackageReferenceToProjectReference(string projectFile, NugetPackageInfo package)
private string GetProjectFile(string solutionFile, NugetPackageInfo package)
{
var projectFiles = Directory.GetFiles(Path.GetDirectoryName(solutionFile), "*.csproj", SearchOption.AllDirectories);
var isSolutionTiered = IsSolutionTiered(projectFiles);

var projectFile = ProjectFinder.FindNuGetTargetProjectFile(
projectFiles,
isSolutionTiered && package.TieredTarget != NuGetPackageTarget.Undefined
? package.TieredTarget
: package.Target);
return projectFile;
}

protected virtual async Task ConvertPackageReferenceToProjectReference(string projectFile,string solutionFile, NugetPackageInfo package)
{
var content = File.ReadAllText(projectFile);
var doc = new XmlDocument() {PreserveWhitespace = true};
Expand All @@ -117,7 +150,7 @@ private async Task ConvertPackageReferenceToProjectReference(string projectFile,
return;
}

var downloadedProjectPath = FindRelativeFolderToDownloadPackage(projectFile, package);
var downloadedProjectPath = FindRelativeFolderToDownloadPackage(projectFile, solutionFile, package);
var oldNodeIncludeValue = nodes[0]?.Attributes?["Include"]?.Value;

if (package.Name == oldNodeIncludeValue)
Expand All @@ -135,9 +168,9 @@ private async Task ConvertPackageReferenceToProjectReference(string projectFile,
File.WriteAllText(projectFile, doc.OuterXml);
}

private async Task AddSourceCode(string projectFile, NugetPackageInfo package, string version = null)
protected virtual async Task AddSourceCode(string projectFile, string solutionFile, NugetPackageInfo package, string version = null)
{
var targetFolder = FindFolderToDownloadPackage(projectFile, package);
var targetFolder = FindFolderToDownloadPackage(solutionFile, package);

if (Directory.Exists(targetFolder))
{
Expand All @@ -147,19 +180,19 @@ private async Task AddSourceCode(string projectFile, NugetPackageInfo package, s
await DownloadSourceCode(targetFolder, package, version);
}

private string FindFolderToDownloadPackage(string projectFile, NugetPackageInfo package)
protected virtual string FindFolderToDownloadPackage(string solutionFile, NugetPackageInfo package)
{
return Path.Combine(FindSolutionFolder(projectFile), "packages", package.Name);
return Path.Combine(Path.GetDirectoryName(solutionFile), "packages", package.Name);
}

private string FindRelativeFolderToDownloadPackage(string projectFile, NugetPackageInfo package)
protected virtual string FindRelativeFolderToDownloadPackage(string projectFile, string solutionFile, NugetPackageInfo package)
{
var folder = Path.Combine(FindSolutionFolder(projectFile), "packages", package.Name);
var folder = Path.Combine(Path.GetDirectoryName(solutionFile), "packages", package.Name);

return new Uri(projectFile).MakeRelativeUri(new Uri(folder)).ToString().Replace("/", "\\");
}

private async Task DownloadSourceCode(string targetFolder, NugetPackageInfo package, string version = null)
protected virtual async Task DownloadSourceCode(string targetFolder, NugetPackageInfo package, string version = null)
{
await SourceCodeDownloadService.DownloadPackageAsync(
package.Name,
Expand All @@ -168,14 +201,14 @@ private async Task DownloadSourceCode(string targetFolder, NugetPackageInfo pack
);
}

private string FindSolutionFile(string projectFile)
protected virtual string FindSolutionFile(string projectFile)
{
var folder = FindSolutionFolder(projectFile);

return Directory.GetFiles(folder, "*.sln", SearchOption.TopDirectoryOnly).FirstOrDefault();
}

private string FindSolutionFolder(string projectFile)
protected virtual string FindSolutionFolder(string projectFile)
{
var targetFolder = Path.GetDirectoryName(projectFile);

Expand All @@ -199,7 +232,7 @@ private string FindSolutionFolder(string projectFile)
return targetFolder;
}

private async Task AddAsPackageReference(string projectFile, NugetPackageInfo package, string version,
protected virtual async Task AddAsPackageReference(string projectFile, NugetPackageInfo package, string version,
bool useDotnetCliToInstall)
{
var projectFileContent = File.ReadAllText(projectFile);
Expand Down Expand Up @@ -250,7 +283,7 @@ private string FindSolutionFolder(string projectFile)
Logger.LogInformation("Successfully installed.");
}

private Task AddUsingDotnetCli(NugetPackageInfo package, string version = null)
protected virtual Task AddUsingDotnetCli(NugetPackageInfo package, string version = null)
{
var versionOption = version == null ? "" : $" -v {version}";

Expand All @@ -259,7 +292,7 @@ private Task AddUsingDotnetCli(NugetPackageInfo package, string version = null)
return Task.CompletedTask;
}

private Task AddToCsprojManuallyAsync(string projectFile, NugetPackageInfo package, string version = null)
protected virtual Task AddToCsprojManuallyAsync(string projectFile, NugetPackageInfo package, string version = null)
{
var projectFileContent = File.ReadAllText(projectFile);
var doc = new XmlDocument() {PreserveWhitespace = true};
Expand Down Expand Up @@ -301,7 +334,7 @@ private Task AddToCsprojManuallyAsync(string projectFile, NugetPackageInfo packa
return Task.CompletedTask;
}

private string GetAbpVersionOrNull(string projectFile)
protected virtual string GetAbpVersionOrNull(string projectFile)
{
var projectFileContent = File.ReadAllText(projectFile);

Expand Down Expand Up @@ -345,5 +378,13 @@ protected virtual async Task RunBundleForBlazorAsync(string projectFile)

await BundleCommand.ExecuteAsync(args);
}

protected virtual bool IsSolutionTiered(string[] projectFiles)
{
return projectFiles.Select(ProjectFileNameHelper.GetAssemblyNameFromProjectPath)
.Any(p => p.EndsWith(".HttpApi.Host"))
&& projectFiles.Select(ProjectFileNameHelper.GetAssemblyNameFromProjectPath)
.Any(p => p.EndsWith(".IdentityServer"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ private async Task DeleteRedundantHostProjects(string targetModuleFolder, string
continue;
}

await ProjectNugetPackageAdder.AddAsync(targetProjectFile, nugetPackage, null, useDotnetCliToInstall);
await ProjectNugetPackageAdder.AddAsync(null, targetProjectFile, nugetPackage, null, useDotnetCliToInstall);
}

var mvcNpmPackages = module.NpmPackages?.Where(p => p.ApplicationType.HasFlag(NpmApplicationType.Mvc))
Expand Down