Skip to content

Commit

Permalink
Change "assembly version to be used" to also respect AssemblyInformat…
Browse files Browse the repository at this point in the history
…ionalVersionAttribute

fixes #99
  • Loading branch information
SimonCropp committed Nov 12, 2019
1 parent deb74f5 commit 24a8755
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 23 deletions.
3 changes: 2 additions & 1 deletion AssemblyToProcess/AssemblyToProcess.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<PropertyGroup>
<TargetFrameworks>net452;netcoreapp2.0</TargetFrameworks>
<DisableFody>true</DisableFody>
<Version>1.0.0</Version>
<Version>1.0.1-x</Version>
<AssemblyVersion>1.0</AssemblyVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Obsolete\Obsolete.csproj" />
Expand Down
6 changes: 6 additions & 0 deletions AssemblyToProcess/ClassWithIsErrorFromInformationalVersion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[ObsoleteEx(
TreatAsErrorFromVersion = "1.0.1",
RemoveInVersion = "1.1")]
public class ClassWithIsErrorFromInformationalVersion
{
}
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>5.1.0</Version>
<Version>5.2.0</Version>
<LangVersion>latest</LangVersion>
<NoWarn>NU5118</NoWarn>
</PropertyGroup>
Expand Down
8 changes: 1 addition & 7 deletions Obsolete.Fody/ModuleWeaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ public override void Execute()
FindEditorBrowsableTypes();
FindObsoleteType();

var version = ModuleDefinition.Assembly.Name.Version;
assemblyVersion = new SemanticVersion
{
Major = version.Major.OrZero(),
Minor = version.Minor.OrZero(),
Patch = version.Build.OrZero()
};
assemblyVersion = VersionReader.Read(ModuleDefinition.Assembly);

ProcessAssembly();
}
Expand Down
48 changes: 48 additions & 0 deletions Obsolete.Fody/VersionReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Linq;
using Mono.Cecil;

static class VersionReader
{
public static SemanticVersion Read(AssemblyDefinition assembly)
{
var informationalAttribute = assembly.CustomAttributes
.SingleOrDefault(x => x.AttributeType.FullName == "System.Reflection.AssemblyInformationalVersionAttribute");
if (informationalAttribute != null)
{
var value = (string)informationalAttribute.ConstructorArguments.Single().Value;
var indexOf = value.IndexOf(x => x != '.' && !char.IsNumber(x));
if (indexOf != -1)
{
var substring = value.Substring(0, indexOf);
if (SemanticVersion.TryParse(substring, out var versionFromInformational))
{
return versionFromInformational;
}
}
}

var version = assembly.Name.Version;
var semanticVersion = new SemanticVersion
{
Major = version.Major.OrZero(),
Minor = version.Minor.OrZero(),
Patch = version.Build.OrZero()
};
return semanticVersion;
}

static int IndexOf(this string source, Func<char, bool> predicate)
{
for (var index = 0; index < source.Length; index++)
{
var item = source[index];
if (predicate.Invoke(item))
{
return index;
}
}

return -1;
}
}
7 changes: 7 additions & 0 deletions Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ public void ClassWithIsError()
ValidateIsError(type);
}

[Fact]
public void ClassWithIsErrorFromInformationalVersion()
{
var type = assembly.GetType("ClassWithIsErrorFromInformationalVersion");
ValidateIsError(type);
}

[Fact]
public void Enum()
{
Expand Down
33 changes: 19 additions & 14 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ Add `<Obsolete/>` to [FodyWeavers.xml](https://github.com/Fody/Home/blob/master/

```csharp
[ObsoleteEx(
Message = "Custom message.",
TreatAsErrorFromVersion = "2.0",
RemoveInVersion = "4.0",
ReplacementTypeOrMember = "NewClass")]
Message = "Custom message.",
TreatAsErrorFromVersion = "2.0",
RemoveInVersion = "4.0",
ReplacementTypeOrMember = "NewClass")]
public class ClassToMark {}
```

Expand Down Expand Up @@ -93,7 +93,12 @@ So given the above example when the assembly version is 5.0 a compile error will
> Cannot process 'ClassToMark'. The assembly version 5.0.0 is higher than version specified in RemoveInVersion 4.0.0. The member should be removed or RemoveInVersion increased.

## The Message property
## Version convention

By default the prefix of [AssemblyInformationalVersionAttribute](https://docs.microsoft.com/en-us/dotnet/api/system.reflection.assemblyinformationalversionattribute) will be used, if it can be parsed to a version. Otherwise the [assembly version](https://docs.microsoft.com/en-us/dotnet/standard/assembly/versioning) is used.


## The Message property

The message property should only be used for useful information. The fact that it is obsoleted does not need to be reiterated in the message.

Expand All @@ -105,12 +110,12 @@ The message property should only be used for useful information. The fact that i
* "The replacement method is"


# Configuration Options
## Configuration Options

All configuration options are access by modifying the `Obsolete` node in `FodyWeavers.xml`


## HideObsoleteMembers
### HideObsoleteMembers

When this is `true` obsolete members will also have `[EditorBrowsable(EditorBrowsableState.Advanced)]` added to them.

Expand All @@ -121,7 +126,7 @@ When this is `true` obsolete members will also have `[EditorBrowsable(EditorBrow
```


## TreatAsErrorFormat
### TreatAsErrorFormat

The string used when informing the user what version the member will be treated as an error.

Expand All @@ -132,7 +137,7 @@ The string used when informing the user what version the member will be treated
```


## ThrowsNotImplementedText
### ThrowsNotImplementedText

The string used when informing the user when the member currently throws a [NotImplementedException](https://msdn.microsoft.com/en-us/library/system.notimplementedexception.aspx).

Expand All @@ -143,7 +148,7 @@ The string used when informing the user when the member currently throws a [NotI
```


## RemoveInVersionFormat
### RemoveInVersionFormat

The string used when informing the user what version the member will be removed it.

Expand All @@ -154,7 +159,7 @@ The string used when informing the user what version the member will be removed
```


## ReplacementFormat
### ReplacementFormat

The string used when informing the user of an alternative member to use instead of the obsolete member.

Expand All @@ -165,12 +170,12 @@ The string used when informing the user of an alternative member to use instead
```


## StepType
### StepType

Used in two cases

* To derive `TreatAsErrorFromVersion` if `RemoveInVersion` is not defined.
* To derive `RemoveInVersion` if `TreatAsErrorFromVersion` is not defined.
* To derive `RemoveInVersion` if `TreatAsErrorFromVersion` is not defined.

*Defaults to `Major`. Other options are `Minor` and `Patch`*

Expand All @@ -179,7 +184,7 @@ Used in two cases
```


## Mute warnings about Obsolete usage
### Mute warnings about Obsolete usage

For `ObsoleteAttribute` is used and should not be replaced, use `DoNotWarnAboutObsoleteUsageAttribute` to mute Fody warnings during build time.

Expand Down

0 comments on commit 24a8755

Please sign in to comment.