Esempio n. 1
0
 public override HttpResponseMessage PutProperty(string domainType, string instanceId, string propertyName, [ModelBinder(typeof(SingleValueArgumentBinder))] SingleValueArgument argument)
 {
     return(base.PutProperty(domainType, instanceId, propertyName, argument));
 }
Esempio n. 2
0
 public override HttpResponseMessage DeleteCollection(string domainType, string instanceId, string propertyName, [ModelBinder(typeof(SingleValueArgumentUrlBinder))] SingleValueArgument argument)
 {
     return(base.DeleteCollection(domainType, instanceId, propertyName, argument));
 }
        public async Task <int> Execute(CommandDefinition definition, ChangeObservable observable)
        {
            if (definition.BaseDefinition == newCommand)
            {
                Entity dataModel = entityFactory.Create(definition.Name, definition);
                IEnumerable <VirtualFile> files = await templateFileGenerator.InitalizeTemplate(dataModel, observable).ConfigureAwait(false);

                userInterface.WriteInformation($"Successfully created template '{dataModel.Template().name}' in {GetCommonPath(files,dataModel.Path)}.");
            }

            if (generateCommands.Contains(definition))
            {
                Entity dataModel = entityFactory.Create(definition.Name, definition);
                userInterface.WriteInformation(definition.Name == "all"
                                                   ? $"Generating all files for {dataModel.Root.Path}."
                                                   : $"Generating all files with the '{definition.Name}' " +
                                               $"generator for {dataModel.Root.Path}.");

                SingleValueArgument singleValueArgument = definition.Argument <SingleValueArgument>(Constants.OutputArgumentName);
                await templateFileGenerator.GenerateFiles(dataModel.Root, definition.Name, singleValueArgument.Value, singleValueArgument.IsDefined, observable)
                .ConfigureAwait(false);

                userInterface.WriteInformation(definition.Name == "all"
                                                   ? $"Successfully generated all files for {dataModel.Root.Path}."
                                                   : $"Successfully generated all files with the '{definition.Name}' " +
                                               $"generator for {dataModel.Root.Path}.");
            }

            if (definition == deployCommand)
            {
                Entity dataModel = entityFactory.Create(definition.Name, definition);

                deployService.DeployFiles(dataModel);

                Entity root = dataModel.Root;
                TemplateDescription template = TemplateEntity.Decorate(root).Template;
                foreach (templateDeployPostStep deployPostStep in template.DeployPostStep ?? Enumerable.Empty <templateDeployPostStep>())
                {
                    IDeployStep step = deploySteps.FirstOrDefault(s => s.Identifier == deployPostStep.identifier);
                    if (step != null)
                    {
                        step.Execute(dataModel, observable);
                    }
                    else
                    {
                        executionContext.WriteWarning(
                            $"Deploy post step '{deployPostStep.identifier}' could not be executed because there is no implementation. " +
                            $"Available implementations are:{Environment.NewLine}" +
                            $"{string.Join(Environment.NewLine, deploySteps.Select(d => d.Identifier))}");
                    }
                }

                userInterface.WriteInformation($"Successfully deployed all files for {dataModel.Root.Path}.");
            }

            return(0);

            string GetCommonPath(IEnumerable <VirtualFile> generatedFiles, string fallback)
            {
                IEnumerable <IEnumerable <VirtualDirectory> > paths = generatedFiles.Select(GetPath);
                VirtualDirectory commonDirectory = paths.Transpose()
                                                   .TakeWhile(d => d.Distinct().Count() == 1)
                                                   .FirstOrDefault()
                                                   ?.First();

                return(commonDirectory?.FullName ?? fallback);

                IEnumerable <VirtualDirectory> GetPath(VirtualFile file)
                {
                    VirtualDirectory current = file.Parent;

                    while (current != null)
                    {
                        yield return(current);

                        current = current.Parent;
                    }
                }
            }
        }
Esempio n. 4
0
 public override ActionResult DeleteCollection(string domainType, string instanceId, string propertyName, SingleValueArgument argument) => base.DeleteCollection(domainType, instanceId, propertyName, argument);
Esempio n. 5
0
 public override ActionResult PutProperty(string domainType, string instanceId, string propertyName, SingleValueArgument argument) => base.PutProperty(domainType, instanceId, propertyName, argument);
 public override ActionResult PostCollection(string domainType, string instanceId, string propertyName, SingleValueArgument argument)
 {
     return(base.PostCollection(domainType, instanceId, propertyName, argument));
 }
        public override Entity Resolve(Entity owner, string key, bool fallback = false)
        {
            templateRelationship[] relationships = owner.HasTemplate() ? owner.Template().Relationship : null;
            templateRelationship   relationship  = relationships?.FirstOrDefault(r => r.name.Equals(key, StringComparison.OrdinalIgnoreCase))
                                                   ?? relationships?.FirstOrDefault(r => r.name.Equals(key.Singular(), StringComparison.OrdinalIgnoreCase));
            TemplateDescription relationshipDescription = relationship != null?templateRepository.Template(relationship.type) : null;

            if (relationshipDescription != null)
            {
                return(GetRelationship());
            }
            if (key == EntityKeys.PathKey)
            {
                return(GetCommandPath());
            }
            if (key == EntityKeys.FullNameKey)
            {
                return(GetFullName());
            }

            return(GetArgument());

            Entity GetFullName()
            {
                SingleValueArgument nameArgument = owner.Value <CommandDefinition>()
                                                   .Argument <SingleValueArgument>(EntityKeys.NameKey);
                SingleValueArgument namespaceArgument = owner.Value <CommandDefinition>()
                                                        .Argument <SingleValueArgument>(EntityKeys.NamespaceKey);
                string fullName = codeLanguage.CombineNamespace(Value(namespaceArgument, EntityKeys.NamespaceKey),
                                                                Value(nameArgument, EntityKeys.NameKey));

                return(owner.Create(key, fullName, nameArgument, namespaceArgument));
            }

            Entity GetCommandPath()
            {
                SingleValueArgument singleValueArgument = owner.Value <CommandDefinition>()
                                                          .Argument <SingleValueArgument>(EntityKeys.OutputKey);
                string basePath = owner.IsRoot() ? string.Empty : owner.Root.Path;
                string path     = fileSystem.GetDirectory(Value(singleValueArgument), basePath, false).FullName;

                return(owner.Create(key, path, singleValueArgument));
            }

            string Value(Argument arg, string argumentName = null)
            {
                if (string.IsNullOrEmpty(argumentName))
                {
                    argumentName = key;
                }
                if (arg != null && arg is BoolArgument boolArgument)
                {
                    return(boolArgument.Value.ToString(CultureInfo.InvariantCulture));
                }

                SingleValueArgument singleValueArgument = (SingleValueArgument)arg;

                if (singleValueArgument?.Value != null)
                {
                    return(ResolveValue(singleValueArgument.Value));
                }

                if ((arg != null && arg.Name != argumentName && TryGetTemplateDefault(arg.Name, out string result)) ||
                    TryGetTemplateDefault(argumentName, out result))
                {
                    return(result);
                }

                return(string.Empty);
            }

            string ResolveValue(string value)
            {
                string result = templateResolver.Resolve(value, owner);

                if (TryGetTemplateFormat(out string format) &&
                    !string.IsNullOrEmpty(format))
                {
                    Entity temporary = owner.Create(key, result);
                    result = temporary.Format()[format].Value <string>();
                }
                return(result);

                bool TryGetTemplateFormat(out string formattedValue)
                {
                    TemplateDescription description = owner.HasTemplate() ? owner.Template() : null;

                    if (description != null)
                    {
                        templateArgumentDefinition templateArgument = description.Arguments.FirstOrDefault(a => a.name == key);
                        if (templateArgument != null)
                        {
                            formattedValue = templateArgument.format;
                            return(true);
                        }
                    }

                    formattedValue = null;
                    return(false);
                }
            }

            bool TryGetTemplateDefault(string argumentName, out string value)
            {
                TemplateDescription description = owner.HasTemplate() ? owner.Template() : null;

                if (description != null)
                {
                    templateArgumentDefinition templateArgument = description.Arguments.FirstOrDefault(a => a.name == argumentName);
                    if (templateArgument != null)
                    {
                        value = ResolveValue(templateArgument.@default);
                        return(true);
                    }
                }

                value = null;
                return(false);
            }

            Entity GetArgument()
            {
                Argument argument = owner.Value <CommandDefinition>().Argument <Argument>(key);

                if (argument == null)
                {
                    if (TryGetTemplateDefault(key, out string value))
                    {
                        return(owner.Create(key, value));
                    }
                    throw new ContentProviderException(key, owner);
                }

                switch (argument)
                {
                case BoolArgument boolArgument:
                    return(owner.Create(key, Value(argument), boolArgument));

                case SingleValueArgument singleValueArgument:
                    return(owner.Create(key, Value(argument), singleValueArgument));

                case MultipleValueArgument multipleValueArgument:
                    IEnumerable <Entity> values = Values(multipleValueArgument);
                    return(owner.Create(key, values));

                default:
                    throw new InvalidOperationException($"Unkown argument type {argument.GetType()}");
                }

                IEnumerable <Entity> Values(MultipleValueArgument multipleValueArgument)
                {
                    if (multipleValueArgument.Values != null)
                    {
                        return(multipleValueArgument.Values
                               .Select(s => owner.Create(key,
                                                         ResolveValue(s),
                                                         multipleValueArgument)));
                    }

                    if (TryGetTemplateDefault(key, out string result))
                    {
                        return(new[] { owner.Create(key, result, multipleValueArgument) });
                    }

                    throw new ContentProviderException(key, owner);
                }
            }

            Entity GetRelationship()
            {
                if (relationshipDescription.isRoot)
                {
                    return(owner.Root);
                }

                if (relationship.multiplicity == multiplicity.One)
                {
                    string name = owner.Value <CommandDefinition>()
                                  .Argument <SingleValueArgument>(relationship.name)
                                  .Value;
                    return(relationship.GetRelationship(relationshipDescription, owner, name));
                }

                IEnumerable <string> names = owner.Value <CommandDefinition>()
                                             .Argument <MultipleValueArgument>(relationship.name)
                                             .Values;

                return(relationship.GetRelationship(relationshipDescription, owner, names.ToArray()));
            }
        }