Skip to content

Commit a96554c

Browse files
feat(updater): add .csproj file support (#95)
1 parent 51ac361 commit a96554c

File tree

6 files changed

+78
-1
lines changed

6 files changed

+78
-1
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ _Having problems? Want to contribute? Join us on the [node-tooling community Sla
1919
- [How It Works:](#how-it-works)
2020
- [`bumpFiles`, `packageFiles` and `updaters`](#bumpfiles-packagefiles-and-updaters)
2121
- [Gradle Support (Java/Kotlin)](#gradle-support-javakotlin)
22+
- [.NET Support](#net-support)
2223
- [Installing `commit-and-tag-version`](#installing-commit-and-tag-version)
2324
- [As a local `npm run` script](#as-a-local-npm-run-script)
2425
- [As global `bin`](#as-global-bin)
@@ -88,6 +89,13 @@ If you are using Gradle, then just point to your `build.gradle` file (or `build.
8889
commit-and-tag-version --packageFiles build.gradle --bumpFiles build.gradle
8990
```
9091

92+
### .NET Support
93+
94+
If you are using .NET with `.csproj` files.
95+
This is going to read and update only the `<Version>` tag in the file.
96+
```sh
97+
commit-and-tag-version --packageFiles <YOUR-PROJECT-NAME>.csproj --bumpFiles <YOUR-PROJECT-NAME>.csproj
98+
```
9199

92100
## Installing `commit-and-tag-version`
93101

lib/updaters/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const JSON_BUMP_FILES = require('../../defaults').bumpFiles
33
const updatersByType = {
44
json: require('./types/json'),
55
'plain-text': require('./types/plain-text'),
6-
gradle: require('./types/gradle')
6+
gradle: require('./types/gradle'),
7+
csproj: require('./types/csproj')
78
}
89
const PLAIN_TEXT_BUMP_FILES = ['VERSION.txt', 'version.txt']
910

@@ -25,6 +26,9 @@ function getUpdaterByFilename (filename) {
2526
if (/build.gradle/.test(filename)) {
2627
return getUpdaterByType('gradle')
2728
}
29+
if (filename.endsWith('.csproj')) {
30+
return getUpdaterByType('csproj')
31+
}
2832
throw Error(
2933
`Unsupported file (${filename}) provided for bumping.\n Please specify the updater \`type\` or use a custom \`updater\`.`
3034
)

lib/updaters/types/csproj.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const versionRegex = /<Version>(.*)<\/Version>/
2+
3+
module.exports.readVersion = function (contents) {
4+
const matches = versionRegex.exec(contents)
5+
if (matches === null || matches.length !== 2) {
6+
throw new Error('Failed to read the Version field in your csproj file - is it present?')
7+
}
8+
return matches[1]
9+
}
10+
11+
module.exports.writeVersion = function (contents, version) {
12+
return contents.replace(versionRegex, `<Version>${version}</Version>`)
13+
}

test/core.spec.js

+16
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,22 @@ describe('commit-and-tag-version', function () {
769769
fs.readFileSync('build.gradle.kts', 'utf-8').should.equal(expected)
770770
})
771771

772+
it('bumps version in .NET `Project.csproj` file', async function () {
773+
const expected = fs.readFileSync('./test/mocks/Project-6.4.0.csproj', 'utf-8')
774+
const filename = 'Project.csproj'
775+
mock({
776+
bump: 'minor',
777+
fs: {
778+
[filename]: fs.readFileSync('./test/mocks/Project-6.3.1.csproj')
779+
}
780+
})
781+
await exec({
782+
packageFiles: [{ filename, type: 'csproj' }],
783+
bumpFiles: [{ filename, type: 'csproj' }]
784+
})
785+
fs.readFileSync(filename, 'utf-8').should.equal(expected)
786+
})
787+
772788
it('bumps version # in npm-shrinkwrap.json', async function () {
773789
mock({
774790
bump: 'minor',

test/mocks/Project-6.3.1.csproj

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Version>6.3.1</Version>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.10"/>
12+
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="7.0.10"/>
13+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.10"/>
14+
<PackageReference Include="Microsoft.Identity.Web" Version="1.16.0"/>
15+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0"/>
16+
</ItemGroup>
17+
18+
</Project>

test/mocks/Project-6.4.0.csproj

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Version>6.4.0</Version>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.10"/>
12+
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="7.0.10"/>
13+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.10"/>
14+
<PackageReference Include="Microsoft.Identity.Web" Version="1.16.0"/>
15+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0"/>
16+
</ItemGroup>
17+
18+
</Project>

0 commit comments

Comments
 (0)