Skip to content

Commit

Permalink
* Implements simple macros
Browse files Browse the repository at this point in the history
  • Loading branch information
njlr committed Sep 16, 2019
1 parent df5275e commit d83f892
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 120 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# TODO: Comment the next line if you want to checkin your web deploy settings
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj

Expand Down
4 changes: 2 additions & 2 deletions buckaroo/AddCommand.fs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ let task (context : Tasks.TaskContext) dependencies = async {

if manifest = newManifest
then
logger.Warning ("The dependency already exists in the manifest")
logger.Warning ("The dependency already exists in the manifest. ")
return 0
else
let! maybeLock = async {
if File.Exists(Constants.LockFileName)
if File.Exists Constants.LockFileName
then
let! lock = Tasks.readLock
return Some lock
Expand Down
2 changes: 1 addition & 1 deletion buckaroo/Constraint.fs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ module Constraint =
(xs |> Seq.map chanceOfSuccess |> Seq.append [ 0 ] |> Seq.sum)
| Complement x -> MaxChanceOfSuccess - (chanceOfSuccess x)

// TODO: Better Sorting!!!!!
// TODO: Better Sorting!
let rec compare (x : Constraint) (y : Constraint) : int =
match (x, y) with
| (Exactly u, Exactly v) -> Version.compare u v
Expand Down
51 changes: 36 additions & 15 deletions buckaroo/DefaultSourceExplorer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ open Buckaroo.Console
open Buckaroo.RichOutput
open Buckaroo.Logger

type DefaultSourceExplorer (console : ConsoleManager, downloadManager : DownloadManager, gitManager : GitManager) =
type DefaultSourceExplorer (console : ConsoleManager, downloadManager : DownloadManager, gitManager : GitManager, buildSystem : BuildSystem) =
let logger = createLogger console (Some "explorer")
let toOptional = Async.Catch >> (Async.map Choice.toOption)

Expand All @@ -23,7 +23,7 @@ type DefaultSourceExplorer (console : ConsoleManager, downloadManager : Download
let extractFileFromHttp (source : HttpLocation) (filePath : string) = async {
if Option.defaultValue ArchiveType.Zip source.Type <> ArchiveType.Zip
then
return raise (System.Exception("Only zip is currently supported"))
return raise (System.Exception "Only zip is currently supported")

let! pathToZip = downloadManager.DownloadToCache source.Url
use file = System.IO.File.OpenRead pathToZip
Expand All @@ -34,7 +34,7 @@ type DefaultSourceExplorer (console : ConsoleManager, downloadManager : Download
| Some stripPrefix ->
let roots =
zip.Entries
|> Seq.map (fun entry -> System.IO.Path.GetDirectoryName(entry.FullName))
|> Seq.map (fun entry -> System.IO.Path.GetDirectoryName entry.FullName)
|> Seq.distinct
|> Seq.filter (fun directory ->
directory |> Glob.isLike stripPrefix
Expand All @@ -57,7 +57,8 @@ type DefaultSourceExplorer (console : ConsoleManager, downloadManager : Download
use streamReader = new System.IO.StreamReader(stream)

return!
streamReader.ReadToEndAsync() |> Async.AwaitTask
streamReader.ReadToEndAsync ()
|> Async.AwaitTask
}

let fetchFile location path =
Expand Down Expand Up @@ -292,27 +293,47 @@ type DefaultSourceExplorer (console : ConsoleManager, downloadManager : Download

member this.FetchManifest (location, versions) =
async {
let! content = fetchFile location Constants.ManifestFileName
return
match Manifest.parse content with
| Result.Ok manifest -> manifest
| Result.Error error ->
let errorMessage =
"Invalid " + Constants.ManifestFileName + " file. \n" +
(Manifest.ManifestParseError.show error)
raise <| System.Exception errorMessage
let! maybeContent =
fetchFile location Constants.ManifestFileName
|> toOptional

match maybeContent with
| Some content ->
return
match Manifest.parse content with
| Result.Ok manifest -> manifest
| Result.Error error ->
let errorMessage =
"Invalid " + Constants.ManifestFileName + " file. \n" +
(Manifest.ManifestParseError.show error)
raise <| System.Exception errorMessage
| None ->
// This might be a Bazel project
if buildSystem = Bazel
then
do!
fetchFile location "WORKSPACE"
|> Async.Ignore

return Manifest.zero
else
return raise <| System.Exception ("No manifest was found at " + (PackageLock.show location) + ". ")
}

member this.FetchLock (location, versions) =
async {
let! maybeContent = fetchFile location Constants.LockFileName |> Async.Catch |> Async.map(Choice.toOption)
let! maybeContent =
fetchFile location Constants.LockFileName
|> Async.Catch
|> Async.map Choice.toOption

return
match maybeContent with
| None ->
logger.RichWarning (
(text "Could not fetch ") + (highlight Constants.LockFileName) + (text " from ") +
(PackageLock.show location |> highlight) + (warn " 404"))
raise <| System.Exception("Could not fetch " + Constants.LockFileName + " file")
raise <| System.Exception ("Could not fetch " + Constants.LockFileName + " file")
| Some content ->
match Lock.parse content with
| Result.Ok manifest -> manifest
Expand Down
63 changes: 32 additions & 31 deletions buckaroo/DownloadManager.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,39 @@ open Buckaroo.RichOutput
open Buckaroo.Console
open Buckaroo.Hashing

type CopyMessage =
type CopyMessage =
| Copy of string * string * AsyncReplyChannel<Async<Unit>>

type DownloadMessage =
type DownloadMessage =
| Download of string * AsyncReplyChannel<Async<string>>

type DownloadManager (console : ConsoleManager, cacheDirectory : string) =
let sanitizeFilename (x : string) =
let regexSearch =
new string(Path.GetInvalidFileNameChars()) +
new string(Path.GetInvalidPathChars()) +
"@.:\\/";
let r = new Regex(String.Format("[{0}]", Regex.Escape(regexSearch)))
type DownloadManager (console : ConsoleManager, cacheDirectory : string) =

let sanitizeFilename (x : string) =
let regexSearch =
new string (Path.GetInvalidFileNameChars()) +
new string (Path.GetInvalidPathChars()) +
"@.:\\/"
let r = Regex (String.Format ("[{0}]", Regex.Escape regexSearch))
Regex.Replace(r.Replace(x, "-"), "-{2,}", "-")

let cachePath (url : string) =
let cachePath (url : string) =
let hash = sha256 url
Path.Combine(cacheDirectory, (sanitizeFilename url).ToLower() + "-" + hash.Substring(0, 16))

let cachePathHash (hash : string) =
let cachePathHash (hash : string) =
Path.Combine(cacheDirectory, hash)

let downloadFile (url : string) (target : string) = async {
console.Write (
(text "Downloading ") +
(text url |> foreground ConsoleColor.Magenta) +
" to " +
(text "Downloading ") +
(text url |> foreground ConsoleColor.Magenta) +
" to " +
(text target |> foreground ConsoleColor.Cyan) + "... ")
let! request = Http.AsyncRequestStream url
use outputFile = new FileStream(target, FileMode.Create)
do!
request.ResponseStream.CopyToAsync outputFile
do!
request.ResponseStream.CopyToAsync outputFile
|> Async.AwaitTask
return target
}
Expand All @@ -53,8 +53,8 @@ type DownloadManager (console : ConsoleManager, cacheDirectory : string) =
let! (Copy(source, destination, replyChannel)) = inbox.Receive()
match cache |> Map.tryFind destination with
| Some task -> replyChannel.Reply(task)
| None ->
let! task =
| None ->
let! task =
async {
if File.Exists destination |> not
then
Expand All @@ -67,7 +67,7 @@ type DownloadManager (console : ConsoleManager, cacheDirectory : string) =

let copy source destination = async {
let! task = hashCache.PostAndAsyncReply (fun ch -> Copy (source, destination, ch))

return! task
}

Expand All @@ -76,51 +76,52 @@ type DownloadManager (console : ConsoleManager, cacheDirectory : string) =

while true do
let! (Download (url, replyChannel)) = inbox.Receive ()

match cache |> Map.tryFind url with
| Some task -> replyChannel.Reply(task)
| None ->
| None ->
let target = cachePath url
let! task =
let! task =
async {
if File.Exists target
then
console.Write ((text "Deleting ") + (text target |> foreground ConsoleColor.Cyan) + "... ")
do! Files.delete target
let! cachePath = downloadFile url target
let! cachePath = downloadFile url target
let! hash = Files.sha256 cachePath
let destination = cachePathHash hash
do! copy cachePath destination
return destination
}
|> Async.StartChild

cache <- cache |> Map.add url task
replyChannel.Reply(task)
})

member this.DownloadToCache (url : string) = async {
let! res = downloadCache.PostAndAsyncReply(fun ch -> Download(url, ch))
return! res
return! res
}

member this.Download (url : string) (path : string) = async {
let! source = this.DownloadToCache url
do! Files.copy source path
}

member this.DownloadHash (sha256 : string) (urls : string list) : Async<string> =
member this.DownloadHash (sha256 : string) (urls : string list) : Async<string> =
let rec processUrls urls = async {
match urls with
| head::tail ->
| head::tail ->
let! cachePath = this.DownloadToCache head
let! actualHash = Files.sha256 cachePath

if actualHash = sha256
then
then
return cachePath
else
return! processUrls tail
| [] ->
return raise <| new Exception("Ran out of URLs to try")
| [] ->
return raise <| Exception "Ran out of URLs to try"
}
processUrls urls
8 changes: 4 additions & 4 deletions buckaroo/Git.fs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ module Git =

let invalidSequences = [ ".."; "@{"; "\\"; "//" ]

if invalidSequences |> Seq.exists (fun x -> branchOrTag.Contains x)
then
let errorMessage = "Cannot contain any of: " + (invalidSequences |> String.concat ", ")
return! fail errorMessage
if invalidSequences |> Seq.exists branchOrTag.Contains
then
let errorMessage = "Cannot contain any of: " + (invalidSequences |> String.concat ", ")
return! fail errorMessage
else
return branchOrTag
}
Expand Down
2 changes: 1 addition & 1 deletion buckaroo/GitCli.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type GitCli (console : ConsoleManager) =
let runBash exe args = async {
let rt =
(
"Running " + exe
"Running " + exe + " "
|> RichOutput.text
|> RichOutput.foreground ConsoleColor.Gray
) +
Expand Down
6 changes: 2 additions & 4 deletions buckaroo/InstallCommand.fs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ let rec private installBazelPackages (context : Tasks.TaskContext) (root : strin
// Install child's child (recurse)
do! installBazelPackages context installPath childParents lockedPackage.PrivatePackages

// TODO: Write buckaroo.bzl
// Write macros
do! Files.writeFile (Path.Combine ("buckaroo", "data.bzl")) (dataBzl packages)
do! Files.writeFile (Path.Combine ("buckaroo", "defs.bzl")) defsBzl
}
Expand Down Expand Up @@ -506,9 +506,7 @@ let task (context : Tasks.TaskContext) = async {
then
let! lock = Tasks.readLock

let buildSystem = BuildSystem.Bazel // TODO: Allow user to select build-system?

match buildSystem with
match context.BuildSystem with
| Buck ->
do! installBuckPackages context "." [] lock.Packages
do! writeTopLevelBuckFiles context "." lock
Expand Down
65 changes: 35 additions & 30 deletions buckaroo/Lock.fs
Original file line number Diff line number Diff line change
Expand Up @@ -94,36 +94,41 @@ module Lock =
)
)

[
"Added: ";
(
additions
|> Seq.map (fun (k, (l, v)) ->
" " + (k |> Seq.map PackageIdentifier.show |> String.concat " ") +
" -> " + (PackageLock.show l) + "@" + (Version.showSet v)
)
|> String.concat "\n"
);
"Removed: ";
(
removals
|> Seq.map (fun (k, (l, v)) ->
" " + (k |> Seq.map PackageIdentifier.show |> String.concat " ") +
" -> " + (PackageLock.show l) + "@" + (Version.showSet v)
)
|> String.concat "\n"
);
"Changed: ";
(
changes
|> Seq.map (fun (k, (bl, bv), (al, av)) ->
" " + (k |> Seq.map PackageIdentifier.show |> String.concat " ") +
" " + (PackageLock.show bl) + "@" + (Version.showSet bv) +
" -> " + (PackageLock.show al) + "@" + (Version.showSet av)
)
|> String.concat "\n"
);
]
seq {
if Seq.isEmpty additions |> not
then
yield "Added: "
yield
additions
|> Seq.map (fun (k, (l, v)) ->
" " + (k |> Seq.map PackageIdentifier.show |> String.concat " ") +
" -> " + (PackageLock.show l) + "@" + (Version.showSet v)
)
|> String.concat "\n"

if Seq.isEmpty removals |> not
then
yield "Removed: "
yield
removals
|> Seq.map (fun (k, (l, v)) ->
" " + (k |> Seq.map PackageIdentifier.show |> String.concat " ") +
" -> " + (PackageLock.show l) + "@" + (Version.showSet v)
)
|> String.concat "\n"

if Seq.isEmpty changes |> not
then
yield "Changed: "
yield
changes
|> Seq.map (fun (k, (bl, bv), (al, av)) ->
" " + (k |> Seq.map PackageIdentifier.show |> String.concat " ") +
" " + (PackageLock.show bl) + "@" + (Version.showSet bv) +
" -> " + (PackageLock.show al) + "@" + (Version.showSet av)
)
|> String.concat "\n"
}
|> String.concat "\n"

let fromManifestAndSolution (manifest : Manifest) (solution : Solution) : Lock =
Expand Down
Loading

0 comments on commit d83f892

Please sign in to comment.