From b6f9544868faa71340a28978e9039804aa6fc0d8 Mon Sep 17 00:00:00 2001 From: evgenyk Date: Sun, 17 Nov 2024 18:47:44 +1100 Subject: [PATCH 1/4] updated configuration context to the function --- projectBundler/projectBundler.go | 18 +++++++++++++++--- projectBundler/projectBundler_test.go | 9 +++++++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/projectBundler/projectBundler.go b/projectBundler/projectBundler.go index 508f630..77793a1 100644 --- a/projectBundler/projectBundler.go +++ b/projectBundler/projectBundler.go @@ -15,11 +15,11 @@ import ( ) const ( - ProjectSettingsContextKey ProjectSettings = "projectSettings" + projectSettingsContextKey projectSettings = "projectSettings" ) type ( - ProjectSettings string + projectSettings string ProjectConfiguration struct { Version string `json:"version"` @@ -52,6 +52,7 @@ type ( DiscoveryOptions[TWorkflowSettings, TPageSettings any] struct { StartFolder string + OnRootDiscovered func(ctx context.Context, bundle ProjectConfiguration) OnWorkflowDiscovered func(ctx context.Context, bundle *bundler.BundlerResult[TWorkflowSettings]) OnPageDiscovered func(ctx context.Context, bundle *bundler.BundlerResult[TPageSettings]) } @@ -65,6 +66,13 @@ type ( } ) +func GetProjectConfiguration(ctx context.Context) *ProjectConfiguration { + if val, ok := ctx.Value(projectSettingsContextKey).(ProjectConfiguration); ok { + return &val + } + return nil +} + func (kw *KindeEnvironment[TWorkflowSettings, TPageSettings]) discoverWorkflows(ctx context.Context, absLocation string) { //environment/workflows workflowsPath := filepath.Join(absLocation, "environment", "workflows") @@ -143,7 +151,11 @@ func (p *projectBundler[TWorkflowSettings, TPageSettings]) Discover(ctx context. return nil, err } - ctx = context.WithValue(ctx, ProjectSettingsContextKey, project.Configuration) + ctx = context.WithValue(ctx, projectSettingsContextKey, project.Configuration) + + if p.options.OnRootDiscovered != nil { + p.options.OnRootDiscovered(ctx, project.Configuration) + } project.Environment.discoverWorkflows(ctx, filepath.Join(project.Configuration.AbsLocation, project.Configuration.RootDir)) project.Environment.discoverPages(ctx, filepath.Join(project.Configuration.AbsLocation, project.Configuration.RootDir)) diff --git a/projectBundler/projectBundler_test.go b/projectBundler/projectBundler_test.go index d5a52dc..5d78700 100644 --- a/projectBundler/projectBundler_test.go +++ b/projectBundler/projectBundler_test.go @@ -24,16 +24,21 @@ func Test_ProjectBunler(t *testing.T) { onWorkflowDiscoveredCalled := 0 onPageDiscoveredCalled := 0 + onRootDiscovered := false projectBundler := NewProjectBundler(DiscoveryOptions[workflowSettings, pageSettings]{ StartFolder: somePathInsideProject, + OnRootDiscovered: func(ctx context.Context, bundle ProjectConfiguration) { + onRootDiscovered = true + assert.True(onRootDiscovered) + }, OnWorkflowDiscovered: func(ctx context.Context, bundle *bundler.BundlerResult[workflowSettings]) { onWorkflowDiscoveredCalled++ - settings := ctx.Value(ProjectSettingsContextKey) + settings := GetProjectConfiguration(ctx) assert.NotNil(settings) }, OnPageDiscovered: func(ctx context.Context, bundle *bundler.BundlerResult[pageSettings]) { onPageDiscoveredCalled++ - settings := ctx.Value(ProjectSettingsContextKey) + settings := GetProjectConfiguration(ctx) assert.NotNil(settings) }, }) From a0f0db5dfca7ecb0cb9c3b3dffa1b79fac8664be Mon Sep 17 00:00:00 2001 From: evgenyk Date: Mon, 18 Nov 2024 11:05:08 +1100 Subject: [PATCH 2/4] adding hooks to support routing --- projectBundler/projectBundler_test.go | 15 ++++++++++++--- workflowBundler/workflowBundler.go | 14 ++++++++------ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/projectBundler/projectBundler_test.go b/projectBundler/projectBundler_test.go index 5d78700..a41d87c 100644 --- a/projectBundler/projectBundler_test.go +++ b/projectBundler/projectBundler_test.go @@ -2,6 +2,8 @@ package project_bundler import ( "context" + "fmt" + "path" "path/filepath" "testing" @@ -20,6 +22,7 @@ func Test_ProjectBunler(t *testing.T) { } type pageSettings struct { + Route string `json:"route"` } onWorkflowDiscoveredCalled := 0 @@ -38,8 +41,13 @@ func Test_ProjectBunler(t *testing.T) { }, OnPageDiscovered: func(ctx context.Context, bundle *bundler.BundlerResult[pageSettings]) { onPageDiscoveredCalled++ - settings := GetProjectConfiguration(ctx) - assert.NotNil(settings) + config := GetProjectConfiguration(ctx) + assert.NotNil(config) + pagesPath := path.Join(config.AbsLocation, config.RootDir, "environment/pages") + entryPoint := bundle.Content.BundlingOptions.EntryPoints[0] + relPath, _ := filepath.Rel(pagesPath, path.Join(bundle.Content.BundlingOptions.WorkingFolder, entryPoint)) + cleanedPath := path.Clean(fmt.Sprintf("/%v", relPath)) + bundle.Content.Settings.Other.Route = cleanedPath }, }) @@ -60,6 +68,7 @@ func Test_ProjectBunler(t *testing.T) { assert.Equal(2, len(kindeProject.Environment.Pages)) assert.Empty(kindeProject.Environment.Pages[0].Bundle.Errors) + assert.NotEmpty(kindeProject.Environment.Pages[0].Bundle.Content.Settings.Other.Route) assert.Empty(kindeProject.Environment.Pages[1].Bundle.Errors) - + assert.NotEmpty(kindeProject.Environment.Pages[1].Bundle.Content.Settings.Other.Route) } diff --git a/workflowBundler/workflowBundler.go b/workflowBundler/workflowBundler.go index 2caebe9..c267a1a 100644 --- a/workflowBundler/workflowBundler.go +++ b/workflowBundler/workflowBundler.go @@ -22,9 +22,10 @@ type ( } BundledContent[TSettings any] struct { - Source []byte `json:"source"` - BundleHash string `json:"hash"` - Settings WorkflowSettings[TSettings] `json:"settings"` + Source []byte `json:"source"` + BundleHash string `json:"hash"` + Settings WorkflowSettings[TSettings] `json:"settings"` + BundlingOptions BundlerOptions[TSettings] `json:"bundling_options"` } BundlerResult[TSettings any] struct { @@ -109,9 +110,10 @@ func (b *builder[TSettings]) Bundle(ctx context.Context) BundlerResult[TSettings file := tr.OutputFiles[0] result.Content = BundledContent[TSettings]{ - Source: file.Contents, - BundleHash: file.Hash, - Settings: result.discoverSettings(b.bundleOptions.IntrospectionExport, file.Contents), + Source: file.Contents, + BundleHash: file.Hash, + Settings: result.discoverSettings(b.bundleOptions.IntrospectionExport, file.Contents), + BundlingOptions: b.bundleOptions, } } From 58453df7840fcd188adcd48a3551bd0bed862283 Mon Sep 17 00:00:00 2001 From: evgenyk Date: Mon, 18 Nov 2024 11:16:47 +1100 Subject: [PATCH 3/4] review changes --- projectBundler/projectBundler_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/projectBundler/projectBundler_test.go b/projectBundler/projectBundler_test.go index a41d87c..8eb34b7 100644 --- a/projectBundler/projectBundler_test.go +++ b/projectBundler/projectBundler_test.go @@ -43,10 +43,10 @@ func Test_ProjectBunler(t *testing.T) { onPageDiscoveredCalled++ config := GetProjectConfiguration(ctx) assert.NotNil(config) - pagesPath := path.Join(config.AbsLocation, config.RootDir, "environment/pages") + pagesPath := path.Join(config.AbsLocation, config.RootDir, "environment", "pages") entryPoint := bundle.Content.BundlingOptions.EntryPoints[0] relPath, _ := filepath.Rel(pagesPath, path.Join(bundle.Content.BundlingOptions.WorkingFolder, entryPoint)) - cleanedPath := path.Clean(fmt.Sprintf("/%v", relPath)) + cleanedPath := path.Clean(fmt.Sprintf("%v", relPath)) bundle.Content.Settings.Other.Route = cleanedPath }, }) From b681a50caf5eb3d36c5d97a3a385a6c9173b2310 Mon Sep 17 00:00:00 2001 From: evgenyk Date: Mon, 18 Nov 2024 11:19:01 +1100 Subject: [PATCH 4/4] added documentation --- projectBundler/projectBundler.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/projectBundler/projectBundler.go b/projectBundler/projectBundler.go index 77793a1..a98b453 100644 --- a/projectBundler/projectBundler.go +++ b/projectBundler/projectBundler.go @@ -21,35 +21,41 @@ const ( type ( projectSettings string + // ProjectConfiguration is the struct that holds the project configuration. ProjectConfiguration struct { Version string `json:"version"` RootDir string `json:"rootDir"` AbsLocation string `json:"location"` } + // KindeWorkflow is the struct that holds the workflow configuration. KindeWorkflow[TSettings any] struct { WorkflowRootDirectory string `json:"workflow_root_directory"` EntryPoints []string `json:"entry_points"` Bundle bundler.BundlerResult[TSettings] `json:"bundle"` } + // KindePage is the struct that holds the page configuration. KindePage[TSettings any] struct { RootDirectory string `json:"root_directory"` EntryPoints []string `json:"entry_points"` Bundle bundler.BundlerResult[TSettings] `json:"bundle"` } + // KindeEnvironment is the struct that holds the workflows and pages. KindeEnvironment[TWorkflowSettings, TPageSettings any] struct { Workflows []KindeWorkflow[TWorkflowSettings] `json:"workflows"` Pages []KindePage[TPageSettings] `json:"pages"` discoveryOptions DiscoveryOptions[TWorkflowSettings, TPageSettings] } + // KindeProject is the struct that holds the project configuration and the environment. KindeProject[TWorkflowSettings, TPageSettings any] struct { Configuration ProjectConfiguration `json:"configuration"` Environment KindeEnvironment[TWorkflowSettings, TPageSettings] `json:"environment"` } + // DiscoveryOptions is the struct that holds the options for the project discovery. DiscoveryOptions[TWorkflowSettings, TPageSettings any] struct { StartFolder string OnRootDiscovered func(ctx context.Context, bundle ProjectConfiguration) @@ -57,6 +63,7 @@ type ( OnPageDiscovered func(ctx context.Context, bundle *bundler.BundlerResult[TPageSettings]) } + // ProjectBundler is the interface that wraps the Discover method. ProjectBundler[TWorkflowSettings, TPageSettings any] interface { Discover(ctx context.Context) (*KindeProject[TWorkflowSettings, TPageSettings], error) } @@ -66,6 +73,8 @@ type ( } ) +// GetProjectConfiguration returns the project configuration from the context. +// If the configuration is not found, it returns nil. func GetProjectConfiguration(ctx context.Context) *ProjectConfiguration { if val, ok := ctx.Value(projectSettingsContextKey).(ProjectConfiguration); ok { return &val @@ -138,7 +147,7 @@ func maybeAddPage[TWorkflowSettings, TPageSettings any](ctx context.Context, fil } } -// Discover implements ProjectBundler. +// Discover discovers the project and returns the project configuration and the environment. func (p *projectBundler[TWorkflowSettings, TPageSettings]) Discover(ctx context.Context) (*KindeProject[TWorkflowSettings, TPageSettings], error) { project := &KindeProject[TWorkflowSettings, TPageSettings]{ Environment: KindeEnvironment[TWorkflowSettings, TPageSettings]{ @@ -192,6 +201,7 @@ func (kp *KindeProject[TWorkflowSettings, TPageSettings]) discoverKindeRoot(star } +// NewProjectBundler returns a new instance of ProjectBundler. func NewProjectBundler[TWorkflowSettings, TPageSettings any](discoveryOptions DiscoveryOptions[TWorkflowSettings, TPageSettings]) ProjectBundler[TWorkflowSettings, TPageSettings] { return &projectBundler[TWorkflowSettings, TPageSettings]{ options: discoveryOptions,