-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.fs
212 lines (171 loc) · 5.94 KB
/
common.fs
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
namespace Common
open System.IO
open System.Text.Json
open System.IO.Compression
open FSLogger
module Map =
let maxBy f table =
let maxKey = Map.maxKeyValue table
let otherMap = table |> Map.remove (fst maxKey)
let result =
Map.fold (fun s k v ->
if f s (k, v) then
s
else
k, v)
maxKey otherMap
result
/// <summary>`table1`有`table2`無的鍵</summary>
let sub table1 table2 =
Map.filter (fun k _ -> table2 |> Map.containsKey k |> not) table1
let keysSet table = Map.keys table |> Set.ofSeq
let intersect table1 table2 =
Set.intersect <| keysSet table1 <| keysSet table2
|> Set.toList
|> List.map (fun k -> k, (table1[k], table2[k]))
|> Map.ofList
/// <summary>合併表</summary>
/// <param name="table1">第一個表</param>
/// <param name="table2">第二個表</param>
/// <returns>保留所有數據</returns>
let union table1 table2 =
let keys1 = table1 |> keysSet
let keys2 = table2 |> keysSet
Set.union keys1 keys2
|> Set.toList
|> List.map (fun k ->
let findK = Map.tryFind k
k, (findK table1, findK table2))
|> Map.ofList
let unionWith f table1 table2 =
union table1 table2
|> Map.map f
let filterMap f table =
Map.fold (fun s k v ->
match f k v with
| Some value -> s |> Map.add k value
| None -> s
) Map.empty table
let diffWith f table1 table2 =
union table1 table2
|> filterMap (fun k (o1, o2) ->
match o1, o2 with
| Some v1, Some v2 ->
if f k v1 v2 then
Some (v1, v2)
else
None
| _ -> None)
module Common =
// let logger = Logger.ColorConsole
let mutable logger = Logger.ColorConsole
let getFileSystemEntries path =
Directory.GetFileSystemEntries(
path,
"*",
SearchOption.AllDirectories)
module DirectoryInfo =
let create s =
new DirectoryInfo(s)
module Path =
let join (a: string) b =
Path.Join(a, b)
let join3 (a: string) b c =
Path.Join(a, b, c)
let join4 (a: string) b c d =
Path.Join(a, b, c, d)
let lastWriteTime path =
(new FileInfo(path)).LastWriteTime
let joinList pathList =
List.reduce (fun a b -> join a b) pathList
let relativePath parent path =
Path.GetRelativePath(parent, path)
let directory path =
(new FileInfo(path)).Directory
module Json =
let serialize value =
JsonSerializer.Serialize value
let deserialize<'a> (value: string) =
JsonSerializer.Deserialize<'a> value
let tryGetDirectory path =
let dir = (new FileInfo(path)).Directory
if dir <> null then
Some dir
else
None
module String =
let countChar c s =
s |> String.filter ((=) c) |> String.length
let startsWith (value: string) (str: string) =
str.StartsWith value
module File =
let exists path =
File.Exists path
let delete path =
File.Delete path
let deleteIfExists path =
if exists path then
delete path
let copy src dest =
if src |> exists |> not then
invalidArg <| nameof src <| sprintf "%s not exists" src
File.Copy(src, dest, true)
/// <summary>寫入文件</summary>
let writeAllText path (contents: string) =
File.WriteAllText(path, contents)
let readAllText path =
File.ReadAllText path
let readAllLines path =
File.ReadAllLines path
module Directory =
let exists path =
Directory.Exists path
let delete path recursive =
Directory.Delete(path, recursive)
let deleteIfExists path recursive =
if exists path then
delete path recursive
let createFor path =
(Path.directory path).Create()
let create path =
Directory.CreateDirectory path |> ignore
let copy src dest =
// printfn "yes"
let logger = logger |> Logger.appendPath "copy"
logger.I $"src {src}"
logger.I $"dest {src}"
let data = getFileSystemEntries src
let sorted =
data
|> Array.sortWith (fun a b ->
let level s = s |> String.countChar '\\'
let la = level a
let lb = level b
match exists a, exists b with
| true, true -> compare lb la
| true, _ -> 1
| false, true -> -1
| false, false -> compare lb la)
sorted
|> Array.iter (fun x ->
let rela = Path.relativePath src x
let newPath = Path.join dest rela
match exists x with
| true ->
if exists newPath |> not then
createFor newPath
create newPath
| false ->
createFor newPath
File.copy x newPath)
let current =
Directory.GetCurrentDirectory()
let parent path =
Directory.GetParent(path).ToString()
let compress path dest =
let mutable dir = new DirectoryInfo(dest)
if dir <> null then
dir <- dir.Parent
if dir.Exists |> not then
dir.Create()
ZipFile.CreateFromDirectory(path, dest, CompressionLevel.Optimal, false)