Example #1
0
        /// <summary>
        /// The static output generator - http://patternlab.io/docs/net-command-line.html
        /// </summary>
        /// <param name="destination">The name of the destination directory</param>
        /// <param name="enableCss">Generate CSS for each pattern. Currently unsupported</param>
        /// <param name="patternsOnly">Generate only the patterns. Does NOT clean the destination folder</param>
        /// <param name="noCache">Set the cacheBuster value to 0</param>
        /// <returns>The results of the generator</returns>
        public string Generate(string destination, bool? enableCss = null, bool? patternsOnly = null, bool? noCache = null)
        {
            var start = DateTime.Now;
            var content = new StringBuilder("configuring pattern lab...<br/>");
            var controller = new Controllers.PatternLabController { ControllerContext = _controllerContext };
            var url = new UrlHelper(_controllerContext.RequestContext);

            // Set location to copy from as root of app
            var sourceDirectory = new DirectoryInfo(HttpRuntime.AppDomainAppPath);
            var destinationDirectory = new DirectoryInfo(string.Format("{0}{1}\\", HttpRuntime.AppDomainAppPath, destination));

            // Determine value for {{ cacheBuster }} variable
            var cacheBuster = noCache.HasValue && noCache.Value ? "0" : _provider.CacheBuster();

            // If not only generating patterns, and cleanPubnlic config setting set to true clean destination directory
            if ((patternsOnly.HasValue && !patternsOnly.Value) || !patternsOnly.HasValue &&
                _provider.Setting("cleanPublic").Equals(bool.TrueString, StringComparison.InvariantCultureIgnoreCase))
            {
                // Clean all files
                CleanAll(destinationDirectory);

                // Copy all files and folders from source to public
                CopyAll(sourceDirectory, destinationDirectory);

                // Create 'Viewer' page
                var view = controller.Index();

                // Capture the view and write its contents to the file
                CreateFile(string.Format("~/{0}", PatternProvider.FileNameViewer), view.Capture(_controllerContext),
                    sourceDirectory, destinationDirectory);

                // Create latest-change.txt
                CreateFile("~/latest-change.txt", cacheBuster, sourceDirectory, destinationDirectory);

                // Create /styleguide/html/styleguide.html
                view = controller.ViewAll(string.Empty, enableCss.HasValue && enableCss.Value,
                    noCache.HasValue && noCache.Value);

                // Capture the view and write its contents to the file
                CreateFile(url.RouteUrl("PatternLabStyleguide"), view.Capture(_controllerContext), sourceDirectory,
                    destinationDirectory);

                // Parse embedded resources for required assets
                const string assetRootFolder = "styleguide";
                var assembly = Assembly.GetExecutingAssembly();
                var assetFolders = new[] {"css", "fonts", "html", "images", "js", "vendor"};
                var assetNamespace = string.Format("{0}.EmbeddedResources.{1}.", assembly.GetName().Name, assetRootFolder);
                var assetNames = assembly.GetManifestResourceNames().Where(r => r.Contains(assetNamespace));

                // Create assets from embedded resources
                foreach (var assetName in assetNames)
                {
                    var virtualPath = assetName.Replace(assetNamespace, string.Empty);
                    virtualPath = assetFolders.Aggregate(virtualPath,
                        (current, assetFolder) =>
                            current.Replace(string.Format("{0}.", assetFolder), string.Format("{0}/", assetFolder)));

                    var embeddedResource = new EmbeddedResource(assetName);

                    // Get the contents of the embedded resource and write it to the file
                    CreateFile(string.Format("~/{0}/{1}", assetRootFolder, virtualPath), embeddedResource.Open(),
                        sourceDirectory, destinationDirectory);
                }
            }

            // Clean all files in /patterns
            CleanAll(destinationDirectory.GetDirectories("patterns").FirstOrDefault());

            // Find all patterns that aren't hidden from navigation
            var patterns = _provider.Patterns().Where(p => !p.Hidden).ToList();
            var typeDashes =
                patterns.Where(p => !string.IsNullOrEmpty(p.SubType))
                    .Select(p => p.TypeDash)
                    .Where(s => !string.IsNullOrEmpty(s))
                    .Distinct()
                    .ToList();

            // Create view-all HTML files
            foreach (var typeDash in typeDashes)
            {
                var view = controller.ViewAll(typeDash, enableCss.HasValue && enableCss.Value,
                    noCache.HasValue && noCache.Value);

                // Capture the view and write its contents to the file
                CreateFile(url.RouteUrl("PatternLabViewAll", new { id = typeDash }), view.Capture(_controllerContext),
                    sourceDirectory, destinationDirectory);
            }

            // Create pattern files
            foreach (var pattern in patterns)
            {
                var virtualPath =
                    url.RouteUrl("PatternLabViewSingle", new { id = pattern.PathDash, path = pattern.PathDash }) ??
                    string.Empty;

                // Create .html
                var view = controller.ViewSingle(pattern.PathDash, PatternProvider.FileNameMaster, null,
                    enableCss.HasValue && enableCss.Value, noCache.HasValue && noCache.Value);

                // Capture the view and write its contents to the file
                CreateFile(virtualPath, view.Capture(_controllerContext), sourceDirectory, destinationDirectory);

                // Create .mustache
                view = controller.ViewSingle(pattern.PathDash, string.Empty, null, enableCss.HasValue && enableCss.Value,
                    noCache.HasValue && noCache.Value);

                // Capture the view and write its contents to the file
                CreateFile(
                    virtualPath.Replace(PatternProvider.FileExtensionHtml, PatternProvider.FileExtensionMustache),
                    view.Capture(_controllerContext), sourceDirectory, destinationDirectory);

                // Create .escaped.html
                view = controller.ViewSingle(pattern.PathDash, string.Empty, true, enableCss.HasValue && enableCss.Value,
                    noCache.HasValue && noCache.Value);

                // Capture the view and write its contents to the file
                CreateFile(
                    virtualPath.Replace(PatternProvider.FileExtensionHtml, PatternProvider.FileExtensionEscapedHtml),
                    view.Capture(_controllerContext), sourceDirectory, destinationDirectory);
            }

            // Determine the time taken the run the generator
            var elapsed = DateTime.Now - start;

            content.Append("your site has been generated...<br/>");
            content.AppendFormat("site generation took {0} seconds...<br/>", elapsed.TotalSeconds);

            // Randomly prints a saying after the generate is complete
            var random = new Random().Next(60);
            var sayings = new[]
            {
                "have fun storming the castle",
                "be well, do good work, and keep in touch",
                "may the sun shine, all day long",
                "smile",
                "namaste",
                "walk as if you are kissing the earth with your feet",
                "to be beautiful means to be yourself",
                "i was thinking of the immortal words of socrates, who said \"...i drank what?\"",
                "let me take this moment to compliment you on your fashion sense, particularly your slippers",
                "42",
                "he who controls the spice controls the universe",
                "the greatest thing you'll ever learn is just to love and be loved in return",
                "nice wand",
                "i don't have time for a grudge match with every poseur in a parka"
            };

            if (sayings.Length > random)
            {
                content.AppendFormat("{0}...<br />", sayings[random]);
            }

            // Display the results of the generator
            return content.ToString();
        }
Example #2
0
        /// <summary>
        /// The static output generator - http://patternlab.io/docs/net-command-line.html
        /// </summary>
        /// <param name="destination">The name of the destination directory</param>
        /// <param name="enableCss">Generate CSS for each pattern. Currently unsupported</param>
        /// <param name="patternsOnly">Generate only the patterns. Does NOT clean the destination folder</param>
        /// <param name="noCache">Set the cacheBuster value to 0</param>
        /// <returns>The results of the generator</returns>
        public string Generate(string destination, bool?enableCss = null, bool?patternsOnly = null, bool?noCache = null)
        {
            var start      = DateTime.Now;
            var content    = new StringBuilder("configuring pattern lab...<br/>");
            var controller = new Controllers.PatternLabController {
                ControllerContext = _controllerContext
            };
            var url = new UrlHelper(_controllerContext.RequestContext);

            // Set location to copy from as root of app
            var sourceDirectory      = new DirectoryInfo(HttpRuntime.AppDomainAppPath);
            var destinationDirectory = new DirectoryInfo(string.Format("{0}{1}\\", HttpRuntime.AppDomainAppPath, destination));

            // Determine value for {{ cacheBuster }} variable
            var cacheBuster = noCache.HasValue && noCache.Value ? "0" : _provider.CacheBuster();

            // If not only generating patterns, and cleanPubnlic config setting set to true clean destination directory
            if ((patternsOnly.HasValue && !patternsOnly.Value) || !patternsOnly.HasValue &&
                _provider.Setting("cleanPublic").Equals(bool.TrueString, StringComparison.InvariantCultureIgnoreCase))
            {
                // Clean all files
                CleanAll(destinationDirectory);

                // Copy all files and folders from source to public
                CopyAll(sourceDirectory, destinationDirectory);

                // Create 'Viewer' page
                var view = controller.Index();

                // Capture the view and write its contents to the file
                CreateFile(string.Format("~/{0}", PatternProvider.FileNameViewer), view.Capture(_controllerContext),
                           sourceDirectory, destinationDirectory);

                // Create latest-change.txt
                CreateFile("~/latest-change.txt", cacheBuster, sourceDirectory, destinationDirectory);

                // Create /styleguide/html/styleguide.html
                view = controller.ViewAll(string.Empty, enableCss.HasValue && enableCss.Value,
                                          noCache.HasValue && noCache.Value);

                // Capture the view and write its contents to the file
                CreateFile(url.RouteUrl("PatternLabStyleguide"), view.Capture(_controllerContext), sourceDirectory,
                           destinationDirectory);

                // Parse embedded resources for required assets
                const string assetRootFolder = "styleguide";
                var          assembly        = Assembly.GetExecutingAssembly();
                var          assetFolders    = new[] { "css", "fonts", "html", "images", "js", "vendor" };
                var          assetNamespace  = string.Format("{0}.EmbeddedResources.{1}.", assembly.GetName().Name, assetRootFolder);
                var          assetNames      = assembly.GetManifestResourceNames().Where(r => r.Contains(assetNamespace));

                // Create assets from embedded resources
                foreach (var assetName in assetNames)
                {
                    var virtualPath = assetName.Replace(assetNamespace, string.Empty);
                    virtualPath = assetFolders.Aggregate(virtualPath,
                                                         (current, assetFolder) =>
                                                         current.Replace(string.Format("{0}.", assetFolder), string.Format("{0}/", assetFolder)));

                    var embeddedResource = new EmbeddedResource(assetName);

                    // Get the contents of the embedded resource and write it to the file
                    CreateFile(string.Format("~/{0}/{1}", assetRootFolder, virtualPath), embeddedResource.Open(),
                               sourceDirectory, destinationDirectory);
                }
            }

            // Clean all files in /patterns
            CleanAll(destinationDirectory.GetDirectories("patterns").FirstOrDefault());

            // Find all patterns that aren't hidden from navigation
            var patterns   = _provider.Patterns().Where(p => !p.Hidden).ToList();
            var typeDashes =
                patterns.Where(p => !string.IsNullOrEmpty(p.SubType))
                .Select(p => p.TypeDash)
                .Where(s => !string.IsNullOrEmpty(s))
                .Distinct()
                .ToList();

            // Create view-all HTML files
            foreach (var typeDash in typeDashes)
            {
                var view = controller.ViewAll(typeDash, enableCss.HasValue && enableCss.Value,
                                              noCache.HasValue && noCache.Value);

                // Capture the view and write its contents to the file
                CreateFile(url.RouteUrl("PatternLabViewAll", new { id = typeDash }), view.Capture(_controllerContext),
                           sourceDirectory, destinationDirectory);
            }

            // Create pattern files
            foreach (var pattern in patterns)
            {
                var virtualPath =
                    url.RouteUrl("PatternLabViewSingle", new { id = pattern.PathDash, path = pattern.PathDash }) ??
                    string.Empty;

                // Create .html
                var view = controller.ViewSingle(pattern.PathDash, PatternProvider.FileNameMaster, null,
                                                 enableCss.HasValue && enableCss.Value, noCache.HasValue && noCache.Value);

                // Capture the view and write its contents to the file
                CreateFile(virtualPath, view.Capture(_controllerContext), sourceDirectory, destinationDirectory);

                // Create .mustache
                view = controller.ViewSingle(pattern.PathDash, string.Empty, null, enableCss.HasValue && enableCss.Value,
                                             noCache.HasValue && noCache.Value);

                // Capture the view and write its contents to the file
                CreateFile(
                    virtualPath.Replace(PatternProvider.FileExtensionHtml, PatternProvider.FileExtensionMustache),
                    view.Capture(_controllerContext), sourceDirectory, destinationDirectory);

                // Create .escaped.html
                view = controller.ViewSingle(pattern.PathDash, string.Empty, true, enableCss.HasValue && enableCss.Value,
                                             noCache.HasValue && noCache.Value);

                // Capture the view and write its contents to the file
                CreateFile(
                    virtualPath.Replace(PatternProvider.FileExtensionHtml, PatternProvider.FileExtensionEscapedHtml),
                    view.Capture(_controllerContext), sourceDirectory, destinationDirectory);
            }

            // Determine the time taken the run the generator
            var elapsed = DateTime.Now - start;

            content.Append("your site has been generated...<br/>");
            content.AppendFormat("site generation took {0} seconds...<br/>", elapsed.TotalSeconds);

            // Randomly prints a saying after the generate is complete
            var random  = new Random().Next(60);
            var sayings = new[]
            {
                "have fun storming the castle",
                "be well, do good work, and keep in touch",
                "may the sun shine, all day long",
                "smile",
                "namaste",
                "walk as if you are kissing the earth with your feet",
                "to be beautiful means to be yourself",
                "i was thinking of the immortal words of socrates, who said \"...i drank what?\"",
                "let me take this moment to compliment you on your fashion sense, particularly your slippers",
                "42",
                "he who controls the spice controls the universe",
                "the greatest thing you'll ever learn is just to love and be loved in return",
                "nice wand",
                "i don't have time for a grudge match with every poseur in a parka"
            };

            if (sayings.Length > random)
            {
                content.AppendFormat("{0}...<br />", sayings[random]);
            }

            // Display the results of the generator
            return(content.ToString());
        }