author | Stefan Vogel <sv@exept.de> |
Wed, 21 Aug 2013 15:56:32 +0200 | |
changeset 13340 | 7a295f0992df |
parent 12717 | dcde1443a6bb |
child 13356 | 51a964477563 |
child 14334 | e54a95bfa77b |
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. |
|
138 |
diffTemplate isNil ifTrue:[ |
|
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:[ |
|
12384 | 164 |
stream := PipeStream readingFrom:('support' asFilename / diffCmd). |
8933 | 165 |
stream isNil ifTrue:[ |
166 |
self error:'cannot execute diff' mayProceed:true. |
|
167 |
^ nil. |
|
168 |
] |
|
169 |
]. |
|
170 |
||
171 |
diffList := OrderedCollection new. |
|
172 |
(stream readWaitWithTimeout:10) ifTrue:[ |
|
173 |
"/ timeout |
|
174 |
stream close. |
|
175 |
self error:'cannot execute diff (timeout)' mayProceed:true. |
|
176 |
^ nil. |
|
177 |
]. |
|
178 |
||
179 |
[stream atEnd] whileFalse:[ |
|
180 |
line := stream nextLine. |
|
181 |
line notNil ifTrue:[diffList add:line] |
|
182 |
]. |
|
183 |
stream close. |
|
184 |
] ensure:[ |
|
185 |
tmpFile1 notNil ifTrue:[ tmpFile1 delete ]. |
|
186 |
tmpFile2 notNil ifTrue:[ tmpFile2 delete ]. |
|
187 |
]. |
|
188 |
^ diffList |
|
189 |
||
190 |
" |
|
191 |
self |
|
192 |
diffListFor:#( |
|
193 |
'hello world' |
|
194 |
'line1' |
|
195 |
'line3' |
|
196 |
'line4' |
|
197 |
) |
|
198 |
and: #( |
|
199 |
'Hello World' |
|
200 |
'line1' |
|
201 |
'line2' |
|
202 |
'line3' |
|
203 |
) |
|
204 |
" |
|
205 |
! ! |
|
206 |
||
207 |
!DiffListUtility class methodsFor:'documentation'! |
|
208 |
||
12384 | 209 |
version |
13340 | 210 |
^ '$Header: /cvs/stx/stx/libtool/DiffListUtility.st,v 1.8 2013-08-21 13:56:32 stefan Exp $' |
12384 | 211 |
! |
212 |
||
8933 | 213 |
version_CVS |
13340 | 214 |
^ '$Header: /cvs/stx/stx/libtool/DiffListUtility.st,v 1.8 2013-08-21 13:56:32 stefan Exp $' |
8933 | 215 |
! ! |
12358 | 216 |