Skip to content

Commit

Permalink
Added support to set visibility of classes to internal
Browse files Browse the repository at this point in the history
  • Loading branch information
LokiMidgard committed Dec 28, 2018
1 parent 0126fc1 commit 6a0e6ec
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
3 changes: 3 additions & 0 deletions XmlSchemaClassGenerator.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ static void Main(string[] args)
var entityFramework = false;
var interfaces = true;
var pascal = true;
var assembly = false;
var collectionType = typeof(Collection<>);
Type collectionImplementationType = null;
var codeTypeReferenceOptions = default(CodeTypeReferenceOptions);
Expand Down Expand Up @@ -75,6 +76,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
{ "f|ef", "generate Entity Framework Code First compatible classes", v => entityFramework = v != null },
{ "t|interface", "generate interfaces for groups and attribute groups (default is enabled)", v => interfaces = v != null },
{ "a|pascal", "use Pascal case for class and property names (default is enabled)", v => pascal = v != null },
{ "a|assemblyVisible", "use the internal visibillity modifier (default is false)", v => assembly = v != null },
{ "u|enableUpaCheck", "should XmlSchemaSet check for Unique Particle Attribution (UPA) (default is enabled)", v => enableUpaCheck = v != null },
{ "ct|collectionType=", "collection type to use (default is " + typeof(Collection<>).FullName + ")", v => collectionType = v == null ? typeof(Collection<>) : Type.GetType(v, true) },
{ "cit|collectionImplementationType=", "the default collection type implementation to use (default is null)", v => collectionImplementationType = v == null ? null : Type.GetType(v, true) },
Expand Down Expand Up @@ -121,6 +123,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
EntityFramework = entityFramework,
GenerateInterfaces = interfaces,
NamingScheme = pascal ? NamingScheme.PascalCase : NamingScheme.Direct,
AssemblyVisible=assembly,
CollectionType = collectionType,
CollectionImplementationType = collectionImplementationType,
CodeTypeReferenceOptions = codeTypeReferenceOptions,
Expand Down
6 changes: 6 additions & 0 deletions XmlSchemaClassGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ public NamingScheme NamingScheme
set { _configuration.NamingScheme = value; }
}

public bool AssemblyVisible
{
get { return _configuration.AssemblyVisible; }
set { _configuration.AssemblyVisible = value; }
}

/// <summary>
/// Emit the "Order" attribute value for XmlElementAttribute to ensure the correct order
/// of the serialized XML elements.
Expand Down
7 changes: 5 additions & 2 deletions XmlSchemaClassGenerator/GeneratorConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ public GeneratorConfiguration()
/// Generate <see cref="System.ComponentModel.DescriptionAttribute"/> from XML comments.
/// </summary>
public bool GenerateDescriptionAttribute { get; set; }

/// <summary>
/// Generate types as <c>internal</c> if <c>true</c>. <c>public</c> otherwise.
/// </summary>
public bool AssemblyVisible { get; set; }
/// <summary>
/// Generator Code reference options
/// </summary>
Expand Down Expand Up @@ -172,7 +175,7 @@ public void WriteLog(string message)

public bool DisableComments { get; set; }
public bool DoNotUseUnderscoreInPrivateMemberNames { get; set; }

/// <summary>
/// Check for Unique Particle Attribution (UPA) violations
/// </summary>
Expand Down
53 changes: 29 additions & 24 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ public override CodeTypeDeclaration Generate()

classDeclaration.IsClass = true;
classDeclaration.IsPartial = true;
if (Configuration.AssemblyVisible)
{
classDeclaration.TypeAttributes = (classDeclaration.TypeAttributes & ~System.Reflection.TypeAttributes.VisibilityMask) | System.Reflection.TypeAttributes.NestedAssembly;
}


if (Configuration.EnableDataBinding)
{
Expand Down Expand Up @@ -382,29 +387,29 @@ public override CodeTypeDeclaration Generate()
keyProperty.IsKey = true;
}

foreach (var property in Properties.GroupBy(x => x.Name))
{
var propertyIndex = 0;
foreach (var p in property)
{
if (propertyIndex > 0)
{
p.Name += $"_{propertyIndex}";
}
p.AddMembersTo(classDeclaration, Configuration.EnableDataBinding);
propertyIndex++;
}
}

if (IsMixed && (BaseClass == null || (BaseClass is ClassModel && !AllBaseClasses.Any(b => b.IsMixed))))
{
var propName = "Text";

// To not collide with any existing members
for (var propertyIndex = 1; Properties.Any(x => x.Name.Equals(propName, StringComparison.Ordinal)) || propName.Equals(classDeclaration.Name, StringComparison.Ordinal); propertyIndex++)
{
propName = $"Text_{propertyIndex}";
}
foreach (var property in Properties.GroupBy(x => x.Name))
{
var propertyIndex = 0;
foreach (var p in property)
{
if (propertyIndex > 0)
{
p.Name += $"_{propertyIndex}";
}
p.AddMembersTo(classDeclaration, Configuration.EnableDataBinding);
propertyIndex++;
}
}

if (IsMixed && (BaseClass == null || (BaseClass is ClassModel && !AllBaseClasses.Any(b => b.IsMixed))))
{
var propName = "Text";

// To not collide with any existing members
for (var propertyIndex = 1; Properties.Any(x => x.Name.Equals(propName, StringComparison.Ordinal)) || propName.Equals(classDeclaration.Name, StringComparison.Ordinal); propertyIndex++)
{
propName = $"Text_{propertyIndex}";
}
var text = new CodeMemberField(typeof(string[]), propName);
// hack to generate automatic property
text.Name += " { get; set; }";
Expand Down Expand Up @@ -893,7 +898,7 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi

var editorBrowsableAttribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(EditorBrowsableAttribute), Configuration.CodeTypeReferenceOptions));
editorBrowsableAttribute.Arguments.Add(new CodeAttributeArgument(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(new CodeTypeReference(typeof(EditorBrowsableState), Configuration.CodeTypeReferenceOptions)), "Never")));
specifiedMember.CustomAttributes.Add(editorBrowsableAttribute);
specifiedMember.CustomAttributes.Add(editorBrowsableAttribute);
member.CustomAttributes.Add(editorBrowsableAttribute);
if (Configuration.EntityFramework) { member.CustomAttributes.Add(notMappedAttribute); }
}
Expand Down

0 comments on commit 6a0e6ec

Please sign in to comment.