Skip to content

Commit d781d7d

Browse files
committed
add better migrator tool
1 parent ebf3fe1 commit d781d7d

File tree

5 files changed

+121
-4
lines changed

5 files changed

+121
-4
lines changed

samples/README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
# Adding samples
22

3+
## New Samples
4+
35
To add a sample, run the the following:
46

57
```powershell
6-
./add-sample.ps1 [word|presentation|spreadsheet] sample-name
8+
./add-sample.ps1 area name
79
```
810

9-
This will scaffold out the projects in a common layout. Samples should be a single file, with the entrypoint being the command line so that `dotnet run -- [args]` can be used in the docs.
11+
This will create an initial scaffold for a sample and add it to the solution file.
1012

11-
In the future, we expect to set up .editorconfig/stylecop to enforce a shared style across the samples, but for now, the goal is to move the inline samples to this compilable solution.
13+
## Migrate old samples
14+
15+
```powershell
16+
./migrate-sample.ps1 path-to-md-file
17+
```
18+
19+
This will do an inital extraction and clean up of the file, as well as add the code to the solution. Additional clean up will be necessary, but should be minimal.
1220

1321
General changes to move a sample:
1422

1523
- Many examples give details on how to open a project; this can be removed
1624
- Sections about what `Dispose/Close/etc` is can be removed - this is an artifact from before `using` was common
1725
- Samples currently have a "How the Sample Works" section followed by the actual sample. Going forward, this will be collapsed to just the sample - any comments required will be in the cs/vb
18-
- Many users use the VB examples, so we will maintain them. Using docfx tabs allows us to hide the languages not needed by a viewer
26+
- Many users use the VB examples, so we will maintain them. Using docfx tabs allows us to hide the languages not needed by a viewer
27+
28+
## Code set up
29+
30+
In the future, we expect to set up .editorconfig/stylecop to enforce a shared style across the samples, but for now, the goal is to move the inline samples to this compilable solution.

samples/migrate-sample.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
param($path)
2+
3+
dotnet run "$(PSScriptRoot)\tools\migrator.csproj" -- $path

samples/samples.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "add_comment_cs", "presentat
1515
EndProject
1616
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "add_comment_vb", "presentation\add_comment\vb\add_comment_vb.vbproj", "{F5204024-B0D0-4106-AE8B-8CB1552C1F87}"
1717
EndProject
18+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "migrator", "tools\migrator\migrator.csproj", "{0470C4B3-18CE-4621-A41C-A1C70DDF8EAD}"
19+
EndProject
1820
Global
1921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2022
Debug|Any CPU = Debug|Any CPU
@@ -37,6 +39,10 @@ Global
3739
{F5204024-B0D0-4106-AE8B-8CB1552C1F87}.Debug|Any CPU.Build.0 = Debug|Any CPU
3840
{F5204024-B0D0-4106-AE8B-8CB1552C1F87}.Release|Any CPU.ActiveCfg = Release|Any CPU
3941
{F5204024-B0D0-4106-AE8B-8CB1552C1F87}.Release|Any CPU.Build.0 = Release|Any CPU
42+
{0470C4B3-18CE-4621-A41C-A1C70DDF8EAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
43+
{0470C4B3-18CE-4621-A41C-A1C70DDF8EAD}.Debug|Any CPU.Build.0 = Debug|Any CPU
44+
{0470C4B3-18CE-4621-A41C-A1C70DDF8EAD}.Release|Any CPU.ActiveCfg = Release|Any CPU
45+
{0470C4B3-18CE-4621-A41C-A1C70DDF8EAD}.Release|Any CPU.Build.0 = Release|Any CPU
4046
EndGlobalSection
4147
GlobalSection(SolutionProperties) = preSolution
4248
HideSolutionNode = FALSE

samples/tools/migrator/Program.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System.Diagnostics;
2+
using System.Text.RegularExpressions;
3+
4+
if (args is not [string path])
5+
{
6+
Console.WriteLine("Must supply a path");
7+
return;
8+
}
9+
10+
var samplesDir = Path.GetFullPath(Path.Combine(path, "..", "..", "samples"))!;
11+
12+
if (!Directory.Exists(samplesDir))
13+
{
14+
Console.WriteLine("Not a valid document");
15+
return;
16+
}
17+
18+
var text = File.ReadAllText(path);
19+
20+
text = Matchers.GetAssemblyDirective().Replace(text, string.Empty);
21+
text = Matchers.HowWorks().Replace(text, "##");
22+
23+
var csMatch = Matchers.Csharp().Match(text);
24+
var cs = csMatch.Groups[2].Value;
25+
var vbMatch = Matchers.Vb().Match(text);
26+
var vb = vbMatch.Groups[2].Value;
27+
28+
var area = Matchers.Area().Match(text).Groups[1].Value;
29+
Console.WriteLine($"Enter name for {Path.GetFileName(path)}");
30+
string name = Console.ReadLine() ?? throw new InvalidOperationException();
31+
32+
name = name.Replace("-", "_");
33+
34+
text = text.Replace(csMatch.Groups[1].Value, $"""
35+
### [CSharp](#tab/cs)
36+
[!code-csharp[](../samples/{area}/{name}/cs/Program.cs)]
37+
""");
38+
39+
text = text.Replace(vbMatch.Groups[1].Value, $"""
40+
### [CSharp](#tab/cs)
41+
[!code-vb[](../samples/{area}/{name}/vb/Program.vb)]
42+
""");
43+
44+
var thisSampleDir = Path.Combine(samplesDir, area, name);
45+
46+
var csDir = Path.Combine(thisSampleDir, "cs");
47+
Directory.CreateDirectory(csDir);
48+
var csProj = Path.Combine(csDir, $"{name}_cs.csproj");
49+
File.WriteAllText(csProj, """<Project Sdk="Microsoft.NET.Sdk" />""");
50+
File.WriteAllText(Path.Combine(csDir, "Program.cs"), cs);
51+
52+
var vbDir = Path.Combine(thisSampleDir, "vb");
53+
Directory.CreateDirectory(vbDir);
54+
var vbProj = Path.Combine(vbDir, $"{name}_vb.vbproj");
55+
File.WriteAllText(vbProj, """<Project Sdk="Microsoft.NET.Sdk" />""");
56+
File.WriteAllText(Path.Combine(vbDir, "Program.vb"), $"""
57+
Module Program `
58+
Sub Main(args As String())`
59+
End Sub`
60+
61+
{vb}
62+
End Module
63+
""");
64+
65+
File.WriteAllText(path, text);
66+
67+
Process.Start(new ProcessStartInfo("dotnet", $"sln add {csProj} --solution-folder {area}") { WorkingDirectory = samplesDir })!.WaitForExit();
68+
Process.Start(new ProcessStartInfo("dotnet", $"sln add {vbProj} --solution-folder {area}") { WorkingDirectory = samplesDir })!.WaitForExit();
69+
70+
partial class Matchers
71+
{
72+
[GeneratedRegex("""The following assembly directives.*?```vb.*?```""", RegexOptions.Singleline)]
73+
public static partial Regex GetAssemblyDirective();
74+
75+
[GeneratedRegex("## How the Sample Code Works .*?##", RegexOptions.Singleline)]
76+
public static partial Regex HowWorks();
77+
78+
[GeneratedRegex(".*(```csharp(.*?)```)", RegexOptions.Singleline)]
79+
public static partial Regex Csharp();
80+
81+
[GeneratedRegex(".*(```vb(.*?)```)", RegexOptions.Singleline)]
82+
public static partial Regex Vb();
83+
84+
[GeneratedRegex("./includes/(.*?)/structure\\.md")]
85+
public static partial Regex Area();
86+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

0 commit comments

Comments
 (0)