public override bool Execute() { if (!String.IsNullOrEmpty(TrackerLogDirectory)) { TrackFileAccess = true; } object rootObject = XamlServices.Load(new StreamReader(Xaml)); XamlTypes.ProjectSchemaDefinitions schemas = rootObject as XamlTypes.ProjectSchemaDefinitions; if (schemas != null) { foreach (XamlTypes.IProjectSchemaNode node in schemas.Nodes) { XamlTypes.Rule rule = node as XamlTypes.Rule; if (rule != null) { parseParameters(rule); CommandLineGenerator generator = new CommandLineGenerator(rule, parameterValues); generator.CommandLineTemplate = this.CommandLineTemplate; generator.AdditionalOptions = AdditionalOptions; CommandLine = generator.GenerateCommandLine(); return(base.Execute()); } } } return(false); }
/// <summary> /// Parse a Xaml document from a TextReader /// </summary> internal bool ParseXamlDocument(TextReader reader, string desiredRule) { ErrorUtilities.VerifyThrowArgumentNull(reader, nameof(reader)); ErrorUtilities.VerifyThrowArgumentLength(desiredRule, nameof(desiredRule)); object rootObject = XamlServices.Load(reader); if (null != rootObject) { XamlTypes.ProjectSchemaDefinitions schemas = rootObject as XamlTypes.ProjectSchemaDefinitions; if (schemas != null) { foreach (XamlTypes.IProjectSchemaNode node in schemas.Nodes) { XamlTypes.Rule rule = node as XamlTypes.Rule; if (rule != null) { if (String.Equals(rule.Name, desiredRule, StringComparison.OrdinalIgnoreCase)) { return(ParseXamlDocument(rule)); } } } throw new XamlParseException(ResourceUtilities.FormatResourceStringStripCodeAndKeyword("Xaml.RuleNotFound", desiredRule)); } else { throw new XamlParseException(ResourceUtilities.GetResourceString("Xaml.InvalidRootObject")); } } return(false); }
private bool parseParameters(XamlTypes.Rule rule) { Dictionary <string, string> strOptions = new Dictionary <string, string>(); string[] paras = Parameters.Split('|'); foreach (string p in paras) { int pos = p.IndexOf('='); string name = p.Substring(0, pos); string value = p.Substring(pos + 1); strOptions[name] = value; switchOrderList.Add(name); } foreach (XamlTypes.BaseProperty property in rule.Properties) { string val; if (strOptions.TryGetValue(property.Name, out val)) { XamlTypes.BoolProperty boolProperty = property as XamlTypes.BoolProperty; XamlTypes.DynamicEnumProperty dynamicEnumProperty = property as XamlTypes.DynamicEnumProperty; XamlTypes.EnumProperty enumProperty = property as XamlTypes.EnumProperty; XamlTypes.IntProperty intProperty = property as XamlTypes.IntProperty; XamlTypes.StringProperty stringProperty = property as XamlTypes.StringProperty; XamlTypes.StringListProperty stringListProperty = property as XamlTypes.StringListProperty; if (stringListProperty != null) { string[] values = val.Split(';'); parameterValues[property.Name] = values; } else if (boolProperty != null) { parameterValues[property.Name] = string.Compare(val, "true", StringComparison.OrdinalIgnoreCase) == 0; } else if (intProperty != null) { parameterValues[property.Name] = Int64.Parse(val); } else { parameterValues[property.Name] = val; } } } return(true); }
private static Dictionary <string, object> parseParameters(string xaml, Dictionary <string, string> strOptions) { object rootObject = XamlServices.Load(new StreamReader(xaml)); XamlTypes.ProjectSchemaDefinitions schemas = rootObject as XamlTypes.ProjectSchemaDefinitions; if (schemas != null) { foreach (XamlTypes.IProjectSchemaNode node in schemas.Nodes) { XamlTypes.Rule rule = node as XamlTypes.Rule; if (rule != null) { return(parseParameters(rule, strOptions)); } } } return(null); }
/// <summary> /// The list of switches in the order they should appear, if set. /// </summary> private Dictionary <String, Object> parseParameters(XamlTypes.Rule rule) { Dictionary <String, Object> parameterValues = new Dictionary <string, object>(); Dictionary <string, string> strOptions = parseParamValues(Parameters); foreach (XamlTypes.BaseProperty property in rule.Properties) { string val; if (strOptions.TryGetValue(property.Name, out val)) { XamlTypes.BoolProperty boolProperty = property as XamlTypes.BoolProperty; XamlTypes.DynamicEnumProperty dynamicEnumProperty = property as XamlTypes.DynamicEnumProperty; XamlTypes.EnumProperty enumProperty = property as XamlTypes.EnumProperty; XamlTypes.IntProperty intProperty = property as XamlTypes.IntProperty; XamlTypes.StringProperty stringProperty = property as XamlTypes.StringProperty; XamlTypes.StringListProperty stringListProperty = property as XamlTypes.StringListProperty; if (stringListProperty != null) { string[] values = val.Split(';'); parameterValues[property.Name] = values; } else if (boolProperty != null) { parameterValues[property.Name] = string.Compare(val, "true", StringComparison.OrdinalIgnoreCase) == 0; } else if (intProperty != null) { parameterValues[property.Name] = Int64.Parse(val); } else { parameterValues[property.Name] = val; } } } return(parameterValues); }
public string GenCmdLine(string xaml) { object rootObject = XamlServices.Load(new StreamReader(xaml)); XamlTypes.ProjectSchemaDefinitions schemas = rootObject as XamlTypes.ProjectSchemaDefinitions; if (schemas != null) { foreach (XamlTypes.IProjectSchemaNode node in schemas.Nodes) { XamlTypes.Rule rule = node as XamlTypes.Rule; if (rule != null) { var parameterValues = parseParameters(rule); CommandLineGenerator generator = new CommandLineGenerator(rule, parameterValues); generator.CommandLineTemplate = this.CommandLineTemplate; generator.AdditionalOptions = AdditionalOptions; return(generator.GenerateCommandLine()); } } } return(null); }
/// <summary> /// Parse a Xaml document from a rule /// </summary> internal bool ParseXamlDocument(XamlTypes.Rule rule) { if (rule == null) { return(false); } DefaultPrefix = rule.SwitchPrefix; ToolName = rule.ToolName; GeneratedTaskName = rule.Name; // Dictionary of property name strings to property objects. If a property is in the argument list of the current property then we want to make sure // that the argument property is a dependency of the current property. // As properties are parsed they are added to this dictionary so that after we can find the property instances from the names quickly. var argumentDependencyLookup = new Dictionary <string, Property>(StringComparer.OrdinalIgnoreCase); // baseClass = attribute.InnerText; // namespaceValue = attribute.InnerText; // resourceNamespaceValue = attribute.InnerText; foreach (XamlTypes.BaseProperty property in rule.Properties) { if (!ParseParameterGroupOrParameter(property, Properties, null, argumentDependencyLookup /*Add to the dictionary properties as they are parsed*/)) { return(false); } } // Go through each property and their arguments to set up the correct dependency mappings. foreach (Property property in Properties) { // Get the arguments on the property itself List <Argument> arguments = property.Arguments; // Find all of the properties in arguments list. foreach (Argument argument in arguments) { if (argumentDependencyLookup.TryGetValue(argument.Parameter, out Property argumentProperty)) { property.DependentArgumentProperties.AddLast(argumentProperty); } } // Properties may be enumeration types, this would mean they have sub property values which themselves can have arguments. List <Value> values = property.Values; // Find all of the properties for the aruments in sub property. foreach (Value value in values) { List <Argument> valueArguments = value.Arguments; foreach (Argument argument in valueArguments) { if (argumentDependencyLookup.TryGetValue(argument.Parameter, out Property argumentProperty)) { // If the property contains a value sub property that has a argument then we will declare that the original property has the same dependenecy. property.DependentArgumentProperties.AddLast(argumentProperty); } } } } return(true); }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public XamlParser (string path) { // // Allow for the potential to load properties through XAML files with a 'ProjectSchemaDefinitions' root node. // object xmlRootNode = XamlServices.Load (path); if (xmlRootNode.GetType () == typeof (ProjectSchemaDefinitions)) { ProjectSchemaDefinitions projectSchemaDefs = (ProjectSchemaDefinitions)xmlRootNode; m_parsedBuildRule = (Rule)projectSchemaDefs.Nodes [0]; } else { m_parsedBuildRule = (Rule)xmlRootNode; } ToolProperties = new Dictionary<string, PropertyWrapper> (); foreach (var prop in m_parsedBuildRule.Properties) { ToolProperties.Add (prop.Name, new PropertyWrapper (prop.GetType (), prop)); } m_typeFunctionMap = new Dictionary<Type, Action<CommandLineBuilder, BaseProperty, string>> { { typeof(StringListProperty), GenerateArgumentStringList }, { typeof(StringProperty), GenerateArgumentString }, { typeof(IntProperty), GenerateArgumentInt }, { typeof(BoolProperty), GenerateArgumentBool }, { typeof(EnumProperty), GenerateArgumentEnum } }; }
/// <summary> /// Creates a generator that generates a command-line based on the specified Xaml file and parameters. /// </summary> public CommandLineGenerator(Rule rule, Dictionary<string, Object> parameterValues) { ErrorUtilities.VerifyThrowArgumentNull(rule, "rule"); ErrorUtilities.VerifyThrowArgumentNull(parameterValues, "parameterValues"); // Parse the Xaml file TaskParser parser = new TaskParser(); bool success = parser.ParseXamlDocument(rule); ErrorUtilities.VerifyThrow(success, "Unable to parse specified file or contents."); // Generate the switch order list _switchOrderList = parser.SwitchOrderList; _activeCommandLineToolSwitches = new Dictionary<string, CommandLineToolSwitch>(StringComparer.OrdinalIgnoreCase); foreach (Property property in parser.Properties) { Object value = null; if (parameterValues.TryGetValue(property.Name, out value)) { CommandLineToolSwitch switchToAdd = new CommandLineToolSwitch(); if (!String.IsNullOrEmpty(property.Reversible) && String.Equals(property.Reversible, "true", StringComparison.OrdinalIgnoreCase)) { switchToAdd.Reversible = true; } switchToAdd.IncludeInCommandLine = property.IncludeInCommandLine; switchToAdd.Separator = property.Separator; switchToAdd.DisplayName = property.DisplayName; switchToAdd.Description = property.Description; if (!String.IsNullOrEmpty(property.Required) && String.Equals(property.Required, "true", StringComparison.OrdinalIgnoreCase)) { switchToAdd.Required = true; } switchToAdd.FallbackArgumentParameter = property.Fallback; switchToAdd.FalseSuffix = property.FalseSuffix; switchToAdd.TrueSuffix = property.TrueSuffix; if (!String.IsNullOrEmpty(property.SwitchName)) { switchToAdd.SwitchValue = property.Prefix + property.SwitchName; } switchToAdd.IsValid = true; // Based on the switch type, cast the value and set as appropriate switch (property.Type) { case PropertyType.Boolean: switchToAdd.Type = CommandLineToolSwitchType.Boolean; switchToAdd.BooleanValue = (bool)value; if (!String.IsNullOrEmpty(property.ReverseSwitchName)) { switchToAdd.ReverseSwitchValue = property.Prefix + property.ReverseSwitchName; } break; case PropertyType.Integer: switchToAdd.Type = CommandLineToolSwitchType.Integer; switchToAdd.Number = (int)value; if (!String.IsNullOrEmpty(property.Min)) { if (switchToAdd.Number < Convert.ToInt32(property.Min, System.Threading.Thread.CurrentThread.CurrentCulture)) { switchToAdd.IsValid = false; } } if (!String.IsNullOrEmpty(property.Max)) { if (switchToAdd.Number > Convert.ToInt32(property.Max, System.Threading.Thread.CurrentThread.CurrentCulture)) { switchToAdd.IsValid = false; } } break; case PropertyType.ItemArray: switchToAdd.Type = CommandLineToolSwitchType.ITaskItemArray; switchToAdd.TaskItemArray = (ITaskItem[])value; break; case PropertyType.None: break; case PropertyType.String: switchToAdd.Type = CommandLineToolSwitchType.String; switchToAdd.ReverseSwitchValue = property.Prefix + property.ReverseSwitchName; if (property.Values.Count > 0) { string enumValueToSelect = (string)value; switchToAdd.Value = (string)value; switchToAdd.AllowMultipleValues = true; // Find the matching value in the enum foreach (Value enumValue in property.Values) { if (String.Equals(enumValue.Name, enumValueToSelect, StringComparison.OrdinalIgnoreCase)) { if (!String.IsNullOrEmpty(enumValue.SwitchName)) { switchToAdd.SwitchValue = enumValue.Prefix + enumValue.SwitchName; } else { switchToAdd = null; break; } } } } else { switchToAdd.Value = (string)value; } break; case PropertyType.StringArray: switchToAdd.Type = CommandLineToolSwitchType.StringArray; switchToAdd.StringList = (string[])value; break; } if (switchToAdd != null) { _activeCommandLineToolSwitches[property.Name] = switchToAdd; } } } }
internal bool ParseXamlDocument(Rule rule) { if (rule == null) { return false; } this.defaultPrefix = rule.SwitchPrefix; this.toolName = string.IsNullOrEmpty(rule.ToolName) ? rule.ToolName : rule.ToolName.Replace(@"\", @"\\"); this.name = rule.Name; foreach (BaseProperty property in rule.Properties) { if (property.IncludeInCommandLine && !this.ParseParameterGroupOrParameter(property, this.properties, null)) { return false; } } return true; }