Skip to content

Commit d640d1e

Browse files
committed
Allow additional files to be copied as part of template extraction
1 parent fa41baa commit d640d1e

File tree

6 files changed

+35
-7
lines changed

6 files changed

+35
-7
lines changed

standard/documentation-comments.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ IDs:
711711

712712
**Fields** are represented by their fully qualified name.
713713

714-
<!-- IncompleteExample: {template:"standalone-lib", name:"IDStringsFields"} -->
714+
<!-- Example: {template:"standalone-lib", name:"IDStringsFields", additionalFiles:["Acme.cs"], ignoredWarnings:["CS0169","CS0649"]} -->
715715
```csharp
716716
namespace Acme
717717
{
@@ -756,7 +756,7 @@ IDs:
756756

757757
**Constructors**
758758

759-
<!-- IncompleteExample: {template:"standalone-lib", name:"IDStringsConstructors", replaceEllipsis:true} -->
759+
<!-- Example: {template:"standalone-lib", name:"IDStringsConstructors", replaceEllipsis:true, additionalFiles:["Acme.cs"]} -->
760760
```csharp
761761
namespace Acme
762762
{
@@ -779,7 +779,7 @@ IDs:
779779

780780
**Finalizers**
781781

782-
<!-- IncompleteExample: {template:"standalone-lib", name:"IDStringsFinalizers", replaceEllipsis: true} -->
782+
<!-- Example: {template:"standalone-lib", name:"IDStringsFinalizers", replaceEllipsis: true, additionalFiles:["Acme.cs"]} -->
783783
```csharp
784784
namespace Acme
785785
{
@@ -798,7 +798,7 @@ IDs:
798798

799799
**Methods**
800800

801-
<!-- IncompleteExample: {template:"standalone-lib", name:"IDStringsMethods", replaceEllipsis:true} -->
801+
<!-- IncompleteExample: {template:"standalone-lib", name:"IDStringsMethods", replaceEllipsis:true, additionalFiles:["Acme.cs"], ignoredWarnings:["CS0169","CS0649"]} -->
802802
```csharp
803803
namespace Acme
804804
{
@@ -878,7 +878,7 @@ IDs:
878878

879879
**Events**
880880

881-
<!-- IncompleteExample: {template:"standalone-lib", name:"IDStringsEvents"} -->
881+
<!-- Example: {template:"standalone-lib", name:"IDStringsEvents", additionalFiles:["Acme.cs"], ignoredWarnings:["CS0067"]} -->
882882
```csharp
883883
namespace Acme
884884
{

tools/ExampleExtractor/ExampleMetadata.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public class ExampleMetadata
2828
public bool InferOutput { get; set; }
2929
public string ExpectedException { get; set; }
3030

31+
/// <summary>
32+
/// Additional files to copy from the special "additional-files" template directory.
33+
/// </summary>
34+
public List<string> AdditionalFiles { get; set; }
35+
3136
// Information provided by the example extractor
3237
public string MarkdownFile { get; set; }
3338
public int StartLine { get; set; }

tools/ExampleExtractor/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
anyErrors = true;
6969
continue;
7070
}
71-
template.Apply(example, outputDirectory);
71+
template.Apply(example, outputDirectory, templateDirectory);
7272
}
7373
}
7474
Console.WriteLine("Finished example extraction.");

tools/ExampleExtractor/Template.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace ExampleExtractor;
44

55
internal class Template
66
{
7+
private const string AdditionalFilesDirectory = "additional-files";
78
private const string ExampleCodeSubstitution = "$example-code";
89
private const string ExampleNameSubstitution = "$example-name";
910

@@ -21,7 +22,8 @@ private Template(string name, Dictionary<string, string> files)
2122
/// </summary>
2223
/// <param name="example">The example to apply.</param>
2324
/// <param name="rootOutputDirectory">The root output directory. (A subdirectory for the example will be created within this.)</param>
24-
internal void Apply(Example example, string rootOutputDirectory)
25+
/// <param name="rootTemplateDirectory">The root template directory, used for finding additional files if necessary.</param>
26+
internal void Apply(Example example, string rootOutputDirectory, string rootTemplateDirectory)
2527
{
2628
var outputDirectory = Path.Combine(rootOutputDirectory, example.Name);
2729
Directory.CreateDirectory(outputDirectory);
@@ -33,6 +35,15 @@ internal void Apply(Example example, string rootOutputDirectory)
3335
.Replace(ExampleNameSubstitution, example.Name);
3436
File.WriteAllText(file, code);
3537
}
38+
if (example.Metadata.AdditionalFiles is List<string> additionalFiles)
39+
{
40+
foreach (var additionalFile in additionalFiles)
41+
{
42+
string sourceFile = Path.Combine(rootTemplateDirectory, AdditionalFilesDirectory, additionalFile);
43+
string destFile = Path.Combine(outputDirectory, additionalFile);
44+
File.Copy(sourceFile, destFile);
45+
}
46+
}
3647
var metadataJson = JsonConvert.SerializeObject(example.Metadata);
3748
File.WriteAllText(Path.Combine(outputDirectory, ExampleMetadata.MetadataFile), metadataJson);
3849
}
@@ -46,6 +57,7 @@ private static Template LoadTemplate(string directory)
4657
{
4758
var name = Path.GetFileName(directory);
4859
var files = Directory.GetFiles(directory)
60+
.Where(file => Path.GetFileName(file) != AdditionalFilesDirectory)
4961
.ToDictionary(file => Path.GetFileName(file), file => File.ReadAllText(file));
5062
return new Template(name, files);
5163
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Common types used in documentation-comments.md
2+
namespace Acme
3+
{
4+
enum Color { Red, Blue, Green }
5+
public interface IProcess {}
6+
public delegate void Del();
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This directory is not a template in itself, but contains additional
2+
files that can be copied into any example. This avoids having to
3+
create separate templates for each file which is common across
4+
multiple templates.

0 commit comments

Comments
 (0)