Skip to content

Commit

Permalink
Add initial project files
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgeary committed Aug 14, 2022
1 parent 63a39ae commit 5171bdc
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
36 changes: 36 additions & 0 deletions .github/workflows/test-actions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: test-actions

on:
push:
branches:
- master
pull_request:
workflow_dispatch:

jobs:
build:
runs-on: windows-latest

steps:
- name: Check out the repository
uses: actions/checkout@v3

- name: Test setting version using default parameters
uses: secondbounce/assemblyinfo-update/set-version@v1
with:
version: '8.8'

- name: Test setting version with custom parameters
id: test-setversion
uses: secondbounce/assemblyinfo-update/set-version@v1
with:
version: '9.9.9-alpha'
directory: './test/'
filename: 'SharedAssemblyInfo.cs'
recursive: false

- name: Upload updated files
uses: actions/upload-artifact@v3
with:
name: Test results for version '${{steps.test-setversion.outputs.version}}'
path: ./test/*.cs
51 changes: 49 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,49 @@
# assemblyinfo-update
Github action for updating _AssemblyInfo.cs_ files in .NET projects with a specified version number
# AssemblyInfo Update

## set-version

This Github action updates _AssemblyInfo.cs_ files in .NET projects with the specified version number, suffixed with [`github.run_number`](https://docs.github.com/en/actions/learn-github-actions/contexts).

The version number is expected to be in [SemVer format](https://semver.org/) with at least the major and minor version numbers, e.g. `1.20`, `2.17.4`, `1.4.9-alpha`. Any pre-release/metadata suffix will be discarded and [`github.run_number`](https://docs.github.com/en/actions/learn-github-actions/contexts) appended to produce a 3- or 4-element version number.

Note that any existing value in the file is overwritten, so may be left at, say, "0.0.0.0". The updated file is not committed back to the repository.

This action is useful for automatically updating the assembly info version of a project prior to building it, for example, where the version number should be set to the latest tag.

### Input arguments

* `version` (required): The assembly version in semver format
* `directory`: the directory where the assembly info file is located (or the top-most directory to search if `recursive` is `true`). Defaults to '.\\'
* `filename`: the file name of the assembly info file. Defaults to 'AssemblyInfo.cs'
* `recursive`: if `true`, updates all assembly info files matching the `filename` argument, in all subdirectories. Defaults to `true`

### Output arguments

* `version`: the value of the version used in the assembly info

### Example Usage

#### Minimal example

```yml
- name: Set version in all AssemblyInfo.cs files
uses: secondbounce/assemblyinfo-update/set-version@v1
with:
version: '1.0.8'
```
#### Complete example
```yml
- name: Set version in .\Properties\SharedAssemblyInfo.cs
id: set-assembly-version
uses: secondbounce/assemblyinfo-update/set-version@v1
with:
version: '2.1.16-alpha'
directory: '.\Properties'
filename: 'SharedAssemblyInfo.cs'
recursive: false

- name: Display the version used
run: echo "{{steps.set-assembly-version.outputs.version}}"
```
51 changes: 51 additions & 0 deletions set-version/SetVersion.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
$version = $Env:VERSION
$directory = $Env:DIRECTORY
$fileName = $Env:FILENAME
$recursive = [System.Convert]::ToBoolean($Env:RECURSIVE)
$runNumber = $Env:RUN_NUMBER

function SetVersion($file)
{
$contents = [System.IO.File]::ReadAllText($file)
$contents = [Regex]::Replace($contents, 'Version\("\d+\.\d+\.(\*|(\d+(\.\*|\.\d+)?))', 'Version("' + $version)
$match = [Regex]::Match($contents, $version)
if ($match.success)
{
$streamWriter = New-Object System.IO.StreamWriter($file, $false, [System.Text.Encoding]::GetEncoding("utf-8"))
$streamWriter.Write($contents)
$streamWriter.Close()

"::set-output name=version::$version"
Write-Host "$file is now set to version $version"
}
else
{
Write-Host "Version has not been set correctly for $file"
}
}

$isSemVer = [Regex]::Match($version, '^\d+\.\d+(\.\d+)?')
if ($isSemVer.success)
{
$version = $isSemVer.Value + '.' + $runNumber
}
else
{
Write-Host "Version number '$version' is invalid for use in assembly info versions"
}

if ($recursive)
{
$assemblyInfoFiles = Get-ChildItem $directory -Recurse -Include $fileName
foreach($file in $assemblyInfoFiles)
{
SetVersion($file)
}
}
else
{
$file = Get-ChildItem $directory -Filter $fileName | Select-Object -First 1
SetVersion($file)
}


36 changes: 36 additions & 0 deletions set-version/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: 'Set AssemblyInfo Version'
author: 'Second Bounce'
description: 'Sets the assembly version number in the specified assembly info file(s)'
branding:
icon: 'tag'
color: 'orange'
inputs:
version:
description: 'Assembly version in SemVer format'
required: true
directory:
description: 'Directory where assembly info file is located'
default: '.\'
filename:
description: 'Filename for assembly info'
default: 'AssemblyInfo.cs'
recursive:
description: 'If `true`, update all assembly info files including in files in subdirectories'
default: true
runs:
using: composite
steps:
- shell: pwsh
id: setversion
env:
VERSION: ${{ inputs.version }}
DIRECTORY: ${{ inputs.directory }}
FILENAME: ${{ inputs.filename }}
RECURSIVE: ${{ inputs.recursive }}
RUN_NUMBER: ${{ github.run_number }}
run: ${{ github.action_path }}\SetVersion.ps1
outputs:
version:
description: Version applied to assembly info
value: ${{steps.setversion.outputs.version}}

17 changes: 17 additions & 0 deletions test/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Reflection;

[assembly: AssemblyTitle("assemblyinfo-update")]
/* Expected result:
[assembly: AssemblyDescription("Test file for use with the `assemblyinfo-update` Github action for updating versions in the format 0.0.0.0")]
*/
[assembly: AssemblyDescription("Test file for use with the `assemblyinfo-update` Github action for updating versions in the format 0.0.0.0")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Second Bounce Ltd")]
[assembly: AssemblyProduct("assemblyinfo-update")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

[assembly: AssemblyVersion("1.11.*")] /* Expected result: "8.8.##" */
[assembly: AssemblyFileVersion("22.2.22.*")] /* Expected result: "8.8.##" */
[assembly: AssemblyInformationalVersion("3.3.3.33")] /* Expected result: "8.8.##" */
17 changes: 17 additions & 0 deletions test/SharedAssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Reflection;

[assembly: AssemblyTitle("assemblyinfo-update")]
/* Expected result:
[assembly: AssemblyDescription("Test file for use with the `assemblyinfo-update` Github action for updating versions in the format 0.0.0.0")]
*/
[assembly: AssemblyDescription("Test file for use with the `assemblyinfo-update` Github action for updating versions in the format 0.0.0.0")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Second Bounce Ltd")]
[assembly: AssemblyProduct("assemblyinfo-update")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

[assembly: AssemblyVersion("1.11.*")] /* Expected result: "9.9.9.##" */
[assembly: AssemblyFileVersion("22.2.22.*")] /* Expected result: "9.9.9.##" */
[assembly: AssemblyInformationalVersion("3.3.3.33")] /* Expected result: "9.9.9.##" */
17 changes: 17 additions & 0 deletions test/sub-project/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Reflection;

[assembly: AssemblyTitle("assemblyinfo-update")]
/* Expected result:
[assembly: AssemblyDescription("Test file for use with the `assemblyinfo-update` Github action for updating versions in the format 0.0.0.0")]
*/
[assembly: AssemblyDescription("Test file for use with the `assemblyinfo-update` Github action for updating versions in the format 0.0.0.0")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Second Bounce Ltd")]
[assembly: AssemblyProduct("assemblyinfo-update")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

[assembly: AssemblyVersion("1.11.*")] /* Expected result: "8.8.##" */
[assembly: AssemblyFileVersion("22.2.22.*")] /* Expected result: "8.8.##" */
[assembly: AssemblyInformationalVersion("3.3.3.33")] /* Expected result: "8.8.##" */
17 changes: 17 additions & 0 deletions test/sub-project/SharedAssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Reflection;

[assembly: AssemblyTitle("assemblyinfo-update")]
/* Expected result:
[assembly: AssemblyDescription("Test file for use with the `assemblyinfo-update` Github action for updating versions in the format 0.0.0.0")]
*/
[assembly: AssemblyDescription("Test file for use with the `assemblyinfo-update` Github action for updating versions in the format 0.0.0.0")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Second Bounce Ltd")]
[assembly: AssemblyProduct("assemblyinfo-update")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

[assembly: AssemblyVersion("1.11.*")] /* Expected result: "1.11.*" */
[assembly: AssemblyFileVersion("22.2.22.*")] /* Expected result: "22.2.22.*" */
[assembly: AssemblyInformationalVersion("3.3.3.33")] /* Expected result: "3.3.3.33" */

0 comments on commit 5171bdc

Please sign in to comment.