Last active
August 29, 2015 14:21
-
-
Save koenmetsu/29d4dfad4d50a9ac91ab to your computer and use it in GitHub Desktop.
FAKE deploy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// include Fake lib | |
#r @"FAKE/tools/FakeLib.dll" | |
open Fake | |
open System | |
open System.IO | |
// param names | |
let sourceEnvParam = "sourceEnv" | |
let destinationDirParam = "destinationDir" | |
// defaults | |
let defaultSource = "TST" | |
let defaultDestinationDir = "publish" | |
// helper methods | |
let getZipNameFor env = | |
sprintf "%s.zip" env | |
let checkRequiredParamOrThrow param = | |
if not (hasBuildParam param) then | |
failwith (sprintf "Required parameter \"%s\" not found!" param) | |
// variables | |
let sourceEnv = getBuildParam sourceEnvParam | |
let sourceZipName = getZipNameFor sourceEnv | |
let destinationDir = getBuildParam destinationDirParam | |
// targets | |
Target "CheckRequirements" (fun _ -> | |
checkRequiredParamOrThrow destinationDirParam | |
checkRequiredParamOrThrow sourceEnvParam | |
() | |
) | |
Target "BackupCurrentEnvironment" (fun _ -> | |
let backupDir = Path.Combine(destinationDir, "backups") | |
CreateDir backupDir | |
let rec getAllFiles dir pattern = | |
seq { | |
yield! Directory.EnumerateFiles(dir, pattern) | |
for d in Directory.EnumerateDirectories(dir) | |
|> Seq.filter (fun directory-> directory <> backupDir) | |
do | |
yield! getAllFiles d pattern | |
} | |
let formatCurrentTime = DateTime.Now.ToString("yyyyMMdd_HHmm") | |
let backupZipName = sprintf "%s.zip" formatCurrentTime | |
let backupZipPath = Path.Combine(backupDir, backupZipName) | |
getAllFiles destinationDir "*" | |
|> Seq.filter (fun fileName -> fileName <> backupZipPath) | |
|> Zip destinationDir backupZipPath | |
) | |
Target "CopyZipToDestination" (fun _ -> | |
CopyFile destinationDir sourceZipName | |
) | |
Target "UnzipAndDeleteInDestination" (fun _ -> | |
let destinationZip = Path.Combine(destinationDir, sourceZipName) | |
Unzip destinationDir destinationZip | |
DeleteFile destinationZip | |
() | |
) | |
// Dependencies | |
"CheckRequirements" | |
==> "BackupCurrentEnvironment" | |
==> "CopyZipToDestination" | |
==> "UnzipAndDeleteInDestination" | |
// start build | |
RunTargetOrDefault "UnzipAndDeleteInDestination" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment