Skip to content

Commit

Permalink
SP5 Cookbook: recipe source code reader
Browse files Browse the repository at this point in the history
  • Loading branch information
swharden committed Nov 11, 2022
1 parent a7c16a7 commit 341d39e
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 11 deletions.
17 changes: 9 additions & 8 deletions src/ScottPlot5/ScottPlot5 Cookbook/Cookbook.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using NUnit.Framework.Internal.Execution;
using System.Reflection;

namespace ScottPlotCookbook;

Expand All @@ -8,27 +9,27 @@ namespace ScottPlotCookbook;
/// </summary>
public static class Cookbook
{
public static readonly string OutputFolder = GetCookbookFolder();
public static readonly string OutputFolder = Path.Combine(GetRepoFolder(), "dev/www/cookbook/5.0");

private static string GetCookbookFolder()
{
string defaultFolder = Path.GetFullPath(TestContext.CurrentContext.TestDirectory);
string cookbookOutputSubFolder = "dev/www/cookbook/5.0";
public static readonly string RecipeSourceFolder = Path.Combine(GetRepoFolder(), "src/ScottPlot5/ScottPlot5 Cookbook/Recipes");

private static string GetRepoFolder()
{
string defaultFolder = Path.GetFullPath(TestContext.CurrentContext.TestDirectory); ;
string? repoFolder = defaultFolder;
while (repoFolder is not null)
{
if (File.Exists(Path.Combine(repoFolder, "LICENSE")))
{
return Path.Combine(repoFolder, cookbookOutputSubFolder);
return repoFolder;
}
else
{
repoFolder = Path.GetDirectoryName(repoFolder);
}
}

return Path.Combine(defaultFolder, cookbookOutputSubFolder);
throw new InvalidOperationException($"repository folder not found in any folder above {defaultFolder}");
}

internal static List<Chapter> GetChapters() => Enum.GetValues<Chapter>().ToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void Generate()
{
if (!chapter.Pages.Any())
continue;
SB.AppendLine($"<h2 class='mt-5'>{chapter.ToString()}</h2>");
SB.AppendLine($"<h2 class='mt-5'>{chapter.Name}</h2>");
chapter.Pages.ForEach(x => AddPage(x));
}

Expand Down
15 changes: 15 additions & 0 deletions src/ScottPlot5/ScottPlot5 Cookbook/RecipeSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace ScottPlotCookbook;

internal struct RecipeSource
{
public string PageName { get; set; }
public string RecipeName { get; set; }
public string SourceCode { get; set; }

public RecipeSource(string page, string recipe, string source)
{
PageName = page;
RecipeName = recipe;
SourceCode = source;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>ScottPlotCookbook</RootNamespace>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
93 changes: 93 additions & 0 deletions src/ScottPlot5/ScottPlot5 Cookbook/SourceReading.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using FluentAssertions;
using System.Text;

namespace ScottPlotCookbook;

internal static class SourceReading
{
public static List<string> GetRecipeSourceFilePaths()
{
List<string> paths = new();

if (!Directory.Exists(Cookbook.RecipeSourceFolder))
throw new DirectoryNotFoundException(Cookbook.RecipeSourceFolder);

paths.AddRange(Directory.GetFiles(Cookbook.RecipeSourceFolder, "*.cs"));

foreach (string subFolder in Directory.GetDirectories(Cookbook.RecipeSourceFolder))
{

paths.AddRange(Directory.GetFiles(subFolder, "*.cs"));
}

if (!paths.Any())
throw new InvalidOperationException("no source files found");

return paths;
}

public static List<RecipeSource> GetRecipeSources()
{
List<RecipeSource> sources = new();

string recipeStartSignal = " {";
string recipeOverSignal = " }";

foreach (string path in GetRecipeSourceFilePaths())
{
string[] rawLines = File.ReadAllLines(path);
string pageNameSafe = string.Empty;
string recipeNameSafe = string.Empty;
StringBuilder sourceBeingExtended = new();
bool InRecipe = false;

foreach (string line in rawLines)
{
if (line.StartsWith(" PageName = "))
{
pageNameSafe = UrlTools.UrlSafe(line.Split('"')[1]);
continue;
}

if (line.StartsWith(" public override string Name ="))
{
recipeNameSafe = UrlTools.UrlSafe(line.Split('"')[1]);
continue;
}

if (!InRecipe && line.StartsWith(recipeStartSignal))
{
InRecipe = true;
continue;
}

if (InRecipe && line.StartsWith(recipeOverSignal))
{
string source = string.Join(Environment.NewLine, sourceBeingExtended);
sources.Add(new RecipeSource(pageNameSafe, recipeNameSafe, source));
InRecipe = false;
sourceBeingExtended.Clear();
continue;
}

bool lineIsWhitespace = line.Trim().Length == 0;
if (InRecipe)
sourceBeingExtended.AppendLine(lineIsWhitespace ? "" : line.Substring(8));
}
}

return sources;
}

[Test]
public static void Test_Recipe_Sources_Found()
{
List<RecipeSource> sources = GetRecipeSources();

sources.Should().NotBeEmpty();
sources.Should().HaveCount(Cookbook.GetRecipes().Count);

foreach (RecipeSource source in sources)
TestContext.WriteLine($"{source.PageName}/{source.RecipeName} ({source.SourceCode.Length} characters)");
}
}
2 changes: 1 addition & 1 deletion src/ScottPlot5/ScottPlot5/ScottPlot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<Version>5.1.0-beta</Version>
<Version>5.0.0-beta</Version>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
</PropertyGroup>

Expand Down

0 comments on commit 341d39e

Please sign in to comment.