author | Jan Vrany <jan.vrany@labware.com> |
Wed, 08 Mar 2023 16:40:41 +0000 | |
branch | jv |
changeset 19643 | b3148ab32c03 |
parent 15566 | 184cea584be5 |
permissions | -rw-r--r-- |
9346 | 1 |
" |
2 |
COPYRIGHT (c) 2009 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
8933 | 12 |
"{ Package: 'stx:libtool' }" |
13 |
||
14 |
Object subclass:#DiffListUtility |
|
15 |
instanceVariableNames:'' |
|
16 |
classVariableNames:'DiffCommandTemplate' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'Views-Text' |
|
19 |
! |
|
20 |
||
21 |
!DiffListUtility class methodsFor:'documentation'! |
|
22 |
||
9346 | 23 |
copyright |
24 |
" |
|
25 |
COPYRIGHT (c) 2009 by eXept Software AG |
|
26 |
All Rights Reserved |
|
27 |
||
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
8933 | 37 |
documentation |
38 |
" |
|
39 |
a utility to encapsulate access to the diff command |
|
9494 | 40 |
(may also be a facade to a smalltalk-diff algorithm, eventually) |
8933 | 41 |
|
42 |
[author:] |
|
43 |
cg (cg@CG-PC) |
|
44 |
||
45 |
[instance variables:] |
|
46 |
||
47 |
[class variables:] |
|
48 |
||
49 |
[see also:] |
|
50 |
||
51 |
" |
|
52 |
! ! |
|
53 |
||
54 |
!DiffListUtility class methodsFor:'defaults'! |
|
55 |
||
56 |
diffCommand |
|
12358 | 57 |
"return the diff-command (with argument placeHolders). |
58 |
By default, diff is used with windows, diff -b with Unix. |
|
59 |
You can change this via the diffCommandTemplate" |
|
8933 | 60 |
|
61 |
DiffCommandTemplate notNil ifTrue:[ ^ DiffCommandTemplate ]. |
|
12358 | 62 |
^ UserPreferences current externalDiffCommandTemplate |
8933 | 63 |
! |
64 |
||
65 |
diffCommandTemplate:aCommandTemplateString |
|
66 |
"set the diff-command template" |
|
67 |
||
68 |
DiffCommandTemplate := aCommandTemplateString |
|
69 |
! ! |
|
70 |
||
71 |
!DiffListUtility class methodsFor:'private'! |
|
72 |
||
73 |
saveForDiff:text as:filename |
|
74 |
|stream| |
|
75 |
||
76 |
[ |
|
77 |
stream := filename writeStream. |
|
78 |
text do:[:line | |
|
79 |
|lOut i| |
|
80 |
||
81 |
line notEmptyOrNil ifTrue:[ |
|
82 |
lOut := line. |
|
83 |
(line includes:Character return) ifTrue: [ |
|
84 |
(line endsWith:Character return) ifTrue:[ |
|
12717
dcde1443a6bb
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
12384
diff
changeset
|
85 |
lOut := line copyButLast:1. |
12384 | 86 |
]. |
87 |
] ifFalse:[ |
|
88 |
(line endsWith:(String crlf)) ifTrue: [ |
|
12717
dcde1443a6bb
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
12384
diff
changeset
|
89 |
lOut := line copyButLast:2. |
8933 | 90 |
] |
12384 | 91 |
"/ i := line indexOf:Character return. |
92 |
"/ (line at:i+1) == Character nl ifTrue:[ |
|
93 |
"/ "/ crnl endings |
|
94 |
"/ lOut := line copyReplaceString:(String crlf) withString:(String lf). |
|
95 |
"/ ] ifFalse:[ |
|
96 |
"/ "/ cr endings |
|
97 |
"/ lOut := line copyReplaceAll:(Character return) with:Character nl. |
|
98 |
"/ ]. |
|
99 |
"/ ] |
|
8933 | 100 |
]. |
12082 | 101 |
lOut isWideString ifTrue:[ |
8933 | 102 |
(lOut first = (Character value:16rFEFF)) ifTrue:[ |
12082 | 103 |
lOut := (lOut copyFrom:2) asSingleByteStringReplaceInvalidWith:(Character value:16rFF). |
8933 | 104 |
]. |
105 |
]. |
|
13340 | 106 |
lOut printOn:stream. |
8933 | 107 |
]. |
108 |
stream cr |
|
109 |
]. |
|
110 |
] ensure:[ |
|
111 |
stream close. |
|
112 |
]. |
|
113 |
||
114 |
"Modified: / 22-10-2008 / 17:52:52 / cg" |
|
115 |
! ! |
|
116 |
||
117 |
!DiffListUtility class methodsFor:'utilities'! |
|
118 |
||
119 |
diffListFor:text1 and:text2 |
|
120 |
"execute DiffCommand to get a list of diffs." |
|
121 |
||
122 |
" |
|
123 |
The returned list is in raw-diff output format, such as: |
|
124 |
1 : '1c1' |
|
125 |
2 : '< hello world' |
|
126 |
3 : '---' |
|
127 |
4 : '> Hello World' |
|
128 |
5 : '2a3' |
|
129 |
6 : '> line2' |
|
130 |
7 : '4d4' |
|
131 |
8 : '< line4' |
|
132 |
" |
|
133 |
||
134 |
|tmpFile1 tmpFile2 stream line |
|
135 |
diffList diffTemplate diffCmd| |
|
136 |
||
137 |
diffTemplate := self diffCommand. |
|
14334 | 138 |
diffTemplate isEmptyOrNil ifTrue:[ |
8933 | 139 |
"/ self warn:'no diff command available'. |
140 |
^ nil |
|
141 |
]. |
|
142 |
||
143 |
text1 = text2 ifTrue:[ |
|
144 |
"no diff" |
|
145 |
^ #() |
|
146 |
]. |
|
147 |
||
148 |
" |
|
149 |
save them texts in two temporary files ... |
|
150 |
" |
|
151 |
[ |
|
152 |
self saveForDiff:text1 as:(tmpFile1 := Filename newTemporary). |
|
153 |
self saveForDiff:text2 as:(tmpFile2 := Filename newTemporary). |
|
154 |
||
155 |
" |
|
156 |
start diff on it ... |
|
157 |
" |
|
158 |
diffCmd := diffTemplate |
|
159 |
bindWith:tmpFile1 asString |
|
160 |
with:tmpFile2 asString. |
|
161 |
||
162 |
stream := PipeStream readingFrom:diffCmd. |
|
163 |
stream isNil ifTrue:[ |
|
14334 | 164 |
"this code is not reached in win32. |
165 |
PipeStream starts a cmd.exe, which is always successful |
|
166 |
at the first place and fails later" |
|
12384 | 167 |
stream := PipeStream readingFrom:('support' asFilename / diffCmd). |
8933 | 168 |
stream isNil ifTrue:[ |
169 |
self error:'cannot execute diff' mayProceed:true. |
|
170 |
^ nil. |
|
171 |
] |
|
172 |
]. |
|
173 |
||
174 |
diffList := OrderedCollection new. |
|
175 |
(stream readWaitWithTimeout:10) ifTrue:[ |
|
176 |
"/ timeout |
|
177 |
stream close. |
|
14334 | 178 |
(stream exitStatus notNil and:[stream exitStatus isError]) ifTrue:[ |
179 |
self error:'cannot execute diff (command error)' mayProceed:true. |
|
180 |
] ifFalse:[ |
|
181 |
self error:'cannot execute diff (timeout)' mayProceed:true. |
|
182 |
]. |
|
8933 | 183 |
^ nil. |
184 |
]. |
|
185 |
||
186 |
[stream atEnd] whileFalse:[ |
|
187 |
line := stream nextLine. |
|
188 |
line notNil ifTrue:[diffList add:line] |
|
189 |
]. |
|
190 |
stream close. |
|
191 |
] ensure:[ |
|
192 |
tmpFile1 notNil ifTrue:[ tmpFile1 delete ]. |
|
193 |
tmpFile2 notNil ifTrue:[ tmpFile2 delete ]. |
|
194 |
]. |
|
195 |
^ diffList |
|
196 |
||
197 |
" |
|
198 |
self |
|
199 |
diffListFor:#( |
|
200 |
'hello world' |
|
201 |
'line1' |
|
202 |
'line3' |
|
203 |
'line4' |
|
204 |
) |
|
205 |
and: #( |
|
206 |
'Hello World' |
|
207 |
'line1' |
|
208 |
'line2' |
|
209 |
'line3' |
|
210 |
) |
|
211 |
" |
|
212 |
! ! |
|
213 |
||
214 |
!DiffListUtility class methodsFor:'documentation'! |
|
215 |
||
12384 | 216 |
version |
14334 | 217 |
^ '$Header: /cvs/stx/stx/libtool/DiffListUtility.st,v 1.9 2014-05-08 17:49:06 stefan Exp $' |
12384 | 218 |
! |
219 |
||
8933 | 220 |
version_CVS |
14334 | 221 |
^ '$Header: /cvs/stx/stx/libtool/DiffListUtility.st,v 1.9 2014-05-08 17:49:06 stefan Exp $' |
8933 | 222 |
! ! |
12358 | 223 |