Example #1
0
        public SparkViewEngine GetSparkEngine <TView>(string templateDirPath) where TView : ISparkView
        {
            var viewFolder = new FileSystemViewFolder(templateDirPath);

            // Create an engine using the templates path as the root location
            // as well as the shared location
            return(new SparkViewEngine
            {
                DefaultPageBaseType = typeof(TView).FullName,
                ViewFolder = viewFolder.Append(new SubViewFolder(viewFolder, "Shared")),
            });
        }
Example #2
0
        public void SharingExtraFolders()
        {
            var normal        = new FileSystemViewFolder("Spark.Tests.Views");
            var otherLocation = new FileSystemViewFolder("Spark.Tests.Views\\Prefix");

            var viewFolder = new CombinedViewFolder(normal, new SubViewFolder(otherLocation, "Shared"));

            var normalSharedCount  = normal.ListViews("Shared").Count;
            var otherLocationCount = otherLocation.ListViews("").Count;
            var totalSharedCount   = viewFolder.ListViews("Shared").Count;

            Assert.AreEqual(normalSharedCount + otherLocationCount, totalSharedCount);
        }
Example #3
0
        public IBuilder Load(Options options)
        {
            var currentDirectory  = Environment.CurrentDirectory;
            var assemblyDirectory = Path.GetDirectoryName(typeof(SakeEngine).Assembly.Location);

            var settings = new SparkSettings()
                           .SetPageBaseType(typeof(BuilderBase))
                           .SetAutomaticEncoding(true)
                           .SetAttributeBehaviour(AttributeBehaviour.TextOriented)
                           .SetDebug(true);

            IViewFolder viewFolder = new FileSystemViewFolder(currentDirectory);

            foreach (var includeDir in options.IncludeDirectory)
            {
                var path = Path.Combine(currentDirectory, includeDir);
                viewFolder = new CombinedViewFolder(viewFolder, new FileSystemViewFolder(path));
                foreach (var file in Directory.EnumerateFiles(path, "*.dll"))
                {
                    try
                    {
                        Assembly.LoadFile(file);
                    }
                    catch
                    {
                    }
                }
            }

            viewFolder = new CombinedViewFolder(viewFolder, new FileSystemViewFolder(assemblyDirectory));

            var engine = new SparkViewEngine(settings)
            {
                ViewFolder       = viewFolder,
                ExtensionFactory = new ExtensionFactory(),
            };

            var descriptor = new SparkViewDescriptor
            {
                Templates = new[] { options.Makefile }
            };

            var builder = (BuilderBase)engine.CreateInstance(descriptor);

            builder.Output       = new StringWriter();
            builder.Log          = _log;
            builder.SakeSettings = _settings;
            builder.Render();
            return(builder);
        }
Example #4
0
        public static IViewExplorer CreateFromActiveDocument(IProjectExplorer projectExplorer)
        {
            if (projectExplorer == null || string.IsNullOrEmpty(projectExplorer.ActiveDocumentPath))
            {
                return(null);
            }
            var activeDocumentPath = projectExplorer.ActiveDocumentPath;

            int viewsLocationStart = activeDocumentPath.LastIndexOf("Views");
            var viewRoot           = activeDocumentPath.Substring(0, viewsLocationStart + 5);
            var currentView        = activeDocumentPath.Replace(viewRoot, string.Empty).TrimStart('\\');
            var viewFolder         = new FileSystemViewFolder(viewRoot);

            return(new ViewExplorer(viewFolder, currentView));
        }
Example #5
0
        public T CreateFrom <T>()
        {
            var settings = new SparkSettings().SetPageBaseType(typeof(T));

            var templates = new FileSystemViewFolder(@"C:\Two to Tango\Internal\Labs\BlogAnonymousObjectAsModelForSparkView\WebApplication1\Templates");

            var engine = new SparkViewEngine(settings)
            {
                ViewFolder = templates
            };

            var descriptor = new SparkViewDescriptor();

            descriptor.AddTemplate("test.sprk.htm");

            return((T)engine.CreateInstance(descriptor));
        }
        public ISparkViewEngine CreateViewEngine()
        {
            var settings = new SparkSettings()
                           .SetPageBaseType(typeof(SparkView))
                           .SetAutomaticEncoding(true)
                           .AddNamespace(typeof(string).Namespace)
                           .AddNamespace(typeof(Enumerable).Namespace)
                           .AddNamespace(typeof(Status).Namespace)
                           .AddNamespace(typeof(Run).Namespace);

            var dir       = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var templates = new FileSystemViewFolder(Path.Combine(dir, "Generation\\Spark\\Templates"));

            return(new SparkViewEngine(settings)
            {
                ViewFolder = templates
            });
        }
        public void ListViewsSameResults()
        {
            var filesystem = new FileSystemViewFolder("FileSystem\\Embedded");

            Assert.IsTrue(filesystem.HasView("Home\\Index.spark"));

            var files = filesystem.ListViews("home");

            Assert.AreEqual(2, files.Count);
            Assert.That(files.Any(f => Path.GetFileName(f) == "Index.spark"));
            Assert.That(files.Any(f => Path.GetFileName(f) == "List.spark"));

            var embedded = new EmbeddedViewFolder(Assembly.Load("Spark.Tests"), "Spark.Tests.FileSystem.Embedded");

            files = embedded.ListViews("home");
            Assert.AreEqual(2, files.Count);
            Assert.That(files.Any(f => Path.GetFileName(f) == "Index.spark"));
            Assert.That(files.Any(f => Path.GetFileName(f) == "List.spark"));
        }
Example #8
0
 public void Init()
 {
     _viewFolder = new FileSystemViewFolder("Spark.Tests.Views");
 }
Example #9
0
 public CachingViewFolder(string basePath)
 {
     _cache   = new InMemoryViewFolder();
     _disk    = new FileSystemViewFolder(basePath);
     BasePath = basePath;
 }
Example #10
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.Write(@"
Transforms Xml using a Spark template

XPARK templatefile [inputfile [outputfile]]

  templatefile  Path to a .spark file.
  inputfile     Source xml. Path to an file or url for http GET.
  outputfile    Target file to receive template output.

If inputfile or outputfile are not provided stdin and stdout are used.

The Model in the template is an XDocument loaded from the source.

The templatefile location may also contain _partial.spark files, and
a _global.spark file with common namespaces, macros, etc.
");
                return;
            }

            // Find the full path to the template file,
            // using current directory if argument isn't fully qualified
            var templatePath    = Path.Combine(Environment.CurrentDirectory, args[0]);
            var templateName    = Path.GetFileName(templatePath);
            var templateDirPath = Path.GetDirectoryName(templatePath);

            var viewFolder = new FileSystemViewFolder(templateDirPath);

            // Create an engine using the templates path as the root location
            // as well as the shared location
            var engine = new SparkViewEngine
            {
                DefaultPageBaseType = typeof(SparkView).FullName,
                ViewFolder          = viewFolder.Append(new SubViewFolder(viewFolder, "Shared"))
            };

            SparkView view;

            // compile and instantiate the template
            view = (SparkView)engine.CreateInstance(
                new SparkViewDescriptor()
                .AddTemplate(templateName));


            // load the second argument, or default to reading stdin
            if (args.Length >= 2)
            {
                view.Model = XDocument.Load(args[1]);
            }
            else
            {
                view.Model = XDocument.Load(XmlReader.Create(Console.OpenStandardInput()));
            }


            // write out to the third argument, or default to writing stdout
            if (args.Length >= 3)
            {
                using (var writer = new StreamWriter(new FileStream(args[2], FileMode.Create), Encoding.UTF8))
                {
                    view.RenderView(writer);
                }
            }
            else
            {
                using (var writer = new StreamWriter(Console.OpenStandardOutput(), Encoding.UTF8))
                {
                    view.RenderView(writer);
                }
            }
        }