Skip to main content

ThisAssembly by Daniel Cazzulino

Nuget / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

info

Name: ThisAssembly

Meta-package that includes all ThisAssembly.* packages.

Author: Daniel Cazzulino

NuGet: https://www.nuget.org/packages/ThisAssembly

You can find more details at https://www.clarius.org/ThisAssembly/

Source : https://github.com/devlooped/ThisAssembly

Original Readme

note

ThisAssembly

Version Downloads License Build

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

Version Downloads

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

Version Downloads

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

  <ItemGroup>
<Constant Include="Foo.Bar" Value="Baz" Comment="Yay!" />
<Constant Include="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>
<FileConstant Include="@(Content)" />
</ItemGroup>

Which results in:

ThisAssembly.Git

Version Downloads

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:

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" />
<PackageReference Include="ThisAssembly.Git" />
<PackageReference Include="NuGetizer" />
</ItemGroup>
</Project>

ThisAssembly.Metadata

Version Downloads

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>
<AssemblyMetadata Include="Foo" Value="Bar" />
</ItemGroup>

ThisAssembly.Project

Version Downloads

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 -->
<ProjectProperty Include="Foo" />
</ItemGroup>

ThisAssembly.Resources

Version Downloads

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>
<EmbeddedResource Include="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:

    <EmbeddedResource Include="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

Version Downloads

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.

The following code would be generated:

partial class ThisAssembly
{
public static partial class Strings
{
public static partial class Infrastructure
{
/// <summary>
/// For logging only!
/// => "Service {0} is required."
/// </summary>
public static string MissingService(object arg0)
=> string.Format(CultureInfo.CurrentCulture,
Strings.GetResourceManager("ThisStore.Properties.Resources").GetString("MissingService"),
arg0);
}

public static partial class Shopping
{
/// <summary>
/// => "We cannot ship {0} to {1}."
/// </summary>
public static string NoShipping(object arg0, object arg1)
=> string.Format(CultureInfo.CurrentCulture,
Strings.GetResourceManager("ThisStore.Properties.Resources").GetString("NoShipping"),
arg0, arg1);

/// <summary>
/// => "Product is out of stock at this time."
/// </summary>
public static string OutOfStock
=> Strings.GetResourceManager("ThisStore.Properties.Resources").GetString("OutOfStock");
}
}
}

Dogfooding

CI Version Build

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 Org C. Augusto Proiete Kirill Osenkov MFB Technologies, Inc. SandRock Andy Gocke Stephen Shaw

Sponsor this project  

Learn more about GitHub Sponsors

About

note

The ThisAssembly.Info allows you access to the Assembly Information as constants, instead of going to reflection each time.

I found useful to see the assembly version right away in any project that I have.

How to use

Example ( source csproj, source files )

This is the CSharp Project that references ThisAssembly

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<PropertyGroup>
<!-- this is the code to start RSCG -->
<Version>2023.5.7.800</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ThisAssembly" Version="1.2.14" OutputItemType="Analyzer" ReferenceOutputAssembly="false">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.CodeDom.Compiler;
using System.Runtime.CompilerServices;

/// <summary>
/// Provides access to the current assembly information as pure constants,
/// without requiring reflection.
/// </summary>
partial class ThisAssembly
{
/// <summary>
/// Gets the AssemblyInfo attributes.
/// </summary>
[GeneratedCode("ThisAssembly.AssemblyInfo", "1.2.14")]
[CompilerGenerated]
public static partial class Info
{
public const string Company = @"RSCG_Version";

public const string Configuration = @"Debug";

public const string FileVersion = @"2023.5.7.800";

public const string InformationalVersion = @"2023.5.7.800+4e734c748598553362149f59ca7aa41c10823d6a";

public const string Product = @"RSCG_Version";

public const string Title = @"RSCG_Version";

public const string Version = @"2023.5.7.800";

}
}

Usefull

Download Example (.NET C# )

Share ThisAssembly

https://ignatandrei.github.io/RSCG_Examples/v2/docs/ThisAssembly

In the same category (EnhancementProject) - 17 other generators

AutoInvoke.Generator

AutoSpectre

BuildInfo

Com

CommandLine

Credfeto.Version.Information.Generator

LinqGen.Generator

Pekspro.BuildInformationGenerator

PlantUmlClassDiagramGenerator

RSCG_AMS

RSCG_ExportDiagram

RSCG_FunctionsWithDI

RSCG_NameGenerator

RSCG_TimeBombComment

RSCG_Wait

ThisAssembly.Constants

ThisAssembly.Metadata