Skip to content

Commit

Permalink
Merge branch 'Add-source-and-destination-options'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérémie Bertrand committed Feb 20, 2015
2 parents c04ada6 + 6ccc89d commit 9e50d67
Show file tree
Hide file tree
Showing 13 changed files with 330 additions and 189 deletions.
32 changes: 25 additions & 7 deletions src/Pretzel.Logic/Commands/CommandParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.IO.Abstractions;
using System.Linq;

namespace Pretzel.Logic.Commands
Expand All @@ -16,21 +17,25 @@ namespace Pretzel.Logic.Commands
public class CommandParameters
{
[ImportingConstructor]
public CommandParameters([ImportMany] IEnumerable<IHaveCommandLineArgs> commandLineExtensions)
public CommandParameters([ImportMany] IEnumerable<IHaveCommandLineArgs> commandLineExtensions, IFileSystem fileSystem)
{
this.fileSystem = fileSystem;

port = 8080;
LaunchBrowser = true;

Settings = new OptionSet
{
{ "t|template=", "The templating engine to use", v => Template = v },
{ "d|directory=", "The path to site directory", p => Path = p },
{ "d|directory=", "[Obsolete, use --source instead] The path to site directory", p => Path = p },
{ "p|port=", "The port to test the site locally", p => decimal.TryParse(p, out port) },
{ "i|import=", "The import type", v => ImportType = v },
{ "f|file=", "Path to import file", v => ImportPath = v },
{ "s|source=", "The path to the source site (default current directory)", p => Path = p},
{ "destination=", "The path to the destination site (default _site)", d => DestinationPath = d},
{ "drafts", "Add the posts in the drafts folder", v => IncludeDrafts = true },
{ "nobrowser", "Do not launch a browser", v => LaunchBrowser = false },
{ "withproject", "Includes a layout VS Solution, to give intellisence when editing razor layout files", v => WithProject = (v!=null) },
{ "withproject", "Includes a layout VS Solution, to give intellisense when editing razor layout files", v => WithProject = (v!=null) },
{ "wiki", "Creates a wiki instead of a blog (razor template only)", v => Wiki = (v!=null) },
{ "cleantarget", "Delete the target directory (_site by default)", v => CleanTarget = true },
{ "safe", "Disable custom plugins", v => Safe = true }
Expand All @@ -57,12 +62,14 @@ public CommandParameters([ImportMany] IEnumerable<IHaveCommandLineArgs> commandL

public bool IncludeDrafts { get; private set; }

public bool CleanTarget { get; set; }
public bool CleanTarget { get; private set; }

public bool LaunchBrowser { get; private set; }

public bool Safe { get; private set; }

public string DestinationPath { get; private set; }

private decimal port;

public decimal Port
Expand All @@ -72,6 +79,8 @@ public decimal Port

private OptionSet Settings { get; set; }

private IFileSystem fileSystem;

public void Parse(IEnumerable<string> arguments)
{
var argumentList = arguments.ToArray();
Expand All @@ -82,12 +91,21 @@ public void Parse(IEnumerable<string> arguments)

if (firstArgument != null && !firstArgument.StartsWith("-") && !firstArgument.StartsWith("/"))
{
Path = System.IO.Path.IsPathRooted(firstArgument)
Path = fileSystem.Path.IsPathRooted(firstArgument)
? firstArgument
: System.IO.Path.Combine(Directory.GetCurrentDirectory(), firstArgument);
: fileSystem.Path.Combine(fileSystem.Directory.GetCurrentDirectory(), firstArgument);
}

Path = string.IsNullOrWhiteSpace(Path) ? Directory.GetCurrentDirectory() : System.IO.Path.GetFullPath(Path);
Path = string.IsNullOrWhiteSpace(Path) ? fileSystem.Directory.GetCurrentDirectory() : fileSystem.Path.GetFullPath(Path);

if (string.IsNullOrEmpty(DestinationPath))
{
DestinationPath = "_site";
}
if (!fileSystem.Path.IsPathRooted(DestinationPath))
{
DestinationPath = fileSystem.Path.Combine(Path, DestinationPath);
}
}

public void DetectFromDirectory(IDictionary<string, ISiteEngine> engines, SiteContext context)
Expand Down
22 changes: 11 additions & 11 deletions src/Pretzel.Logic/Templating/Context/SiteContextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public class SiteContextGenerator
private static readonly Regex categoryRegex = new Regex(@":category(\d*)", RegexOptions.Compiled);
private static readonly Regex slashesRegex = new Regex(@"/{1,}", RegexOptions.Compiled);

readonly Dictionary<string, Page> pageCache = new Dictionary<string, Page>();
readonly IFileSystem fileSystem;
readonly IEnumerable<IContentTransform> contentTransformers;
readonly List<string> includes = new List<string>();
readonly List<string> excludes = new List<string>();
private readonly Dictionary<string, Page> pageCache = new Dictionary<string, Page>();
private readonly IFileSystem fileSystem;
private readonly IEnumerable<IContentTransform> contentTransformers;
private readonly List<string> includes = new List<string>();
private readonly List<string> excludes = new List<string>();

[ImportingConstructor]
public SiteContextGenerator(IFileSystem fileSystem, [ImportMany]IEnumerable<IContentTransform> contentTransformers)
Expand All @@ -32,7 +32,7 @@ public SiteContextGenerator(IFileSystem fileSystem, [ImportMany]IEnumerable<ICon
this.contentTransformers = contentTransformers;
}

public SiteContext BuildContext(string path, bool includeDrafts)
public SiteContext BuildContext(string path, string destinationPath, bool includeDrafts)
{
try
{
Expand Down Expand Up @@ -63,7 +63,7 @@ public SiteContext BuildContext(string path, bool includeDrafts)
var context = new SiteContext
{
SourceFolder = path,
OutputFolder = Path.Combine(path, "_site"),
OutputFolder = destinationPath,
Posts = new List<Page>(),
Pages = new List<Page>(),
Config = config,
Expand Down Expand Up @@ -135,7 +135,6 @@ private IEnumerable<Page> BuildPosts(Dictionary<string, object> config, SiteCont
);
}


return posts;
}

Expand Down Expand Up @@ -175,7 +174,6 @@ private static void BuildTagsAndCategories(SiteContext context)
}
}
}

}

context.Tags = tags.Select(x => new Tag { Name = x.Key, Posts = x.Value }).OrderBy(x => x.Name).ToList();
Expand Down Expand Up @@ -240,7 +238,7 @@ private Page CreatePage(SiteContext context, IDictionary<string, object> config,
if (isPost)
{
if (header.ContainsKey("categories") && header["categories"] is IEnumerable<string>)
page.Categories = (IEnumerable<string>) header["categories"];
page.Categories = (IEnumerable<string>)header["categories"];
else if (header.ContainsKey("category"))
page.Categories = new[] { header["category"].ToString() };

Expand Down Expand Up @@ -468,11 +466,13 @@ private string GetPathWithTimestamp(string outputDirectory, string file)
return Path.Combine(outputDirectory, timestamp, title);
}

static readonly Regex TimestampAndTitleFromPathRegex = new Regex(@"\\(?:(?<timestamp>\d+-\d+-\d+)-)?(?<title>[^\\]*)\.[^\.]+$");
private static readonly Regex TimestampAndTitleFromPathRegex = new Regex(@"\\(?:(?<timestamp>\d+-\d+-\d+)-)?(?<title>[^\\]*)\.[^\.]+$");

public static string GetTitle(string file)
{
return TimestampAndTitleFromPathRegex.Match(file).Groups["title"].Value;
}

private string GetPageTitle(string file)
{
return Path.GetFileNameWithoutExtension(file);
Expand Down
7 changes: 4 additions & 3 deletions src/Pretzel.Logic/Templating/ISiteEngine.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System.ComponentModel.Composition;
using Pretzel.Logic.Templating.Context;
using System.ComponentModel.Composition;

namespace Pretzel.Logic.Templating
{
[InheritedExport]
public interface ISiteEngine
{
void Initialize();

bool CanProcess(SiteContext context);

void Process(SiteContext context, bool skipFileOnError = false);
string GetOutputDirectory(string path);
}
}
}
14 changes: 3 additions & 11 deletions src/Pretzel.Logic/Templating/JekyllEngineBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,20 @@ public void Process(SiteContext siteContext, bool skipFileOnError = false)
Context = siteContext;
PreProcess();

var outputDirectory = Path.Combine(Context.SourceFolder, "_site");

for (int index = 0; index < siteContext.Posts.Count; index++)
{
var p = siteContext.Posts[index];
var previous = GetPrevious(siteContext.Posts, index);
var next = GetNext(siteContext.Posts, index);
ProcessFile(outputDirectory, p, previous, next, skipFileOnError, p.Filepath);
ProcessFile(siteContext.OutputFolder, p, previous, next, skipFileOnError, p.Filepath);
}

for (int index = 0; index < siteContext.Pages.Count; index++)
{
var p = siteContext.Pages[index];
var previous = GetPrevious(siteContext.Pages, index);
var next = GetNext(siteContext.Pages, index);
ProcessFile(outputDirectory, p, previous, next, skipFileOnError);
ProcessFile(siteContext.OutputFolder, p, previous, next, skipFileOnError);
}
}

Expand All @@ -67,12 +65,6 @@ private static Page GetPrevious(IList<Page> pages, int index)
return index >= 1 ? pages[index - 1] : null;
}

// TODO factorize with outputDirectory in Process(...)?
public virtual string GetOutputDirectory(string path)
{
return Path.Combine(path, "_site");
}

private void ProcessFile(string outputDirectory, Page page, Page previous, Page next, bool skipFileOnError, string relativePath = "")
{
if (string.IsNullOrWhiteSpace(relativePath))
Expand Down Expand Up @@ -247,6 +239,7 @@ private void CreateOutputDirectory(string outputFile)
}

private static readonly string[] layoutExtensions = { ".html", ".htm" };

protected virtual string[] LayoutExtensions
{
get { return layoutExtensions; }
Expand Down Expand Up @@ -274,7 +267,6 @@ public bool CanProcess(SiteContext context)
return context.Engine == engineInfo.Engine;
}


private string FindLayoutPath(string layout)
{
foreach (var extension in LayoutExtensions)
Expand Down
5 changes: 4 additions & 1 deletion src/Pretzel.Tests/CommandParameterOutputTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Pretzel.Logic.Commands;
using Pretzel.Logic.Extensibility;
using System.IO;
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using Xunit;

Expand All @@ -13,7 +14,7 @@ public class CommandParameterOutputTests

public CommandParameterOutputTests()
{
subject = new CommandParameters(Enumerable.Empty<IHaveCommandLineArgs>());
subject = new CommandParameters(Enumerable.Empty<IHaveCommandLineArgs>(), new MockFileSystem());
writer = new StringWriter();
}

Expand All @@ -36,6 +37,8 @@ public void WriteOptions_WithNoParametersSpecified_DisplaysAll()
Assert.True(output.Contains("--file="));
Assert.True(output.Contains("--cleantarget"));
Assert.True(output.Contains("--safe"));
Assert.True(output.Contains("--source"));
Assert.True(output.Contains("--destination"));
}

[Fact]
Expand Down
Loading

0 comments on commit 9e50d67

Please sign in to comment.