Skip to content

Commit

Permalink
cookbook: use json method (obsolete flat-file method)
Browse files Browse the repository at this point in the history
  • Loading branch information
swharden committed Sep 28, 2021
1 parent 5e713fc commit 170d2a7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 57 deletions.
56 changes: 3 additions & 53 deletions src/cookbook/CookbookGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static void Main(string[] args)
}
else
{
Parser.Default.ParseArguments<CommandLineOptions>(args).WithParsed(RunOptions);
var res = Parser.Default.ParseArguments<CommandLineOptions>(args).WithParsed(RunOptions);
}
}

Expand All @@ -49,64 +49,14 @@ static void RunOptions(CommandLineOptions opts)
private static void GenerateEverything(string outputImageFolder, string outputCodeFolder, string cookbookFolder)
{
Stopwatch sw = Stopwatch.StartNew();

Console.WriteLine($"Generating images: {outputImageFolder}");
Chef.CreateCookbookImages(outputImageFolder);

Console.WriteLine($"Generating source: {outputCodeFolder}");
CreateMultiFileCookbookSource(cookbookFolder, outputCodeFolder);
CreateRecipesJson(cookbookFolder, Path.Combine(outputCodeFolder, "recipes.json"));
Chef.CreateRecipesJson(cookbookFolder, Path.Combine(outputCodeFolder, "recipes.json"));

Console.WriteLine($"Finished in {sw.Elapsed.TotalSeconds:F3} seconds");
}

private static void CreateMultiFileCookbookSource(string sourcePath, string outputPath)
{
outputPath = Path.GetFullPath(outputPath);
if (!Directory.Exists(outputPath))
Directory.CreateDirectory(outputPath);

RecipeSource[] sources = SourceParsing.GetRecipeSources(sourcePath, 600, 400);

foreach (RecipeSource recipe in sources)
{
StringBuilder sb = new();
sb.AppendLine("/// ID: " + recipe.ID);
sb.AppendLine("/// TITLE: " + recipe.Title);
sb.AppendLine("/// CATEGORY: " + recipe.Category);
sb.AppendLine("/// DESCRIPTION: " + recipe.Description);
sb.Append(recipe.Code);

string filePath = Path.Combine(outputPath, recipe.ID.ToLower() + ".cs");
File.WriteAllText(filePath, sb.ToString());
Debug.WriteLine($"Saved: {filePath}");
}
}

private static void CreateRecipesJson(string cookbookFolder, string saveFilePath)
{
RecipeSource[] recipes = SourceParsing.GetRecipeSources(cookbookFolder, 600, 400);

using var stream = File.OpenWrite(saveFilePath);
var options = new JsonWriterOptions() { Indented = true };
using var writer = new Utf8JsonWriter(stream, options);

writer.WriteStartObject();
writer.WriteString("version", ScottPlot.Plot.Version);
writer.WriteString("generated", DateTime.UtcNow);

writer.WriteStartArray("recipes");
foreach (RecipeSource recipe in recipes)
{
writer.WriteStartObject();
writer.WriteString("id", recipe.ID.ToLower());
writer.WriteString("category", recipe.Category);
writer.WriteString("title", recipe.Title);
writer.WriteString("description", recipe.Description);
writer.WriteString("code", recipe.Code.Replace("\r", ""));
writer.WriteEndObject();
}
writer.WriteEndArray();
writer.WriteEndObject();
}
}
}
33 changes: 29 additions & 4 deletions src/cookbook/ScottPlot.Cookbook/Chef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

Expand Down Expand Up @@ -51,10 +52,34 @@ public static void CreateCookbookImages(string outputPath, int width = 600, int
});
}

/// <summary>
/// Read all .cs files in the source folder to identify IRecipe source code, isolate just the recipe
/// component of each source file, and save the output as a text file using the recipe ID as its base filename.
/// </summary>
public static void CreateRecipesJson(string cookbookFolder, string saveFilePath, int width = 600, int height = 400)
{
RecipeSource[] recipes = SourceParsing.GetRecipeSources(cookbookFolder, width, height);

using var stream = File.OpenWrite(saveFilePath);
var options = new JsonWriterOptions() { Indented = true };
using var writer = new Utf8JsonWriter(stream, options);

writer.WriteStartObject();
writer.WriteString("version", ScottPlot.Plot.Version);
writer.WriteString("generated", DateTime.UtcNow);

writer.WriteStartArray("recipes");
foreach (RecipeSource recipe in recipes)
{
writer.WriteStartObject();
writer.WriteString("id", recipe.ID.ToLower());
writer.WriteString("category", recipe.Category);
writer.WriteString("title", recipe.Title);
writer.WriteString("description", recipe.Description);
writer.WriteString("code", recipe.Code.Replace("\r", ""));
writer.WriteEndObject();
}
writer.WriteEndArray();
writer.WriteEndObject();
}

[Obsolete("use json method", true)]
public static void CreateCookbookSource(string sourcePath, string outputPath, int width = 600, int height = 400)
{
outputPath = Path.GetFullPath(outputPath);
Expand Down
4 changes: 4 additions & 0 deletions src/cookbook/ScottPlot.Cookbook/ScottPlot.Cookbook.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
<Content Include="Images\*.*" CopyToOutputDirectory="Always" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Text.Json" Version="5.0.2" />
</ItemGroup>

</Project>

0 comments on commit 170d2a7

Please sign in to comment.