Skip to content

Commit

Permalink
Merge pull request #169 from SeasideSt/temporaryfiles-gemstone
Browse files Browse the repository at this point in the history
Change temporary file creation api
  • Loading branch information
jbrichau authored Feb 4, 2024
2 parents 933bc73 + 5ae43f8 commit 1a66633
Show file tree
Hide file tree
Showing 36 changed files with 182 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
files
newTemporaryFile
"Create a new temporary file in the systems temp directory and answer its pathString.
It is the users responsibility to delete or move the file, it will not be cleaned up automatically
(unless the host system has a policy for it)."
^ self subclassResponsibility
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
files
newTemporaryFileNamed: aName
"Create a new temporary file in the systems temp directory and answer its pathString.
It is the users responsibility to delete or move the file, it will not be cleaned up automatically
(unless the host system has a policy for it)."
self subclassResponsibility

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
files
deleteFile: aPathString

GsFile removeServerFile: aPathString
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
files
newTemporaryFile

^ self newTemporaryFileNamed: UUID new greaseString
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
files
newTemporaryFileNamed: aName

| fileName |
fileName := GRPlatform current pathSeparator,'tmp', GRPlatform current pathSeparator, aName.
(self fileExists: fileName) ifTrue: [ GRError new signal: 'A (temporary) file with name ', aName, ' already exists.' ].
^ (GsFile openWriteOnServer: fileName) pathName
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
files
newTemporaryFile

^ self newTemporaryFileNamed: UUID new greaseString
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
files
newTemporaryFileNamed: aName

| newFile |
newFile := FileLocator temp / aName.
newFile exists ifTrue: [ GRError new signal: 'A (temporary) file with name ', aName, ' already exists.' ].
^ newFile pathString
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ newTemporaryFileReference
It is the users responsibility to delete or move the file,
it will not be cleaned up automatically (unless the host system
has a policy for it)."
self
greaseDeprecatedApi: 'GRPlatform>>newTemporaryFileReference'
details: 'Use newTemporaryFile'.
^ self newTemporaryFileReferenceNamed: UUID new asString
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ newTemporaryFileReferenceNamed: aName
It is the users responsibility to delete or move the file,
it will not be cleaned up automatically (unless the host system
has a policy for it)."
self
greaseDeprecatedApi: 'GRPlatform>>newTemporaryFileReferenceNamed:'
details: 'Use newTemporaryFileNamed:'.
^ FileLocator temp / aName
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SystemOrganization addCategory: #'Grease-Pharo100-Core'!
self packageOrganizer ensurePackage: #'Grease-Pharo100-Core' withTags: #()!
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
files
newTemporaryFile

^ self newTemporaryFileNamed: UUID new greaseString
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
files
newTemporaryFileNamed: aName

| newFile |
newFile := FileLocator temp / aName.
newFile exists ifTrue: [ GRError new signal: 'A (temporary) file with name ', aName, ' already exists.' ].
^ newFile pathString
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ newTemporaryFileReference
It is the users responsibility to delete or move the file,
it will not be cleaned up automatically (unless the host system
has a policy for it)."
self
greaseDeprecatedApi: 'GRPlatform>>newTemporaryFileReference'
details: 'Use newTemporaryFile'.
^ self newTemporaryFileReferenceNamed: UUID new asString
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ newTemporaryFileReferenceNamed: aName
It is the users responsibility to delete or move the file,
it will not be cleaned up automatically (unless the host system
has a policy for it)."
self
greaseDeprecatedApi: 'GRPlatform>>newTemporaryFileReferenceNamed:'
details: 'Use newTemporaryFileNamed:'.
^ FileLocator temp / aName
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
files
newTemporaryFile

^ self newTemporaryFileNamed: UUID new greaseString
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
files
newTemporaryFileNamed: aName

| newFile |
newFile := FileLocator temp / aName.
newFile exists ifTrue: [ GRError new signal: 'A (temporary) file with name ', aName, ' already exists.' ].
^ newFile pathString
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ newTemporaryFileReference
It is the users responsibility to delete or move the file,
it will not be cleaned up automatically (unless the host system
has a policy for it)."
self
greaseDeprecatedApi: 'GRPlatform>>newTemporaryFileReference'
details: 'Use newTemporaryFile'.
^ self newTemporaryFileReferenceNamed: UUID new asString
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ newTemporaryFileReferenceNamed: aName
It is the users responsibility to delete or move the file,
it will not be cleaned up automatically (unless the host system
has a policy for it)."
self
greaseDeprecatedApi: 'GRPlatform>>newTemporaryFileReferenceNamed:'
details: 'Use newTemporaryFileNamed:'.
^ FileLocator temp / aName
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
files
deleteFile: aPathString
FileDirectory deleteFilePath: aPathString
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
files
newTemporaryFile

^ self newTemporaryFileNamed: UUID new greaseString
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
files
newTemporaryFileNamed: aName

| file |
file := FileDirectory default / aName.
file exists ifTrue: [ GRError new signal: 'A (temporary) file with name ', aName, ' already exists.' ].
^ file pathName
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
as yet unclassified
files
writeFileStreamOn: aString do: aBlock binary: aBoolean
| stream |
stream := aBoolean
ifTrue: [ (MultiByteFileStream fileNamed: aString) ascii; wantsLineEndConversion: true; yourself ]
ifFalse: [ (FileStream fileNamed: aString) binary ].
ifTrue: [ (FileStream fileNamed: aString) binary ]
ifFalse: [ (MultiByteFileStream fileNamed: aString) ascii; wantsLineEndConversion: true; yourself ].
[ aBlock value: stream ]
ensure: [ stream close ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
files
deleteFile: aPathString
FileDirectory deleteFilePath: aPathString
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
files
newTemporaryFile

^ self newTemporaryFileNamed: UUID new greaseString
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
files
newTemporaryFileNamed: aName

| file |
file := FileDirectory default / aName.
file exists ifTrue: [ GRError new signal: 'A (temporary) file with name ', aName, ' already exists.' ].
^ file pathName
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
files
writeFileStreamOn: aString do: aBlock binary: aBoolean
| stream |
stream := aBoolean
ifTrue: [ (FileStream fileNamed: aString) binary ]
ifFalse: [ (MultiByteFileStream fileNamed: aString) ascii; wantsLineEndConversion: true; yourself ].
[ aBlock value: stream ]
ensure: [ stream close ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
tests-files
testNewTemporaryFile

| temporaryFile1 temporaryFile2 |
[
temporaryFile1 := GRPlatform current newTemporaryFile.
temporaryFile2 := GRPlatform current newTemporaryFile.
GRPlatform current
writeFileStreamOn: temporaryFile1
do: [ :str | str nextPutAll: 'test temporary' ]
binary: false.
GRPlatform current
writeFileStreamOn: temporaryFile2
do: [ :str | str nextPutAll: #[116 101 115 116 32 116 101 109 112 111 114 97 114 121] ]
binary: true.
self assert: (GRPlatform current fileExists: temporaryFile1).
self deny: (GRPlatform current localNameOf: temporaryFile1) equals: (GRPlatform current localNameOf: temporaryFile2)
] ensure: [ GRPlatform current deleteFile: temporaryFile1. GRPlatform current deleteFile: temporaryFile2 ].
self deny: (GRPlatform current fileExists: temporaryFile1).
self deny: (GRPlatform current fileExists: temporaryFile2)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
tests-files
testNewTemporaryFileNamed

| temporaryFile fileName |
fileName := 'GRPlatformTestTemporaryFile'.
[
temporaryFile := GRPlatform current newTemporaryFileNamed: fileName.
GRPlatform current
writeFileStreamOn: temporaryFile
do: [ :str | str nextPutAll: 'test temporary' ]
binary: false.
self assert: (GRPlatform current fileExists: temporaryFile).
self should: [ GRPlatform current newTemporaryFileNamed: fileName ] raise: GRError.
] ensure: [ GRPlatform current deleteFile: temporaryFile ].
self deny: (GRPlatform current fileExists: temporaryFile).

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
tests-files
testWriteToFileInFolder

| bytes text |
bytes := ByteArray new.
self
writeToFile: bytes
withFileNameDo:[ :fileName |
self assert: (GRPlatform current contentsOfFile: fileName binary: true) equals: bytes ].

bytes := #[80 104 39 110 103 108 117 105 32 109 103 108 119 39 110 97 102 104 32 67 116 104 117 108 104 117 32 82 39 108 121 101 104 32 119 103 97 104 39 110 97 103 108 32 102 104 116 97 103 110].
self
writeToFile: bytes
withFileNameDo:[ :fileName |
self assert:(GRPlatform current contentsOfFile: fileName binary: true) equals: bytes ].

text := String new.
self
writeToFile: text
withFileNameDo:[ :fileName |
self assert: (GRPlatform current contentsOfFile: fileName binary: false) equals: text ].

text := 'Ph''nglui mglw''nafh Cthulhu R''lyeh wgah''nagl fhtagn'.
self
writeToFile: text
withFileNameDo:[ :fileName |
self assert: (GRPlatform current contentsOfFile: fileName binary: false) equals: text ]
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SystemOrganization addCategory: #'Grease-Tests-Core'!
self packageOrganizer ensurePackage: #'Grease-Tests-Core' withTags: #()!

0 comments on commit 1a66633

Please sign in to comment.