-
Notifications
You must be signed in to change notification settings - Fork 1
/
copy-files.red
190 lines (167 loc) · 5.25 KB
/
copy-files.red
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
Red [
Title: "copy-file.red"
Builds: [
0.0.0.1 {Initial build with file versioning or /force and checksum}
]
Iterations: [
0.0.0.1.18 [
Purpose: {Error management}
Change: {Quit if error except /no-quit-if-error}
]
0.0.0.1.16 [
Purpose: {support windows path for >target}
Change: {>target: to-red-file form >target}
]
0.0.0.1.15 [
Purpose: {Support for local file}
Change: {>source: to-red-file form >source}
]
0.0.0.1.14 [
purpose: {case file doesn't exist}
change: {
if error? try [
write >target read >source
print [>target "created."]
][
print [{error copy} >source {to} >target]
]
}
]
0.0.0.1.12 {if previous-file <> >target [}
0.0.0.1.10 {Add checksum}
0.0.0.1.2 {Protecting existing files}
0.0.0.1.1 {Initial build}
]
]
compare-checksum: function [>file1 >file2][
file1-content: read >file1
checksum-file1: checksum file1-content 'SHA256
file2-content: read >file2
checksum-file2: checksum file2-content 'SHA256
return (checksum-file1 = checksum-file2)
]
unless value? '.redlang [
do https://redlang.red
]
unless value? 'get-short-filename-without-extension [ ; fixed bug
.redlang file-path ; 0.0.0.1.22
]
unless value? 'list-files [
.redlang list-files ; 0.0.0.1.22
]
.copy-file: function [
>source >target
/force
/no-checksum
/no-quit-if-error
/_debug
][
>source: to-red-file form >source
>target: to-red-file form >target
if not exists? >source [ ; 0.0.0.1.01.23
print [{error line 69 copy-file:} >source {does not exist.}]
return false
]
either force [
if error? try [
write >target read >source
print [>target "created."]
return (>target)
][
either exists? >source [
either exists? >target [
print [{error line 73 copy-file:} {check} >target {is not protected.}]
][
print [{error line 75 copy-file:} {unknown error}]
]
][
print [{error line 78 copy-file:} >source {does not exist.}]
]
unless no-quit-if-error [
print "quit (unless /no-quit-if-error)"
quit
]
return false
]
][
either exists? >target [
print [>target "already exists."]
short-filename-wo-extension: get-short-filename-without-extension >target
short-filename: .get-short-filename >target
extension: .get-file-extension >target
target-folder: pick (split-path >target) 1
get-next-file: function [
/_debug
/local counter
][
counter: []
if empty? counter [
append counter 0
]
counter/1: counter/1 + 1
i: counter/1
if _debug [
do-trace 105 [
?? short-filename-wo-extension
] %copy-files.20.red
]
next-file: rejoin [short-filename-wo-extension "." i extension]
]
next-file: >target
while [exists? next-file][
previous-file: next-file
next-file: rejoin [target-folder get-next-file]
]
list-files: .get-list-files (target-folder) ; 0.0.0.1.20 () necessary
unless _debug [
?? list-files
]
if previous-file <> >target [
unless no-checksum [
either false = compare-checksum >source previous-file [
write next-file read >source
print [next-file "created."]
return (next-file )
][
print [previous-file "is up to date."]
return false
]
]
]
; no-checksum
if error? try [
write next-file read >source
print [next-file "created."]
return (next-file )
][
print [{error line 87 copy-file} >source {to} next-file ]
return false
]
;return true
return next-file; 0.0.0.1.19
][
if error? try [
write >target read >source
print [>target "created."]
return (>target)
][
print [{error line 149 copy-file} >source {to} >target]
]
]
]
]
if not value? 'copy-file [
copy-file: :.copy-file
]
.copy-files: function[>list][
list: >list
forall list [
files: list/1
source: files/1
target: files/2
copy-file source target
]
]
if not value? 'copy-files [
copy-files: :.copy-files
]