Example #1
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);
        }
        /// <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));
        }