Skip to content
This repository was archived by the owner on Nov 9, 2021. It is now read-only.

Commit a149533

Browse files
committed
Backporting LicenseAttribute
1 parent 585dd47 commit a149533

File tree

6 files changed

+146
-10
lines changed

6 files changed

+146
-10
lines changed

Assembly/AssemblyInfo.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
using System;
2424
using System.Reflection;
25+
using Commons;
2526

2627
[assembly: AssemblyTitle("Commons.GetOptions")]
2728
[assembly: AssemblyDescription("Command line arguments parsing library")]
@@ -31,6 +32,9 @@
3132
[assembly: AssemblyTrademark("")]
3233
[assembly: AssemblyCulture("")]
3334
[assembly: CLSCompliant(true)]
34-
[assembly: AssemblyVersion("1.0.2015.1")]
35+
[assembly: AssemblyVersion("1.1.2015.2")]
36+
[assembly: AssemblyFileVersion("1.1.2015.2")]
3537
[assembly: AssemblyProduct("Managed.Commons.GetOptions")]
36-
[assembly: AssemblyInformationalVersion("1.0.1")]
38+
[assembly: AssemblyInformationalVersion("1.1.0")]
39+
40+
[assembly: License(LicenseType.MIT)]

Commons.GetOptions.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<AppDesignerFolder>Assembly</AppDesignerFolder>
1111
<RootNamespace>Commons.GetOptions</RootNamespace>
1212
<AssemblyName>Commons.GetOptions</AssemblyName>
13-
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
1414
<FileAlignment>512</FileAlignment>
1515
<TargetFrameworkProfile />
1616
</PropertyGroup>
@@ -38,14 +38,14 @@
3838
<Compile Include="Commons.GetOptions\ArgumentProcessorAttribute.cs" />
3939
<Compile Include="Assembly\AssemblyInfo.cs" />
4040
<Compile Include="Commons.GetOptions\KillInheritedOptionAttribute.cs" />
41+
<Compile Include="Commons\LicenseAttribute.cs" />
4142
<Compile Include="Commons.GetOptions\OptionAttribute.cs" />
4243
<Compile Include="Commons.GetOptions\OptionDetails.cs" />
4344
<Compile Include="Commons.GetOptions\OptionList.cs" />
4445
<Compile Include="Commons.GetOptions\Options.cs" />
4546
<Compile Include="Commons.GetOptions\OptionsParsingMode.cs" />
4647
<Compile Include="Commons\AboutAttribute.cs" />
4748
<Compile Include="Commons\AdditionalInfoAttribute.cs" />
48-
<Compile Include="Commons\AuthorAttribute.cs" />
4949
<Compile Include="Commons\IsPartOfPackageAttribute.cs" />
5050
<Compile Include="Commons\ReportBugsToAttribute.cs" />
5151
<Compile Include="Commons\UsageComplementAttribute.cs" />
@@ -58,6 +58,7 @@
5858
<None Include="Commons.GetOptions-license.md">
5959
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
6060
</None>
61+
<None Include="Commons.GetOptions.nuspec" />
6162
</ItemGroup>
6263
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
6364
<PropertyGroup>

Commons.GetOptions/OptionList.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using System.IO;
2626
using System.Reflection;
2727
using System.Text;
28+
using System.Linq;
2829

2930
namespace Commons.GetOptions
3031
{
@@ -210,6 +211,7 @@ internal bool MaybeAnOption(string arg)
210211
private ArrayList _list = new ArrayList();
211212
private Options _optionBundle = null;
212213
private OptionsParsingMode _parsingMode;
214+
private string _appLicense;
213215

214216
private static int IndexOfAny(string where, params char[] what)
215217
{
@@ -253,19 +255,25 @@ private void ExtractEntryAssemblyInfo(Type optionsType)
253255
}
254256

255257
_appExeName = _entry.GetName().Name;
256-
_appVersion = _entry.GetName().Version.ToString();
258+
GetAssemblyAttributeValue(typeof(AssemblyInformationalVersionAttribute), "InformationalVersion", ref _appVersion);
259+
if (string.IsNullOrWhiteSpace(_appVersion))
260+
_appVersion = _entry.GetName().Version.ToString();
257261
GetAssemblyAttributeValue(typeof(AssemblyTitleAttribute), "Title", ref _appTitle);
258262
GetAssemblyAttributeValue(typeof(AssemblyCopyrightAttribute), "Copyright", ref _appCopyright);
259263
GetAssemblyAttributeValue(typeof(AssemblyDescriptionAttribute), "Description", ref _appDescription);
260264
GetAssemblyAttributeValue(typeof(Commons.AboutAttribute), ref _appAboutDetails);
261265
GetAssemblyAttributeValue(typeof(Commons.UsageComplementAttribute), ref _appUsageComplement);
262266
GetAssemblyAttributeValue(typeof(Commons.AdditionalInfoAttribute), ref _appAdditionalInfo);
263267
GetAssemblyAttributeValue(typeof(Commons.ReportBugsToAttribute), ref _appReportBugsTo);
264-
_appAuthors = GetAssemblyAttributeStrings(typeof(AuthorAttribute));
265-
if (_appAuthors.Length == 0) {
268+
string authors = string.Empty;
269+
GetAssemblyAttributeValue(typeof(AssemblyCompanyAttribute), "Company", ref authors);
270+
_appLicense = GetAssemblyAttributeStrings(typeof(LicenseAttribute)).FirstOrDefault();
271+
if (string.IsNullOrWhiteSpace(authors)) {
266272
_appAuthors = new String[1];
267-
_appAuthors[0] = "Add one or more [assembly: Commons.Author(\"Here goes the author name\")] to your assembly";
268-
}
273+
_appAuthors[0] = "Add one or more [assembly: AssemblyCompany(\"Here goes the authors names, separated by commas\")] to your assembly";
274+
} else
275+
_appAuthors = authors.Split(',');
276+
269277
}
270278

271279
private object[] GetAssemblyAttributes(Type type)
@@ -455,6 +463,8 @@ private void ShowTitleLines()
455463
{
456464
ShowBanner();
457465
Console.WriteLine(translate(_appDescription));
466+
if (!string.IsNullOrWhiteSpace(_appLicense))
467+
Console.WriteLine("\r\n" + translate("License: ") + _appLicense);
458468
Console.WriteLine();
459469
}
460470

Commons/LicenseAttribute.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//
2+
// OptionAttribute.cs
3+
//
4+
// Copyright ©2002-2014 Rafael 'Monoman' Teixeira, Managed Commons Team
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining
7+
// a copy of this software and associated documentation files (the
8+
// "Software"), to deal in the Software without restriction, including
9+
// without limitation the rights to use, copy, modify, merge, publish,
10+
// distribute, sublicense, and/or sell copies of the Software, and to
11+
// permit persons to whom the Software is furnished to do so, subject to
12+
// the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be
15+
// included in all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
//
25+
using System;
26+
using System.Collections.Generic;
27+
using System.Linq;
28+
using System.Text;
29+
30+
namespace Commons
31+
{
32+
public enum LicenseType
33+
{
34+
Proprietary,
35+
AGPL3,
36+
Apache2,
37+
BSD3Clause,
38+
BSD2Clause,
39+
GPL2,
40+
GPL3,
41+
LGPL21,
42+
LGPL3,
43+
MIT,
44+
Mozilla2,
45+
CDDL,
46+
Eclipse,
47+
Other
48+
}
49+
50+
[AttributeUsage(AttributeTargets.Assembly)]
51+
public class LicenseAttribute : Attribute
52+
{
53+
public string Details;
54+
public string Name;
55+
public LicenseType Type;
56+
57+
private struct LicenseDescriptor
58+
{
59+
public readonly string Name;
60+
public readonly string Details;
61+
public LicenseDescriptor(string name, string details) { Name = name; Details = details; }
62+
}
63+
64+
private static Dictionary<LicenseType, LicenseDescriptor> Licenses = new Dictionary<LicenseType, LicenseDescriptor>();
65+
66+
static LicenseAttribute()
67+
{
68+
Licenses.Add(LicenseType.Proprietary, new LicenseDescriptor("Proprietary", null));
69+
Licenses.Add(LicenseType.AGPL3, new LicenseDescriptor("GNU Affero General Public License, Version 3 (AGPL-3.0)", "See http://opensource.org/licenses/AGPL-3.0"));
70+
Licenses.Add(LicenseType.Apache2, new LicenseDescriptor("Apache License, Version 2.0", "See http://opensource.org/licenses/Apache-2.0"));
71+
Licenses.Add(LicenseType.BSD3Clause, new LicenseDescriptor("BSD 3-Clause License", "See http://opensource.org/licenses/BSD-3-Clause"));
72+
Licenses.Add(LicenseType.BSD2Clause, new LicenseDescriptor("BSD 2-Clause License", "See http://opensource.org/licenses/BSD-2-Clause"));
73+
Licenses.Add(LicenseType.GPL2, new LicenseDescriptor("GNU General Public License, Version 2 (GPL-2.0)", "See http://opensource.org/licenses/GPL-2.0"));
74+
Licenses.Add(LicenseType.GPL3, new LicenseDescriptor("GNU General Public License, Version 3 (GPL-3.0)", "See http://opensource.org/licenses/GPL-3.0"));
75+
Licenses.Add(LicenseType.LGPL21, new LicenseDescriptor("GNU Lesser General Public License, Version 2.1 (LGPL-2.1)", "See http://opensource.org/licenses/LGPL-2.1"));
76+
Licenses.Add(LicenseType.LGPL3, new LicenseDescriptor("GNU Lesser General Public License, Version 3.0 (LGPL-3.0)", "See http://opensource.org/licenses/LGPL-3.0"));
77+
Licenses.Add(LicenseType.MIT, new LicenseDescriptor("MIT License", "See http://opensource.org/licenses/MIT"));
78+
Licenses.Add(LicenseType.Mozilla2, new LicenseDescriptor("Mozilla Public License 2.0 (MPL-2.0)", "See http://opensource.org/licenses/MPL-2.0"));
79+
Licenses.Add(LicenseType.CDDL, new LicenseDescriptor("COMMON DEVELOPMENT AND DISTRIBUTION LICENSE Version 1.0 (CDDL-1.0)", "See http://opensource.org/licenses/CDDL-1.0"));
80+
Licenses.Add(LicenseType.Eclipse, new LicenseDescriptor("Eclipse Public License, Version 1.0 (EPL-1.0)", "See http://opensource.org/licenses/EPL-1.0"));
81+
}
82+
83+
public LicenseAttribute(LicenseType type)
84+
{
85+
if (type == LicenseType.Other)
86+
throw new ArgumentOutOfRangeException("type", "The license 'type' should not be 'Other', use the alternate constructor");
87+
var license = Licenses[type];
88+
@Type = type;
89+
Name = license.Name;
90+
Details = license.Details;
91+
}
92+
93+
public LicenseAttribute(string name, string details)
94+
{
95+
@Type = LicenseType.Other;
96+
Name = name;
97+
Details = details;
98+
}
99+
100+
public override string ToString()
101+
{
102+
return string.IsNullOrWhiteSpace(Details) ? Name : (Name + " - " + Details);
103+
}
104+
}
105+
}

Samples/mcat/Assembly/AssemblyInfo.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,19 @@
1919
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
22+
23+
using System;
24+
using System.Reflection;
25+
26+
[assembly: AssemblyTitle("mcat")]
27+
[assembly: AssemblyDescription("Just a simulated cat to demonstrate Commons.GetOptions")]
28+
[assembly: AssemblyConfiguration("")]
29+
[assembly: AssemblyCompany("Rafael 'Monoman' Teixeira, Managed Commons Team")]
30+
[assembly: AssemblyCopyright("Copyright ©2002-2015 Rafael 'Monoman' Teixeira, Managed Commons Team")]
31+
[assembly: AssemblyTrademark("")]
32+
[assembly: AssemblyCulture("")]
33+
[assembly: CLSCompliant(true)]
34+
[assembly: AssemblyVersion("1.1.0.0")]
35+
[assembly: AssemblyFileVersion("1.1.0.0")]
36+
[assembly: AssemblyProduct("Managed.Commons.GetOptions")]
37+
[assembly: AssemblyInformationalVersion("1.1.0")]

Samples/mcat/mcat.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ Report bugs to <bug-coreutils@gnu.org>.
5757

5858

5959
[assembly: Commons.About("Just a simulated cat to demonstrate Commons.GetOptions")]
60-
[assembly: Commons.Author("Rafael Teixeira")]
6160
[assembly: Commons.UsageComplement("[FILE]...\nConcatenate FILE(s), or standard input, to standard output.")]
6261
[assembly: Commons.AdditionalInfo("With no FILE, or when FILE is -, read standard input.")]
6362
[assembly: Commons.ReportBugsTo("rafaelteixeirabr@hotmail.com")]
63+
[assembly: Commons.License(Commons.LicenseType.MIT)]
6464

6565
public class CatLikeOptions : Options
6666
{

0 commit comments

Comments
 (0)