From a6a3b211461004d0f03488102c27c90d748e9df4 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 17 Apr 2023 11:01:19 +0300 Subject: [PATCH 1/6] Created SwitchToLocal (Initial) --- .../Volo/Abp/Cli/AbpCliCoreModule.cs | 1 + .../Abp/Cli/Commands/SwitchToLocalCommand.cs | 113 ++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchToLocalCommand.cs diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs index 53d23cb508..d1736ee8c1 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs @@ -56,6 +56,7 @@ public class AbpCliCoreModule : AbpModule options.Commands[SwitchToPreviewCommand.Name] = typeof(SwitchToPreviewCommand); options.Commands[SwitchToStableCommand.Name] = typeof(SwitchToStableCommand); options.Commands[SwitchToNightlyCommand.Name] = typeof(SwitchToNightlyCommand); + options.Commands[SwitchToLocal.Name] = typeof(SwitchToLocal); options.Commands[TranslateCommand.Name] = typeof(TranslateCommand); options.Commands[BuildCommand.Name] = typeof(BuildCommand); options.Commands[BundleCommand.Name] = typeof(BundleCommand); diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchToLocalCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchToLocalCommand.cs new file mode 100644 index 0000000000..7f49fa763d --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchToLocalCommand.cs @@ -0,0 +1,113 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Volo.Abp.Cli.Args; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Cli.Commands; + +public class SwitchToLocal : IConsoleCommand, ITransientDependency +{ + public const string Name = "switch-to-local"; + + public ILogger Logger { get; set; } + + public async Task ExecuteAsync(CommandLineArgs commandLineArgs) + { + var workingDirectory = GetWorkingDirectory(commandLineArgs) ?? Directory.GetCurrentDirectory(); + + if (!Directory.Exists(workingDirectory)) + { + throw new CliUsageException( + "Specified directory does not exist." + + Environment.NewLine + Environment.NewLine + + GetUsageInfo() + ); + } + + var paths = GetPaths(commandLineArgs); + + } + + private List GetPaths(CommandLineArgs commandLineArgs) + { + var paths = commandLineArgs.Options.GetOrNull( + Options.LocalPaths.Short, + Options.LocalPaths.Long + ); + + if (paths == null) + { + throw new CliUsageException( + "Local paths are not specified!" + + Environment.NewLine + Environment.NewLine + + GetUsageInfo() + ); + } + + return paths.Split("|").Select(x=> x.Trim()).ToList(); + } + + private string GetWorkingDirectory(CommandLineArgs commandLineArgs) + { + var path = commandLineArgs.Options.GetOrNull( + Options.SolutionPath.Short, + Options.SolutionPath.Long + ); + + if (path == null) + { + return null; + } + + if (path.EndsWith(".sln") || path.EndsWith(".csproj")) + { + return Path.GetDirectoryName(path); + } + + return path; + } + + public string GetShortDescription() + { + return "Bundles all third party styles and scripts required by modules and updates index.html file."; + } + + public string GetUsageInfo() + { + var sb = new StringBuilder(); + + sb.AppendLine(""); + sb.AppendLine("Usage:"); + sb.AppendLine(""); + sb.AppendLine(" abp switch-to-local [options]"); + sb.AppendLine(""); + sb.AppendLine("Options:"); + sb.AppendLine(""); + sb.AppendLine("-s|--solution (default: current directory)"); + sb.AppendLine("-p | --paths (Required)"); + sb.AppendLine(""); + sb.AppendLine("See the documentation for more info: https://docs.abp.io/en/abp/latest/CLI"); + + return sb.ToString(); + } + + public static class Options + { + public static class SolutionPath + { + public const string Short = "s"; + public const string Long = "solution"; + } + + public static class LocalPaths + { + public const string Short = "p"; + public const string Long = "paths"; + } + } +} From 481d679132e9cefddd618964445c982829a73f9f Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 17 Apr 2023 11:30:01 +0300 Subject: [PATCH 2/6] Created LocalReferenceConverter --- .../Abp/Cli/Commands/SwitchToLocalCommand.cs | 23 +++- .../LocalReferenceConverter.cs | 109 ++++++++++++++++++ 2 files changed, 127 insertions(+), 5 deletions(-) create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/LocalReferenceConverter.cs diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchToLocalCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchToLocalCommand.cs index 7f49fa763d..32a6e4b063 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchToLocalCommand.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchToLocalCommand.cs @@ -6,16 +6,23 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Volo.Abp.Cli.Args; +using Volo.Abp.Cli.ProjectModification; using Volo.Abp.DependencyInjection; namespace Volo.Abp.Cli.Commands; public class SwitchToLocal : IConsoleCommand, ITransientDependency { + private readonly LocalReferenceConverter _localReferenceConverter; public const string Name = "switch-to-local"; public ILogger Logger { get; set; } + public SwitchToLocal(LocalReferenceConverter localReferenceConverter) + { + _localReferenceConverter = localReferenceConverter; + } + public async Task ExecuteAsync(CommandLineArgs commandLineArgs) { var workingDirectory = GetWorkingDirectory(commandLineArgs) ?? Directory.GetCurrentDirectory(); @@ -29,8 +36,8 @@ public class SwitchToLocal : IConsoleCommand, ITransientDependency ); } - var paths = GetPaths(commandLineArgs); - + await _localReferenceConverter.ConvertAsync(workingDirectory, GetPaths(commandLineArgs)); + } private List GetPaths(CommandLineArgs commandLineArgs) @@ -74,7 +81,8 @@ public class SwitchToLocal : IConsoleCommand, ITransientDependency public string GetShortDescription() { - return "Bundles all third party styles and scripts required by modules and updates index.html file."; + return "Changes all NuGet package references to local project references for all the .csproj files in the specified folder" + + " (and all its subfolders with any deep)"; } public string GetUsageInfo() @@ -88,9 +96,14 @@ public class SwitchToLocal : IConsoleCommand, ITransientDependency sb.AppendLine(""); sb.AppendLine("Options:"); sb.AppendLine(""); - sb.AppendLine("-s|--solution (default: current directory)"); - sb.AppendLine("-p | --paths (Required)"); + sb.AppendLine("-s |--solution (default: current directory)"); + sb.AppendLine("-p | --paths (Required)"); + sb.AppendLine(""); + sb.AppendLine("Examples:"); sb.AppendLine(""); + sb.AppendLine(" abp switch-to-local --paths D:\\Github\\abp"); + sb.AppendLine(" abp switch-to-local --paths D:\\Github\\abp --solution D:\\test\\MyProject"); + sb.AppendLine(" abp switch-to-local --paths \"D:\\Github\\abp|D:\\Github\\volo\""); sb.AppendLine("See the documentation for more info: https://docs.abp.io/en/abp/latest/CLI"); return sb.ToString(); diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/LocalReferenceConverter.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/LocalReferenceConverter.cs new file mode 100644 index 0000000000..b76ac9ef5f --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/LocalReferenceConverter.cs @@ -0,0 +1,109 @@ +using System.Collections.Generic; +using System.IO; +using System.Threading.Tasks; +using System.Xml; +using System.Xml.Linq; +using JetBrains.Annotations; +using Microsoft.Extensions.Logging; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Cli.ProjectModification; + +public class LocalReferenceConverter : ITransientDependency +{ + + public ILogger Logger { get; set; } + + public async Task ConvertAsync( + [NotNull] string directory, + [NotNull] List localPaths) + { + Check.NotNull(directory, nameof(directory)); + Check.NotNull(localPaths, nameof(localPaths)); + + var localProjects = GetLocalProjects(localPaths); + var targetProjects = Directory.GetFiles(directory, "*.csproj", SearchOption.AllDirectories); + + Logger.LogInformation($"Converting projects to local reference."); + + foreach (var targetProject in targetProjects) + { + Logger.LogInformation($"Converting to local reference: {targetProject}"); + + await ConvertProjectToLocalReferences(targetProject, localProjects); + } + + Logger.LogInformation($"Converted {targetProjects.Length} projects to local references."); + } + + private async Task ConvertProjectToLocalReferences(string targetProject, LocalProjectList localProjects) + { + var xmlDocument = new XmlDocument() { PreserveWhitespace = true }; + xmlDocument.Load(GenerateStreamFromString(File.ReadAllText(targetProject))); + + var matchedNodes = xmlDocument.SelectNodes($"/Project/ItemGroup/PackageReference[@Include]"); + + if (matchedNodes == null || matchedNodes.Count == 0) + { + return; + } + + foreach (XmlNode matchedNode in matchedNodes) + { + var packageName = matchedNode!.Attributes!["Include"].Value; + + var localProject = localProjects.GetByName(packageName); + + if (localProject == null) + { + continue; + } + + var parentNode = matchedNode.ParentNode; + parentNode!.RemoveChild(matchedNode); + + var newNode = xmlDocument.CreateElement("ProjectReference"); + var includeAttr = xmlDocument.CreateAttribute("Include"); + includeAttr.Value = localProject; + newNode.Attributes.Append(includeAttr); + parentNode.AppendChild(newNode); + } + + File.WriteAllText(targetProject, XDocument.Parse(xmlDocument.OuterXml).ToString()); + } + + private LocalProjectList GetLocalProjects(List localPaths) + { + var list = new LocalProjectList(); + + foreach (var localPath in localPaths) + { + if (!Directory.Exists(localPath)) + { + continue; + } + + list.AddRange(Directory.GetFiles(localPath, "*.csproj", SearchOption.AllDirectories)); + } + + return list; + } + + private MemoryStream GenerateStreamFromString(string s) + { + var stream = new MemoryStream(); + var writer = new StreamWriter(stream); + writer.Write(s); + writer.Flush(); + stream.Position = 0; + return stream; + } + + private class LocalProjectList : List + { + public string GetByName(string projectName) + { + return Find(x => x.EndsWith($"{projectName}.csproj") || x.EndsWith(projectName)); + } + } +} From f1a7618889a97ebd5ef755589a6a3f1941454234 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 17 Apr 2023 11:39:41 +0300 Subject: [PATCH 3/6] Add documentation for switch-to-local --- docs/en/CLI.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/en/CLI.md b/docs/en/CLI.md index 49b5ca830b..ced482f743 100644 --- a/docs/en/CLI.md +++ b/docs/en/CLI.md @@ -41,6 +41,7 @@ Here, is the list of all available commands before explaining their details: * **`switch-to-preview`**: Switches to the latest preview version of the ABP Framework. * **`switch-to-nightly`**: Switches to the latest [nightly builds](Nightly-Builds.md) of the ABP related packages on a solution. * **`switch-to-stable`**: Switches to the latest stable versions of the ABP related packages on a solution. +* **`switch-to-local`**: Changes NuGet package references on a solution to local project references. * **`translate`**: Simplifies to translate localization files when you have multiple JSON [localization](Localization.md) files in a source control repository. * **`login`**: Authenticates on your computer with your [abp.io](https://abp.io/) username and password. * **`login-info`**: Shows the current user's login information. @@ -453,6 +454,20 @@ abp switch-to-stable [options] * `--solution-directory` or `-sd`: Specifies the directory. The solution should be in that directory or in any of its sub directories. If not specified, default is the current directory. +### switch-to-local + +Changes all NuGet package references to local project references for all the .csproj files in the specified folder (and all its subfolders with any deep). It is not limited to ABP Framework or Module packages. + +Usage: + +````bash +abp switch-to-local [options] +```` +#### Options + +* `--solution` or `-s`: Specifies the solution directory. The solution should be in that directory or in any of its sub directories. If not specified, default is the current directory. +* `--paths` or `-p`: Specifies the local paths that the projects are inside. + ### translate Simplifies to translate [localization](Localization.md) files when you have multiple JSON [localization](Localization.md) files in a source control repository. From 486cd77247668ddfe949e01e237fbafa1f059f99 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 17 Apr 2023 11:48:04 +0300 Subject: [PATCH 4/6] Update SwitchToLocalCommand.cs --- .../Volo/Abp/Cli/Commands/SwitchToLocalCommand.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchToLocalCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchToLocalCommand.cs index 32a6e4b063..1d57110a93 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchToLocalCommand.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchToLocalCommand.cs @@ -37,7 +37,6 @@ public class SwitchToLocal : IConsoleCommand, ITransientDependency } await _localReferenceConverter.ConvertAsync(workingDirectory, GetPaths(commandLineArgs)); - } private List GetPaths(CommandLineArgs commandLineArgs) From 4db845309ebf4d57c1be61381c15653e6496f1c0 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 17 Apr 2023 11:48:26 +0300 Subject: [PATCH 5/6] Use List instead of custom LocalProjectList --- .../LocalReferenceConverter.cs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/LocalReferenceConverter.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/LocalReferenceConverter.cs index b76ac9ef5f..a326fe064d 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/LocalReferenceConverter.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/LocalReferenceConverter.cs @@ -36,7 +36,7 @@ public class LocalReferenceConverter : ITransientDependency Logger.LogInformation($"Converted {targetProjects.Length} projects to local references."); } - private async Task ConvertProjectToLocalReferences(string targetProject, LocalProjectList localProjects) + private async Task ConvertProjectToLocalReferences(string targetProject, List localProjects) { var xmlDocument = new XmlDocument() { PreserveWhitespace = true }; xmlDocument.Load(GenerateStreamFromString(File.ReadAllText(targetProject))); @@ -52,7 +52,10 @@ public class LocalReferenceConverter : ITransientDependency { var packageName = matchedNode!.Attributes!["Include"].Value; - var localProject = localProjects.GetByName(packageName); + var localProject = localProjects.Find(x => + x.EndsWith($"\\{packageName}.csproj") || + x.EndsWith($"/{packageName}.csproj") + ); if (localProject == null) { @@ -72,9 +75,9 @@ public class LocalReferenceConverter : ITransientDependency File.WriteAllText(targetProject, XDocument.Parse(xmlDocument.OuterXml).ToString()); } - private LocalProjectList GetLocalProjects(List localPaths) + private List GetLocalProjects(List localPaths) { - var list = new LocalProjectList(); + var list = new List(); foreach (var localPath in localPaths) { @@ -98,12 +101,4 @@ public class LocalReferenceConverter : ITransientDependency stream.Position = 0; return stream; } - - private class LocalProjectList : List - { - public string GetByName(string projectName) - { - return Find(x => x.EndsWith($"{projectName}.csproj") || x.EndsWith(projectName)); - } - } } From 69110a4fd8cc7ea9bc8932c690ef60b8c7f68ddf Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 17 Apr 2023 11:53:00 +0300 Subject: [PATCH 6/6] CalculateRelativePath in LocalReferenceConverter --- .../Cli/ProjectModification/LocalReferenceConverter.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/LocalReferenceConverter.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/LocalReferenceConverter.cs index a326fe064d..67debab2eb 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/LocalReferenceConverter.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/LocalReferenceConverter.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using System.Xml; @@ -67,13 +68,18 @@ public class LocalReferenceConverter : ITransientDependency var newNode = xmlDocument.CreateElement("ProjectReference"); var includeAttr = xmlDocument.CreateAttribute("Include"); - includeAttr.Value = localProject; + includeAttr.Value = CalculateRelativePath(targetProject, localProject); newNode.Attributes.Append(includeAttr); parentNode.AppendChild(newNode); } File.WriteAllText(targetProject, XDocument.Parse(xmlDocument.OuterXml).ToString()); } + + private string CalculateRelativePath(string targetProject, string localProject) + { + return new Uri(targetProject).MakeRelativeUri(new Uri(localProject)).ToString(); + } private List GetLocalProjects(List localPaths) {