Skip to content

Repository files navigation

Icon ThisAssembly

VersionDownloadsLicenseBuild

Exposes project and assembly level information as constants in the ThisAssembly class using source generators powered by Roslyn.

The main generated entry point type is ThisAssembly in the global namespace, and is declared as partial so you can extend it too with manually created members.

Each package in turn extends this partial class to add their own nestes types and members.

The ThisAssembly meta-package includes all the other packages for convenience.

NOTE: for now, ThisAssembly only generates C# code.

ThisAssembly.AssemblyInfo

VersionDownloads

This package generates a static ThisAssembly.Info class with public constants exposing the following attribute values generated by default for SDK style projects:

  • AssemblyConfigurationAttribute

  • AssemblyCompanyAttribute

  • AssemblyTitleAttribute

  • AssemblyDescriptionAttribute

  • AssemblyProductAttribute

  • AssemblyCopyrightAttribute

  • AssemblyVersionAttribute

  • AssemblyInformationalVersionAttribute

  • AssemblyFileVersionAttribute

If your project includes these attributes by other means, they will still be emitted properly on the ThisAssembly.Info class.

ThisAssembly.Constants

VersionDownloads

This package generates a static ThisAssembly.Constants class with public constants for @(Constant) MSBuild items in the project.

 <ItemGroup>
<ConstantInclude="Foo.Bar"Value="Baz"Comment="Yay!" />
<ConstantInclude="Foo.Hello"Value="World"Comment="Comments make everything better 😍" />
</ItemGroup>

In addition to arbitrary constants via <Constant ...>, it's quite useful (in particular in test projects) to generate constants for files in the project, so there's also a shorthand for those:

 <ItemGroup>
<FileConstantInclude="@(Content)" />
</ItemGroup>

Which results in:

ThisAssembly.Git

VersionDownloads

This package generates a static ThisAssembly.Git class with constants for the following Git properties from the current project:

  • Commit
  • Sha (first 9 chars from Commit)
  • Root (normalized to forward slashes)
  • Url (if PublishRepositoryUrl=true)
  • Branch (from CI environment variables)

This package relies on your project's installed Microsoft.SourceLink.* package reference according to your specific Git-based source control server (such as GitHub, Azure DevOps, BitBucket, etc).

The Branch property is populated from environment variables provided by the currently supported CI systems: GitHub Actions, Azure DevOps, AppVeyor, TeamCity, Travis CI, Circle CI, GitLab CI, Buddy, and Jenkins.

Whenever the CI system provides a pull request number, the branch name is pr[NUMBER], such as pr123. This makes it easy to use it as a semver metadata label.

Note: by default, the values of these constants are populated during "real" builds (that is, not IDE/design-time builds used to populate intellisense). This is to avoid negatively affecting the editor's performance. This means, however, that the properties will seem to always be empty when inspecting them in the IDE (although never at run-time). If you want to force population of these values for design-time builds, set the EnableSourceControlManagerQueries property to true. This property is defined and documented by dotnet/sourcelink.

At the MSBuild level, targets can take a dependency on the provided InitializeGitInformation target, which sets the equivalent properties named:

  • RepositoryCommit
  • RepositorySha
  • RepositoryRoot
  • RepositoryUrl
  • RepositoryBranch

The names of these properties were chosen on purpose to match the properties used by nuget pack and nugetizer to populate the relevant package metadata.

So if you have a GitHub repository, installing these three packages will ensure you have the proper metadata out of the box and the simplest packaging experience possible:

<ProjectSdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReferenceInclude="Microsoft.SourceLink.GitHub" />
<PackageReferenceInclude="ThisAssembly.Git" />
<PackageReferenceInclude="NuGetizer" />
</ItemGroup>
</Project>

ThisAssembly.Metadata

VersionDownloads

This package provides a static ThisAssembly.Metadata class with public constants exposing each [System.Reflection.AssemblyMetadata(..)] defined for the project.

For an attribute declared (i.e. in AssemblyInfo.cs) like:

[assembly:System.Reflection.AssemblyMetadataAttribute("Foo","Bar")]

A corresponding ThisAssembly.Metadata.Foo constant with the value Bar is provided. The metadata attribute can alternatively be declared using MSBuild syntax in the project (for .NET 5.0+ projects that have built-in support for @(AssemblyMetadata) items):

 <ItemGroup>
<AssemblyMetadataInclude="Foo"Value="Bar" />
</ItemGroup>

ThisAssembly.Project

VersionDownloads

This package generates a static ThisAssembly.Project class with public constants exposing project properties that have been opted into this mechanism by adding them as ProjectProperty MSBuild items in the project file, such as:

 <PropertyGroup>
<!-- Some arbitrary MSBuild property declared somewhere -->
<Foo>Bar</Foo>
</PropertyGroup>
<ItemGroup>
<!-- Opt-in to emitting that property value as a constant in ThisAssembly.Project -->
<ProjectPropertyInclude="Foo" />
</ItemGroup>

ThisAssembly.Resources

VersionDownloads

This package generates a static ThisAssembly.Resources class with public properties exposing shortcuts to retrieve the contents of embedded resources.

This package generates a static ThisAssembly.Resources class with public properties exposing typed APIs to retrieve the contents of embedded resources.

 <ItemGroup>
<EmbeddedResourceInclude="Content/Docs/License.md" />
</ItemGroup>

Since markdown files are text files, the API will expose a Text property property for it that will read its content once and cache it:

The $(EmbeddedResourceStringExtensions) MSBuild property allows customizing which file extensions get treated as text files. By default, it's defined as:

 <PropertyGroup>
<EmbeddedResourceStringExtensions>.txt|.cs|.sql|.json|.md</EmbeddedResourceStringExtensions>
</PropertyGroup>

You can append additional file extensions to this list, or override it completely. The list must be pipe-separated.

You can always use the provided GetStream and GetBytes for more advanced scenarios (or for non-text resources).

Optionally, you can specify the Kind metadata for a specific EmbeddedResource you want treated as a text file:

 <EmbeddedResourceInclude="query.kql"Kind="Text" />

You can also add a Comment item metadata attribute, which will be used as the <summary> XML doc for the generated member.

ThisAssembly.Strings

VersionDownloads

This package generates a static ThisAssembly.Strings class with public constants exposing string resources in .resx files or methods with the right number of parameters for strings that use formatting parameters.

In addition, it groups constants and methods in nested classes according to an optional underscore separator to organize strings. For example, User_InvalidCredentials can be accessed with ThisAssembly.Strings.User.InvalidCredentials if it contains a simple string, or as a method with the right number of parametres if its value has a format string.

Given the following Resx file:

NameValueComment
Infrastructure_MissingServiceService {0} is required.For logging only!
Shopping_NoShippingWe cannot ship {0} to {1}.
Shopping_OutOfStockProduct is out of stock at this time.
Shopping_AvailableOnProduct available on {date:yyyy-MM}.

The following code would be generated:

partialclassThisAssembly{publicstaticpartialclassStrings{publicstaticpartialclassInfrastructure{/// <summary>/// For logging only!/// => "Service {0} is required."/// </summary>publicstaticstringMissingService(objectarg0)=>string.Format(CultureInfo.CurrentCulture,Strings.GetResourceManager("ThisStore.Properties.Resources").GetString("MissingService"),arg0);}publicstaticpartialclassShopping{/// <summary>/// => "We cannot ship {0} to {1}."/// </summary>publicstaticstringNoShipping(objectarg0,objectarg1)=>string.Format(CultureInfo.CurrentCulture,Strings.GetResourceManager("ThisStore.Properties.Resources").GetString("NoShipping"),arg0,arg1);/// <summary>/// => "Product is out of stock at this time."/// </summary>publicstaticstringOutOfStock=>Strings.GetResourceManager("ThisStore.Properties.Resources").GetString("OutOfStock");/// <summary>/// Product available on {date:yyyy-MM}./// </summary>publicstaticstringAvailableOn(objectdate)=>string.Format(CultureInfo.CurrentCulture,Strings.GetResourceManager("ThisAssemblyTests.Resources").GetString("WithNamedFormat").Replace("{date:yyyy-MM}","{0}"),((IFormattable)date).ToString("yyyy-MM",CultureInfo.CurrentCulture));}}}

Dogfooding

CI VersionBuild

We also produce CI packages from branches and pull requests so you can dogfood builds as quickly as they are produced.

The CI feed is https://pkg.kzu.io/index.json.

The versioning scheme for packages is:

  • PR builds: 42.42.42-pr[NUMBER]
  • Branch builds: 42.42.42-[BRANCH].[COMMITS]

Sponsors

Clarius OrgKirill OsenkovMFB Technologies, Inc.Stephen ShawTorutekDRIVE.NET, Inc.Ashley MedwayKeith PickfordThomas BolonKori FrancisToni WenzelGiorgi DalakishviliUno PlatformDan SiegelReuben SwartzJacob FosheeEric JohnsonIx Technologies B.V.David JENNIJonathan Oleg KyrylchukCharley WuJakob Tikjøb AndersenSeann AlexanderTino HagerMark SeemannKen BonnySimon Croppagileworks-eusorahexZheyu ShenVezelChilliCream4OTC

Sponsor this project

Learn more about GitHub Sponsors

About

Exposes project and assembly level information as constants in the ThisAssembly class using source generators powered by Roslyn.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages