Skip to content

Commit

Permalink
RideResource is now in charge of rebuilding the js assets and assetMap
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianconcept committed Jan 2, 2024
1 parent 0e3035e commit 05efaf5
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 97 deletions.
96 changes: 93 additions & 3 deletions Ride/RideResource.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ RideResource class >> assetsMapFile [
^ self assetsMapDirectory / 'assets.json'
]

{ #category : #actions }
RideResource class >> assetsSetUp [

| map jsPath |
map := self assetsMap.
map ifEmpty: [
[ 'Assets map not found.' ] logWarning.
^ self rebuildJavaScriptAssets ].

[ 'JavaScript assets map found.' ] logInfo.

jsPath := (self assetsFileDirectory / 'js') asFileReference
ensureCreateDirectory.
jsPath entries isEmpty ifTrue: [
[ 'No JavaScript assets found.' ] logWarning.
self buildJavaScriptAssetsFor: map ]
]

{ #category : #accessing }
RideResource class >> baseUrl [

Expand All @@ -69,6 +87,52 @@ RideResource class >> baseUrl [
ifAbsent: [ self defaultBaseUrl ] ] systemValueAt: #baseUrl
]

{ #category : #actions }
RideResource class >> buildJavaScriptAssets [

| destinationHash destinationFile |
[ 'Building JavaScript assets and new map...' ] logInfo.
Ride resource javaScriptAssets keysAndValuesDo: [
:presenterSelector
:assets |
assets copy keysAndValuesDo: [ :jsSelector :jsFile |
destinationHash := UUID new asString36.
destinationFile := (self assetsDirectory / 'js'
/ (destinationHash , '.js')) asFileReference.
self makeJavaScriptAsset: jsFile to: destinationFile.
((self assetsMap
at: 'js'
ifAbsentPut: [ Dictionary new ])
at: presenterSelector
ifAbsentPut: [ Dictionary new ])
at: jsSelector
put: destinationFile basename ] ].

[ 'JavaScript assets ready.' ] logInfo.

self writeAssetsMap.
[ 'JavaScript assets map updated.' ] logInfo
]

{ #category : #actions }
RideResource class >> buildJavaScriptAssetsFor: assetsMap [

| destinationHash destinationFile mappedJsFilename |
Ride log: 'Building JavaScript assets as mapped...' level: #INFO.
Ride resource javaScriptAssets keysAndValuesDo: [
:presenterSelector
:assets |
assets copy keysAndValuesDo: [ :jsSelector :jsFile |
mappedJsFilename := (assetsMap js at: presenterSelector) at:
jsSelector.
destinationHash := (mappedJsFilename substrings: '.') first.
destinationFile := (self locator assetsFileDirectory / 'js'
/ (destinationHash , '.js')) asFileReference.
self makeJavaScriptAsset: jsFile to: destinationFile ] ].

Ride log: 'JavaScript assets ready.' level: #INFO
]

{ #category : #defaults }
RideResource class >> defaultAssetsDirectoryString [

Expand All @@ -87,6 +151,13 @@ RideResource class >> defaultUploadsBaseDirectoryString [
^ (Smalltalk imageDirectory / 'uploads') pathString
]

{ #category : #actions }
RideResource class >> deleteJavaScriptAssets [

(self assetsDirectory / 'js') asFileReference ensureDeleteAllChildren.
[ 'JavaScript assets deleted.' ] logInfo
]

{ #category : #accessing }
RideResource class >> emailTemplatesDirectory [

Expand Down Expand Up @@ -198,6 +269,15 @@ RideResource class >> layoutTemplatesDirectory [
^ self templatesDirectory / 'layouts'
]

{ #category : #actions }
RideResource class >> makeJavaScriptAsset: jsSourceFile to: destinationFile [

"We can improve this by minifying it in the destination."
self flag: #minifyJs.
destinationFile parent ensureCreateDirectory.
jsSourceFile copyTo: destinationFile
]

{ #category : #accessing }
RideResource class >> projectRootDirectory [

Expand All @@ -223,7 +303,17 @@ RideResource class >> readAssetsMap [
file := self assetsMapFile.
^ file exists
ifTrue: [ file readStreamDo: [ :stream | Json readFrom: stream ] ]
ifFalse: [ SmallDictionary new asJsonObject ]
ifFalse: [ Dictionary new asJsonObject ]
]

{ #category : #actions }
RideResource class >> rebuildJavaScriptAssets [

[ 'Rebuilding JavaScript assets...' ] logWarning.

self
deleteJavaScriptAssets;
buildJavaScriptAssets
]

{ #category : #actions }
Expand All @@ -236,9 +326,9 @@ RideResource class >> reset [
RideResource class >> resetAssetsMapFile [

self assetsMapFile deleteIfAbsent: [
^ Ride log: 'Assets map file not found' level: #WARNING ].
^ [ 'Assets map file not found' ] logWarning ].

Ride log: 'Assets map file deleted' level: #INFO
[ 'Assets map file deleted' ] logInfo
]

{ #category : #accessing }
Expand Down
98 changes: 4 additions & 94 deletions Ride/RideService.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -102,68 +102,6 @@ RideService class >> setUp [
self log: 'Router setup...'
]

{ #category : #actions }
RideService >> assetsSetUp [

| map jsPath |
map := Ride resource assetsMap.
map ifEmpty: [
Ride log: 'Assets map not found.' level: #WARNING.
^ self rebuildJavaScriptAssets ].

Ride log: 'JavaScript assets map found.'.

jsPath := (Ride resource assetsFileDirectory / 'js') asFileReference
ensureCreateDirectory.
jsPath entries isEmpty ifTrue: [
Ride log: 'No JavaScript assets found.' level: #WARNING.
self buildJavaScriptAssetsFor: map ]
]

{ #category : #actions }
RideService >> buildJavaScriptAssets [

| destinationHash destinationFile |
Ride log: 'Building JavaScript assets and new map...' level: #INFO.
Ride resource javaScriptAssets keysAndValuesDo: [
:presenterSelector
:assets |
assets copy keysAndValuesDo: [ :jsSelector :jsFile |
destinationHash := UUID new asString36.
destinationFile := (Ride resource assetsDirectory / 'js'
/ (destinationHash , '.js')) asFileReference.
self makeJavaScriptAsset: jsFile to: destinationFile.
((Ride resource assetsMap at: 'js' ifAbsentPut: [ SmallDictionary new ])
at: presenterSelector
ifAbsentPut: [ SmallDictionary new ])
at: jsSelector
put: destinationFile basename ] ].

Ride log: 'JavaScript assets ready.' level: #INFO.

Ride resource writeAssetsMap.
Ride log: 'JavaScript assets map updated.' level: #INFO
]

{ #category : #actions }
RideService >> buildJavaScriptAssetsFor: assetsMap [

| destinationHash destinationFile mappedJsFilename |
Ride log: 'Building JavaScript assets as mapped...' level: #INFO.
Ride resource javaScriptAssets keysAndValuesDo: [
:presenterSelector
:assets |
assets copy keysAndValuesDo: [ :jsSelector :jsFile |
mappedJsFilename := (assetsMap js at: presenterSelector) at:
jsSelector.
destinationHash := (mappedJsFilename substrings: '.') first.
destinationFile := (self locator assetsFileDirectory / 'js'
/ (destinationHash , '.js')) asFileReference.
self makeJavaScriptAsset: jsFile to: destinationFile ] ].

Ride log: 'JavaScript assets ready.' level: #INFO
]

{ #category : #accessing }
RideService >> cache [

Expand All @@ -183,14 +121,6 @@ RideService >> databaseStart [
self repository databaseStartOn: self
]

{ #category : #actions }
RideService >> deleteJavaScriptAssets [

(Ride resource assetsDirectory / 'js') asFileReference
ensureDeleteAllChildren.
Ride log: 'JavaScript assets deleted.' level: #INFO
]

{ #category : #accessing }
RideService >> getSessionFrom: request [

Expand Down Expand Up @@ -277,31 +207,12 @@ RideService >> maitre: anObject [
maitre := anObject
]

{ #category : #actions }
RideService >> makeJavaScriptAsset: jsSourceFile to: destinationFile [

"We can improve this by minifying it in the destination."

destinationFile parent ensureCreateDirectory.
jsSourceFile copyTo: destinationFile
]

{ #category : #accessing }
RideService >> newSessionId [

^ self class newSessionId
]

{ #category : #actions }
RideService >> rebuildJavaScriptAssets [

Ride log: 'Rebuilding JavaScript assets...' level: #WARNING.

self
deleteJavaScriptAssets;
buildJavaScriptAssets
]

{ #category : #accessing }
RideService >> repository [

Expand Down Expand Up @@ -372,12 +283,11 @@ RideService >> sessions: anObject [
{ #category : #actions }
RideService >> setUp [

Ride log: ('{1} setup...' format: { self class name }).
[ '{1} setup...' format: { self class name } ] logInfo.
self class setUp.

Ride log: 'Assets setup...'.
self assetsSetUp.


[ 'Assets setup...' ] logInfo.
Ride resource assetsSetUp
]

{ #category : #actions }
Expand Down

0 comments on commit 05efaf5

Please sign in to comment.