コード例 #1
0
        private void InitializeTemplateAssets(TemplateConfiguration configuration)
        {
            var tmplAssets = this.assets
                .Where(a => a.Path.EndsWith("." + configuration.Extension))
                .ToList();

            foreach (var asset in tmplAssets)
            {
                this.assets.Remove(asset);
            }

            if (tmplAssets.Count > 0)
            {
                this.assets.Add(new TemplateAsset("templates/" + Guid.NewGuid().ToString().ToLower() + ".js", tmplAssets, configuration));
            }
        }
コード例 #2
0
        public void Should_add_templates_to_keyed_by_stripping_the_file_extension()
        {
            // Arrange
            var builder = new AssetBuilder();
            var a1 = builder.Path("tmpl/A.jst").Data("<h1 />").Build();
            var a2 = builder.Path("views/B.jst").Data("<h2 />").Build();

            var config = new TemplateConfiguration("JST", "tmpl", "jst");
            var tmpl = this.CreateTemplateAsset(config, a1, a2);

            // Act
            var data = tmpl.GetData();

            // Assert
            Assert.IsTrue(data.Contains("ns[\"tmpl/A\"] = lazyTemplate('<h1 />');"));
            Assert.IsTrue(data.Contains("ns[\"views/B\"] = lazyTemplate('<h2 />');"));
        }
コード例 #3
0
        public void Can_use_custom_name_casing()
        {
            // Arrange
            var builder = new AssetBuilder();
            var a1 = builder.Path("tMpL/A.jst").Data("<h1 />").Build();
            var a2 = builder.Path("VIEWS/b.jst").Data("<h2 />").Build();

            var config = new TemplateConfiguration("JST", "tmpl", "jst");
            config.NameCasing = p => p.ToLower();
            var tmpl = this.CreateTemplateAsset(config, a1, a2);

            // Act
            var data = tmpl.GetData();

            // Assert
            Assert.IsTrue(data.Contains("ns[\"tmpl/a\"] = lazyTemplate('<h1 />');"));
            Assert.IsTrue(data.Contains("ns[\"views/b\"] = lazyTemplate('<h2 />');"));
        }
コード例 #4
0
        public void Should_escape_single_quotes_for_asset_data()
        {
            // Arrange
            var builder = new AssetBuilder();
            var a1 = builder.Path("a.jst").Data("<a href='#' />").Build();
            var a2 = builder.Path("b.jst").Data("<a href=\\'#\\' />").Build();
            var a3 = builder.Path("c.jst").Data("<a href=\"#\" />").Build();

            var config = new TemplateConfiguration("JST", "tmpl", "jst");
            var tmpl = this.CreateTemplateAsset(config, a1, a2, a3);

            // Act
            var data = tmpl.GetData();

            // Assert
            Assert.IsTrue(data.Contains("ns[\"a\"] = lazyTemplate('<a href=\\'#\\' />');"));
            Assert.IsTrue(data.Contains("ns[\"b\"] = lazyTemplate('<a href=\\\\'#\\\\' />');"));
            Assert.IsTrue(data.Contains("ns[\"c\"] = lazyTemplate('<a href=\"#\" />');"));
        }
コード例 #5
0
        private void CreateStore()
        {
            string template         = @"Text\Store.js.t4";
            string templateBasePath = GetTemplateBasePath();

            if (!Path.IsPathRooted(template))
            {
                template = Path.Combine(templateBasePath, template);
            }
            template = new FileInfo(template).FullName;
            if (!template.StartsWith(templateBasePath))
            {
                throw new ArgumentException("Template Not found");
            }
            string templateCode = File.ReadAllText(template);

            var addProjectItemAction = new AddProjectItemAction();

            addProjectItemAction.Content        = Render(templateCode, template).ToString();
            addProjectItemAction.IsValid        = true;
            addProjectItemAction.TargetFileName = ModelClassName + "s.js";
            addProjectItemAction.Project        = CurrentProject;
            addProjectItemAction.Execute(DteHelper.FindInCollection(CurrentProject.ProjectItems, TemplateConfiguration.GetConfiguration().ExtRootFolderName + "\\Store"));
        }
コード例 #6
0
        public void Should_remove_common_prefixes()
        {
            // Arrange
            var builder = new AssetBuilder();
            var a1 = builder.Path("tmpl/a.jst").Data("<h1 />").Build();
            var a2 = builder.Path("tmpl/b.jst").Data("<h1 />").Build();
            var a3 = builder.Path("tmpl/c.jst").Data("<h1 />").Build();
            var a4 = builder.Path("tmpl/test/d.jst").Data("<h1 />").Build();

            var config = new TemplateConfiguration("JST", "tmpl", "jst");
            var tmpl = this.CreateTemplateAsset(config, a1, a2, a3, a4);

            // Act
            var data = tmpl.GetData();

            // Assert
            Assert.IsTrue(data.Contains("ns[\"a\"] = lazyTemplate('<h1 />');"));
            Assert.IsTrue(data.Contains("ns[\"b\"] = lazyTemplate('<h1 />');"));
            Assert.IsTrue(data.Contains("ns[\"c\"] = lazyTemplate('<h1 />');"));
            Assert.IsTrue(data.Contains("ns[\"test/d\"] = lazyTemplate('<h1 />');"));
        }
コード例 #7
0
 private TemplateAsset CreateTemplateAsset(TemplateConfiguration tmplConfiguration, params IAsset[] assets)
 {
     return new TemplateAsset(Guid.NewGuid().ToString(), assets, tmplConfiguration);
 }
コード例 #8
0
        public void Should_replace_line_breaks_with_new_line_characters()
        {
            // Arrange
            var builder = new AssetBuilder();
            var a1 = builder.Path("a.jst").Data(@"<h1>
            </h1>").Build();
            var a2 = builder.Path("b.jst").Data(@"<h1>
            </h1>").Build();

            var config = new TemplateConfiguration("JST", "tmpl", "jst");
            var tmpl = this.CreateTemplateAsset(config, a1, a2);

            // Act
            var data = tmpl.GetData();

            // Assert
            Assert.IsTrue(data.Contains("ns[\"a\"] = lazyTemplate('<h1>\\n</h1>');"));
            // Preserve whitespace.
            Assert.IsTrue(data.Contains("ns[\"b\"] = lazyTemplate('<h1>\\n    </h1>');"));
        }
コード例 #9
0
 public override bool OnBeginRecipe(object currentValue, out object newValue)
 {
     newValue = TemplateConfiguration.GetConfiguration().ExtRootNamespace;
     return(true);
 }
コード例 #10
0
 public JavaScriptPackage(string name, IList<IAsset> assets, IJavaScriptProcessor processor, IDebugState debugState, TemplateConfiguration configuration)
     : base(name, assets, processor, debugState)
 {
     this.InitializeTemplateAssets(configuration);
 }
コード例 #11
0
 public TemplateAsset(string path, IList<IAsset> templates, TemplateConfiguration configuration)
 {
     this.Path = path;
     this.templates = templates;
     this.configuration = configuration;
 }
コード例 #12
0
 public static IConfigurationBuilder AddJsonTemplateFile(this IConfigurationBuilder builder, string path, TemplateConfiguration templateConfiguration = null)
 {
     return(builder.AddJsonTemplateFile(path, false, templateConfiguration));
 }
コード例 #13
0
        public static IConfigurationBuilder AddJsonTemplateFile(this IConfigurationBuilder builder, string path, bool optional, bool reloadOnChange, TemplateConfiguration templateConfiguration = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException("File path must be a non-empty string.", nameof(path));
            }

            var source = new JsonTemplateConfigurationSource(path, templateConfiguration)
            {
                Optional       = optional,
                ReloadOnChange = reloadOnChange
            };

            return(builder.Add(source));
        }
コード例 #14
0
 public JsonTemplateConfigurationSource(string path, TemplateConfiguration templateConfiguration) : this()
 {
     TemplateConfiguration = templateConfiguration;
     Path = path;
 }
コード例 #15
0
        private List <Project> CreateProjectsForConfig(string moduleName, Configuration config,
                                                       TemplateConfiguration templateConfig)
        {
            var projects = new List <Project>();

            foreach (ProjectDelcaration declaration in templateConfig.ProjectDeclarations.Values)
            {
                string projectName = ExpandableVars.Instance.ExpandModuleNameInCopy(declaration.ProjectName, moduleName)
                                     .ToString();

                using (new ExpandableVars.ScopedVariable(ExpandableVars.Instance, ExpandableVars.VAR_PROJECT_NAME,
                                                         projectName))
                {
                    Log.Heading(
                        "Creating project config '{0} - {1}' for project '{2}' (module '{3}') with settings '{4}'",
                        config.GroupName, config.Name, projectName, moduleName, declaration.SettingsName);


                    using (new Log.ScopedIndent())
                    {
                        if (solution.IncludedProjectsPatterns.Count > 0 && !solution.CanIncludeProject(projectName))
                        {
                            Log.Info("Project '{0}' is excluded by solution '{1}' property white list",
                                     projectName, Settings.PROP_INCLUDE_PROJECTS);
                            excludedProjects.Add(projectName);
                            continue;
                        }

                        Settings projectSettings = templateConfig.ProjectSettingsLookup[declaration.SettingsName];
                        if (projectSettings.GetProperty <string>(Settings.PROP_EXCLUDE) == "true")
                        {
                            Log.Info("Project '{0}' is excluded from configuration '{1} - {2}'",
                                     projectName, config.GroupName, config.Name);
                            excludedProjects.Add(projectName);
                            continue;
                        }

                        projectSettings = projectSettings.ExpandVariablesInCopy();
                        string moduleSourcePath =
                            projectSettings.GetProperty <string>(Settings.PROP_PROJECT_SOURCE_PATH);

                        string guidStr = projectSettings.GetProperty <string>(Settings.PROP_GUID);
                        Guid   guid    = string.IsNullOrEmpty(guidStr) ? Guid.NewGuid() : Guid.Parse(guidStr);

                        // All configurations of a project must have the same guid.
                        if (!idLookup.TryGetValue(projectName, out Project.Identifier id))
                        {
                            string relativeSourcePath =
                                Path.GetRelativePath(solution.SolutionConfigDir, moduleSourcePath);

                            id = new Project.Identifier(projectName, guid, moduleSourcePath, relativeSourcePath);
                            idLookup[projectName] = id;
                        }

                        var project = new Project(solution, moduleName, id, config, projectSettings);

                        if (solution.IncludedProjectsPatterns.Count > 0)
                        {
                            string[] invalidProjectRefs = project.ProjectRefs
                                                          .Where(r => !solution.CanIncludeProject(r))
                                                          .ToArray();

                            if (invalidProjectRefs.Length > 0)
                            {
                                throw new InvalidProjectReferenceException(project,
                                                                           $"Referenced project is not in the '{Settings.PROP_INCLUDE_PROJECTS}' whitelist property." +
                                                                           $" Invalid references are [{string.Join(", ", invalidProjectRefs)}]");
                            }
                        }

                        projects.Add(project);
                    }
                }
            }

            return(projects);
        }