Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Commit

Permalink
Move to System.Text.Json
Browse files Browse the repository at this point in the history
  • Loading branch information
erwinvanhunen committed Jul 15, 2020
1 parent 0d5d134 commit 37fecca
Show file tree
Hide file tree
Showing 90 changed files with 585 additions and 545 deletions.
13 changes: 8 additions & 5 deletions Commands/Admin/AddTenantTheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
using PnP.PowerShell.Commands.Base.PipeBinds;
using System;
using PnP.PowerShell.Commands.Model;
using Newtonsoft.Json;
using System.Linq;
using System.Text.Json;

namespace PnP.PowerShell.Commands.Admin
{
Expand Down Expand Up @@ -73,14 +73,17 @@ protected override void ExecuteCmdlet()
{
if (Overwrite.ToBool())
{
Tenant.UpdateTenantTheme(Identity.Name, JsonConvert.SerializeObject(theme));
Tenant.UpdateTenantTheme(Identity.Name, JsonSerializer.Serialize(theme));
ClientContext.ExecuteQueryRetry();
} else
}
else
{
WriteError(new ErrorRecord(new Exception($"Theme exists"), "THEMEEXISTS", ErrorCategory.ResourceExists, Identity.Name));
}
} else {
Tenant.AddTenantTheme(Identity.Name, JsonConvert.SerializeObject(theme));
}
else
{
Tenant.AddTenantTheme(Identity.Name, JsonSerializer.Serialize(theme));
ClientContext.ExecuteQueryRetry();
}
}
Expand Down
5 changes: 2 additions & 3 deletions Commands/Admin/GetStorageEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
using PnP.PowerShell.Commands.Base;
using PnP.PowerShell.Commands.Enums;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text.Json;

namespace PnP.PowerShell.Commands
{
Expand Down Expand Up @@ -62,7 +61,7 @@ protected override void ExecuteCmdlet()

if (!string.IsNullOrEmpty(storageEntitiesIndex))
{
var storageEntitiesDict = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, string>>>(storageEntitiesIndex);
var storageEntitiesDict = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(storageEntitiesIndex);

var storageEntities = new List<StorageEntity>();
foreach (var key in storageEntitiesDict.Keys)
Expand Down
18 changes: 7 additions & 11 deletions Commands/Admin/GetTenantId.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
#if !ONPREMISES
using Microsoft.SharePoint.Client;
using Newtonsoft.Json.Linq;
using PnP.PowerShell.CmdletHelpAttributes;
using PnP.PowerShell.Commands.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
using System.Web;

namespace PnP.PowerShell.Commands.Admin
Expand Down Expand Up @@ -47,14 +43,17 @@ protected override void ProcessRecord()
}
catch (Exception ex)
{
#if !NETSTANDARD2_1
if (ex.InnerException != null)
{
if (ex.InnerException is HttpException)
{
var message = ex.InnerException.Message;
var obj = JObject.Parse(message);
WriteObject(obj["error_description"].ToString());

using (var jdoc = JsonDocument.Parse(message))
{
var errorDescription = jdoc.RootElement.GetProperty("error_description").GetString();
WriteObject(errorDescription);
}
}
else
{
Expand All @@ -65,9 +64,6 @@ protected override void ProcessRecord()
{
throw ex;
}
#else
throw ex;
#endif
}
}
}
Expand Down
25 changes: 23 additions & 2 deletions Commands/Admin/GetTenantTheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System;
using System.Linq;
using PnP.PowerShell.Commands.Model;
using System.Text.Json;

namespace PnP.PowerShell.Commands.Admin
{
Expand All @@ -23,26 +24,46 @@ namespace PnP.PowerShell.Commands.Admin
[CmdletExample(
Code = @"PS:> Get-PnPTenantTheme -Name ""MyCompanyTheme""",
Remarks = @"Returns the specified theme", SortOrder = 1)]
[CmdletExample(
Code = @"PS:> Get-PnPTenantTheme -Name ""MyCompanyTheme"" -AsJson",
Remarks = @"Returns the specified theme formatted as JSON", SortOrder = 2)]
public class GetTenantTheme : PnPAdminCmdlet
{
[Parameter(Mandatory = false, Position = 0, ValueFromPipeline = true, HelpMessage = "The name of the theme to retrieve")]
public string Name;

[Parameter(Mandatory = false, Position = 1, HelpMessage = "Outputs the themes as JSON")]
public SwitchParameter AsJson;

protected override void ExecuteCmdlet()
{
if (ParameterSpecified(nameof(Name)))
{
var theme = Tenant.GetTenantTheme(Name);
ClientContext.Load(theme);
ClientContext.ExecuteQueryRetry();
WriteObject(new SPOTheme(theme.Name, theme.Palette, theme.IsInverted));
if (AsJson)
{
WriteObject(JsonSerializer.Serialize(theme.Palette));
}
else
{
WriteObject(new SPOTheme(theme.Name, theme.Palette, theme.IsInverted));
}
}
else
{
var themes = Tenant.GetAllTenantThemes();
ClientContext.Load(themes);
ClientContext.ExecuteQueryRetry();
WriteObject(themes.Select(t => new SPOTheme(t.Name, t.Palette, t.IsInverted)), true);
if (AsJson)
{
WriteObject(JsonSerializer.Serialize(themes.Select(t => new SPOTheme(t.Name, t.Palette, t.IsInverted))));
}
else
{
WriteObject(themes.Select(t => new SPOTheme(t.Name, t.Palette, t.IsInverted)), true);
}
}

}
Expand Down
6 changes: 0 additions & 6 deletions Commands/Admin/RemoveStorageEntity.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
#if !ONPREMISES
using System.Linq;
using System.Management.Automation;
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;
using PnP.PowerShell.CmdletHelpAttributes;
using PnP.PowerShell.Commands.Base;
using PnP.PowerShell.Commands.Enums;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace PnP.PowerShell.Commands
{
Expand Down
6 changes: 0 additions & 6 deletions Commands/Admin/SetStorageEntity.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
#if !ONPREMISES
using System.Linq;
using System.Management.Automation;
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;
using PnP.PowerShell.CmdletHelpAttributes;
using PnP.PowerShell.Commands.Base;
using PnP.PowerShell.Commands.Enums;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace PnP.PowerShell.Commands
{
Expand Down
6 changes: 3 additions & 3 deletions Commands/Base/AddStoredCredential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace PnP.PowerShell.Commands.Base
{
[Cmdlet(VerbsCommon.Add, "PnPStoredCredential")]
#if !NETSTANDARD2_1
#if !PNPPSCORE
[CmdletHelp("Adds a credential to the Windows Credential Manager",
@"Adds an entry to the Windows Credential Manager. If you add an entry in the form of the URL of your tenant/server PnP PowerShell will check if that entry is available when you connect using Connect-PnPOnline. If it finds a matching URL it will use the associated credentials.
Expand Down Expand Up @@ -41,7 +41,7 @@ public class AddStoredCredential : PSCmdlet
[Parameter(Mandatory = false, HelpMessage = @"If not specified you will be prompted to enter your password.
If you want to specify this value use ConvertTo-SecureString -String 'YourPassword' -AsPlainText -Force")]
public SecureString Password;
#if NETSTANDARD2_1
#if PNPPSCORE
[Parameter(Mandatory = false)]
public SwitchParameter Overwrite;
#endif
Expand All @@ -53,7 +53,7 @@ protected override void ProcessRecord()
Password = Host.UI.ReadLineAsSecureString();
}

#if !NETSTANDARD2_1
#if !PNPPSCORE
Utilities.CredentialManager.AddCredential(Name, Username, Password);
#else
Utilities.CredentialManager.AddCredential(Name, Username, Password, Overwrite.ToBool());
Expand Down
54 changes: 45 additions & 9 deletions Commands/Base/BasePSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ namespace PnP.PowerShell.Commands.Base
/// </summary>
public class BasePSCmdlet : PSCmdlet
{
private Assembly newtonsoftAssembly;
private static Assembly newtonsoftAssembly;
private static Assembly systemBuffersAssembly;
private static Assembly systemRuntimeCompilerServicesUnsafeAssembly;
private static Assembly systemThreadingTasksExtensionsAssembly;

protected override void BeginProcessing()
{
Expand All @@ -32,21 +35,42 @@ protected override void EndProcessing()

private void FixAssemblyResolving()
{
var newtonsoftAssemblyByLocation = Path.Combine(AssemblyDirectoryFromLocation, "Newtonsoft.Json.dll");
if (File.Exists(newtonsoftAssemblyByLocation))
if (BasePSCmdlet.newtonsoftAssembly == null)
{
// Local run, network run, etc.
newtonsoftAssembly = Assembly.LoadFrom(newtonsoftAssemblyByLocation);
newtonsoftAssembly = GetAssembly("Newtonsoft.Json.dll");
}
else
if (systemBuffersAssembly == null)
{
systemBuffersAssembly = GetAssembly("System.Buffers.dll");
}
if (systemRuntimeCompilerServicesUnsafeAssembly == null)
{
systemRuntimeCompilerServicesUnsafeAssembly = GetAssembly("System.Runtime.CompilerServices.Unsafe.dll");
}
if (systemThreadingTasksExtensionsAssembly == null)
{
// Running from Azure Function
var newtonsoftAssemblyByCodeBase = Path.Combine(AssemblyDirectoryFromCodeBase, "Newtonsoft.Json.dll");
newtonsoftAssembly = Assembly.LoadFrom(newtonsoftAssemblyByCodeBase);
systemThreadingTasksExtensionsAssembly = GetAssembly("System.Threading.Tasks.Extensions.dll");
}

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}

private Assembly GetAssembly(string assemblyName)
{
Assembly assembly = null;
var assemblyPath = Path.Combine(AssemblyDirectoryFromLocation, assemblyName);
if (File.Exists(assemblyPath))
{
assembly = Assembly.LoadFrom(assemblyPath);
}
else
{
var codebasePath = Path.Combine(AssemblyDirectoryFromCodeBase, assemblyName);
assembly = Assembly.LoadFrom(codebasePath);
}
return assembly;
}

private string AssemblyDirectoryFromLocation
{
get
Expand Down Expand Up @@ -74,6 +98,18 @@ private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs a
{
return newtonsoftAssembly;
}
if (args.Name.StartsWith("System.Buffers", StringComparison.InvariantCultureIgnoreCase))
{
return systemBuffersAssembly;
}
if (args.Name.StartsWith("System.Runtime.CompilerServices.Unsafe", StringComparison.InvariantCultureIgnoreCase))
{
return systemRuntimeCompilerServicesUnsafeAssembly;
}
if (args.Name.StartsWith("System.Threading.Tasks.Extensions", StringComparison.InvariantCultureIgnoreCase))
{
return systemThreadingTasksExtensionsAssembly;
}
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
if (assembly.FullName == args.Name)
Expand Down
Loading

0 comments on commit 37fecca

Please sign in to comment.