Skip to content

Commit

Permalink
Upgrade command fixes (#120)
Browse files Browse the repository at this point in the history
* Remove double slash from nuget package API calls

* Allow for other projects to be upgraded if one or more are with old Sitefinity version

* Improve logic for SyncReferencesWithPackages;

* Add logic to remove references to non-existing files in nuget packages

* Use Path.Combine where approriate

* Improve logic for updating hint path; Update variable names;

* Fix bug with parsing assembly version

* Replaced string.format with string interpolation

* Bump version to 1.1.0.20

Co-authored-by: PROGRESS\sruzmano <[email protected]>
  • Loading branch information
yasen-yankov and RuzmanovDev authored Apr 12, 2021
1 parent de4021a commit 809251e
Show file tree
Hide file tree
Showing 10 changed files with 300 additions and 175 deletions.
43 changes: 13 additions & 30 deletions Sitefinity CLI/Commands/UpgradeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ protected virtual async Task ExecuteUpgrade()

await this.GeneratePowershellConfig(sitefinityProjectFilePaths, newSitefinityPackage);

var updaterPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, PowershellFolderName, "Updater.ps1");
var updaterPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Constants.SitefinityUpgradePowershellFolderName, "Updater.ps1");
this.visualStudioWorker.Initialize(this.SolutionPath);
this.visualStudioWorker.ExecuteScript(updaterPath);
this.EnsureOperationSuccess();
Expand Down Expand Up @@ -215,15 +215,16 @@ private string DetectSitefinityVersion(string sitefinityProjectPath)

private bool ContainsSitefinityRefKeyword(CsProjectFileReference projectReference)
{
return (projectReference.Include.Contains(TelerikSitefinityReferenceKeyWords) || projectReference.Include.Contains(ProgressSitefinityReferenceKeyWords)) && !projectReference.Include.Contains(ProgressSitefinityRendererReferenceKeyWords);
return (projectReference.Include.Contains(Constants.TelerikSitefinityReferenceKeyWords) || projectReference.Include.Contains(Constants.ProgressSitefinityReferenceKeyWords)) &&
!projectReference.Include.Contains(Constants.ProgressSitefinityRendererReferenceKeyWords);
}

private void EnsureOperationSuccess()
{
this.logger.LogInformation("Waiting for operation to complete...");

var resultFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, PowershellFolderName, "result.log");
var progressFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, PowershellFolderName, "progress.log");
var resultFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Constants.SitefinityUpgradePowershellFolderName, "result.log");
var progressFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Constants.SitefinityUpgradePowershellFolderName, "progress.log");
File.Delete(resultFile);
int waitStep = 500;
int iterations = 0;
Expand Down Expand Up @@ -280,8 +281,6 @@ private async Task GeneratePowershellConfig(IEnumerable<string> projectFilePaths
{
this.logger.LogInformation("Exporting powershell config...");

this.packagesPerProject = new Dictionary<string, List<NuGetPackage>>();

var powerShellXmlConfig = new XmlDocument();
var powerShellXmlConfigNode = powerShellXmlConfig.CreateElement("config");
powerShellXmlConfig.AppendChild(powerShellXmlConfigNode);
Expand All @@ -294,8 +293,6 @@ private async Task GeneratePowershellConfig(IEnumerable<string> projectFilePaths
projectNode.Attributes.Append(projectNameAttr);
powerShellXmlConfigNode.AppendChild(projectNode);

packagesPerProject[projectFilePath] = new List<NuGetPackage>();

var currentSitefinityVersion = this.DetectSitefinityVersion(projectFilePath);

if (string.IsNullOrEmpty(currentSitefinityVersion))
Expand All @@ -321,12 +318,10 @@ private async Task GeneratePowershellConfig(IEnumerable<string> projectFilePaths
packageNode.Attributes.Append(nameAttr);
packageNode.Attributes.Append(versionAttr);
projectNode.AppendChild(packageNode);

packagesPerProject[projectFilePath].Add(package);
});
}

powerShellXmlConfig.Save(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, PowershellFolderName, "config.xml"));
powerShellXmlConfig.Save(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Constants.SitefinityUpgradePowershellFolderName, "config.xml"));

this.logger.LogInformation("Successfully exported powershell config!");
}
Expand Down Expand Up @@ -385,9 +380,7 @@ private void SyncProjectReferencesWithPackages(IEnumerable<string> projectFilePa
{
foreach (string projectFilePath in projectFilePaths)
{
var packages = new List<NuGetPackage>(this.packagesPerProject[projectFilePath]);
packages.Reverse();
this.sitefinityPackageManager.SyncReferencesWithPackages(projectFilePath, solutionFolder, packages, this.Version);
this.sitefinityPackageManager.SyncReferencesWithPackages(projectFilePath, solutionFolder);
}
}

Expand All @@ -405,13 +398,13 @@ private IList<string> GetProjectsPathsFromSolution(string solutionPath, bool onl
if (onlySitefinityProjects)
{
projectFilesAbsolutePaths = projectFilesAbsolutePaths
.Where(ap => this.HasSitefinityReferences(ap) && this.ValidateSfVersion(ap));
.Where(ap => this.HasSitefinityReferences(ap) && this.HasValidSitefinityVersion(ap));
}

return projectFilesAbsolutePaths.ToList();
}

private bool ValidateSfVersion(string projectFilePath)
private bool HasValidSitefinityVersion(string projectFilePath)
{
var currentSfVersionString = this.DetectSitefinityVersion(projectFilePath);
var currentVersion = System.Version.Parse(currentSfVersionString);
Expand All @@ -424,7 +417,9 @@ private bool ValidateSfVersion(string projectFilePath)
var projectName = Path.GetFileName(projectFilePath);
if (versionToUpgrade <= currentVersion)
{
throw new UpgradeException(string.Format(Constants.VersionIsGreaterThanOrEqual, projectName, currentSfVersionString, versionToUpgrade));
this.logger.LogWarning(string.Format(Constants.VersionIsGreaterThanOrEqual, projectName, currentSfVersionString, versionToUpgrade));

return false;
}

return true;
Expand All @@ -440,7 +435,7 @@ private bool HasSitefinityReferences(string projectFilePath)

private bool IsSitefinityReference(CsProjectFileReference reference)
{
return this.ContainsSitefinityRefKeyword(reference) && reference.Include.Contains($"PublicKeyToken={SitefinityPublicKeyToken}");
return this.ContainsSitefinityRefKeyword(reference) && reference.Include.Contains($"PublicKeyToken={Constants.SitefinityPublicKeyToken}");
}

private readonly IPromptService promptService;
Expand All @@ -456,17 +451,5 @@ private bool IsSitefinityReference(CsProjectFileReference reference)
private readonly IVisualStudioWorker visualStudioWorker;

private readonly IDictionary<string, HashSet<string>> processedPackagesPerProjectCache;

private Dictionary<string, List<NuGetPackage>> packagesPerProject;

private const string TelerikSitefinityReferenceKeyWords = "Telerik.Sitefinity";

private const string ProgressSitefinityReferenceKeyWords = "Progress.Sitefinity";

private const string ProgressSitefinityRendererReferenceKeyWords = "Progress.Sitefinity.Renderer";

private const string PowershellFolderName = "PowerShell";

private const string SitefinityPublicKeyToken = "b28c218413bdf563";
}
}
7 changes: 6 additions & 1 deletion Sitefinity CLI/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class Constants
public const string FileIsNotSolutionMessage = "File \"{0}\" is not a sln file";
public const string ErrorOccuredWhileCreatingItemFromTemplate = "An error occured while creating an item from template. Path: {0}";
public const string VersionNotFound = "Version: {0} was not found in https://nuget.sitefinity.com";
public const string VersionIsGreaterThanOrEqual = "{0} Sitefinity version ({1}) is >= than the version you are trying to upgrade to ({2})!";
public const string VersionIsGreaterThanOrEqual = "{0} Sitefinity version ({1}) is >= than the version you are trying to upgrade to ({2})";

// Warning messages
public const string EnterResourcePackagePromptMessage = "Enter the name of the resource package where the resource should be added:";
Expand Down Expand Up @@ -161,5 +161,10 @@ public class Constants
public const string DependenciesElem = "Dependencies";
public const string VersionElem = "Version";
public const string TitleElem = "title";
public const string TelerikSitefinityReferenceKeyWords = "Telerik.Sitefinity";
public const string ProgressSitefinityReferenceKeyWords = "Progress.Sitefinity";
public const string ProgressSitefinityRendererReferenceKeyWords = "Progress.Sitefinity.Renderer";
public const string SitefinityUpgradePowershellFolderName = "PowerShell";
public const string SitefinityPublicKeyToken = "b28c218413bdf563";
}
}
15 changes: 15 additions & 0 deletions Sitefinity CLI/PackageManagement/AssemblyReference.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace Sitefinity_CLI.PackageManagement
{
internal class AssemblyReference
{
public string Name { get; set; }

public string FullName { get; set; }

public Version Version { get; set; }

public string HintPath { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
namespace Sitefinity_CLI.PackageManagement
using System.Collections.Generic;

namespace Sitefinity_CLI.PackageManagement
{
internal interface IPackagesConfigFileEditor
{
IEnumerable<NuGetPackage> GetPackages(string packagesConfigFilePath);

NuGetPackage FindPackage(string packagesConfigFilePath, string packageId);

void RemovePackage(string packagesConfigFilePath, string packageId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal interface ISitefinityPackageManager

Task<NuGetPackage> GetSitefinityPackageTree(string version, IEnumerable<string> packageSources);

void SyncReferencesWithPackages(string projectPath, string solutionFolder, IEnumerable<NuGetPackage> packages, string sitefinityVersion);
void SyncReferencesWithPackages(string projectFilePath, string solutionFolder);

IEnumerable<string> DefaultPackageSource { get; }

Expand Down
5 changes: 2 additions & 3 deletions Sitefinity CLI/PackageManagement/NuGetApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
using System.Linq;
using System.Text.RegularExpressions;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;

namespace Sitefinity_CLI.PackageManagement
Expand Down Expand Up @@ -114,7 +112,8 @@ private async Task<XDocument> GetPackageXmlDocument(string id, string version, I
HttpResponseMessage response = null;
foreach (string source in sources)
{
response = await this.httpClient.GetAsync($"{source}/Packages(Id='{id}',Version='{version}')");
string sourceUrl = source.TrimEnd('/');
response = await this.httpClient.GetAsync($"{sourceUrl}/Packages(Id='{id}',Version='{version}')");
if (response.StatusCode == HttpStatusCode.OK)
{
break;
Expand Down
40 changes: 31 additions & 9 deletions Sitefinity CLI/PackageManagement/PackagesConfigFileEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,35 @@ namespace Sitefinity_CLI.PackageManagement
{
internal class PackagesConfigFileEditor : XmlFileEditorBase, IPackagesConfigFileEditor
{
public IEnumerable<NuGetPackage> GetPackages(string packagesConfigFilePath)
{
IEnumerable<NuGetPackage> nuGetPackages = null;

base.ReadFile(packagesConfigFilePath, (doc) =>
{
IEnumerable<XElement> xmlPackageElements = doc.Element(Constants.PackagesElem)
.Elements(Constants.PackageElem);

nuGetPackages = xmlPackageElements.Select(xpe => this.CreateNuGetPackageFromXmlPackageElement(xpe));
});

return nuGetPackages;
}

public NuGetPackage FindPackage(string packagesConfigFilePath, string packageId)
{
NuGetPackage nuGetPackage = null;

base.ReadFile(packagesConfigFilePath, (doc) =>
{
IEnumerable<XElement> packages = doc.Element(Constants.PackagesElem)
IEnumerable<XElement> xmlPackageElements = doc.Element(Constants.PackagesElem)
.Elements(Constants.PackageElem);

XElement package = packages.FirstOrDefault(p => p.Attribute(Constants.IdAttribute).Value == packageId);
XElement xmlPackageElement = xmlPackageElements.FirstOrDefault(p => p.Attribute(Constants.IdAttribute).Value == packageId);

if (package != null)
if (xmlPackageElement != null)
{
nuGetPackage = new NuGetPackage();
nuGetPackage.Id = package.Attribute(Constants.IdAttribute).Value;
nuGetPackage.Version = package.Attribute(Constants.VersionAttribute).Value;
nuGetPackage = this.CreateNuGetPackageFromXmlPackageElement(xmlPackageElement);
}
});

Expand All @@ -32,14 +45,23 @@ public void RemovePackage(string packagesConfigFilePath, string packageId)
{
base.ModifyFile(packagesConfigFilePath, (doc) =>
{
IEnumerable<XElement> packages = doc.Element(Constants.PackagesElem)
IEnumerable<XElement> xmlPackageElements = doc.Element(Constants.PackagesElem)
.Elements(Constants.PackageElem);

XElement packageToRemove = packages.FirstOrDefault(p => p.Attribute(Constants.IdAttribute).Value == packageId);
packageToRemove.Remove();
XElement xmlPackageElementToRemove = xmlPackageElements.FirstOrDefault(p => p.Attribute(Constants.IdAttribute).Value == packageId);
xmlPackageElementToRemove.Remove();

return doc;
});
}

private NuGetPackage CreateNuGetPackageFromXmlPackageElement(XElement xmlPackageElement)
{
NuGetPackage nuGetPackage = new NuGetPackage();
nuGetPackage.Id = xmlPackageElement.Attribute(Constants.IdAttribute).Value;
nuGetPackage.Version = xmlPackageElement.Attribute(Constants.VersionAttribute).Value;

return nuGetPackage;
}
}
}
Loading

0 comments on commit 809251e

Please sign in to comment.