Skip to content

Commit

Permalink
Updated CoreToolsDownloader to use NuGet v3
Browse files Browse the repository at this point in the history
  • Loading branch information
BetimBeja committed Apr 12, 2021
1 parent c8d932e commit b9c7656
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 30 deletions.
1 change: 1 addition & 0 deletions AlbanianXrm.EarlyBound/AlbanianXrm.EarlyBound.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<PackageReference Include="EnvDTE" Version="8.0.2" />
<PackageReference Include="ILMerge" Version="3.0.41" />
<PackageReference Include="ILMerge.MSBuild.Task" Version="1.0.7" />
<PackageReference Include="NuGet.Protocol" Version="5.9.0" />
<PackageReference Include="Syncfusion.Tools.Windows" Version="17.2.0.34" />
<PackageReference Include="Syncfusion.SfDataGrid.WinForms" Version="17.2.0.34" />
<PackageReference Include="XrmToolBoxPackage" Version="1.2020.11.42" Link="Test" />
Expand Down
15 changes: 15 additions & 0 deletions AlbanianXrm.EarlyBound/Helpers/ProgressReport.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AlbanianXrm.EarlyBound.Helpers
{
public class ProgressReport
{
public Exception Error { get; set; }

public object Result { get; set; }
}
}
8 changes: 7 additions & 1 deletion AlbanianXrm.EarlyBound/ILMergeConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
"OutputFile": "$(TargetDir)XrmToolBox\\Plugins\\$(TargetFileName)",
"InputAssemblies": [
"AlbanianXrm.ForrestSerializer.dll",
"NuGet.Common.dll",
"NuGet.Configuration.dll",
"NuGet.Frameworks.dll",
"NuGet.Packaging.dll",
"NuGet.Protocol.dll",
"NuGet.Versioning.dll",
"EnvDTE.dll",
"stdole.dll",
"Syncfusion.Core.WinForms.dll",
Expand All @@ -25,6 +31,6 @@
"Advanced": {
"CopyAttributes": false,
"AttributeFile": "$(TargetDir)AlbanianXrm.EarlyBound.dll",
"DeleteCopiesOverwriteTarget": true
"DeleteCopiesOverwriteTarget": true
}
}
114 changes: 85 additions & 29 deletions AlbanianXrm.EarlyBound/Logic/CoreToolsDownloader.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
using AlbanianXrm.EarlyBound.Helpers;
using AlbanianXrm.EarlyBound.Properties;
using NuGet.Common;
using NuGet.Packaging;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using XrmToolBox.Extensibility;

Expand All @@ -11,6 +17,7 @@ namespace AlbanianXrm.EarlyBound.Logic
internal class CoreToolsDownloader
{
private readonly MyPluginControl myPlugin;
private Task asyncTask;

public CoreToolsDownloader(MyPluginControl myPlugin)
{
Expand All @@ -19,36 +26,12 @@ public CoreToolsDownloader(MyPluginControl myPlugin)

public void DownloadCoreTools()
{
myPlugin.StartWorkAsync(new WorkAsyncInfo
if (asyncTask != null)
{
Message = Resources.DOWNLOADING_CORE_TOOLS,
Work = (worker, args) =>
{
//ID of the package to be looked
string coreToolsId = "Microsoft.CrmSdk.CoreTools";

//Connect to the official package repository IPackageRepository
var repo = NuGet.PackageRepositoryFactory.Default.CreateRepository(myPlugin.options.NuGetFeed);

string dir = Path.GetDirectoryName(typeof(MyPluginControl).Assembly.Location).ToUpperInvariant();
string folder = Path.GetFileNameWithoutExtension(typeof(MyPluginControl).Assembly.Location);
dir = Path.Combine(dir, folder);
Directory.CreateDirectory(dir);

var coreToolsPackage = repo.GetPackages().Where(x => x.Id == coreToolsId && x.IsLatestVersion)
.OrderByDescending(x => x.Version).FirstOrDefault();
if (coreToolsPackage == null)
{
args.Result = string.Format(Resources.Culture, Resources.CORE_TOOLS_NOT_FOUND, coreToolsId, myPlugin.options.NuGetFeed);
return;
}
foreach (var file in coreToolsPackage.GetFiles())
{
using (var stream = File.Create(Path.Combine(dir, Path.GetFileName(file.Path))))
file.GetStream().CopyTo(stream);
}
},
PostWorkCallBack = (args) =>
return;
}
Progress<ProgressReport> progress = new Progress<ProgressReport>(
(args) =>
{
try
{
Expand All @@ -70,9 +53,82 @@ public void DownloadCoreTools()
finally
{
myPlugin.WorkAsyncEnded();
asyncTask = null;
}
}
);
myPlugin.StartWorkAsync(new WorkAsyncInfo()
{
Message = Resources.DOWNLOADING_CORE_TOOLS,
Work = (worker, args) => { asyncTask = Task.Factory.StartNew(async () => await DownloadCoreToolsAsync(progress)); }
});


}

public async Task DownloadCoreToolsAsync(IProgress<ProgressReport> progress)
{
try
{
//ID of the package to be looked
string coreToolsId = "Microsoft.CrmSdk.CoreTools";

string dir = Path.GetDirectoryName(typeof(MyPluginControl).Assembly.Location).ToUpperInvariant();
string folder = Path.GetFileNameWithoutExtension(typeof(MyPluginControl).Assembly.Location);
dir = Path.Combine(dir, folder);
Directory.CreateDirectory(dir);

//Connect to the official package repository IPackageRepository
ILogger logger = NullLogger.Instance;
CancellationToken cancellationToken = CancellationToken.None;
SourceCacheContext cache = new SourceCacheContext();

SourceRepository repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
PackageSearchResource packageSearch = await repository.GetResourceAsync<PackageSearchResource>();
FindPackageByIdResource findPackageById = await repository.GetResourceAsync<FindPackageByIdResource>();

var metadata = (await packageSearch.SearchAsync(coreToolsId, new SearchFilter(false, SearchFilterType.IsLatestVersion), 0, 1, logger, cancellationToken)).FirstOrDefault();
var version = (await metadata.GetVersionsAsync()).Max(v => v.Version);
System.Diagnostics.Debug.WriteLine($"Version {version}");
using (MemoryStream packageStream = new MemoryStream())
{
if (!await findPackageById.CopyNupkgToStreamAsync(
coreToolsId,
version,
packageStream,
cache,
logger,
cancellationToken))
{
progress.Report(new ProgressReport() { Result = string.Format(Resources.Culture, Resources.CORE_TOOLS_NOT_FOUND, coreToolsId, myPlugin.options.NuGetFeed) });
return;
}



using (PackageArchiveReader packageReader = new PackageArchiveReader(packageStream))
{
foreach (var packageFile in await packageReader.GetFilesAsync(cancellationToken))
{
System.Diagnostics.Debug.WriteLine($"{packageFile} {Path.GetDirectoryName(packageFile)} {Path.GetFileName(Path.GetDirectoryName(packageFile))}");

if (Path.GetFileName(Path.GetDirectoryName(packageFile)) == "coretools")
{
using (var fileStream = File.OpenWrite(Path.Combine(dir, Path.GetFileName(packageFile))))
using (var stream = await packageReader.GetStreamAsync(packageFile, cancellationToken))
{
await stream.CopyToAsync(fileStream);
}
}
}
}
}
progress.Report(new ProgressReport() { Result = null });
}
catch (Exception ex)
{
progress.Report(new ProgressReport() { Error = ex });
}
}
}
}

0 comments on commit b9c7656

Please sign in to comment.