2194 |
2194 |
2195 |aStream fileName newFileName savFilename needRename |
2195 |aStream fileName newFileName savFilename needRename |
2196 mySourceFileName sameFile s mySourceFileID anySourceRef| |
2196 mySourceFileName sameFile s mySourceFileID anySourceRef| |
2197 |
2197 |
2198 self isLoaded ifFalse:[ |
2198 self isLoaded ifFalse:[ |
2199 ^ FileOutErrorSignal |
2199 ^ FileOutErrorSignal |
2200 raiseRequestWith:self |
2200 raiseRequestWith:self |
2201 errorString:' - will not fileOut unloaded class: ', self name |
2201 errorString:' - will not fileOut unloaded class: ', self name |
2202 ]. |
2202 ]. |
2203 |
2203 |
2204 fileName := fileNameString asFilename. |
2204 fileName := fileNameString asFilename. |
2205 |
2205 |
2206 " |
2206 " |
2207 if file exists, copy the existing to a .sav-file, |
2207 if file exists, copy the existing to a .sav-file, |
2208 create the new file as XXX.new-file, |
2208 create the new file as XXX.new-file, |
2209 and, if that worked rename afterwards ... |
2209 and, if that worked rename afterwards ... |
2210 " |
2210 " |
2211 (fileName exists) ifTrue:[ |
2211 (fileName exists) ifTrue:[ |
2212 sameFile := false. |
2212 sameFile := false. |
2213 |
2213 |
2214 "/ check carefully - maybe, my source does not really come from that |
2214 "/ check carefully - maybe, my source does not really come from that |
2215 "/ file (i.e. all of my methods have their source as string) |
2215 "/ file (i.e. all of my methods have their source as string) |
2216 |
2216 |
2217 anySourceRef := false. |
2217 anySourceRef := false. |
2218 self instAndClassMethodsDo:[:m | |
2218 self instAndClassMethodsDo:[:m | |
2219 m sourcePosition notNil ifTrue:[ |
2219 m sourcePosition notNil ifTrue:[ |
2220 anySourceRef := true |
2220 anySourceRef := true |
2221 ] |
2221 ] |
2222 ]. |
2222 ]. |
2223 |
2223 |
2224 anySourceRef ifTrue:[ |
2224 anySourceRef ifTrue:[ |
2225 s := self sourceStream. |
2225 s := self sourceStream. |
2226 s notNil ifTrue:[ |
2226 s notNil ifTrue:[ |
2227 mySourceFileID := s pathName asFilename info id. |
2227 mySourceFileID := s pathName asFilename info id. |
2228 sameFile := (fileName info id) == mySourceFileID. |
2228 sameFile := (fileName info id) == mySourceFileID. |
2229 s close. |
2229 s close. |
2230 ] ifFalse:[ |
2230 ] ifFalse:[ |
2231 classFilename notNil ifTrue:[ |
2231 classFilename notNil ifTrue:[ |
2232 " |
2232 " |
2233 check for overwriting my current source file |
2233 check for overwriting my current source file |
2234 this is not allowed, since it would clobber my methods source |
2234 this is not allowed, since it would clobber my methods source |
2235 file ... you have to save it to some other place. |
2235 file ... you have to save it to some other place. |
2236 This happens if you ask for a fileOut into the source-directory |
2236 This happens if you ask for a fileOut into the source-directory |
2237 (from which my methods get their source) |
2237 (from which my methods get their source) |
2238 " |
2238 " |
2239 mySourceFileName := Smalltalk getSourceFileName:classFilename. |
2239 mySourceFileName := Smalltalk getSourceFileName:classFilename. |
2240 sameFile := (fileNameString = mySourceFileName). |
2240 sameFile := (fileNameString = mySourceFileName). |
2241 sameFile ifFalse:[ |
2241 sameFile ifFalse:[ |
2242 mySourceFileName notNil ifTrue:[ |
2242 mySourceFileName notNil ifTrue:[ |
2243 sameFile := (fileName info id) == (mySourceFileName asFilename info id) |
2243 sameFile := (fileName info id) == (mySourceFileName asFilename info id) |
2244 ] |
2244 ] |
2245 ]. |
2245 ]. |
2246 ] |
2246 ] |
2247 ]. |
2247 ]. |
2248 ]. |
2248 ]. |
2249 |
2249 |
2250 sameFile ifTrue:[ |
2250 sameFile ifTrue:[ |
2251 ^ FileOutErrorSignal |
2251 ^ FileOutErrorSignal |
2252 raiseRequestWith:fileNameString |
2252 raiseRequestWith:fileNameString |
2253 errorString:(' - may not overwrite sourcefile:', fileNameString) |
2253 errorString:(' - may not overwrite sourcefile:', fileNameString) |
2254 ]. |
2254 ]. |
2255 |
2255 |
2256 savFilename := Filename newTemporary. |
2256 savFilename := Filename newTemporary. |
2257 fileName copyTo:savFilename. |
2257 fileName copyTo:savFilename. |
2258 newFileName := fileName withSuffix:'new'. |
2258 newFileName := fileName withSuffix:'new'. |
2259 needRename := true |
2259 needRename := true |
2260 ] ifFalse:[ |
2260 ] ifFalse:[ |
2261 "/ another possible trap: if my sourceFileName is |
2261 "/ another possible trap: if my sourceFileName is |
2262 "/ the same as the written one AND the new files directory |
2262 "/ the same as the written one AND the new files directory |
2263 "/ is along the sourcePath, we also need a temporary file |
2263 "/ is along the sourcePath, we also need a temporary file |
2264 "/ first, to avoid accessing the newly written file. |
2264 "/ first, to avoid accessing the newly written file. |
2265 |
2265 |
2266 anySourceRef := false. |
2266 anySourceRef := false. |
2267 self instAndClassMethodsDo:[:m | |
2267 self instAndClassMethodsDo:[:m | |
2268 |mSrc| |
2268 |mSrc| |
2269 |
2269 |
2270 (mSrc := m sourceFilename) notNil ifTrue:[ |
2270 (mSrc := m sourceFilename) notNil ifTrue:[ |
2271 mSrc asFilename baseName = fileName baseName ifTrue:[ |
2271 mSrc asFilename baseName = fileName baseName ifTrue:[ |
2272 anySourceRef := true |
2272 anySourceRef := true |
2273 ] |
2273 ] |
2274 ] |
2274 ] |
2275 ]. |
2275 ]. |
2276 anySourceRef ifTrue:[ |
2276 anySourceRef ifTrue:[ |
2277 newFileName := fileName withSuffix:'new'. |
2277 newFileName := fileName withSuffix:'new'. |
2278 needRename := true |
2278 needRename := true |
2279 ] ifFalse:[ |
2279 ] ifFalse:[ |
2280 newFileName := fileName. |
2280 newFileName := fileName. |
2281 needRename := false |
2281 needRename := false |
2282 ] |
2282 ] |
2283 ]. |
2283 ]. |
2284 [ |
2284 [ |
2285 aStream := newFileName writeStream. |
2285 aStream := newFileName writeStream. |
2286 ] on:FileStream openErrorSignal do:[:ex| |
2286 ] on:FileStream openErrorSignal do:[:ex| |
2287 savFilename notNil ifTrue:[ |
2287 savFilename notNil ifTrue:[ |
2288 savFilename delete |
2288 savFilename delete |
2289 ]. |
2289 ]. |
2290 ^ FileOutErrorSignal |
2290 ^ FileOutErrorSignal |
2291 raiseRequestWith:newFileName |
2291 raiseRequestWith:newFileName name |
2292 errorString:(' - cannot create file:', newFileName name) |
2292 errorString:(' - cannot create file:', newFileName name) |
2293 ]. |
2293 ]. |
2294 self fileOutOn:aStream. |
2294 self fileOutOn:aStream. |
2295 aStream close. |
2295 aStream close. |
2296 |
2296 |
2297 " |
2297 " |
2298 finally, replace the old-file |
2298 finally, replace the old-file |
2299 be careful, if the old one is a symbolic link; in this case, |
2299 be careful, if the old one is a symbolic link; in this case, |
2300 we have to do a copy ... |
2300 we have to do a copy ... |
2301 " |
2301 " |
2302 needRename ifTrue:[ |
2302 needRename ifTrue:[ |
2303 newFileName copyTo:fileName. |
2303 newFileName copyTo:fileName. |
2304 newFileName delete |
2304 newFileName delete |
2305 ]. |
2305 ]. |
2306 savFilename notNil ifTrue:[ |
2306 savFilename notNil ifTrue:[ |
2307 savFilename delete |
2307 savFilename delete |
2308 ]. |
2308 ]. |
2309 |
2309 |
2310 " |
2310 " |
2311 add a change record; that way, administration is much easier, |
2311 add a change record; that way, administration is much easier, |
2312 since we can see in that changeBrowser, which changes have |
2312 since we can see in that changeBrowser, which changes have |