Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/radj307/assemblyattribute
Reusable base attribute designed to be applied to XML tags in csproj files.
https://github.com/radj307/assemblyattribute
assembly assembly-attribute csharp xml
Last synced: 3 months ago
JSON representation
Reusable base attribute designed to be applied to XML tags in csproj files.
- Host: GitHub
- URL: https://github.com/radj307/assemblyattribute
- Owner: radj307
- License: mit
- Created: 2022-05-16T21:25:22.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-12-22T05:09:17.000Z (about 2 years ago)
- Last Synced: 2024-11-08T22:52:48.794Z (3 months ago)
- Topics: assembly, assembly-attribute, csharp, xml
- Language: C#
- Homepage:
- Size: 83 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AssemblyAttribute
Reusable base attribute designed to streamline the process of using auto-versioning scripts in C# project files.
## Usage
The `BaseAssemblyAttribute` abstract class accepts any number of strings via `params string[]`, but since it is an abstract class it cannot be used directly.
You can use it to create your own assembly attributes, or just as example code - the implementation is very simple.### `ExtendedVersionAttribute`
Derives from `BaseAssemblyAttribute` and acts as a method to retrieve the **full version string** and **an array of its segments** after being split by all occurrences of `.`, `-`, or `+` via the `ExtendedVersionAttribute.Version` and `ExtendedVersionAttribute.VersionSegments` properties, respectively.
I use it with my auto-versioning scripts so I can retrieve the latest git tag in the local repository directly in the code, and that's what this guide will focus on.1. Add the **[AssemblyAttribute](https://www.nuget.org/packages/AssemblyAttribute nuget package)** to your project.
2. Open the `.csproj` project file in the editor.
*For .NET Core projects, you can do this by Right-Clicking on the project in the solution explorer & selecting the **Edit Project File** option.*
3. Add an element within the `PropertyGroup` tag to store the version number:
```csproj
```
4. Add a new `ItemGroup` element within the `Project` tag to add the attribute to that assembly, and pass in the value of the `PropertyGroup` variable from the previous step:
```csproj
<_Parameter1>$(ExtendedVersion)
```
5. You can now retrieve the `ExtendedVersionAttribute` with the properties you passed in the csproj file in your code with:
```csharp
System.Reflection.Assembly.GetCallingAssembly().GetCustomAttribute();
// ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲
// Adapt as needed to get the assembly that corresponds to your csproj file!
```Now all you have to do is update the version number in your `csproj` file **before** *(re)*building the solution somewhere in your build script *(This example uses PowerShell)*:
/*```ps1
[xml]$CONTENT = Get-Content -Path ""
$CONTENT.Project.PropertyGroup.ExtendedVersion = git describe --tags --abbrev=0
```