Ticket #239: encoding.st

File encoding.st, 4.2 KB (added by patrik.svestka@…, 5 years ago)

Smalltalk script used for repatching (some minor manual intervetion was still needed)

Line 
1"/init
2directoryList := OrderedCollection new.
3directoryPathToBeSearchedFor := String new.
4aWholeFileContent := String new.
5aFirstLine := String new.
6aWholeFileContentToWrite := String new.
7
8"/ testing 'C:\prg_sdk\test\stx'
9"/ Filename currentDirectory - is the directory of the smalltalk.bat executable
10scriptRunningDirectory := ((Smalltalk commandLine last) copyUpToLast: $\) asFilename.
11scriptRunningDirectory isEmptyOrNil ifTrue: [
12 Transcript showCR: 'Did you run the stx.com with --execute <name>.st?'.
13 ^ self.
14].
15
16scriptRunningDirectory allDirectoriesDo: [ :eachDirectory |
17 "/ finding only direct directories with .hg as subdirectory (last string)
18 ((eachDirectory asString findString: '.hg') = ((eachDirectory asString) size - 2)) ifTrue: [
19 directoryList add: (eachDirectory asString copyUpToLast: $\) asFilename.
20 directoryPathToBeSearchedFor := eachDirectory asString copyUpToLast: $\.
21 ].
22 "/ include all subdirectories of a directory containing .hg directory
23 "/ excluding all .hg and objmingw subdirectories (can't contain any *.st files that should be changed)
24 ((eachDirectory asString includesString: directoryPathToBeSearchedFor)
25 and: [(eachDirectory asString includesString: '.hg') not]
26 and: [(eachDirectory asString includesString: 'objmingw') not]) ifTrue: [
27 directoryList add: eachDirectory
28 ]
29].
30
31"/ go through the directory list + check encoding at all smalltalk files
32directoryList do: [ :eachDirectory |
33 currentDirectoryStFiles := eachDirectory filesMatching: '*.st' do: [ :eachStFile |
34 fileWithPath := (eachDirectory asString,'\', eachStFile asString) asFilename.
35 unicodeDetected := false.
36 fileReadStream := fileWithPath readStream.
37 [ [ fileReadStream atEnd ] whileFalse: [
38 eachLine := fileReadStream nextLine.
39 eachLine containsNon7BitAscii ifTrue: [ unicodeDetected := true ]
40 ].
41 ] ensure: [ fileReadStream close ].
42
43 "/ read whole file
44 aWholeFileContent := fileWithPath contentsOfEntireFile.
45 aFirstLine := aWholeFileContent firstLine.
46
47 "/Transcript showCR: ('Firstline: ', aFirstLine).
48
49 "/ change only if unicode is NOT detected and the conteint of the file is preceeded by
50 "/ "{ Encoding: utf8 }"
51 (unicodeDetected
52 or: [ (aFirstLine includesString: '"{ Encoding: utf8 }"') not]) ifTrue: [
53 Transcript showCR: 'Skipping file: ', fileWithPath asString.
54 ] ifFalse: [
55 Transcript showCR: 'Editing file: ', fileWithPath asString.
56 aReadStream := aWholeFileContent readStream.
57 [
58 aReadStream skipThroughAll: ('"{ Encoding: utf8 }"', String lf, String lf).
59 aWholeFileContentToWrite := aReadStream upToEnd.
60 ] ensure: [ aReadStream close ].
61
62 fileWithPath writingFileDo: [ :stream |
63 stream lineEndConvention: #lf. "/ *nix ends (only lf)
64 stream nextPutAllUnicode: aWholeFileContentToWrite. "/ could be also nextPutAllUnicode: for Utf-8 harmonization
65 "/ (not needed in this case)
66 ].
67 ]
68 ]
69].
70
71
72
73"/'Testing Unicode chars: ř ž č ルすしかき' isUnicodeString.
74
75"/dir := 'C:\prg_sdk\test\goodies\cypress' asFilename.
76"/file := 'CypressAbstractReader.st' asFilename.
77
78"/directoriesWithFiles inspect
79
80"directoriesWithFiles keysAndValuesDo: [ :aDirectory :aFileList |
81 Directory directory: aDirectory.
82 aFileList do: [:aFile |
83 aFileName := aFile copy asFilename.
84 read files
85 aWholeFileContents := aFileName contentsOfEntireFile.
86 self halt.
87 ]"
88 "/ write files
89 "/aFile writingFileDo: [ :stream | stream nextPutAll: aWholeFileContents ]
90"/].
91
92
93
94"/'C:\prg_sdk\test\goodies\builder\.hg' copyUpToLast: $\
95
96
97"/'fasdfas.hg' findString: '.hg'8
98"/ 'fasdfas.hg' size.10
99
100
101
102
103
104
105
106
107
108
109
110
111