Skip to content

Commit 6c7dc91

Browse files
Set PackageValidationBaselineVersion
Set the correct `<PackageValidationBaselineVersion>` value when generating the project file based on the current latest release in NuGet.org.
1 parent c840920 commit 6c7dc91

File tree

5 files changed

+166
-3
lines changed

5 files changed

+166
-3
lines changed

generators/app/index.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
2+
const fetch = require('cross-fetch');
23
const Generator = require('yeoman-generator');
4+
const queryString = require('query-string');
35
const yosay = require('yosay');
46

57
module.exports = class extends Generator {
@@ -49,6 +51,8 @@ module.exports = class extends Generator {
4951
this.templateData.tokenendpoint = answers.tokenendpoint;
5052
this.templateData.userinformationendpoint = answers.userinformationendpoint;
5153

54+
this.templateData.currentVersion = await this.getCurrentVersion();
55+
5256
this.name = answers.name;
5357
this.applicationName = 'AspNet.Security.OAuth.' + answers.name;
5458
}
@@ -60,4 +64,55 @@ module.exports = class extends Generator {
6064
this.fs.copyTpl(this.templatePath('AuthenticationHandler.cs'), this.applicationName + '/' + this.name + 'AuthenticationHandler.cs', this.templateData)
6165
this.fs.copyTpl(this.templatePath('AuthenticationOptions.cs'), this.applicationName + '/' + this.name + 'AuthenticationOptions.cs', this.templateData)
6266
}
67+
68+
async getCurrentVersion() {
69+
70+
let response = await fetch('https://api.nuget.org/v3/index.json');
71+
72+
if (!response.ok) {
73+
throw new Error(`Failed to query NuGet service index. HTTP status code: ${response.status}.`);
74+
}
75+
76+
const serviceIndex = await response.json();
77+
const baseAddress = serviceIndex.resources.find(resource => resource['@type'] === 'SearchQueryService/3.5.0')['@id'];
78+
79+
if (!baseAddress) {
80+
throw new Error('Failed to determine the base address for the NuGet search query service.');
81+
}
82+
83+
const query = queryString.stringify({
84+
prerelease: false,
85+
q: 'PackageId:AspNet.Security.OAuth.GitHub',
86+
semVerLevel: '2.0.0',
87+
take: 1
88+
});
89+
90+
const searchUrl = `${baseAddress}?${query}`;
91+
response = await fetch(searchUrl);
92+
93+
if (!response.ok) {
94+
throw new Error(`Failed to search for NuGet package from '${searchUrl}'. HTTP status code: ${response.status}.`);
95+
}
96+
97+
const searchResult = await response.json();
98+
99+
let latestVersion = null;
100+
101+
if (searchResult.data && searchResult.data.length > 0) {
102+
latestVersion = searchResult.data[0].version;
103+
}
104+
105+
if (!latestVersion) {
106+
throw new Error('Failed to determine the latest version of the OAuth providers.');
107+
}
108+
109+
const dot = '.';
110+
const versionParts = latestVersion.split(dot);
111+
112+
// Increment the build number by one for the release that
113+
// would be the first one including this new provider.
114+
versionParts[2] = parseInt(versionParts[2], 10) + 1;
115+
116+
return versionParts.join(dot);
117+
}
63118
};

generators/app/templates/Project.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4+
<PackageValidationBaselineVersion><%= currentVersion %></PackageValidationBaselineVersion>
45
<TargetFrameworks>$(DefaultNetCoreTargetFramework)</TargetFrameworks>
56
</PropertyGroup>
67

7-
<!-- This property group required until the new provider is first published to NuGet.org -->
8+
<!-- This property group is required until the new provider is first published to NuGet.org -->
89
<PropertyGroup>
910
<DisablePackageBaselineValidation>true</DisablePackageBaselineValidation>
1011
</PropertyGroup>

package-lock.json

Lines changed: 98 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
"oauth"
2727
],
2828
"dependencies": {
29+
"cross-fetch": "^3.1.5",
30+
"query-string": "^7.1.1",
2931
"yosay": "^2.0.2",
3032
"yeoman-generator": "^5.7.0"
3133
},

test/test-app.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
'use strict';
22

3-
const path = require('path');
43
const assert = require('yeoman-assert');
54
const helpers = require('yeoman-test');
5+
const path = require('path');
66

77
describe('aspnet-oauth:app', () => {
8-
before(async () => {
8+
before(async function () {
9+
this.timeout(10000);
910
return await helpers.run(path.join(__dirname, '../generators/app'))
1011
.withOptions({ skipInstall: true })
1112
.withPrompts({
@@ -44,6 +45,12 @@ describe('aspnet-oauth:app', () => {
4445
/<PackageTags>foo;aspnetcore;authentication;oauth;security<\/PackageTags>/
4546
);
4647
});
48+
it('sets the package baseline version', () => {
49+
assert.fileContent(
50+
'AspNet.Security.OAuth.Foo/AspNet.Security.OAuth.Foo.csproj',
51+
/<PackageValidationBaselineVersion>[0-9]+\.[0-9]+\.[0-9]+<\/PackageValidationBaselineVersion>/
52+
);
53+
});
4754
it('sets the defaults class name', () => {
4855
assert.fileContent(
4956
'AspNet.Security.OAuth.Foo/FooAuthenticationDefaults.cs',

0 commit comments

Comments
 (0)