-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.fsx
129 lines (95 loc) · 4.12 KB
/
fetch.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#r "nuget: SharpZipLib"
#r "System.Formats.Tar.dll"
open System
open System.IO
open System.Net.Http
open System.Formats.Tar
open ICSharpCode.SharpZipLib.BZip2
let version = File.ReadAllText "VERSION.txt"
let platforms =
Map.ofList [
"win-x64", "windows64"
]
let content =
[| "Release"; "Resources" |]
module Download =
[<Struct>]
type Progress(received : int64, total : int64) =
member x.Received = received
member x.Total = total
member x.Relative = float received / float total
override x.ToString() =
sprintf "%.2f%%" (100.0 * float received / float total)
let file (url: string) (file: string) =
Console.WriteLine($"Retrieving {url}")
try
use c = new HttpClient()
let response = c.GetAsync(System.Uri url, HttpCompletionOption.ResponseHeadersRead).Result
let len =
let f = response.Content.Headers.ContentLength
if f.HasValue then f.Value
else 1L <<< 30
if not response.IsSuccessStatusCode then
let code = response.StatusCode
raise <| HttpRequestException($"Http GET request failed with status code {int code} ({code}).")
let mutable lastProgress = Progress(0L,len)
Console.WriteLine($"Downloading {len} bytes...")
Console.WriteLine($"{lastProgress}")
let sw = System.Diagnostics.Stopwatch.StartNew()
use stream = response.Content.ReadAsStreamAsync().Result
if File.Exists file then File.Delete file
use output = File.OpenWrite(file)
let buffer : byte[] = Array.zeroCreate (4 <<< 20)
let mutable remaining = len
let mutable read = 0L
while remaining > 0L do
let cnt = int (min remaining buffer.LongLength)
let r = stream.Read(buffer, 0, cnt)
output.Write(buffer, 0, r)
remaining <- remaining - int64 r
read <- read + int64 r
let p = Progress(read, len)
if sw.Elapsed.TotalSeconds >= 0.1 && p.Relative - lastProgress.Relative > 0.025 then
Console.WriteLine($"{p}")
lastProgress <- p
sw.Restart()
Console.WriteLine($"Written {read} bytes to {file}")
with _ ->
Console.Error.WriteLine("FAILED")
if File.Exists file then File.Delete file
reraise()
module Directory =
let rec copy (src: string) (dst: string) =
let dir = DirectoryInfo(src)
Directory.CreateDirectory dst |> ignore
for f in dir.GetFiles() do
f.CopyTo(Path.Combine(dst, f.Name), true) |> ignore
for d in dir.GetDirectories() do
let newDst = Path.Combine(dst, d.Name)
copy d.FullName newDst
let keepDownloads =
fsi.CommandLineArgs |> Array.contains "--keep-downloads"
try
for KeyValue (rid, platform) in platforms do
let name = $"cef_binary_{version}_{platform}_minimal.tar.bz2"
let file = Path.Combine("download", name)
let dir = file.Replace(".tar.bz2", "")
if not <| Directory.Exists dir then
Directory.CreateDirectory("download") |> ignore
if not <| File.Exists file then
let url = $"https://cef-builds.spotifycdn.com/cef_binary_{version}_{platform}_minimal.tar.bz2"
Download.file url file
Console.WriteLine($"Unpacking {file}...")
use bz2Stream = File.OpenRead(file)
use tarStream = new MemoryStream()
BZip2.Decompress(bz2Stream, tarStream, false)
tarStream.Seek(0L, SeekOrigin.Begin) |> ignore
TarFile.ExtractToDirectory(tarStream, "download", true)
let dst = Path.Combine("cef", rid)
Directory.CreateDirectory(dst) |> ignore
Console.WriteLine($"Copying to {dst}...")
for c in content do
let src = Path.Combine(dir, c)
Directory.copy src dst
finally
if not keepDownloads && Directory.Exists "download" then Directory.Delete("download", true)