Skip to content
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

Add Visual Basic support to AssemblyFileInfo task and make Namespace optional in config #471

Merged
merged 2 commits into from
Jun 16, 2014
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
79 changes: 57 additions & 22 deletions src/app/FakeLib/AssemblyInfoFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ let private NormalizeVersion version =
else version

/// Represents options for configuring the emission of AssemblyInfo
type AssemblyInfoFileConfig =
{ /// If true, a module (for F#) or static class (for C#), which contains the assembly version, will be generated
GenerateClass : bool
// The namespace into which assembly info will be generated; defaults to "System"
UseNamespace : string }
static member Default =
{ GenerateClass = true
UseNamespace = "System" }
type AssemblyInfoFileConfig
( // If true, a module (for F#) or static class (for C#), which contains the assembly version, will be generated
generateClass : bool,
// The optional namespace into which assembly info will be generated; defaults to "System".
?useNamespace : string ) =
member x.GenerateClass = generateClass
member x.UseNamespace =
match useNamespace with
| Some n -> n
| None -> "System"
static member Default = AssemblyInfoFileConfig(true)

/// Represents AssemblyInfo attributes
type Attribute(name, value, inNamespace) =
Expand Down Expand Up @@ -115,34 +118,35 @@ let private getAssemblyVersionInfo attributes =

/// Creates a C# AssemblyInfo file with the given attributes and configuration.
/// The generated AssemblyInfo file contains an AssemblyVersionInformation class which can be used to retrieve the current version no. from inside of an assembly.
let CreateCSharpAssemblyInfoWithConfig outputFileName attributes config =
let CreateCSharpAssemblyInfoWithConfig (outputFileName, attributes, config : AssemblyInfoFileConfig) =
traceStartTask "AssemblyInfo" outputFileName
let { GenerateClass = generateClass; UseNamespace = useNamespace } = config
let generateClass, useNamespace = config.GenerateClass, config.UseNamespace

let attributeLines =
"// <auto-generated/>" :: (getDependencies attributes |> List.map (sprintf "using %s;"))
@ [ "" ]
@ (attributes
|> Seq.toList
|> List.map (fun (attr : Attribute) -> sprintf "[assembly: %sAttribute(%s)]" attr.Name attr.Value))
@ [ sprintf "namespace %s {" useNamespace ]

let sourceLines =
if generateClass then
[ " internal static class AssemblyVersionInformation {"

let sourceLines =
if generateClass then
[ sprintf "namespace %s {" useNamespace
" internal static class AssemblyVersionInformation {"
sprintf " internal const string Version = %s;" (getAssemblyVersionInfo attributes)
" }" ]
" }"
"}" ]
else []

attributeLines @ sourceLines @ [ "}" ]
attributeLines @ sourceLines
|> writeToFile outputFileName
traceEndTask "AssemblyInfo" outputFileName

/// Creates a F# AssemblyInfo file with the given attributes and configuration.
/// The generated AssemblyInfo file contains an AssemblyVersionInformation class which can be used to retrieve the current version no. from inside of an assembly.
let CreateFSharpAssemblyInfoWithConfig outputFileName attributes config =
let CreateFSharpAssemblyInfoWithConfig (outputFileName, attributes, config : AssemblyInfoFileConfig) =
traceStartTask "AssemblyInfo" outputFileName
let { GenerateClass = generateClass; UseNamespace = useNamespace } = config
let generateClass, useNamespace = config.GenerateClass, config.UseNamespace

let sourceLines =
let required =
Expand All @@ -163,12 +167,43 @@ let CreateFSharpAssemblyInfoWithConfig outputFileName attributes config =
sourceLines |> writeToFile outputFileName
traceEndTask "AssemblyInfo" outputFileName

/// Creates a VB AssemblyInfo file with the given attributes and configuration.
/// The generated AssemblyInfo file contains an AssemblyVersionInformation class which can be used to retrieve the current version no. from inside of an assembly.
let CreateVisualBasicAssemblyInfoWithConfig (outputFileName, attributes, config : AssemblyInfoFileConfig) =
traceStartTask "AssemblyInfo" outputFileName
let generateClass, useNamespace = config.GenerateClass, config.UseNamespace

let attributeLines =
"' <auto-generated/>" :: (getDependencies attributes |> List.map (sprintf "Imports %s"))
@ [ "" ]
@ (attributes
|> Seq.toList
|> List.map (fun (attr : Attribute) -> sprintf "<assembly: %sAttribute(%s)>" attr.Name attr.Value))

let sourceLines =
if generateClass then
[ sprintf "Namespace %s" useNamespace
" Friend NotInheritable Class"
sprintf " Friend Const Version As String = %s" (getAssemblyVersionInfo attributes)
" End Class"
"End Namespace" ]
else []

attributeLines @ sourceLines
|> writeToFile outputFileName
traceEndTask "AssemblyInfo" outputFileName

/// Creates a C# AssemblyInfo file with the given attributes.
/// The generated AssemblyInfo file contains an AssemblyVersionInformation class which can be used to retrieve the current version no. from inside of an assembly.
let CreateCSharpAssemblyInfo outputFileName attributes =
CreateCSharpAssemblyInfoWithConfig outputFileName attributes AssemblyInfoFileConfig.Default
CreateCSharpAssemblyInfoWithConfig (outputFileName, attributes, AssemblyInfoFileConfig.Default)

/// Creates a F# AssemblyInfo file with the given attributes.
/// The generated AssemblyInfo file contains an AssemblyVersionInformation class which can be used to retrieve the current version no. from inside of an assembly.
let CreateFSharpAssemblyInfo outputFileName attributes =
CreateFSharpAssemblyInfoWithConfig outputFileName attributes AssemblyInfoFileConfig.Default
CreateFSharpAssemblyInfoWithConfig (outputFileName, attributes, AssemblyInfoFileConfig.Default)

/// Creates a VB AssemblyInfo file with the given attributes.
/// The generated AssemblyInfo file contains an AssemblyVersionInformation class which can be used to retrieve the current version no. from inside of an assembly.
let CreateVisualBasicAssemblyInfo outputFileName attributes =
CreateVisualBasicAssemblyInfoWithConfig (outputFileName, attributes, AssemblyInfoFileConfig.Default)
3 changes: 2 additions & 1 deletion src/test/Test.FAKECore/AssemblyInfoSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using Fake;
using Machine.Specifications;
using Microsoft.FSharp.Core;

namespace Test.FAKECore
{
Expand Down Expand Up @@ -34,7 +35,7 @@ public class when_using_fsharp_task_with_custom_config
{
It should_use_custom_namespace_and_not_emit_a_version_module = () =>
{
var customConfig = new AssemblyInfoFile.AssemblyInfoFileConfig(false, "Custom");
var customConfig = new AssemblyInfoFile.AssemblyInfoFileConfig(false, new FSharpOption<string>("Custom"));
string infoFile = Path.GetTempFileName();
var attributes = new[]
{
Expand Down