Skip to content

Instantly share code, notes, and snippets.

@Vidarls
Last active August 29, 2015 14:17
Show Gist options
  • Save Vidarls/549ce388ea6d219ed417 to your computer and use it in GitHub Desktop.
Save Vidarls/549ce388ea6d219ed417 to your computer and use it in GitHub Desktop.
Flattening extracting binaries from packages
#r @"packages/FAKE/tools/FakeLib.dll"
open Fake
open System
open System.IO
type FxVersion =
| Net4
| Net35
type PackageCopySettings =
{
MaxSupportedFx: FxVersion
PackagesToExclude: string list
CopyOnlyThesePackages: string list
PackageSubFoldersToCopy: string list
PackagesRoot: string
TargetFolder: string
}
let defaultCopySettings =
{
MaxSupportedFx = Net4
PackagesToExclude = ["Fake"]
CopyOnlyThesePackages = []
PackageSubFoldersToCopy = ["lib";"tools";"content"]
PackagesRoot = "packages"
TargetFolder = "bin"
}
let fxVersionFolderNames = function
| Net4 -> ["net40";"40"]
| Net35 -> ["net35";"35"]
let fxVersionLists = function
| Net4 -> [Net4;Net35]
| Net35 -> [Net35]
let createFxData fxVersion =
fxVersionLists fxVersion |> List.map (fun v -> (v, fxVersionFolderNames v))
let rec findRoot (versions: (FxVersion * string list) list) (root: DirectoryInfo) =
match versions with
| [] -> root
| (_,folders) :: tail -> match root |> subDirectories |> Array.tryFind (fun d -> folders |> List.exists (fun f -> f = d.Name.ToLower())) with
| None -> findRoot tail root
| Some folder -> folder
let folderContainsOtherStuffThanPs1AndHtml (folder:DirectoryInfo) =
let files = folder.GetFiles("*", SearchOption.AllDirectories)
|> Array.filter (fun f -> not (f.Extension.ToLower().StartsWith(".htm")))
|> Array.filter (fun f -> not (f.Extension.ToLower() = ".ps1"))
|> Array.filter (fun f -> not (f.Extension.ToLower() = ".targets"))
(files |> Array.length) > 0
let getPossibleRootsForPackage (packageRoot: DirectoryInfo) (maxFxVersion: FxVersion) (subFoldersToInclude:string list)=
let fxData = maxFxVersion |> createFxData
packageRoot
|> subDirectories
|> List.ofArray
|> List.filter (fun f -> subFoldersToInclude |> List.exists (fun r -> r = f.Name.ToLower()))
|> List.map (fun d -> (d.Name.ToLower(), d |> findRoot fxData))
|> List.filter (fun (_, d) -> (d.GetFiles() |> Array.length) > 0)
|> List.filter (fun (name, d) -> if name <> "tools" then true else folderContainsOtherStuffThanPs1AndHtml d)
|> List.map (fun (_,d) -> d)
let CopyBinariesFromPackages (setCopySettings: PackageCopySettings -> PackageCopySettings) =
let settings = defaultCopySettings |> setCopySettings
let excludes = settings.PackagesToExclude |> List.map (fun e -> e.ToLower().Trim())
let strictIncludes = settings.CopyOnlyThesePackages |> List.map (fun e -> e.ToLower().Trim())
let folders = settings.PackagesRoot
|> directoryInfo
|> subDirectories
|> Array.filter (fun d -> if (strictIncludes |> List.length) = 0 then true else strictIncludes |> List.exists (fun i -> i = d.Name.ToLower()))
|> Array.filter (fun d -> not (excludes |> List.exists (fun e -> e = d.Name.ToLower())))
|> Array.map (fun p -> (p.Name, getPossibleRootsForPackage p settings.MaxSupportedFx settings.PackageSubFoldersToCopy))
|> Array.filter (fun (name, roots) -> (roots |> List.length) > 0)
|> List.ofArray
|> List.collect (fun (_, roots) -> roots)
folders |> List.iter (fun root -> CopyDir settings.TargetFolder root.FullName (fun _ -> true))
Target "Clean" (fun _ ->
CleanDirs ["bin";]
)
Target "CopyBinaries" (fun _ ->
CopyBinariesFromPackages (fun s ->
{ s with PackagesToExclude = ["Fake";"Package.To.Handle.Differently"] })
CopyBinariesFromPackages (fun s ->
{ s with
CopyOnlyThesePackages = ["Package.To.Handle.Differently"]
TargetFolder = "bin" @@ "AdminClient" })
)
"Clean"
==> "CopyBinaries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment