diff --git a/projectBundler/projectBundler.go b/projectBundler/projectBundler.go index 508f630..a98b453 100644 --- a/projectBundler/projectBundler.go +++ b/projectBundler/projectBundler.go @@ -15,47 +15,55 @@ import ( ) const ( - ProjectSettingsContextKey ProjectSettings = "projectSettings" + projectSettingsContextKey projectSettings = "projectSettings" ) type ( - ProjectSettings string + 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) OnWorkflowDiscovered func(ctx context.Context, bundle *bundler.BundlerResult[TWorkflowSettings]) 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) } @@ -65,6 +73,15 @@ 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 + } + return nil +} + func (kw *KindeEnvironment[TWorkflowSettings, TPageSettings]) discoverWorkflows(ctx context.Context, absLocation string) { //environment/workflows workflowsPath := filepath.Join(absLocation, "environment", "workflows") @@ -130,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]{ @@ -143,7 +160,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)) @@ -180,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, diff --git a/projectBundler/projectBundler_test.go b/projectBundler/projectBundler_test.go index d5a52dc..8eb34b7 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,21 +22,32 @@ func Test_ProjectBunler(t *testing.T) { } type pageSettings struct { + Route string `json:"route"` } 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) - 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 }, }) @@ -55,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, } }