I'm trying to create FAKE(F# Make) build target that will update AssemblyInfo files in my project after sucsess build. Version info files are stored in TFS CVS, so I need to checkin updated files to TFS from FAKE build task.
Using TFS 2010, call FAKE from custom activity.
My flow is:
- ... cleanup, call build target
- Update AssemblyInfo files
- Check-in files to TFS
Faced with check-in to TFS issues...
Is there any way to check-in to TFS from FAKE (F# Make) target?
Yes, but this will come in two parts. Also, this is working for TFS 2015 new Build Definitions and TFS Git repository. You can modify this as necessary to fit your particular situation. These two targets can be called as you wish.
Part I - Update the AssemblyInfo files
let AssemblyInfoFiles = !! #"src\**\AssemblyInfo.cs";
Target "SetVersions" (fun _ ->
AssemblyInfoFiles
|> Seq.iter (fun a ->
if tfBuildNumber <> null then
if isLocalBuild = false then
UpdateAttributes a [ Attribute.FileVersion tfBuildNumber]
)
)
Part II - Check in the modified AssemblyInfo file(s)
let CheckInMessage = "Files related to versioning where checked in. ***NO_CI***"
let tfBuildSourceBranchName = environVar "BUILD_SOURCEBRANCHNAME"
let tfBuilderRepositoryProvider = environVar "BUILD_REPOSITORY_PROVIDER"
Target "CheckinFiles" (fun _ ->
AssemblyInfoFiles
|> Seq.iter (fun a ->
//This is for GIT REPOSITORY in TFS 2015 Build Definition
if isLocalBuild = false && tfBuilderRepositoryProvider = "TfsGit" then
checkoutBranch solutionDir tfBuildSourceBranchName
trace ("File to stage: " + a)
StageFile solutionDir a |> ignore
Commit solutionDir CheckInMessage
pull solutionDir "origin" tfBuildSourceBranchName
push solutionDir
)
)
The problem with Part II is that it does a commit for EACH AssemblyInfo, whereas I would like to batch these in one single commit. I can do that, I'm just lazy to fix it.
Related
I am using FxCop in FAKE but it is giving an error
i.e.
Analysis was not performed; at least one valid rules assembly and one valid target file must be specified.
* 1 total analysis engine exceptions.
While all targets are successfully build.
here is my code :
Target "FxCop" (fun _ ->
!! (buildDir + "/**/*.dll")
++ (buildDir + "/**/*.exe")
|> FxCop (fun p ->
{p with
//Override default parameters
ReportFileName = testDir + "FXCopResults.xml";
ToolPath = "D:/Fake/FAKE-Calculator/tools/FxCop/FxCopCmd.exe"})
)
It also shows : Project error : No targets were selected .
The FAKE documentation doesn't make it clear enough, but apparently you need to explicitly specify one of two things:
Which FxCop rules you want to run, or
The path to an "FxCop project file" (a file with the .FxCop extension).
I can't tell you how to write an FxCop project file since I've never done so myself, but maybe the programmer who set up the MsBuild system you've working with already did so. If he did, then you just need to add the following parameter to your FxCop call in your FAKE build script:
ProjectFile = buildDir </> "filename.FxCop"
where filename, of course, should be replaced by a real file name.
If you don't have an FxCop project file, then apparently you have to explicitly specify a list of FxCop rules in the RuleLibraries parameter. First, you need to find out which FxCop rules are available. To do that, look in your FxCop installation directory (on my system, where I have an older version of FxCop installed, it was C:\Program Files (x86)\Microsoft FxCop 1.36, but it may be different for you) for a Rules folder. That folder should contain several DLLs; for example, on my system, the Rules folder contained:
DesignRules.dll
GlobalizationRules.dll
InteroperabilityRules.dll
... and several other DLLs that I'm not going to bother typing out. Now you just make that list of filenames into an F# list:
RulesLibraries = ["DesignRules.dll"; "GlobalizationRules.dll"] // and so on
There should be sensible defaults for that, but currently it looks like you have to specify that list of rules by hand. So try writing your target to look something like this:
Target "FxCop" (fun _ ->
!! (buildDir + "/**/*.dll")
++ (buildDir + "/**/*.exe")
|> FxCop (fun p ->
{p with
//Override default parameters
ReportFileName = testDir + "FXCopResults.xml";
RulesLibraries = ["DesignRules.dll"; "GlobalizationRules.dll"] // etc.
ToolPath = "D:/Fake/FAKE-Calculator/tools/FxCop/FxCopCmd.exe"})
)
Remember to separate your list items with ; (semicolon): in F#, the , (comma) character is ONLY for tuples. And don't just copy my example verbatim, but actually look in your FxCop installation directory to see what rule DLLs are available, and include those. (As many, or as few, as your project actually needs).
Also, I don't know if you actually have to specify the .dll extension; you might be able to use ["DesignRules"; "GlobalizationRules"] (and so on). But it's probably just as simple to use the .dll extension and just copy and paste from the filenames.
I haven't tested this myself, so I hope this works for you. If it doesn't, let me know.
I created a standalone console application using the yo fsharp generator. The github repo concerning this particular question is here: https://github.com/KurtRMueller/PascalsTriangleKata.
I'd like to build and run some basic FsCheck.Xunit tests. I'm aware that I need to add some targets to FAKE's build.fsx, but as I'm quite new to .net and the C#/F# ecosystem, I don't know how to do that.
The example from the FAKE example page is as follows:
// define test dlls
let testDlls = !! (testDir + "/Test.*.dll")
Target "xUnitTest" (fun _ ->
testDlls
|> xUnit (fun p ->
{p with
ShadowCopy = false;
HtmlOutput = true;
XmlOutput = true;
OutputDir = testDir })
)
I'm not quite sure how to build the these Test dlls.
Any help is appreciated. Thanks.
I'm used to working with TeamCity so it might be that I should completely change my workflow, in that case answer with a suggestion of a new workflow instead.
In TeamCity I usually build and run unit tests as one build task (at every commit). Longer running tests are scheduled nightly and are run in the same way. So far I've manged to replicate the process in TFS. But on top of this I have a build task to deploy/publish a package. This is something I start manually once we are ready for it. This script references the artifact from a previous build (i.e. a drop folder or a drop zip in TFS).
I've read this article about deployment scripts but I can't find any information about how I can trigger them in TFS.
So the question in short: How do I reference a "drop as zip" or a drop folder instead of the source when building in TFS?
You can "Get Specific Build" or the "Latest Successful Build" on a specific Build, and then you can refer to that build's drop location.
Using TFS API, getting latest one should look something like this:
using (TfsTeamProjectCollection tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://tfsserver:8080/tfs/DefaultCollection")))
{
var buildServer= tpc.GetService<IBuildServer>();
var buildSpec = buildServer.CreateBuildDetailSpec(teamProjectName, buildDefinition);
buildSpec.InformationTypes = null;
buildSpec.MinFinishTime = DateTime.Now.AddHours(-lastXHours);
buildSpec.MaxBuildsPerDefinition = 1;
buildSpec.QueryOrder = Microsoft.TeamFoundation.Build.Client.BuildQueryOrder.FinishTimeDescending;
buildSpec.Status=Microsoft.TeamFoundation.Build.Client.BuildStatus.Succeeded;
var buildDetails = buildServer.QueryBuilds(buildSpec).Builds;
if (buildDetails.Length ==1){var dropLocation= buildDetails[0].DropLocation; }
else { Console.WriteLine("No builds found." );}
}
I'm writing a FAKE script which will basically do the following: -
1. Clean build outputs
2. Modify a configuration file with a specific value.
3. Perform a build.
4. Upload the outputs somewhere.
I've written all the individual tasks. What I now need to do is to set up a set of FAKE build steps to essentially repeat the above steps, once for each configuration value e.g. let's assume the configuration file had an attribute "colour". I want to repeat the above four build steps, and in step 2 use one of the values [ "black"; "blue"; "red"; "white" ].
What's the best way of achieving this? Should I just make one big build task that does all of this in one e.g. for loop (seems wrong)? Or create multiple build steps e.g. "Set Config to Blue" and "Set Config to Red" etc. and repeat the whole build flow for each colour (again, seems wrong)?
It's not very good documented, but you can create targets programmatically via TargetTemplateWithDependecies
I will add some docs.
You can do it like this
#r "tools/FAKE/tools/FakeLib.dll"
open Fake
Target "Clean" (fun _ ->
trace "Cleaning stuff..."
)
let config color = ignore()
Target "ConfigBlack" (fun _ ->
config "black"
)
Target "ConfigRed" (fun _ ->
config "red"
)
Target "Build" (fun _ ->
trace "Build solution"
)
Target "Upload" (fun _ ->
trace "Upload artifacts"
)
"Clean"
=?> ("ConfigBlack",hasBuildParam "black")
=?> ("ConfigRed",hasBuildParam "red")
==> "Build"
==> "Upload"
Run "Upload"
After that you will be able to call in like this build Upload black or build Upload red
The script below works but uses ExecutedTargets which I don't think you should use.
#I "tools/FAKE/tools"
#r "FakeLib.dll"
open Fake
let mutable value = "Foo"
Target "Clean" (fun _ ->
trace "clean target"
)
Target "Modify config file" (fun _ ->
trace (sprintf "===========> Modify config file: '%s'" value)
)
Target "Perform build" (fun _ ->
trace "Perform build"
)
Target "Default" (fun _ -> ())
"Clean"
==> "Modify config file"
==> "Perform build"
==> "Default"
["black"; "blue"; "red"; "white"]
|> List.iter(fun v ->
value <- v
Run <| getBuildParamOrDefault "target" "Default"
ExecutedTargets.Clear()
)
Can you use various configurations? For example:
BlueBuild:
[configsection]
[Color]Blue[/Color]
[/configsection]
RedBuild:
[configsection]
[Color]Red[/Color]
[/configsection]
See the following Articles:
How to select different app.config for several build configurations
http://www.hanselman.com/blog/managingmultipleconfigurationfileenvironmentswithprebuildevents.aspx
The way I've done this is to basically write a helper function which generates targets - much like the template dependency does - but also generates multiple instances of the "common" targets; each one differs based on the name of the variable element e.g.
let targets =
[ "Blue: Clean"
"Blue: Modify Config"
"Blue: Perform Build"
"Blue: Upload outputs"
"Red: Clean"
"Red: Modify Config"
"Red: Perform Build"
"Red: Upload outputs"
"Yellow: Clean"
"Yellow: Modify Config"
"Yellow: Perform Build"
"Yellow: Upload outputs" ]
You can then easily build then into a sequential chain of dependencies with e.g.
targets |> List.reduce (==>)
Whilst this is not exactly what I had hoped for, it works well enough for me and lets me see through Fake where things are.
In my build script, when I run it on my local machine (Win 8.1 x64) the whole script works perfectly fine.
When I run it on my build server (Jenkins, Server 2012 r2 x64), the FxCop task fails, because it cannot resolve any of the System.* assembly references which are required by some of the 3rd party libs I'm using.
I know full well that the version of FxCop (10.0) being used is the same, because it is checked into the project's git repo.
I don't understand why it works ok on my local machine, and not on the build server, but as I understand it from the results of my googling, I should be able to pass the /gac switch to FxCop in order to tell it that it should look there to resolve references it needs when scanning assemblies.
I just can't work out how to pass that switch to FxCop, using FAKE.
The target is as follows, but it's honestly essentially the same as the FxCop tutorial on the FAKE website, I've just removed the names of some dlls and exes.
Target "FxCop" (fun _ ->
!! (build ## "**/*.dll")
++ (build ## "**/*.exe")
|> FxCop (fun p ->
{p with
ReportFileName = testResults + "FXCopResults.xml";
ToolPath = "./tools/FxCop/FxCopCmd.exe" })
)
I've had a look at the source of the FxCopHelper, but my F# isn't that great, and there doesn't appear (to me) to be a way of passing extra command line options other than the ones already specified.
Does anyone have any suggestions?
The parameter you are looking for is UseGACSwitch and it is a boolean.
https://github.com/fsharp/FAKE/blob/master/src/app/FakeLib/FXCopHelper.fs#L77
To update your example:
Target "FxCop" (fun _ ->
!! (build ## "**/*.dll")
++ (build ## "**/*.exe")
|> FxCop (fun p ->
{p with
ReportFileName = testResults + "FXCopResults.xml";
UseGACSwitch = true;
ToolPath = "./tools/FxCop/FxCopCmd.exe" })
)
That should get you what you need for your build server to be happy. You may have already found this out or something else, but it is good that everyone knows there are options. Thank you. Good day.