From 05efaf5e906e7ef6b94b8bfd7f84335579eb2de9 Mon Sep 17 00:00:00 2001 From: Sebastian Sastre Date: Tue, 2 Jan 2024 14:22:43 -0300 Subject: [PATCH] RideResource is now in charge of rebuilding the js assets and assetMap --- Ride/RideResource.class.st | 96 +++++++++++++++++++++++++++++++++++-- Ride/RideService.class.st | 98 ++------------------------------------ 2 files changed, 97 insertions(+), 97 deletions(-) diff --git a/Ride/RideResource.class.st b/Ride/RideResource.class.st index 1c89287..ea2bfbb 100644 --- a/Ride/RideResource.class.st +++ b/Ride/RideResource.class.st @@ -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 [ @@ -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 [ @@ -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 [ @@ -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 [ @@ -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 } @@ -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 } diff --git a/Ride/RideService.class.st b/Ride/RideService.class.st index b2a71d7..4336433 100644 --- a/Ride/RideService.class.st +++ b/Ride/RideService.class.st @@ -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 [ @@ -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 [ @@ -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 [ @@ -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 }