diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..5ace460 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,6 @@ +version: 2 +updates: + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" diff --git a/.github/workflows/test-actions.yml b/.github/workflows/test-actions.yml new file mode 100644 index 0000000..d583589 --- /dev/null +++ b/.github/workflows/test-actions.yml @@ -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 diff --git a/README.md b/README.md index ed1f646..bbd3e1f 100644 --- a/README.md +++ b/README.md @@ -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}}" +``` diff --git a/set-version/SetVersion.ps1 b/set-version/SetVersion.ps1 new file mode 100644 index 0000000..7d0d677 --- /dev/null +++ b/set-version/SetVersion.ps1 @@ -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) +} + + diff --git a/set-version/action.yml b/set-version/action.yml new file mode 100644 index 0000000..9b233e5 --- /dev/null +++ b/set-version/action.yml @@ -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}} + \ No newline at end of file diff --git a/test/AssemblyInfo.cs b/test/AssemblyInfo.cs new file mode 100644 index 0000000..7a862b9 --- /dev/null +++ b/test/AssemblyInfo.cs @@ -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.##" */ diff --git a/test/SharedAssemblyInfo.cs b/test/SharedAssemblyInfo.cs new file mode 100644 index 0000000..3db014d --- /dev/null +++ b/test/SharedAssemblyInfo.cs @@ -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.##" */ diff --git a/test/sub-project/AssemblyInfo.cs b/test/sub-project/AssemblyInfo.cs new file mode 100644 index 0000000..7a862b9 --- /dev/null +++ b/test/sub-project/AssemblyInfo.cs @@ -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.##" */ diff --git a/test/sub-project/SharedAssemblyInfo.cs b/test/sub-project/SharedAssemblyInfo.cs new file mode 100644 index 0000000..9607718 --- /dev/null +++ b/test/sub-project/SharedAssemblyInfo.cs @@ -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" */