Skip to content

Instantly share code, notes, and snippets.

@Vidarls
Last active August 29, 2015 14:16
Show Gist options
  • Save Vidarls/5d615f2c9dd68118d231 to your computer and use it in GitHub Desktop.
Save Vidarls/5d615f2c9dd68118d231 to your computer and use it in GitHub Desktop.
Working first attempt at VB6 build helper for FAKE
//Example use
Target "BuildVb6" (fun _ ->
!! "src/**/*.vbp"
|> Vb6Make (fun c ->
{ c with
Logdir = temp
Outdir = bin })
)
[<AutoOpen>]
module Fake.Vb6helper
open Fake
open System
type Vb6BuildParams =
{ Toolpath:string;
Outdir:string;
Logdir:string;
Timeout:System.TimeSpan }
type Vb6BuildJob =
{ Path:string;
Name:string;
Started:System.DateTime;
Finished:System.DateTime;
IsStarted:bool;
IsFinished:bool;
LogFile:string;
IsSuccessful:bool;
ErrorMessage:string }
type Vb6BuildResult =
| Success
| Pending
| Failed of string
let public Vb6Make (getConfig: Vb6BuildParams->Vb6BuildParams) (vb6Projects: string seq) =
let defaultVb6BuildParams = {
Toolpath = ProgramFilesX86 + @"\Microsoft Visual Studio\VB98\VB6.exe"
Outdir = "bin"
Logdir = "temp"
Timeout = System.TimeSpan.FromMinutes 10.0
}
let config = defaultVb6BuildParams |> getConfig
let jobs = vb6Projects
|> List.ofSeq
|> List.map (fun p ->
let name = System.IO.Path.GetFileNameWithoutExtension p
{
Path = p
Name = name
Started = System.DateTime.Now
Finished = System.DateTime.Now
IsFinished = false
IsStarted = false
IsSuccessful = false
ErrorMessage = ""
LogFile = config.Logdir @@ (name + ".log")
})
let startBuild j =
let startResult = ExecProcess (fun i ->
i.FileName <- config.Toolpath
i.WorkingDirectory <- config.Logdir
i.Arguments <- sprintf "/m \"%s\" /out \"%s\" /outdir \"%s\"" j.Path j.LogFile config.Outdir
) config.Timeout
if startResult <> 0 then failwith (sprintf "Start build of %s failed" j.Name)
{j with IsStarted = true; Started = System.DateTime.Now}
let rec waitForFinish started finishedJobs pendingJobs =
let update j =
let getLogFileStatus j =
let logFile = System.IO.File.ReadAllText(j.LogFile)
match logFile with
| x when x.ToLower().Contains("succeeded") -> Success
| x when x.ToLower().Contains("failed") -> Failed(x)
| _ -> Pending
match System.IO.File.Exists(j.LogFile) with
| false -> j
| true -> match getLogFileStatus j with
| Pending -> j
| Success -> printfn "%s finished successfully after %A" j.Name (System.DateTime.Now - j.Started)
{j with IsFinished = true; IsSuccessful = true; Finished = System.DateTime.Now}
| Failed error -> printfn "%s failed after %A due to %s" j.Name (System.DateTime.Now - j.Started) error
{j with IsFinished = true; ErrorMessage = error; Finished = System.DateTime.Now}
let timeout timedOutJobs =
timedOutJobs |> List.map (fun j ->
printfn "%s has timed out after %A" j.Name (System.DateTime.Now - j.Started)
{j with IsFinished = true; IsSuccessful = false; ErrorMessage = "Timed out"})
let updated = pendingJobs |> List.map update
let finished = (updated |> List.filter (fun j -> j.IsFinished)) @ finishedJobs
let pending = updated |> List.filter(fun j -> not j.IsFinished)
match (pending, ((DateTime.Now - started) > config.Timeout) ) with
| [], _ -> finished
| _ , true -> waitForFinish started ((pending |> timeout) @ finished) []
| _ , false -> System.Threading.Thread.Sleep 500
waitForFinish started finished pending
let startTime = System.DateTime.Now
let completedWork = jobs
|> List.map startBuild
|> waitForFinish startTime []
let failedJobs = completedWork |> List.filter (fun j -> not j.IsSuccessful)
match failedJobs with
| [] -> printfn "Vb6 build completed successfully in %A" (System.DateTime.Now - startTime)
| _ -> failwith "Vb6 build failed after %A" (System.DateTime.Now - startTime)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment