diff --git a/src/Mobile.BuildTools/Generators/Secrets/SecretsClassGenerator.cs b/src/Mobile.BuildTools/Generators/Secrets/SecretsClassGenerator.cs index d76ab2c0..f10fb2c7 100644 --- a/src/Mobile.BuildTools/Generators/Secrets/SecretsClassGenerator.cs +++ b/src/Mobile.BuildTools/Generators/Secrets/SecretsClassGenerator.cs @@ -195,7 +195,7 @@ internal CodeWriter GenerateClass(SecretsConfig secretsConfig, IDictionary secret, SecretsConfig se } var mapping = valueConfig.PropertyType.GetPropertyTypeMapping(); + var accessibility = secretsConfig.Accessibility.ToString().ToLower(); - var standardOutput = PropertyBuilder(secret, mapping.Type, mapping.Handler, valueConfig.IsArray, false); - var safeOutput = PropertyBuilder(secret, mapping.Type, mapping.Handler, valueConfig.IsArray, true); + var standardOutput = PropertyBuilder(secret, mapping.Type, mapping.Handler, valueConfig.IsArray, false, accessibility); + var safeOutput = PropertyBuilder(secret, mapping.Type, mapping.Handler, valueConfig.IsArray, true, accessibility); writer.AppendAttribute(CompileGeneratedAttribute); writer.AppendLine(standardOutput, safeOutput); } @@ -253,7 +254,9 @@ internal string ProcessSecret(KeyValuePair secret, SecretsConfig } var mapping = valueConfig.PropertyType.GetPropertyTypeMapping(); - return PropertyBuilder(secret, mapping.Type, mapping.Handler, valueConfig.IsArray, safeOutput); + var accessibility = secretsConfig.Accessibility.ToString().ToLower(); + + return PropertyBuilder(secret, mapping.Type, mapping.Handler, valueConfig.IsArray, safeOutput, accessibility); } internal ValueConfig GenerateValueConfig(KeyValuePair secret, SecretsConfig config) @@ -308,7 +311,7 @@ internal string GetNamespace(string relativeNamespace) return $"{BaseNamespace}.{relativeNamespace}"; } - internal string PropertyBuilder(KeyValuePair secret, Type type, IValueHandler valueHandler, bool isArray, bool safeOutput) + internal string PropertyBuilder(KeyValuePair secret, Type type, IValueHandler valueHandler, bool isArray, bool safeOutput, string accessibility) { var output = string.Empty; var typeDeclaration = type.GetStandardTypeName(); @@ -329,7 +332,7 @@ internal string PropertyBuilder(KeyValuePair secret, Type type, output = output.ToLower(); } - return $"internal {accessModifier} {typeDeclaration} {secret.Key} = {output};"; + return $"{accessibility} {accessModifier} {typeDeclaration} {secret.Key} = {output};"; } internal static IEnumerable GetValueArray(JToken token) => diff --git a/src/Mobile.BuildTools/Models/Secrets/Accessibility.cs b/src/Mobile.BuildTools/Models/Secrets/Accessibility.cs new file mode 100644 index 00000000..22f370f5 --- /dev/null +++ b/src/Mobile.BuildTools/Models/Secrets/Accessibility.cs @@ -0,0 +1,9 @@ + +namespace Mobile.BuildTools.Models.Secrets +{ + public enum Accessibility + { + Internal, + Public + } +} diff --git a/src/Mobile.BuildTools/Models/Secrets/SecretsConfig.cs b/src/Mobile.BuildTools/Models/Secrets/SecretsConfig.cs index 2bdaeb80..b300d5f8 100644 --- a/src/Mobile.BuildTools/Models/Secrets/SecretsConfig.cs +++ b/src/Mobile.BuildTools/Models/Secrets/SecretsConfig.cs @@ -16,8 +16,17 @@ public class SecretsConfig public string Prefix { get; set; } [JsonProperty("className")] - public string ClassName { get; set; } - + public string ClassName { get; set; } + +#if SCHEMAGENERATOR + [System.ComponentModel.DefaultValue("Internal")] + [System.ComponentModel.DataAnnotations.EnumDataType(typeof(Accessibility))] + public string Accessibility { get; set; } +#else + [JsonProperty("accessibility")] + public Accessibility Accessibility { get; set; } +#endif + [JsonProperty("namespace")] public string Namespace { get; set; } diff --git a/tests/Mobile.BuildTools.Tests/Fixtures/Generators/SecretsClassGeneratorFixture.cs b/tests/Mobile.BuildTools.Tests/Fixtures/Generators/SecretsClassGeneratorFixture.cs index 0dedf776..a4c99a87 100644 --- a/tests/Mobile.BuildTools.Tests/Fixtures/Generators/SecretsClassGeneratorFixture.cs +++ b/tests/Mobile.BuildTools.Tests/Fixtures/Generators/SecretsClassGeneratorFixture.cs @@ -55,75 +55,87 @@ public void ProcessSecretHidesValueForSafeOutput(bool firstRun) } [Theory] - [InlineData(true)] - [InlineData(false)] - public void ProcessSecretGeneratesBooleanValue(bool firstRun) + [InlineData(Accessibility.Internal, true)] + [InlineData(Accessibility.Internal, false)] + [InlineData(Accessibility.Public, true)] + [InlineData(Accessibility.Public, false)] + public void ProcessSecretGeneratesBooleanValue(Accessibility accessibility, bool firstRun) { var key = "TestKey"; var value = bool.TrueString; var pair = new KeyValuePair(key, value); var generator = GetGenerator(); var config = new SecretsConfig() - { + { + Accessibility = accessibility, Properties = new List { new ValueConfig { Name = TestKey, PropertyType = PropertyType.Bool } } }; var output = generator.ProcessSecret(pair, config, firstRun); - Assert.Contains($"const bool {key}", output); + Assert.Contains($"{accessibility.ToString().ToLower()} const bool {key}", output); } [Theory] - [InlineData(true)] - [InlineData(false)] - public void ProcessSecretGeneratesIntegerValue(bool firstRun) + [InlineData(Accessibility.Internal, true)] + [InlineData(Accessibility.Internal, false)] + [InlineData(Accessibility.Public, true)] + [InlineData(Accessibility.Public, false)] + public void ProcessSecretGeneratesIntegerValue(Accessibility accessibility, bool firstRun) { var key = "TestKey"; var value = "2"; var pair = new KeyValuePair(key, value); var generator = GetGenerator(); var config = new SecretsConfig() - { + { + Accessibility = accessibility, Properties = new List { new ValueConfig { Name = TestKey, PropertyType = PropertyType.Int } } }; var output = generator.ProcessSecret(pair, config, firstRun); - Assert.Contains($"const int {key}", output); + Assert.Contains($"{accessibility.ToString().ToLower()} const int {key}", output); } [Theory] - [InlineData(true)] - [InlineData(false)] - public void ProcessSecretGeneratesDoubleValue(bool firstRun) + [InlineData(Accessibility.Internal, true)] + [InlineData(Accessibility.Internal, false)] + [InlineData(Accessibility.Public, true)] + [InlineData(Accessibility.Public, false)] + public void ProcessSecretGeneratesDoubleValue(Accessibility accessibility, bool firstRun) { var key = "TestKey"; var value = "2.2"; var pair = new KeyValuePair(key, value); var generator = GetGenerator(); var config = new SecretsConfig() - { + { + Accessibility = accessibility, Properties = new List { new ValueConfig { Name = TestKey, PropertyType = PropertyType.Double } } }; var output = generator.ProcessSecret(pair, config, firstRun); - Assert.Contains($"const double {key}", output); + Assert.Contains($"{accessibility.ToString().ToLower()} const double {key}", output); } [Theory] - [InlineData(true)] - [InlineData(false)] - public void ProcessSecretGeneratesStringValue(bool firstRun) + [InlineData(Accessibility.Internal, true)] + [InlineData(Accessibility.Internal, false)] + [InlineData(Accessibility.Public, true)] + [InlineData(Accessibility.Public, false)] + public void ProcessSecretGeneratesStringValue(Accessibility accessibility, bool firstRun) { var key = "TestKey"; var value = "TestValue"; var pair = new KeyValuePair(key, value); var generator = GetGenerator(); var config = new SecretsConfig() - { + { + Accessibility = accessibility, Properties = new List { new ValueConfig { Name = TestKey, PropertyType = PropertyType.String } } }; var output = generator.ProcessSecret(pair, config, firstRun); - Assert.Contains($"const string {key}", output); + Assert.Contains($"{accessibility.ToString().ToLower()} const string {key}", output); } [Theory]