private bool Compile() { IMetadataCompilerOptions options = CreateOptions(out ITaskItem scriptTaskItem); scripts.Add(scriptTaskItem); MetadataCompiler compiler = new MetadataCompiler(this); return(compiler.Compile(options) && !hasErrors); }
public void CompileTest() { var schemaText = @"{ ""api_scan"": { ""type"": ""boolean"", ""is_multivalued"": false, ""is_queryable"": false, ""is_required"": false, ""is_visible"": false, ""query_name"": null, ""display_name"": ""Api Scan"", ""choice_set"": null, ""description"": ""Indicate whether the depot need to API Scan."" }, ""choice_test"": { ""type"": ""string"", ""is_multivalued"": true, ""is_queryable"": false, ""is_required"": false, ""is_visible"": false, ""query_name"": null, ""display_name"": ""Choice test"", ""choice_set"": [ ""A"", ""B"" ], ""description"": ""Test choice set!"" } }"; var schema = MetadataParser.Load(schemaText); var compiler = new MetadataCompiler(); { compiler.Compile(schema, "test", "A.B", "X"); var type = Type.GetType("A.B.X, test"); dynamic obj = JsonConvert.DeserializeObject(@"{ api_scan : true, choice_test: [ ""A"", ""B"",""A"" ], unknown: ""what's this?"" }", type); Assert.Equal(true, obj.api_scan); Assert.Equal(new string[] { "A", "B", "A" }, obj.choice_test); Assert.Equal("what's this?", (string)obj.__Additional["unknown"]); } { compiler.Namer = s => Regex.Replace(s, "(^|_)([a-z])", m => m.Groups[2].Value.ToUpper()); compiler.Compile(schema, "test_Namer", "A.B", "X"); var type = Type.GetType("A.B.X, test_Namer"); dynamic obj = JsonConvert.DeserializeObject(@"{ api_scan : true, choice_test: [ ""A"", ""B"",""A"" ], unknown: ""what's this?"" }", type); Assert.Equal(true, obj.ApiScan); Assert.Equal(new string[] { "A", "B", "A" }, obj.ChoiceTest); Assert.Equal("what's this?", (string)obj.__Additional["unknown"]); } }
static int Main(string[] args) { var opts = new Options(); foreach (var arg in args) { var parts = arg.Split("="); var name = parts[0]; if (typeof(Options).GetProperty(name) is var prop) { if (typeof(IEnumerable <string>).IsAssignableFrom(prop.PropertyType)) { var values = parts[1].Split(";").Where(s => !string.IsNullOrWhiteSpace(s)).ToArray(); prop.SetValue(opts, values); } else { var value = parts[1]; if (prop.PropertyType == typeof(bool)) { if (bool.TryParse(value, out bool result)) { prop.SetValue(opts, result); } } else { prop.SetValue(opts, value); } } } } MetadataCompiler compiler = new MetadataCompiler(new ConsoleErrorHandler()); return((compiler.Compile(opts) && !hasErrors) ? 0 : 1); }