Example #1
0
        public PackageTester TestScriptControllerText(string controllerName)
        {
            Uri fakeOutputPath = new Uri(@"C:\FolderA\FolderB\FolderX\FolderY\SomeController.ts");

            ControllerContext _context = new ControllerContext()
            {
                AjaxFunctionName            = "TestAjax",
                WebMethodNamespace          = "MethodNamespace",
                ExtractedTypes              = _typeCollection,
                ServerObjectsResultFilepath = new Uri(@"C:\FolderA\FolderB\FolderC\FolderD\ServerObjects.ts"),
                AjaxFunctionModulePath      = new Uri(@"C:\FolderA\FolderB\FolderM\FolderN\AjaxFunc.ts")
            };

            string scriptText = _scriptWriter.CreateControllerTextTemplate().GetText(
                _typeCollection.GetMvcControllers().Where(c => c.Name == controllerName).First(),
                _context,
                fakeOutputPath
                );

            Assert.IsFalse(string.IsNullOrEmpty(scriptText));
            return(this);
        }
        /// <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));
        }