public TypeCollectionTester(ExtractedTypeCollection typeCollection)
        {
            _typeCollection = typeCollection;

            // TODO any way to define this?  Or maybe an option when getting type name
            _typeFormatter = new TypeScriptTypeFormatter(_typeCollection, new FakeTypePrefixer());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a new type formatter for typescript
        /// </summary>
        /// <param name="typeCollection"></param>
        /// <param name="resolver">The type prefix resolver</param>
        public TypeScriptTypeFormatter(ExtractedTypeCollection typeCollection, ITypePrefixResolver resolver)
        {
            _resolver = resolver;

            // Cache an index of the count of each name.
            // This will help for cases when a type has the same name, but a different number of type params because typescript doesn't allow it
            NameCountIndex = typeCollection.GetReferenceTypes().GroupBy(refType => GetIndexName(refType)).ToDictionary(grp => grp.Key, grp => grp.Count());
        }
Exemplo n.º 3
0
 public TypeWriteContext(
     IEnumerable <ExtractedType> types,
     ExtractedTypeCollection extractedTypes,
     string outputPath)
     : base(extractedTypes, outputPath)
 {
     IncludedTypes = types?.ToList() ?? (IEnumerable <ExtractedType>)Array.Empty <ExtractedType>();
 }
Exemplo n.º 4
0
 public ScriptWriteContext(
     ExtractedTypeCollection extractedTypes,
     string outputPath
     )
 {
     OutputPath     = outputPath;
     TypeCollection = extractedTypes;
 }
Exemplo n.º 5
0
 public ControllerContext(
     MvcController controller,
     string outputPath,
     ExtractedTypeCollection types,
     FetchFunctionResolver fetchResolver
     )
     : this(new [] { controller }, outputPath, types, fetchResolver)
 {
 }
Exemplo n.º 6
0
 public ControllerContext(
     IEnumerable <MvcController> controllers,
     string outputPath,
     ExtractedTypeCollection types,
     FetchFunctionResolver fetchResolver
     )
     : base(types, outputPath)
 {
     Controllers           = controllers;
     FetchFunctionResolver = fetchResolver;
 }
Exemplo n.º 7
0
        public PackageTester(ScriptPackage package, TypeFilter dispNameFilter, TypeFilter mvcActionFilter)
        {
            _typeCollection = new ExtractedTypeCollection(package, new ProcessorSettings()
            {
                TypeNamespace     = ReferenceTypeTester.TestNamespace,
                EnumNamespace     = EnumTester.TestNamespace,
                DisplayNameFilter = dispNameFilter,
                MvcActionFilter   = mvcActionFilter
            });

            // TODO not hardcode?
            _scriptWriter = new NamespaceTemplate();
            _scriptWriter = new ModuleTemplate();


            // TODO any way to define this?  Or maybe an option when getting type name
            _typeFormatter = new TypeScriptTypeFormatter(_typeCollection);
        }
 public string GetText(ExtractedTypeCollection extractedTypeCollection)
 {
     _typeCollection = extractedTypeCollection;
     _formatter      = new PrefixedTypeFormatter(extractedTypeCollection, "", "");         // Add no prefix to the types
     return(TransformText());
 }
Exemplo n.º 9
0
        /// <summary>
        /// Generates the scripts
        /// </summary>
        /// <returns>The script generation result</returns>
        public ScriptGenerationResult GenerateScripts(ScriptGenerationParameters parameters)
        {
            string        projectPath   = parameters.ProjectPath;
            ConfigOptions configOptions = ConfigParser.GetForProject(parameters.ProjectPath);

            if (parameters.TypeIterator == null)
            {
                return(new ScriptGenerationResult(false, $"A {typeof(ITypeIterator).Name} was not provided"));
            }

            if (configOptions == null || (!configOptions.Enabled && !parameters.Force))
            {
                return(new ScriptGenerationResult(false, $"Script generation is disabled in the configuration options."));
            }

            if (string.IsNullOrEmpty(configOptions.ServerObjectsResultFilepath))
            {
                return(new ScriptGenerationResult(false, "ResultFilePath is not specified in the configuration options."));
            }

            Uri projUri = new Uri(projectPath);

            Uri resultRelative;

            try
            {
                resultRelative = new Uri(configOptions.ServerObjectsResultFilepath, UriKind.RelativeOrAbsolute);
            }
            catch (UriFormatException)
            {
                return(new ScriptGenerationResult(false, "ResultFilePath is not in a valid format."));
            }

            Uri      resultAbsolute = resultRelative.IsAbsoluteUri ? resultRelative : new Uri(projUri, resultRelative);
            FileInfo fi             = new FileInfo(resultAbsolute.LocalPath);

            if (!fi.Directory.Exists)
            {
                return(new ScriptGenerationResult(false, $"The directory in ResultFilePath of the config file ({fi.Directory.FullName}) does not exist."));
            }

            ProcessorSettings processorSettings = new ProcessorSettings()
            {
                DefaultResultPath = resultAbsolute.LocalPath,
                ProjectPath       = projUri.LocalPath,
                NamingStrategy    = NamingStrategy.Create(configOptions.NameCasingConverter)
            };

            // At this point we are good
            TypeVisitor visitor = new TypeVisitor(processorSettings);

            parameters.TypeIterator.IterateTypes(visitor);

            ExtractedTypeCollection typeCollection        = visitor.TypeCollection;
            ScriptTemplateFactory   scriptTemplateFactory = new ScriptTemplateFactory(configOptions);

            // Write the object script text
            foreach (var typeGroup in typeCollection.GroupBy(t => t.TargetPath))
            {
                TypeWriteContext scriptContext = new TypeWriteContext(
                    typeGroup,
                    typeCollection,
                    typeGroup.Key
                    );

                var    typeTemplate = scriptTemplateFactory.CreateTypeTextTemplate();
                string scriptText   = typeTemplate.GetText(scriptContext);
                File.WriteAllText(typeGroup.Key, scriptText);
            }

            // Write MVC controllers
            FetchFunctionResolver fetchResolver = FetchFunctionResolver.FromConfig(projUri, configOptions);

            foreach (var controllerGroup in typeCollection.GetMvcControllers().GroupBy(c => c.ResultPath))
            {
                ControllerContext context = new ControllerContext(
                    controllerGroup,
                    controllerGroup.Key,
                    typeCollection,
                    fetchResolver
                    );

                var    controllerTemplate = scriptTemplateFactory.CreateControllerTextTemplate(context);
                string controllerScript   = controllerTemplate.GetText();
                File.WriteAllText(context.OutputPath, controllerScript);
            }

            return(new ScriptGenerationResult(true, null));
        }
 /// <summary>
 /// Creates a prefixed type formatter
 /// </summary>
 /// <param name="typeCollection">The collection of all types</param>
 /// <param name="typePrefix">The prefix to use for reference types</param>
 /// <param name="enumPrefix">The prefix to use for enums</param>
 public PrefixedTypeFormatter(ExtractedTypeCollection typeCollection, string typePrefix, string enumPrefix) : base(typeCollection)
 {
     _typePrefix = typePrefix;
     _enumPrefix = enumPrefix;
 }
Exemplo n.º 11
0
        /// <summary>
        /// Generates the scripts
        /// </summary>
        /// <returns>The script generation result</returns>
        public IScriptGenerationResult GenerateScripts()
        {
            if (ConfigurationOptions == null || !ConfigurationOptions.Enabled)
            {
                return(new ScriptGenerationResult(false, $"Script generation is disabled in the configuration options."));
            }

            if (string.IsNullOrEmpty(ConfigurationOptions.ServerObjectsResultFilepath))
            {
                return(new ScriptGenerationResult(false, "ResultFilePath is not specified in the configuration options."));
            }

            Uri projUri = new Uri(ProjectPath);

            Uri resultRelative;

            try
            {
                resultRelative = new Uri(ConfigurationOptions.ServerObjectsResultFilepath, UriKind.RelativeOrAbsolute);
            }
            catch (UriFormatException)
            {
                return(new ScriptGenerationResult(false, "ResultFilePath is not in a valid format."));
            }

            Uri      resultAbsolute = resultRelative.IsAbsoluteUri ? resultRelative : new Uri(projUri, resultRelative);
            FileInfo fi             = new FileInfo(resultAbsolute.LocalPath);

            if (!fi.Directory.Exists)
            {
                return(new ScriptGenerationResult(false, $"The directory in ResultFilePath of the config file ({fi.Directory.FullName}) does not exist."));
            }

            // At this point we are good
            ScriptPackage package = CreatePackage();


            ProcessorSettings processorSettings = new ProcessorSettings()
            {
                TypeNamespace = ConfigurationOptions.ClassNamespace,
                EnumNamespace = ConfigurationOptions.EnumNamespace
            };

            if (!string.IsNullOrEmpty(ConfigurationOptions.MvcActionAttributeName))
            {
                processorSettings.MvcActionFilter = new IsOfTypeFilter(ConfigurationOptions.MvcActionAttributeName);
            }

            ExtractedTypeCollection typeCollection = new ExtractedTypeCollection(package, processorSettings);
            IScriptTemplate         scriptGen      = ScriptTemplateFactory.GetTemplate(ConfigurationOptions.TemplateType);

            // Write the object script text
            string scriptText = scriptGen.CreateTypeTemplate().GetText(typeCollection);

            File.WriteAllText(resultAbsolute.LocalPath, scriptText);

            // Write MVC controllers
            Uri ajaxModuleUri         = string.IsNullOrEmpty(ConfigurationOptions.AjaxFunctionModulePath) ? null : new Uri(projUri, ConfigurationOptions.AjaxFunctionModulePath);
            ControllerContext context = new ControllerContext()
            {
                ServerObjectsResultFilepath = new Uri(resultAbsolute.LocalPath),
                AjaxFunctionName            = ConfigurationOptions.AjaxFunctionName,
                WebMethodNamespace          = ConfigurationOptions.WebMethodNamespace,
                ExtractedTypes         = typeCollection,
                AjaxFunctionModulePath = ajaxModuleUri
            };

            foreach (MvcControllerInfo controller in typeCollection.GetMvcControllers())
            {
                string outputPath       = GetControllerResultPath(controller);
                string controllerScript = scriptGen.CreateControllerTextTemplate().GetText(controller, context, new Uri(outputPath));
                File.WriteAllText(outputPath, controllerScript);
            }

            return(new ScriptGenerationResult(true, null));
        }
Exemplo n.º 12
0
 public string GetText(ExtractedTypeCollection typeCollection)
 {
     _typeCollection = typeCollection;
     _formatter      = new TypeScriptTypeFormatter(typeCollection);
     return(TransformText());
 }
Exemplo n.º 13
0
 /// <summary>
 /// Creates a new type visitor with the given settings
 /// </summary>
 /// <param name="settings">The processing settings</param>
 public TypeVisitor(ProcessorSettings settings)
 {
     TypeCollection = new ExtractedTypeCollection(settings);
 }