Skip to content

Commit

Permalink
Replaced ExTonelWriter with new ExercismCodeExporter to ensure versio…
Browse files Browse the repository at this point in the history
…n specific tonel formats of export.
  • Loading branch information
Bajger committed Feb 9, 2024
1 parent 6f1e244 commit 8840e09
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 122 deletions.
30 changes: 18 additions & 12 deletions dev/src/ExercismDev/ExercismGenerator.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Class {
#name : #ExercismGenerator,
#superclass : #Object,
#instVars : [
'exTonelWriter',
'codeExporter',
'exercismExercise',
'exercisesPath',
'osSubProcess'
Expand Down Expand Up @@ -87,7 +87,7 @@ ExercismGenerator class >> generate [
path ifNotNil: [
self new
exercisesPath: path;
exTonelWriter: (ExTonelWriter on: path);
codeExporter: (ExercismCodeExporter on: path);
generate
]
]
Expand All @@ -106,6 +106,12 @@ ExercismGenerator >> basePathReference [
^ self exercisesPath parent
]

{ #category : #accessing }
ExercismGenerator >> codeExporter: anObject [

codeExporter := anObject
]

{ #category : #helper }
ExercismGenerator >> createTagSnapshotFor: packageOrTag [
| parentSnapshot |
Expand All @@ -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 [

Expand Down Expand Up @@ -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 |
Expand Down
4 changes: 2 additions & 2 deletions dev/src/ExercismTests/ExercismGeneratorTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
67 changes: 0 additions & 67 deletions dev/src/ExercismTools/ExTonelWriter.class.st

This file was deleted.

40 changes: 0 additions & 40 deletions dev/src/ExercismTools/ExTonelWriter.extension.st

This file was deleted.

103 changes: 103 additions & 0 deletions dev/src/ExercismTools/ExercismCodeExporter.class.st
Original file line number Diff line number Diff line change
@@ -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
]
2 changes: 1 addition & 1 deletion dev/src/ExercismTools/ExercismExercise.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -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 ].
Expand Down
15 changes: 15 additions & 0 deletions dev/src/ExercismTools/MCSnapshot.extension.st
Original file line number Diff line number Diff line change
@@ -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
]

0 comments on commit 8840e09

Please sign in to comment.