Skip to content

Instantly share code, notes, and snippets.

@magicmonty
Last active May 14, 2016 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save magicmonty/858066629b08c0e2e310 to your computer and use it in GitHub Desktop.
Save magicmonty/858066629b08c0e2e310 to your computer and use it in GitHub Desktop.
Handling TFS checkout on paket install, paket update and paket add
@ECHO OFF
.paket\paket.bootstrapper.exe
IF ERRORLEVEL 1 (
EXIT /B %ERRORLEVEL%
)
IF "%1" == "update" GOTO checkout
IF "%1" == "add" GOTO checkout
IF "%1" == "install" GOTO checkout
GOTO runpaket
:checkout
packages\build\FAKE\tools\FAKE.exe paket.fsx
:runpaket
.paket\paket.exe %*
#r @"packages/build/FAKE/tools/Fakelib.dll"
#load "tfsHelper.fsx"
open Fake
open Fake.Paket
open Fake.TFSHelper
Target "CheckoutFiles" (fun _ ->
let dependendFiles =
!!"**/*.*proj"
++"paket.references"
++"paket.lock"
++"paket.dependencies"
dependendFiles
|> Seq.iter (fun df -> TFSCheckOutForEdit df)
)
RunTargetOrDefault "CheckoutFiles"
module Fake.TFSHelper
#r @"packages\build\Microsoft.TeamFoundation.All\lib\net45\Microsoft.TeamFoundation.Client.dll"
#r @"packages\build\Microsoft.TeamFoundation.VersionControl.All\lib\net45\Microsoft.TeamFoundation.VersionControl.Client.dll"
#r @"packages\build\Microsoft.TeamFoundation.VersionControl.All\lib\net45\Microsoft.TeamFoundation.VersionControl.Common.dll"
#r @"packages\build\FAKE\tools\FakeLib.dll"
open Microsoft.TeamFoundation.Client
open Microsoft.TeamFoundation.VersionControl.Client
open Fake
let isGitDir = directoryExists "./.git"
let workspaceInfo =
if isGitDir
then
trace "TFS: The current directory is managed by Git!"
None
else
match Workstation.Current.GetLocalWorkspaceInfo("tfsHelper.fsx") with
| ws when ws <> null ->
tracefn "TFS: The current directory is managed by TFS. Server URI: %s" (ws.ServerUri.AbsoluteUri)
Some ws
| _ ->
trace "TFS: Could not load workspace information"
None
let tfsServer =
match workspaceInfo with
| Some wsi -> Some (new TfsTeamProjectCollection(wsi.ServerUri))
| _ -> None
let workspace =
match (tfsServer, workspaceInfo) with
| Some s, Some wsi ->
let result = Some (wsi.GetWorkspace(s))
tracefn "Workspace: %s" result.Value.Name
let workingFolder = result.Value.Folders.[0]
tracefn "Local folder: %s" workingFolder.LocalItem
tracefn "Server folder: %s" workingFolder.ServerItem
result
| _ -> None
/// Checks, if there are pending changes on the assemblyInfo
let TFSHasPendingChanges () =
match workspace with
| Some ws -> not (ws.GetPendingChanges() |> Seq.isEmpty)
| _ -> false
let exists (filePath:string) =
(filePath |> directoryExists) || (filePath |> fileExists)
let TFSCheckOutForEdit (filepath:string) =
match workspace with
| Some ws ->
if filepath |> exists
then
tracefn @"TFS: Checking out ""%s""" filepath
filepath
|> ws.PendEdit
|> ignore
| _ -> ()
let TFSUndo (filepath:string) =
match workspace with
| Some ws ->
if filepath |> exists
then
tracefn @"TFS: Undoing all pending changes in ""%s""" filepath
filepath
|> ws.Undo
|> ignore
| _ -> ()
let TFSCheckInVersion version (filepath:string) =
match workspace with
| Some ws ->
let comment = sprintf "Updated version to %s in preparation of a deployment" version
let pendingChanges = ws.GetPendingChanges filepath
let mutable parameters = WorkspaceCheckInParameters(pendingChanges, comment)
parameters.OverrideGatedCheckIn <- true
parameters.PolicyOverride <- PolicyOverrideInfo("Automatic Build Process", null)
tracefn @"TFS: Checking in all pending changes in ""%s""" filepath
ws.CheckIn(parameters) |> ignore
| _ -> ()
let TFSCreateVersionLabel version =
match workspace with
| Some ws ->
let vcs = ws.VersionControlServer
let label = VersionControlLabel(vcs, version, null, null, "Deployed version " + version)
let folder = Seq.head(ws.Folders).ServerItem
let items = vcs.GetItems(folder, RecursionType.Full).Items
let lastChangeSet =
ws.VersionControlServer.QueryHistory(folder, RecursionType.Full)
|> Seq.head
let csVersion = VersionSpec.ParseSingleSpec(sprintf "C%i" lastChangeSet.ChangesetId, null)
let labelItems =
items
|> Seq.map (fun item ->
let itemSpec = ItemSpec(item.ServerItem, RecursionType.None)
LabelItemSpec(itemSpec, csVersion, false))
|> Seq.toArray
tracefn @"TFS: Creating label ""%s"" for folder ""%s""" version folder
vcs.CreateLabel(label, labelItems, LabelChildOption.Replace) |> ignore
| _ -> ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment