Skip to content

Commit

Permalink
Merge pull request #261 from jumppad-labs/b-docs-refresh
Browse files Browse the repository at this point in the history
Fix checks for docs refresh
  • Loading branch information
eveld authored Dec 15, 2023
2 parents 1613525 + 82dd021 commit 308a5b9
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 161 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
},
"args": [
"dev",
"--disable-tty",
"${input:blueprint}",
],
},
Expand Down
5 changes: 5 additions & 0 deletions cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ func doUpdates(v view.View, e jumppad.Engine, source string, variables map[strin
v.Logger().Error(err.Error())
}

v.Logger().Debug("Changes detected", "new", len(new), "changed", len(changed), "removed", len(removed))
for _, n := range changed {
v.Logger().Debug("Changed", "resource", n.Metadata().ID)
}

if len(new) > 0 || len(changed) > 0 || len(removed) > 0 {
v.UpdateStatus(
fmt.Sprintf(
Expand Down
2 changes: 1 addition & 1 deletion examples/docs/docs.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ resource "chapter" "introduction" {
title = "Introduction"

page "introduction" {
content = "Some content"
content = "Some more content"
}
}
13 changes: 1 addition & 12 deletions pkg/config/resources/docs/provider_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package docs

import (
"fmt"
"os"

htypes "github.com/jumppad-labs/hclconfig/types"
"github.com/jumppad-labs/jumppad/pkg/clients/logger"
"github.com/jumppad-labs/jumppad/pkg/utils"
)

type BookProvider struct {
Expand All @@ -27,8 +25,6 @@ func (p *BookProvider) Init(cfg htypes.Resource, l logger.Logger) error {
}

func (p *BookProvider) Create() error {
p.log.Info(fmt.Sprintf("Creating %s", p.config.Type), "ref", p.config.Name)

index := BookIndex{
Title: p.config.Title,
}
Expand All @@ -48,26 +44,19 @@ func (p *BookProvider) Create() error {
}

func (p *BookProvider) Destroy() error {
// clean up the library folder
err := os.RemoveAll(utils.LibraryFolder("", os.ModePerm))

return err
return nil
}

func (p *BookProvider) Lookup() ([]string, error) {
return nil, nil
}

func (p *BookProvider) Refresh() error {
p.log.Debug("Refresh Book", "ref", p.config.ID)

p.Destroy()
p.Create()

return nil
}

func (p *BookProvider) Changed() (bool, error) {
p.log.Debug("Checking changes", "ref", p.config.ID)
return false, nil
}
9 changes: 1 addition & 8 deletions pkg/config/resources/docs/provider_chapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ func (p *ChapterProvider) Init(cfg htypes.Resource, l logger.Logger) error {
}

func (p *ChapterProvider) Create() error {
p.log.Info(fmt.Sprintf("Creating %s", p.config.Type), "ref", p.config.ID)

index := ChapterIndex{
Title: p.config.Title,
}
Expand Down Expand Up @@ -78,16 +76,11 @@ func (p *ChapterProvider) Lookup() ([]string, error) {
}

func (p *ChapterProvider) Refresh() error {
p.log.Debug("Refresh Chapter", "ref", p.config.ID)

p.Destroy()
p.Create()
p.Create() // always generate content

return nil
}

func (p *ChapterProvider) Changed() (bool, error) {
p.log.Debug("Checking changes", "ref", p.config.ID)

return false, nil
}
99 changes: 57 additions & 42 deletions pkg/config/resources/docs/provider_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ func (p *DocsProvider) Create() error {
return err
}

return nil
// write the content
return p.Refresh()
}

// Destroy the documentation container
Expand All @@ -114,6 +115,10 @@ func (p *DocsProvider) Destroy() error {
}
}

// remove the cached files
contentPath := utils.LibraryFolder("", 0775)
os.RemoveAll(contentPath)

return nil
}

Expand All @@ -123,14 +128,24 @@ func (p *DocsProvider) Lookup() ([]string, error) {
}

func (p *DocsProvider) Refresh() error {
p.log.Debug("Refresh Docs", "ref", p.config.ID)
changed, err := p.checkChanged()
if err != nil {
return fmt.Errorf("unable to check if content has changed: %s", err)
}

// no changes return
if !changed {
return nil
}

p.log.Info("Refresh Docs", "ref", p.config.ID)

// refresh content on disk
configPath := utils.LibraryFolder("config", 0775)

// jumppad.config.js
frontendConfigPath := filepath.Join(configPath, "jumppad.config.js")
err := p.writeConfig(frontendConfigPath)
err = p.writeConfig(frontendConfigPath)
if err != nil {
return err
}
Expand Down Expand Up @@ -171,9 +186,48 @@ func (p *DocsProvider) Refresh() error {
}
}

// store a checksum of the content
cs, err := p.generateContentChecksum()
if err != nil {
return fmt.Errorf("unable to generate checksum for content: %s", err)
}

p.config.ContentChecksum = cs

return nil
}

func (p *DocsProvider) Changed() (bool, error) {
p.log.Debug("Checking changes", "ref", p.config.ID)

// since the content has not been processed we can not reliably determine
// if the content has changed, so we will assume it has
return true, nil
}

// check if the content has changed
func (p *DocsProvider) checkChanged() (bool, error) {
if p.config.ContentChecksum == "" {
return true, nil
}

cs, err := p.generateContentChecksum()
if err != nil {
return true, fmt.Errorf("unable to generate checksum for content: %s", err)
}

return cs != p.config.ContentChecksum, nil
}

func (p *DocsProvider) generateContentChecksum() (string, error) {
cs, err := utils.ChecksumFromInterface(p.config.Content)
if err != nil {
return "", fmt.Errorf("unable to generate checksum for content: %s", err)
}

return cs, nil
}

func (p *DocsProvider) getDefaultPage() string {
if len(p.config.Content) > 0 {
book := p.config.Content[0]
Expand All @@ -188,12 +242,6 @@ func (p *DocsProvider) getDefaultPage() string {
return "/"
}

func (p *DocsProvider) Changed() (bool, error) {
p.log.Debug("Checking changes", "ref", p.config.ID)

return false, nil
}

func (p *DocsProvider) createDocsContainer() error {
// set the FQDN
fqdn := utils.FQDN(p.config.Name, p.config.Module, p.config.Type)
Expand Down Expand Up @@ -243,25 +291,6 @@ func (p *DocsProvider) createDocsContainer() error {
// ~/.jumppad/library/content
contentPath := utils.LibraryFolder("content", 0775)

for _, book := range p.config.Content {
bookPath := filepath.Join(contentPath, book.Name)

for _, chapter := range book.Chapters {
chapterPath := filepath.Join(bookPath, chapter.Name)
os.MkdirAll(chapterPath, 0755)
os.Chmod(chapterPath, 0755)

for _, page := range chapter.Pages {
pageFile := fmt.Sprintf("%s.mdx", page.Name)
pagePath := filepath.Join(chapterPath, pageFile)
err := os.WriteFile(pagePath, []byte(page.Content), 0755)
if err != nil {
return fmt.Errorf("unable to write page %s to disk at %s", page.Name, pagePath)
}
}
}
}

// mount the content
cc.Volumes = append(
cc.Volumes,
Expand All @@ -274,20 +303,6 @@ func (p *DocsProvider) createDocsContainer() error {
// ~/.jumppad/library/config
configPath := utils.LibraryFolder("config", 0775)

// write the navigation
navigationPath := filepath.Join(configPath, "navigation.jsx")
err = p.writeNavigation(navigationPath)
if err != nil {
return err
}

// write the progress
progressPath := filepath.Join(configPath, "progress.jsx")
err = p.writeProgress(progressPath)
if err != nil {
return err
}

cc.Volumes = append(
cc.Volumes,
types.Volume{
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/resources/docs/resource_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ type Docs struct {
// ContainerName is the fully qualified resource name for the container, this can be used
// to access the container from other sources
ContainerName string `hcl:"fqdn,optional" json:"fqdn,omitempty"`

// ContentChecksum is the checksum of the content directory, this is used to determine if the
// docs need to be recreated
ContentChecksum string `hcl:"content_checksum,optional" json:"content_checksum,omitempty"`
}

type Logo struct {
Expand All @@ -54,6 +58,7 @@ func (d *Docs) Process() error {
if r != nil {
kstate := r.(*Docs)
d.ContainerName = kstate.ContainerName
d.ContentChecksum = kstate.ContentChecksum
}
}

Expand Down
7 changes: 3 additions & 4 deletions pkg/jumppad/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ func (e *EngineImpl) Diff(path string, variables map[string]string, variablesFil
continue
}

// check if the resource has changed
if cr.Metadata().Checksum != r.Metadata().Checksum {
// check if the hcl resource text has changed
if cr.Metadata().Checksum.Parsed != r.Metadata().Checksum.Parsed {
// resource has changes rebuild
changed = append(changed, r)
continue
Expand Down Expand Up @@ -274,9 +274,8 @@ func (e *EngineImpl) ApplyWithVariables(path string, vars map[string]string, var
processErr := e.readAndProcessConfig(path, vars, variablesFile, e.createCallback)

// we need to remove any resources that are in the state but not in the config
e.log.Debug("removing resources in state but not in current config")
for _, r := range removed {
e.log.Debug("removing", "id", r.Metadata().ID)
e.log.Debug("removing resource in state but not current config", "id", r.Metadata().ID)

p := e.providers.GetProvider(r)
if p == nil {
Expand Down
Loading

0 comments on commit 308a5b9

Please sign in to comment.