Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pages routing hooks #26

Merged
merged 4 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions projectBundler/projectBundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,55 @@
)

const (
ProjectSettingsContextKey ProjectSettings = "projectSettings"
projectSettingsContextKey projectSettings = "projectSettings"
evgenyk marked this conversation as resolved.
Show resolved Hide resolved
)

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)
}
Expand All @@ -65,6 +73,15 @@
}
)

// 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

Check warning on line 82 in projectBundler/projectBundler.go

View check run for this annotation

Codecov / codecov/patch

projectBundler/projectBundler.go#L82

Added line #L82 was not covered by tests
}
evgenyk marked this conversation as resolved.
Show resolved Hide resolved

func (kw *KindeEnvironment[TWorkflowSettings, TPageSettings]) discoverWorkflows(ctx context.Context, absLocation string) {
//environment/workflows
workflowsPath := filepath.Join(absLocation, "environment", "workflows")
Expand Down Expand Up @@ -130,7 +147,7 @@
}
}

// 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]{
Expand All @@ -143,7 +160,11 @@
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)
}
evgenyk marked this conversation as resolved.
Show resolved Hide resolved

project.Environment.discoverWorkflows(ctx, filepath.Join(project.Configuration.AbsLocation, project.Configuration.RootDir))
project.Environment.discoverPages(ctx, filepath.Join(project.Configuration.AbsLocation, project.Configuration.RootDir))
Expand Down Expand Up @@ -180,6 +201,7 @@

}

// 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,
Expand Down
22 changes: 18 additions & 4 deletions projectBundler/projectBundler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package project_bundler

import (
"context"
"fmt"
"path"
"path/filepath"
"testing"

Expand All @@ -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)
},
evgenyk marked this conversation as resolved.
Show resolved Hide resolved
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
},
})

Expand All @@ -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)
}
14 changes: 8 additions & 6 deletions workflowBundler/workflowBundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}

}
Expand Down
Loading