From 8840e097f88a5de6362b7021553383779263d914 Mon Sep 17 00:00:00 2001 From: David Bajger <45875448+Bajger@users.noreply.github.com> Date: Fri, 9 Feb 2024 16:04:29 +0100 Subject: [PATCH] Replaced ExTonelWriter with new ExercismCodeExporter to ensure version specific tonel formats of export. --- .../ExercismDev/ExercismGenerator.class.st | 30 +++-- .../ExercismGeneratorTest.class.st | 4 +- dev/src/ExercismTools/ExTonelWriter.class.st | 67 ------------ .../ExercismTools/ExTonelWriter.extension.st | 40 ------- .../ExercismCodeExporter.class.st | 103 ++++++++++++++++++ .../ExercismTools/ExercismExercise.class.st | 2 +- dev/src/ExercismTools/MCSnapshot.extension.st | 15 +++ 7 files changed, 139 insertions(+), 122 deletions(-) delete mode 100644 dev/src/ExercismTools/ExTonelWriter.class.st delete mode 100644 dev/src/ExercismTools/ExTonelWriter.extension.st create mode 100644 dev/src/ExercismTools/ExercismCodeExporter.class.st create mode 100644 dev/src/ExercismTools/MCSnapshot.extension.st diff --git a/dev/src/ExercismDev/ExercismGenerator.class.st b/dev/src/ExercismDev/ExercismGenerator.class.st index 6a1252f7..060fe0a1 100644 --- a/dev/src/ExercismDev/ExercismGenerator.class.st +++ b/dev/src/ExercismDev/ExercismGenerator.class.st @@ -52,7 +52,7 @@ Class { #name : #ExercismGenerator, #superclass : #Object, #instVars : [ - 'exTonelWriter', + 'codeExporter', 'exercismExercise', 'exercisesPath', 'osSubProcess' @@ -87,7 +87,7 @@ ExercismGenerator class >> generate [ path ifNotNil: [ self new exercisesPath: path; - exTonelWriter: (ExTonelWriter on: path); + codeExporter: (ExercismCodeExporter on: path); generate ] ] @@ -106,6 +106,12 @@ ExercismGenerator >> basePathReference [ ^ self exercisesPath parent ] +{ #category : #accessing } +ExercismGenerator >> codeExporter: anObject [ + + codeExporter := anObject +] + { #category : #helper } ExercismGenerator >> createTagSnapshotFor: packageOrTag [ | parentSnapshot | @@ -119,12 +125,6 @@ ExercismGenerator >> createTagSnapshotFor: packageOrTag [ [ :mc | mc className isNil or: [ mc actualClass category endsWith: packageOrTag name ] ]) ] -{ #category : #accessing } -ExercismGenerator >> exTonelWriter: anExTonelWriter [ - - exTonelWriter := anExTonelWriter -] - { #category : #accessing } ExercismGenerator >> exercisesPath [ @@ -227,13 +227,19 @@ ExercismGenerator >> generateSourceFilesFor: packageOrTag to: filePathString [ exerciseDirectoryRef ensureCreateDirectory. exerciseDirectoryRef deleteAll. - exTonelWriter - sourceDirectory: (solutionDirectoryRef relativeTo: exampleDirectoryRef) pathString; + codeExporter + directoryReference: solutionDirectoryRef; writeSnapshot: (self createTagSnapshotFor: packageOrTag). - + + "move files to root solution directory and remove unnecessary package dir" + ((solutionDirectoryRef / packageOrTag name) allChildrenMatching: '*.st') do: [:aFile | + aFile moveTo: solutionDirectoryRef + ]. + (solutionDirectoryRef / packageOrTag name) delete. + "Remove the package file as its not needed for Exercism" (solutionDirectoryRef / 'package.st') delete. - + "Move the test file down to the exerciseDirectory" testClasses := packageOrTag classes select: [ :cls | cls superclass = ExercismTest ]. testClasses do: [ :tc | diff --git a/dev/src/ExercismTests/ExercismGeneratorTest.class.st b/dev/src/ExercismTests/ExercismGeneratorTest.class.st index 344925ac..dc67037e 100644 --- a/dev/src/ExercismTests/ExercismGeneratorTest.class.st +++ b/dev/src/ExercismTests/ExercismGeneratorTest.class.st @@ -239,11 +239,11 @@ ExercismGeneratorTest >> setUp [ lineEnding := OSPlatform current lineEnding. memoryFileReference := FileSystem memory root / 'exercises'. - writer := ExTonelWriter on: memoryFileReference. + writer := ExercismCodeExporter on: memoryFileReference. instance := ExercismGenerator new exercisesPath: memoryFileReference; - exTonelWriter: writer; + codeExporter: writer; exercismExercise: MockExercismExercise; yourself. diff --git a/dev/src/ExercismTools/ExTonelWriter.class.st b/dev/src/ExercismTools/ExTonelWriter.class.st deleted file mode 100644 index 3e86110c..00000000 --- a/dev/src/ExercismTools/ExTonelWriter.class.st +++ /dev/null @@ -1,67 +0,0 @@ -" -I am a TonelWriter that provides finer level control of writing Tonel files for Exercism -" -Class { - #name : #ExTonelWriter, - #superclass : #TonelWriter, - #instVars : [ - 'sourceDirectory' - ], - #category : #'ExercismTools-Core' -} - -{ #category : #'*ExercismTools' } -ExTonelWriter >> mappedSnapshot: aSnapshot [ - "extracted from #writeSnapshot: to customise behavior" - - |tonelMap extensionDefinitions| - snapshot := aSnapshot. - tonelMap := Dictionary new. - - "Tonel export classes with their methods, mapped from their filename to content streams" - (snapshot definitions select: #isClassDefinition) - do: [ :classDef | |filename tonelStream| - filename := classDef exTonelFilename. - tonelStream := WriteStream on: String new. - self writeClass: classDef on: tonelStream. - tonelMap at: filename put: tonelStream ]. - - "... and method extensions" - extensionDefinitions := (snapshot definitions select: [ :each | - each isMethodDefinition and: [ each isExtensionMethod ] ]) removeDuplicates. - - extensionDefinitions do: [ :methodDef | |filename| - filename := methodDef exTonelFilename. - tonelMap at: filename ifAbsentPut: [ - (WriteStream on: String new) nextPutAll: 'Extension { #name : #', methodDef className, ' }' ; lf; yourself ]. - self writeMethodDefinition: methodDef on: (tonelMap at: filename). - ]. - ^tonelMap - -] - -{ #category : #private } -ExTonelWriter >> obtainPackageDir: aDefinition [ - "Overridden to allow a specific directory to be supplied" - - ^self sourceDirectory ifNil: [ super obtainPackageDir: aDefinition ] -] - -{ #category : #accessing } -ExTonelWriter >> sourceDirectory [ - ^ sourceDirectory -] - -{ #category : #accessing } -ExTonelWriter >> sourceDirectory: anObject [ - sourceDirectory := anObject -] - -{ #category : #'*ExercismTools' } -ExTonelWriter >> writeClass: aClassDefinition on: aStream [ - "Not clear on whether this is an override and still needed? ~tma~" - - self writeClassDefinition: aClassDefinition on: aStream. - self writeClassSideMethodDefinitions: aClassDefinition on: aStream. - self writeInstanceSideMethodDefinitions: aClassDefinition on: aStream -] diff --git a/dev/src/ExercismTools/ExTonelWriter.extension.st b/dev/src/ExercismTools/ExTonelWriter.extension.st deleted file mode 100644 index 84d86097..00000000 --- a/dev/src/ExercismTools/ExTonelWriter.extension.st +++ /dev/null @@ -1,40 +0,0 @@ -Extension { #name : #ExTonelWriter } - -{ #category : #'*ExercismTools' } -ExTonelWriter >> mappedSnapshot: aSnapshot [ - "extracted from #writeSnapshot: to customise behavior" - - |tonelMap extensionDefinitions| - snapshot := aSnapshot. - tonelMap := Dictionary new. - - "Tonel export classes with their methods, mapped from their filename to content streams" - (snapshot definitions select: #isClassDefinition) - do: [ :classDef | |filename tonelStream| - filename := classDef exTonelFilename. - tonelStream := WriteStream on: String new. - self writeClass: classDef on: tonelStream. - tonelMap at: filename put: tonelStream ]. - - "... and method extensions" - extensionDefinitions := (snapshot definitions select: [ :each | - each isMethodDefinition and: [ each isExtensionMethod ] ]) removeDuplicates. - - extensionDefinitions do: [ :methodDef | |filename| - filename := methodDef exTonelFilename. - tonelMap at: filename ifAbsentPut: [ - (WriteStream on: String new) nextPutAll: 'Extension { #name : #', methodDef className, ' }' ; lf; yourself ]. - self writeMethodDefinition: methodDef on: (tonelMap at: filename). - ]. - ^tonelMap - -] - -{ #category : #'*ExercismTools' } -ExTonelWriter >> writeClass: aClassDefinition on: aStream [ - "Not clear on whether this is an override and still needed? ~tma~" - - self writeClassDefinition: aClassDefinition on: aStream. - self writeClassSideMethodDefinitions: aClassDefinition on: aStream. - self writeInstanceSideMethodDefinitions: aClassDefinition on: aStream -] diff --git a/dev/src/ExercismTools/ExercismCodeExporter.class.st b/dev/src/ExercismTools/ExercismCodeExporter.class.st new file mode 100644 index 00000000..0147959a --- /dev/null +++ b/dev/src/ExercismTools/ExercismCodeExporter.class.st @@ -0,0 +1,103 @@ +" +I represent code export functionality for given Exercism exercise. Output is in Tonel format (version specific) which is represented as dictionary, where file names are keys, and source code in Tonel format is represented as values in disctionary. +" +Class { + #name : #ExercismCodeExporter, + #superclass : #Object, + #instVars : [ + 'tonelWriter' + ], + #category : #'ExercismTools-Core' +} + +{ #category : #'instance creation' } +ExercismCodeExporter class >> on: directoryReference [ + + ^ self basicNew + initializeWith: directoryReference; + yourself +] + +{ #category : #private } +ExercismCodeExporter >> addClassDefinitionsIn: tonelMap using: collectionOfDefinitions [ + + collectionOfDefinitions do: [ :classDef | + | filename tonelStream | + filename := classDef exTonelFilename. + tonelStream := WriteStream on: String new. + self writeClass: classDef on: tonelStream. + tonelMap at: filename put: tonelStream + ]. + +] + +{ #category : #private } +ExercismCodeExporter >> addExtensionDefinitionsIn: tonelMap using: collectionOfDefinitions [ + + collectionOfDefinitions do: [ :methodDef | + |filename| + filename := methodDef exTonelFilename. + tonelMap at: filename ifAbsentPut: [ + (WriteStream on: String new) + nextPutAll: 'Extension { #name : #'; + nextPutAll: methodDef className; + nextPutAll: ' }'; + lf; + yourself + ]. + tonelWriter writeMethodDefinition: methodDef on: (tonelMap at: filename). + ] +] + +{ #category : #initialize } +ExercismCodeExporter >> directoryReference: directoryReference [ + + tonelWriter directoryReference: directoryReference +] + +{ #category : #initialize } +ExercismCodeExporter >> initialize [ + + super initialize. + tonelWriter := TonelWriter new. +] + +{ #category : #initialize } +ExercismCodeExporter >> initializeWith: directoryReference [ + + self initialize. + self directoryReference: directoryReference +] + +{ #category : #'API - actions' } +ExercismCodeExporter >> mappedSnapshot: aSnapshot [ + "extracted from #writeSnapshot: to customise behavior" + + | tonelMap | + "this is needed since methods below that use tonel writer uses snapshot inst. variable" + tonelWriter snapshot: aSnapshot. + tonelMap := Dictionary new. + + "Tonel export classes with their methods, mapped from their filename to content streams" + self addClassDefinitionsIn: tonelMap using: aSnapshot classDefinitions. + + "... and method extensions" + self addExtensionDefinitionsIn: tonelMap using: aSnapshot extensionDefinitions. + ^tonelMap + +] + +{ #category : #private } +ExercismCodeExporter >> writeClass: aClassDefinition on: aStream [ + "Not clear on whether this is an override and still needed? ~tma~" + + tonelWriter writeClassDefinition: aClassDefinition on: aStream. + tonelWriter writeClassSideMethodDefinitions: aClassDefinition on: aStream. + tonelWriter writeInstanceSideMethodDefinitions: aClassDefinition on: aStream +] + +{ #category : #'API - actions' } +ExercismCodeExporter >> writeSnapshot: aSnapshot [ + + tonelWriter writeSnapshot: aSnapshot +] diff --git a/dev/src/ExercismTools/ExercismExercise.class.st b/dev/src/ExercismTools/ExercismExercise.class.st index 7a0512e8..d63c9ea3 100644 --- a/dev/src/ExercismTools/ExercismExercise.class.st +++ b/dev/src/ExercismTools/ExercismExercise.class.st @@ -335,7 +335,7 @@ ExercismExercise >> solutionSources [ | packageFileMap testResult resultDictionary solutionClassNames solutionFileNames | - packageFileMap := ExTonelWriter new mappedSnapshot: self snapshot. + packageFileMap := ExercismCodeExporter new mappedSnapshot: self snapshot. "Exclude non-solution classes, i.e. filter out tests" solutionClassNames := self solutionClasses collect: [ :c | c name ]. diff --git a/dev/src/ExercismTools/MCSnapshot.extension.st b/dev/src/ExercismTools/MCSnapshot.extension.st new file mode 100644 index 00000000..e80517e9 --- /dev/null +++ b/dev/src/ExercismTools/MCSnapshot.extension.st @@ -0,0 +1,15 @@ +Extension { #name : #MCSnapshot } + +{ #category : #'*ExercismTools' } +MCSnapshot >> classDefinitions [ + + ^ self definitions select: #isClassDefinition +] + +{ #category : #'*ExercismTools' } +MCSnapshot >> extensionDefinitions [ + + ^ (self definitions select: [ :each | + each isMethodDefinition and: [ each isExtensionMethod ] ]) + removeDuplicates +]