Exemplo n.º 1
0
        /// <summary>
        /// Creates a set of dependencies that references the decorator.
        /// </summary>
        /// <returns>A set of dependencies that references the decorator.</returns>
        public CodeDependencies CreateImportDependency()
        {
            // Source is optional; might be an ambient type (like "any").
            if (string.IsNullOrWhiteSpace(Import)) return CodeDependencies.Empty;

            // Construct dependencies referencing the mapped type.
            return CodeDependencies.FromImports(new[] { new Import(DecoratorName, Import) });
        }
        /// <summary>
        /// Is overridden to define how the type reference is translated by this strategy.
        /// </summary>
        /// <param name="referencedType">Type reference that should be tranlated.</param>
        /// <param name="translator">Full translator that can be used to translate parts of the complete type reference.</param>
        /// <returns>Result of the translation.</returns>
        protected override TypeReferenceTranslationResult Translate(Type referencedType, ITypeReferenceTranslator translator)
        {
            var translatedTypeArguments = referencedType.GetGenericArguments().Select(translator.Translate);
            var referencedTypeName      = referencedType.GetNameWithoutGenericTypeParameters()
                                          + $"<{ string.Join(", ", translatedTypeArguments.Select(x => x.ReferencedTypeName)) }>";

            return(new TypeReferenceTranslationResult(referencedTypeName,
                                                      CodeDependencies.FromCodeFragments(new[] { CodeFragmentId.ForClrType(referencedType.GetGenericTypeDefinition()) })
                                                      .Merge(translatedTypeArguments.Aggregate(CodeDependencies.Empty, (x, n) => x.Merge(n.Dependencies)))));
        }
        /// <summary>
        /// Creates a <see cref="TypeReferenceTranslationResult"/>.
        /// </summary>
        /// <param name="referencedTypeName">Name, by which the type should be referenced in the translation.</param>
        /// <param name="dependencies">Dependencies that are required by referencing the type.</param>
        public TypeReferenceTranslationResult(string referencedTypeName, CodeDependencies dependencies)
        {
            if (string.IsNullOrWhiteSpace(referencedTypeName))
            {
                throw new ArgumentException("ReferencedTypeName cannot be empty.", nameof(referencedTypeName));
            }

            ReferencedTypeName = referencedTypeName;
            Dependencies       = dependencies ?? throw new ArgumentNullException(nameof(dependencies));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a set of dependencies that references the mapped type.
        /// </summary>
        /// <returns>A set of dependencies that references the mapped type.</returns>
        public CodeDependencies CreateImportDependency()
        {
            // Source is optional; might be an ambient type (like "any").
            if (string.IsNullOrWhiteSpace(Source))
            {
                return(CodeDependencies.Empty);
            }

            // Construct dependencies referencing the mapped type.
            return(CodeDependencies.FromImports(new[] { new Import(Name, Source) }));
        }
        private string GenerateDependencyInitialization(CodeDependencies dependencies, ITemplatingEngine templatingEngine)
        {
            if (!Configuration.RuntimeDependencyLoading)
            {
                return(string.Empty);
            }

            var loadableDependencies = dependencies.CodeFragments
                                       .Where(x => x.TryRecreateClrType(out var type) && !type.IsInterface)
                                       .Select(x => x.ExportedName).Concat(dependencies.Imports.Select(x => x.Name));

            return(templatingEngine.UseTemplate("DependencyInitialization", new Dictionary <string, string>
            {
                { "Dependencies", string.Join(", ", loadableDependencies) }
            }));
        }
Exemplo n.º 6
0
 /// <summary>
 /// Creates a DecoratorTranslationResult.
 /// </summary>
 /// <param name="decoratorCode">TypeScript code that contains the decorators.</param>
 /// <param name="dependencies">Dependencies that are required for the decorators.</param>
 public DecoratorTranslationResult(string decoratorCode, CodeDependencies dependencies)
 {
     DecoratorCode = decoratorCode;
     Dependencies  = dependencies ?? throw new ArgumentNullException(nameof(dependencies));
 }
Exemplo n.º 7
0
 /// <summary>
 /// Is overridden to define how the type reference is translated by this strategy.
 /// </summary>
 /// <param name="referencedType">Type reference that should be tranlated.</param>
 /// <param name="translator">Full translator that can be used to translate parts of the complete type reference.</param>
 /// <returns>Result of the translation.</returns>
 public TypeReferenceTranslationResult Translate(Type referencedType)
 => new TypeReferenceTranslationResult(
     referencedType.Name,
     CodeDependencies.FromCodeFragments(new[] { CodeFragmentId.ForClrType(referencedType) }));
Exemplo n.º 8
0
        private string GeneratePropertyDefinitions(Type type, ITemplatingEngine templatingEngine, out CodeDependencies dependencies)
        {
            var propertyCodeSnippets = new List <string>();
            var deps = CodeDependencies.Empty;

            foreach (var property in type.GetProperties())
            {
                Logger.WriteInformation($"Translating property {property.Name} on type {type}.");

                var typeReferenceTranslation = TypeReferenceTranslator.Translate(property.PropertyType);
                deps = deps.Merge(typeReferenceTranslation.Dependencies);

                propertyCodeSnippets.Add(templatingEngine.UseTemplate("InterfacePropertyDefinition", new Dictionary <string, string>
                {
                    { "PropertyName", GetTypeScriptPropertyName(property) },
                    { "Documentation", GenerateDocumentationComment(property) },
                    { "PropertyType", typeReferenceTranslation.ReferencedTypeName }
                }));
            }

            dependencies = deps;
            return(string.Join(Environment.NewLine, propertyCodeSnippets));
        }