Skip to content

Commit 934e890

Browse files
committed
Adding FSharp support, including unit tests for the associated tasks
1 parent f000746 commit 934e890

File tree

2 files changed

+75
-17
lines changed

2 files changed

+75
-17
lines changed

src/NerdBank.GitVersioning.Tests/NerdBank.GitVersioning.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<EmbeddedResource Include="repos\submodules.7z" />
2121
</ItemGroup>
2222
<ItemGroup>
23+
<ProjectReference Include="..\MSBuildExtensionTask\MSBuildExtensionTask.csproj" />
2324
<ProjectReference Include="..\Nerdbank.GitVersioning.Tasks\Nerdbank.GitVersioning.Tasks.csproj" />
2425
<ProjectReference Include="..\NerdBank.GitVersioning\NerdBank.GitVersioning.csproj" />
2526
</ItemGroup>

src/Nerdbank.GitVersioning.Tasks/AssemblyVersionInfo.cs

Lines changed: 74 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.CodeDom;
55
using System.CodeDom.Compiler;
66
using System.Collections.Generic;
7-
using System.Diagnostics;
87
using System.IO;
98
using System.Linq;
109
using System.Reflection;
@@ -22,7 +21,7 @@ public class AssemblyVersionInfo : Task
2221
};
2322

2423
private CodeCompileUnit generatedFile;
25-
#else
24+
#endif
2625
private const string FileHeaderComment = @"------------------------------------------------------------------------------
2726
<auto-generated>
2827
This code was generated by a tool.
@@ -35,9 +34,7 @@ the code is regenerated.
3534
";
3635

3736
private CodeGenerator generator;
38-
39-
#endif
40-
37+
4138
[Required]
4239
public string CodeLanguage { get; set; }
4340

@@ -101,7 +98,17 @@ public override bool Execute()
10198
}
10299
else
103100
{
104-
this.Log.LogError("CodeDomProvider not available for language: {0}. No version info will be embedded into assembly.", this.CodeLanguage);
101+
// attempt to use local codegen
102+
string fileContent = this.BuildCode();
103+
if (fileContent != null)
104+
{
105+
Directory.CreateDirectory(Path.GetDirectoryName(this.OutputFile));
106+
Utilities.FileOperationWithRetry(() => File.WriteAllText(this.OutputFile, fileContent));
107+
}
108+
else
109+
{
110+
this.Log.LogError("CodeDomProvider not available for language: {0}. No version info will be embedded into assembly.", this.CodeLanguage);
111+
}
105112
}
106113

107114
return !this.Log.HasLoggedErrors;
@@ -205,24 +212,35 @@ private static CodeAttributeDeclaration DeclareAttribute(Type attributeType, par
205212
#else
206213

207214
public override bool Execute()
215+
{
216+
string fileContent = this.BuildCode();
217+
if (fileContent != null)
218+
{
219+
Directory.CreateDirectory(Path.GetDirectoryName(this.OutputFile));
220+
Utilities.FileOperationWithRetry(() => File.WriteAllText(this.OutputFile, fileContent));
221+
}
222+
return !this.Log.HasLoggedErrors;
223+
}
224+
225+
#endif
226+
227+
public string BuildCode()
208228
{
209229
this.generator = this.CreateGenerator();
210230
if (this.generator != null)
211231
{
212232
this.generator.AddComment(FileHeaderComment);
213233
this.generator.AddBlankLine();
214-
this.CreateAssemblyAttributes();
215-
this.CreateThisAssemblyClass();
216-
217-
Directory.CreateDirectory(Path.GetDirectoryName(this.OutputFile));
218-
string fileContent = this.generator.GetCode();
219-
Utilities.FileOperationWithRetry(() => File.WriteAllText(this.OutputFile, fileContent));
234+
this.generator.StartNamespace(this.RootNamespace ?? "AssemblyInfo");
235+
this.GenerateAssemblyAttributes();
236+
this.GenerateThisAssemblyClass();
237+
this.generator.EndNamespace();
238+
return this.generator.GetCode();
220239
}
221-
222-
return !this.Log.HasLoggedErrors;
240+
return null;
223241
}
224242

225-
private void CreateAssemblyAttributes()
243+
private void GenerateAssemblyAttributes()
226244
{
227245
this.generator.DeclareAttribute(typeof(AssemblyVersionAttribute), this.AssemblyVersion);
228246
this.generator.DeclareAttribute(typeof(AssemblyFileVersionAttribute), this.AssemblyFileVersion);
@@ -240,7 +258,7 @@ private void CreateAssemblyAttributes()
240258
}
241259
}
242260

243-
private void CreateThisAssemblyClass()
261+
private void GenerateThisAssemblyClass()
244262
{
245263
this.generator.StartThisAssemblyClass();
246264

@@ -290,6 +308,8 @@ private CodeGenerator CreateGenerator()
290308
case "visualbasic":
291309
case "vb":
292310
return new VisualBasicCodeGenerator();
311+
case "f#":
312+
return new FSharpCodeGenerator();
293313
default:
294314
this.Log.LogError("Code provider not available for language: {0}. No version info will be embedded into assembly.", this.CodeLanguage);
295315
return null;
@@ -315,6 +335,10 @@ internal CodeGenerator()
315335

316336
internal abstract void EndThisAssemblyClass();
317337

338+
internal virtual void StartNamespace(string ns) { }
339+
340+
internal virtual void EndNamespace() { }
341+
318342
internal string GetCode() => this.codeBuilder.ToString();
319343

320344
internal void AddBlankLine()
@@ -334,6 +358,39 @@ protected void AddCodeComment(string comment, string token)
334358
}
335359
}
336360

361+
private class FSharpCodeGenerator : CodeGenerator
362+
{
363+
internal override void AddComment(string comment)
364+
{
365+
this.AddCodeComment(comment, "//");
366+
}
367+
368+
internal override void AddThisAssemblyMember(string name, string value)
369+
{
370+
this.codeBuilder.AppendLine($" static member {name} = \"{value}\"");
371+
}
372+
373+
internal override void StartNamespace(string ns)
374+
{
375+
this.codeBuilder.AppendLine($"namespace {ns}");
376+
}
377+
378+
internal override void DeclareAttribute(Type type, string arg)
379+
{
380+
this.codeBuilder.AppendLine($"[<assembly: {type.FullName}(\"{arg}\")>]");
381+
}
382+
383+
internal override void EndThisAssemblyClass()
384+
{
385+
this.codeBuilder.AppendLine("do()");
386+
}
387+
388+
internal override void StartThisAssemblyClass()
389+
{
390+
this.codeBuilder.AppendLine("do()\r\ntype ThisAssembly() =");
391+
}
392+
}
393+
337394
private class CSharpCodeGenerator : CodeGenerator
338395
{
339396
internal override void AddComment(string comment)
@@ -389,7 +446,7 @@ internal override void EndThisAssemblyClass()
389446
this.codeBuilder.AppendLine("End Class");
390447
}
391448
}
392-
#endif
449+
//#endif
393450

394451
private static string ToHex(byte[] data)
395452
{

0 commit comments

Comments
 (0)