Skip to content

Commit 79c812c

Browse files
authored
Added package Id to package manifest. (#19)
* Added package Id to package manifest. * Fixed build. * Added comment.
1 parent 5763b70 commit 79c812c

File tree

7 files changed

+163
-18
lines changed

7 files changed

+163
-18
lines changed

Directory.Build.props

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,15 @@
22
<PropertyGroup>
33
<TargetFramework>net6.0</TargetFramework>
44
<LangVersion>latest</LangVersion>
5-
<Company>Umbraco HQ</Company>
6-
<Authors>Umbraco</Authors>
7-
<Copyright>Copyright © Umbraco $([System.DateTime]::Today.ToString('yyyy'))</Copyright>
8-
<Product>Umbraco Authorized Services</Product>
9-
<PackageProjectUrl>https://github.com/umbraco/Umbraco.AuthorizedServices</PackageProjectUrl>
10-
<RepositoryUrl>https://github.com/umbraco/Umbraco.AuthorizedServices</RepositoryUrl>
11-
<PackageIcon>icon.png</PackageIcon>
125
<Nullable>enable</Nullable>
136
<WarningsAsErrors>Nullable</WarningsAsErrors>
147
<ImplicitUsings>enable</ImplicitUsings>
158
<GenerateDocumentationFile>true</GenerateDocumentationFile>
16-
<PackageReadmeFile>NuGetReadMe.md</PackageReadmeFile>
9+
<PackageIcon>icon.png</PackageIcon>
1710
</PropertyGroup>
1811

1912
<PropertyGroup>
20-
<!-- TODO: Enable when final version is shipped (because there's currently no previous version) -->
13+
<!-- TODO: Enable when 1.0 version is shipped -->
2114
<EnablePackageValidation>false</EnablePackageValidation>
2215
<PackageValidationBaselineVersion>1.0.0</PackageValidationBaselineVersion>
2316
<EnableStrictModeForCompatibleFrameworksInPackage>true</EnableStrictModeForCompatibleFrameworksInPackage>
@@ -32,12 +25,11 @@
3225
<PackageReference Include="Umbraco.GitVersioning.Extensions" Version="0.2.0" PrivateAssets="all" IsImplicitlyDefined="true" />
3326
</ItemGroup>
3427

35-
<ItemGroup>
36-
<Content Include="$(MSBuildThisFileDirectory)icon.png" Pack="true" PackagePath="" Visible="false" />
37-
<None Include="docs\NuGetReadMe.md" Pack="true" PackagePath="\"/>
38-
</ItemGroup>
39-
4028
<PropertyGroup>
4129
<GitVersionBaseDirectory>$(MSBuildThisFileDirectory)</GitVersionBaseDirectory>
4230
</PropertyGroup>
31+
32+
<ItemGroup>
33+
<Content Include="$(MSBuildThisFileDirectory)icon.png" Pack="true" PackagePath="" Visible="false" />
34+
</ItemGroup>
4335
</Project>

examples/Umbraco.AuthorizedServices.TestSite/Umbraco.AuthorizedServices.TestSite.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Umbraco.Cms" Version="11.2.0" />
11+
<PackageReference Include="Umbraco.Cms" Version="12.0.1" />
1212
</ItemGroup>
1313

1414
<Import Project="..\..\src\Umbraco.AuthorizedServices\buildTransitive\Umbraco.AuthorizedServices.targets" />

src/Umbraco.AuthorizedServices/Constants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ namespace Umbraco.AuthorizedServices;
66

77
public static class Constants
88
{
9+
internal const string PackageId = "Umbraco.AuthorizedServices";
10+
911
internal const string PackageName = "Umbraco Authorized Services";
1012

1113
public const string PluginName = "UmbracoAuthorizedServices";
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System.Reflection;
2+
3+
namespace Umbraco.AuthorizedServices.Helpers;
4+
5+
internal static class ReflectionHelper
6+
{
7+
/// <summary>
8+
/// Gets a property value by reflection. If the property does not exist, a default value is returned.
9+
/// </summary>
10+
/// <typeparam name="T">Object type.</typeparam>
11+
/// <param name="obj">Object from which to get property value.</param>
12+
/// <param name="propertyName">Name of property.</param>
13+
/// <param name="defaultValue">Default value to use if property not available.</param>
14+
/// <remarks>
15+
/// This is used for accessing properties added in a supported version of Umbraco that's later than the
16+
/// one we take a dependency on.
17+
/// </remarks>
18+
/// <returns>The property value, or the default.</returns>
19+
public static T GetOptionalPropertyValue<T>(this object obj, string propertyName, T defaultValue)
20+
{
21+
PropertyInfo? propertyInfo = GetPropertyInfo(obj, propertyName);
22+
if (propertyInfo == null || !propertyInfo.CanRead)
23+
{
24+
return defaultValue;
25+
}
26+
27+
var value = propertyInfo.GetValue(obj, null);
28+
if (value == null)
29+
{
30+
return defaultValue;
31+
}
32+
33+
if (value is T t)
34+
{
35+
return t;
36+
}
37+
38+
return defaultValue;
39+
}
40+
41+
/// <summary>
42+
/// Sets a property value by reflection. If the property does not exist, no exception is thrown.
43+
/// </summary>
44+
/// <typeparam name="T">Object type.</typeparam>
45+
/// <param name="obj">Object on which to set property value.</param>
46+
/// <param name="propertyName">Name of property.</param>
47+
/// <param name="value">Value to set.</param>
48+
/// <remarks>
49+
/// See notes on <see cref="GetOptionalPropertyValue{T}(object, string, T)"/>
50+
/// </remarks>
51+
public static void SetOptionalPropertyValue<T>(this object obj, string propertyName, T value)
52+
{
53+
PropertyInfo? propertyInfo = GetPropertyInfo(obj, propertyName);
54+
if (propertyInfo == null || !propertyInfo.CanWrite)
55+
{
56+
return;
57+
}
58+
59+
if (propertyInfo.PropertyType.IsAssignableFrom(typeof(T)))
60+
{
61+
propertyInfo.SetValue(obj, value, null);
62+
}
63+
}
64+
65+
private static PropertyInfo? GetPropertyInfo(object obj, string propertyName) => obj.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance);
66+
}

src/Umbraco.AuthorizedServices/Manifests/AuthorizedServicesManifestFilter.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Umbraco.AuthorizedServices.Helpers;
12
using Umbraco.Cms.Core.Manifest;
23

34
namespace Umbraco.AuthorizedServices.Manifests;
@@ -6,7 +7,7 @@ public class AuthorizedServicesManifestFilter : IManifestFilter
67
{
78
public void Filter(List<PackageManifest> manifests)
89
{
9-
manifests.Add(new PackageManifest
10+
var manifest = new PackageManifest
1011
{
1112
AllowPackageTelemetry = true,
1213
Version = Constants.InformationalVersion,
@@ -20,6 +21,14 @@ public void Filter(List<PackageManifest> manifests)
2021
{
2122
"/App_Plugins/UmbracoAuthorizedServices/css/style.css"
2223
}
23-
});
24+
};
25+
26+
// The PackageId property was added in Umbraco 12, so we have to use reflection here to set it if available
27+
// as the package depdendency is on Umbraco 10.
28+
// If and when we release a version with a depdendency on 12+, this should be removed and replaced with
29+
// a standard property setter.
30+
ReflectionHelper.SetOptionalPropertyValue(manifest, "PackageId", Constants.PackageId);
31+
32+
manifests.Add(manifest);
2433
}
2534
}

src/Umbraco.AuthorizedServices/Umbraco.AuthorizedServices.csproj

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44
<Description>
55
Authorized Services is an extension from Umbraco intended to get you up to speed quickly with integrating external services. It handles the authentication and authorization flow for services using OAuth.
66
</Description>
7+
<Company>Umbraco HQ</Company>
8+
<Authors>Umbraco</Authors>
9+
<Copyright>Copyright © Umbraco $([System.DateTime]::Today.ToString('yyyy'))</Copyright>
10+
<Product>Umbraco Authorized Services</Product>
11+
<PackageProjectUrl>https://github.com/umbraco/Umbraco.AuthorizedServices</PackageProjectUrl>
12+
<RepositoryUrl>https://github.com/umbraco/Umbraco.AuthorizedServices</RepositoryUrl>
713
<PackageTags>umbraco umbraco-marketplace</PackageTags>
814
<StaticWebAssetBasePath>App_Plugins/UmbracoAuthorizedServices</StaticWebAssetBasePath>
15+
<PackageReadmeFile>NuGetReadMe.md</PackageReadmeFile>
916
</PropertyGroup>
1017

1118
<ItemGroup>
12-
<PackageReference Include="Umbraco.Cms.Web.BackOffice" Version="[10.0, 12]" />
19+
<PackageReference Include="Umbraco.Cms.Web.BackOffice" Version="[10.0, 13)" />
1320
</ItemGroup>
1421

1522
<ItemGroup>
@@ -23,6 +30,10 @@
2330
</AssemblyAttribute>
2431
</ItemGroup>
2532

33+
<ItemGroup>
34+
<None Include="docs\NuGetReadMe.md" Pack="true" PackagePath="\"/>
35+
</ItemGroup>
36+
2637
<!-- Build client assets using NPM -->
2738
<Import Project="build\Microsoft.AspNetCore.ClientAssets.targets" />
2839
<Target Name="ClientAssetsBuildOutputPath" BeforeTargets="ClientAssetsBuild">
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using Umbraco.AuthorizedServices.Helpers;
2+
3+
namespace Umbraco.AuthorizedServices.Tests.Helpers;
4+
5+
[TestFixture]
6+
internal class ReflectionHelperTests
7+
{
8+
[Test]
9+
public void GetOptionalPropertyValue_WithPropertyFound_ReturnsPropertyuValue()
10+
{
11+
var obj = new TestClass
12+
{
13+
TestStringProperty = "test",
14+
TestIntProperty = 99,
15+
TestBoolProperty = true,
16+
};
17+
var value1 = obj.GetOptionalPropertyValue("TestStringProperty", string.Empty);
18+
var value2 = obj.GetOptionalPropertyValue("TestIntProperty", 0);
19+
var value3 = obj.GetOptionalPropertyValue("TestBoolProperty", false);
20+
21+
Assert.AreEqual("test", value1);
22+
Assert.AreEqual(99, value2);
23+
Assert.True(value3);
24+
}
25+
26+
[Test]
27+
public void GetOptionalPropertyValue_WithPropertyNotFound_ReturnsDefaultValue()
28+
{
29+
var obj = new TestClass
30+
{
31+
TestStringProperty = "test",
32+
TestIntProperty = 99,
33+
TestBoolProperty = true
34+
};
35+
var value1 = obj.GetOptionalPropertyValue("MissingStringProperty", "default");
36+
var value2 = obj.GetOptionalPropertyValue("MissingIntProperty", 10);
37+
var value3 = obj.GetOptionalPropertyValue("MissingBoolProperty", false);
38+
39+
Assert.AreEqual("default", value1);
40+
Assert.AreEqual(10, value2);
41+
Assert.False(value3);
42+
}
43+
44+
[Test]
45+
public void SetOptionalPropertyValue_WithPropertyFound_SetsValues()
46+
{
47+
var obj = new TestClass();
48+
obj.SetOptionalPropertyValue("TestStringProperty", "test");
49+
obj.SetOptionalPropertyValue("TestIntProperty", 99);
50+
obj.SetOptionalPropertyValue("TestBoolProperty", true);
51+
52+
Assert.AreEqual("test", obj.TestStringProperty);
53+
Assert.AreEqual(99, obj.TestIntProperty);
54+
Assert.True(obj.TestBoolProperty);
55+
}
56+
57+
private class TestClass
58+
{
59+
public string TestStringProperty { get; set; } = string.Empty;
60+
61+
public int TestIntProperty { get; set; }
62+
63+
public bool TestBoolProperty { get; set; }
64+
}
65+
}

0 commit comments

Comments
 (0)