public static ResourceTree LoadResourcesFromString(string yamlContent) { var result = new ResourceTree(); var resources = new List <ResourceSpecification>(); result.Resources = resources; var reader = new StringReader(yamlContent); var yaml = new YamlStream(); yaml.Load(reader); var document = yaml.Documents[0]; var rootNode = ((YamlMappingNode)((YamlMappingNode)document.RootNode).Children["deployment"]); if (rootNode.Children.Keys.Contains("location")) { result.Location = rootNode.Children["location"].ToString(); } foreach (var resourceNode in (YamlSequenceNode)rootNode.Children["resources"]) { var resource = new ResourceSpecification(); foreach (var attributeNode in ((YamlMappingNode)resourceNode).Children) { if (((YamlScalarNode)attributeNode.Key).Value == "type") { resource.ResourceType = standardizeResourceType(((YamlMappingNode)resourceNode).Children["type"].ToString()); } else if (attributeNode.Value.NodeType == YamlNodeType.Scalar) { resource.StringProperties.Add( ((YamlScalarNode)attributeNode.Key).Value, ((YamlScalarNode)attributeNode.Value).Value); } else if (attributeNode.Value.NodeType == YamlNodeType.Sequence) { resource.ListProperties.Add( ((YamlScalarNode)attributeNode.Key).Value, ((YamlSequenceNode)attributeNode.Value).Children .Select(listElement => listElement.ToString()) .ToArray() ); } else { throw new YamlLexingException($"Don't know what to do with element {attributeNode.Key} of type {attributeNode.Value.NodeType}"); } } if (resource.ResourceType == null) { throw new YamlLexingException($"Missing a resource type at line {resourceNode.Start.Line}:{resourceNode.Start.Column}"); } resources.Add(resource); } return(result); }
internal static ResourceTree LexCommandLine(string[] commandLine) { var tree = new ResourceTree(); var index = 0; ResourceSpecification resource = null; List <ResourceSpecification> resources = new List <ResourceSpecification>(); tree.Resources = resources; while (index < commandLine.Length) { if (isResource(commandLine[index])) { resource = new ResourceSpecification() { ResourceType = commandLine[index] }; resources.Add(resource); } else if (isOption(commandLine[index])) { var optionName = getOptionName(commandLine[index]); index++; if (!isList(commandLine[index])) { if (optionName == "location" && resource == null) { tree.Location = commandLine[index]; } else { resource.StringProperties.Add(optionName, commandLine[index]); } } else { resource.ListProperties.Add(optionName, parseList(commandLine[index])); } } index++; } return(tree); }
public static Context Parse(ResourceTree resourceTree) { var context = new Context(); if (resourceTree.Location != null) { context.DefaultLocation = resourceTree.Location; } foreach (ResourceSpecification resourceSpecification in resourceTree.Resources) { IResourceParser parser = Resolver .GetReference(resourceSpecification.ResourceType) .GetParser(); ParsedResource resource = parser.ParseResourceSpecification(context, resourceSpecification); context.Resources.Add(resource.Resource); context.ExplicitDependencyRequirements.Add(resource.Resource, resource.ExplicitDependenciesToBeInjected.ToList()); } return(context); }