Skip to content

Allow additional files to be copied as part of template extraction #662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions standard/documentation-comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ IDs:

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

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

**Constructors**

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

**Finalizers**

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

**Methods**

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

**Events**

<!-- IncompleteExample: {template:"standalone-lib", name:"IDStringsEvents"} -->
<!-- Example: {template:"standalone-lib", name:"IDStringsEvents", additionalFiles:["Acme.cs"], ignoredWarnings:["CS0067"]} -->
```csharp
namespace Acme
{
Expand Down
5 changes: 5 additions & 0 deletions tools/ExampleExtractor/ExampleMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public class ExampleMetadata
public bool InferOutput { get; set; }
public string ExpectedException { get; set; }

/// <summary>
/// Additional files to copy from the special "additional-files" template directory.
/// </summary>
public List<string> AdditionalFiles { get; set; }

// Information provided by the example extractor
public string MarkdownFile { get; set; }
public int StartLine { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion tools/ExampleExtractor/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
anyErrors = true;
continue;
}
template.Apply(example, outputDirectory);
template.Apply(example, outputDirectory, templateDirectory);
}
}
Console.WriteLine("Finished example extraction.");
Expand Down
14 changes: 13 additions & 1 deletion tools/ExampleExtractor/Template.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace ExampleExtractor;

internal class Template
{
private const string AdditionalFilesDirectory = "additional-files";
private const string ExampleCodeSubstitution = "$example-code";
private const string ExampleNameSubstitution = "$example-name";

Expand All @@ -21,7 +22,8 @@ private Template(string name, Dictionary<string, string> files)
/// </summary>
/// <param name="example">The example to apply.</param>
/// <param name="rootOutputDirectory">The root output directory. (A subdirectory for the example will be created within this.)</param>
internal void Apply(Example example, string rootOutputDirectory)
/// <param name="rootTemplateDirectory">The root template directory, used for finding additional files if necessary.</param>
internal void Apply(Example example, string rootOutputDirectory, string rootTemplateDirectory)
{
var outputDirectory = Path.Combine(rootOutputDirectory, example.Name);
Directory.CreateDirectory(outputDirectory);
Expand All @@ -33,6 +35,15 @@ internal void Apply(Example example, string rootOutputDirectory)
.Replace(ExampleNameSubstitution, example.Name);
File.WriteAllText(file, code);
}
if (example.Metadata.AdditionalFiles is List<string> additionalFiles)
{
foreach (var additionalFile in additionalFiles)
{
string sourceFile = Path.Combine(rootTemplateDirectory, AdditionalFilesDirectory, additionalFile);
string destFile = Path.Combine(outputDirectory, additionalFile);
File.Copy(sourceFile, destFile);
}
}
var metadataJson = JsonConvert.SerializeObject(example.Metadata);
File.WriteAllText(Path.Combine(outputDirectory, ExampleMetadata.MetadataFile), metadataJson);
}
Expand All @@ -46,6 +57,7 @@ private static Template LoadTemplate(string directory)
{
var name = Path.GetFileName(directory);
var files = Directory.GetFiles(directory)
.Where(file => Path.GetFileName(file) != AdditionalFilesDirectory)
.ToDictionary(file => Path.GetFileName(file), file => File.ReadAllText(file));
return new Template(name, files);
}
Expand Down
7 changes: 7 additions & 0 deletions tools/example-templates/additional-files/Acme.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Common types used in documentation-comments.md
namespace Acme
{
enum Color { Red, Blue, Green }
public interface IProcess {}
public delegate void Del();
}
4 changes: 4 additions & 0 deletions tools/example-templates/additional-files/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This directory is not a template in itself, but contains additional
files that can be copied into any example. This avoids having to
create separate templates for each file which is common across
multiple templates.