Skip to content

Commit

Permalink
🚧 Migrate dependencies, update to ortfodb v1.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
gwennlbh committed Apr 20, 2024
1 parent c358ee7 commit 849e7e7
Show file tree
Hide file tree
Showing 46 changed files with 3,914 additions and 3,311 deletions.
1 change: 1 addition & 0 deletions .current-config-dir
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public/build/
.config

mock-projects/
!mock-projects/*/.portfoliodb/
!mock-projects/*/.ortfo/
!mock-projects/neptune/cover-arts/renders/square.png

vite.config.ts.js
Expand Down
18 changes: 9 additions & 9 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"label": "front",
"type": "shell",
"command": "pnpm frontend-dev",
"runOptions": {
"runOn": "folderOpen"
},
// "runOptions": {
// "runOn": "folderOpen"
// },
"presentation": {
"group": "dev"
},
Expand All @@ -17,9 +17,9 @@
"label": "back",
"type": "shell",
"command": "pnpm backend-dev",
"runOptions": {
"runOn": "folderOpen"
},
// "runOptions": {
// "runOn": "folderOpen"
// },
"presentation": {
"group": "dev"
}
Expand All @@ -28,9 +28,9 @@
"label": "tests",
"type": "shell",
"command": "pnpm vitest watch",
"runOptions": {
"runOn": "folderOpen"
},
// "runOptions": {
// "runOn": "folderOpen"
// },
"presentation": {
"group": "tdd"
}
Expand Down
2 changes: 1 addition & 1 deletion .wakatime-project
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ortfogui
ortfo
11 changes: 1 addition & 10 deletions Makefile → Justfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
.PHONY: build, installers

build:
# Build frontend
pnpm frontend-build
# Gather generated files
statik -f -src=dist/
# Build backend
cd backend && go build -o ../ortfo

install:
mv ortfo ~/.local/bin/

installers:
$(MAKE) build
just build
cd installers && ./create.sh

format:
Expand All @@ -21,14 +16,10 @@ format:
prettier --write frontend/** --plugin-search-dir=.

setup:
# Install frontend dependencies
pnpm install || yarn install || npm install
# Install the statik tool
go install github.com/rakyll/statik
# Prepare statik content
mkdir -p dist/
statik -f -src=dist/
# Install backend dependencies
go mod tidy

test:
Expand Down
32 changes: 18 additions & 14 deletions backend/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/mitchellh/go-homedir"
ortfodb "github.com/ortfo/db"
ortfomk "github.com/ortfo/mk"
)

func (settings *Settings) InitializeDatabase() error {
Expand All @@ -16,7 +16,7 @@ func (settings *Settings) InitializeDatabase() error {
return fmt.Errorf("couldn't create data directories: %w", err)
}
// initialize portfolio database
err = WriteIfNotExist(ConfigurationDirectory("portfolio-database", "database.json"), []byte("[]"))
err = WriteIfNotExist(ConfigurationDirectory("portfolio-database", "database.json"), []byte("{}"))
if err != nil {
return fmt.Errorf("couldn't initialize portfolio database file: %w", err)
}
Expand Down Expand Up @@ -48,7 +48,7 @@ func (settings *Settings) InitializeDatabase() error {
return nil
}

func (settings *Settings) LoadDatabase() (db ortfomk.Database, err error) {
func (settings *Settings) LoadDatabase() (db ortfodb.Database, err error) {
// Check if projects folder exists
projectsFolder, err := homedir.Expand(settings.ProjectsFolder)
if err != nil {
Expand All @@ -64,25 +64,27 @@ func (settings *Settings) LoadDatabase() (db ortfomk.Database, err error) {
}

println("Not re-building database...")
return ortfomk.LoadDatabase(ConfigurationDirectory("portfolio-database"))
return ortfodb.LoadDatabase(ConfigurationDirectory("portfolio-database", "database.json"), true)
}

func (settings *Settings) RebuildDatabase() error {
os.Chdir(ConfigurationDirectory("portfolio-database"))
LogToBrowser("Rebuilding database...")
LogToBrowser(fmt.Sprintf("context is %#v", ctx))
projectsFolder, err := homedir.Expand(settings.ProjectsFolder)
if err != nil {
return fmt.Errorf("while expanding ~: %w", err)
}
ortfodbConfig, err := ortfodb.NewConfiguration(ConfigurationDirectory("ortfodb.yaml"), ConfigurationDirectory("portfolio-database"))
ortfodbConfig, err := ortfodb.NewConfiguration(ConfigurationDirectory("ortfodb.yaml"))
if err != nil {
return fmt.Errorf("couldn't load database configuration: %w", err)
}

go ortfodb.BuildAll(
LogToBrowser("building with ctx %#v", ctx)
ctx.BuildAll(
projectsFolder,
ConfigurationDirectory("portfolio-database", "database.json"),
ortfodb.Flags{Scattered: true, Silent: true, ProgressFile: ConfigurationDirectory("portfolio-database", "progress.json")},
ortfodb.Flags{Scattered: true, Silent: true, ProgressInfoFile: ConfigurationDirectory("progress.jsonl")},
ortfodbConfig,
)
if crash := recover(); crash != nil {
Expand All @@ -91,15 +93,14 @@ func (settings *Settings) RebuildDatabase() error {
if err != nil {
return fmt.Errorf("couldn't build the portfolio's database: %w", err)
}
LogToBrowser("Finish rebuilding database")
return nil
}

func (settings *Settings) DeleteWorks(ids []string) error {
var err error
for _, id := range ids {
LogToBrowser("Deleting %s", JoinPaths(settings.ProjectsFolder, id, ".portfoliodb"))
err = os.RemoveAll(JoinPaths(settings.ProjectsFolder, id, ".portfoliodb"))
LogToBrowser("Deleting %s", JoinPaths(settings.ProjectsFolder, id, ".ortfo"))
err = os.RemoveAll(JoinPaths(settings.ProjectsFolder, id, ".ortfo"))
if err != nil {
ErrorToBrowser(err.Error())
return err
Expand All @@ -108,21 +109,24 @@ func (settings *Settings) DeleteWorks(ids []string) error {
return nil
}

func (settings *Settings) ProgressFile() ortfomk.ProgressFile {
var progressFile ortfomk.ProgressFile
progressFilePath := ConfigurationDirectory("portfolio-database", "progress.json")
func (settings *Settings) ProgressFile() ortfodb.ProgressInfoEvent {
var progressFile ortfodb.ProgressInfoEvent
progressFilePath := ConfigurationDirectory("progress.jsonl")
if _, err := os.Stat(progressFilePath); os.IsNotExist(err) {
return progressFile
}
raw, err := os.ReadFile(progressFilePath)
if string(raw) == "" {
return settings.ProgressFile()
}
LogToBrowser("Progress file raw is %s", string(raw))
// keep only the last line
if err != nil {
ErrorToBrowser("Couldn't read progress file: %s", err)
return progressFile
}
// Keep only the last line
lines := strings.Split(string(raw), "\n")
raw = []byte(lines[len(lines)-2])
err = json.Unmarshal(raw, &progressFile)
if err != nil {
ErrorToBrowser("Couldn't parse progress file: %s. Raw was %q", err, string(raw))
Expand Down
70 changes: 33 additions & 37 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/davecgh/go-spew/spew"
"github.com/mitchellh/go-homedir"
ortfodb "github.com/ortfo/db"
ortfomk "github.com/ortfo/mk"
"github.com/skratchdot/open-golang/open"
"github.com/sqweek/dialog"
"github.com/webview/webview"
Expand All @@ -28,7 +27,7 @@ type PickFileConstraint struct {
}

var w webview.WebView
var ctx ortfodb.RunContext
var ctx *ortfodb.RunContext
var settings Settings
var Port = randomAvailablePort()

Expand All @@ -53,14 +52,15 @@ func main() {

func newOrtfoContext() error {
var err error
ortfodbConfig, err := ortfodb.NewConfiguration(ConfigurationDirectory("ortfodb.yaml"), ConfigurationDirectory("portfolio-database"))
// ortfodbConfig, err := ortfodb.NewConfiguration(ConfigurationDirectory("ortfodb.yaml"))
ortfodbConfig, err := settings.InitializeOtfodbConfig()
if err != nil {
return fmt.Errorf("couldn't load database configuration: %w", err)
}
ctx, err = ortfodb.PrepareBuild(projectsFolder(), ConfigurationDirectory("portfolio-database", "database.json"), ortfodb.Flags{
Scattered: true,
Silent: true,
ProgressFile: ConfigurationDirectory("portfolio-database", "progress.json"),
Scattered: true,
Silent: true,
ProgressInfoFile: ConfigurationDirectory("progress.jsonl"),
}, ortfodbConfig)
if err != nil {
return fmt.Errorf("while preparing ortfodb build context: %w", err)
Expand All @@ -69,6 +69,9 @@ func newOrtfoContext() error {
}

func startWebview() error {
ortfodb.LogFilePath = ConfigurationDirectory("ortfodb.log")
ortfodb.PrependDateToLogs = true
ortfodb.ReleaseBuildLock(ConfigurationDirectory("portfolio-database", "database.json"))
err := newOrtfoContext()
if err != nil {
return err
Expand All @@ -77,6 +80,9 @@ func startWebview() error {
w = webview.New(true)
defer w.Destroy()
w.SetTitle("ortfo")
if os.Getenv("DEV") == "yes" {
w.SetTitle("ortfo [dev]")
}
w.SetSize(800, 600, webview.HintMin)
w.Navigate("http://localhost:" + func() string {
if os.Getenv("DEV") == "yes" {
Expand Down Expand Up @@ -106,25 +112,21 @@ func startWebview() error {
w.Terminate()
return nil
})
w.Bind("backend__databaseRead", func() (ortfomk.Database, error) {
w.Bind("backend__databaseRead", func() (ortfodb.Database, error) {
return settings.LoadDatabase()
})
w.Bind("backend__rebuildDatabase", func() error {
return settings.RebuildDatabase()
})
w.Bind("backend__rebuildWork", func(workID string) error {
newOrtfoContext()
return ortfodb.BuildSome(workID, projectsFolder(), ctx.OutputDatabaseFile, ctx.Flags, *ctx.Config)
})
w.Bind("backend__analyzeMedia", func(workID string, mediaEmbed ortfodb.MediaEmbedDeclaration) (ortfodb.Media, error) {
settings, _ := LoadSettings()
projectsFolder, err := homedir.Expand(settings.ProjectsFolder)
if err != nil {
return ortfodb.Media{}, fmt.Errorf("while expanding projects folder: %w", err)
if workID == "" {
return fmt.Errorf("workID is empty")
}
_, media, err := (&ortfodb.RunContext{
DatabaseDirectory: projectsFolder,
}).AnalyzeMediaFile(workID, mediaEmbed)
_, err := ctx.BuildSome(workID, projectsFolder(), ctx.OutputDatabaseFile, ctx.Flags, *ctx.Config)
return err
})
w.Bind("backend__analyzeMedia", func(workID string, mediaEmbed ortfodb.Media) (ortfodb.Media, error) {
_, media, _, err := ctx.AnalyzeMediaFile(workID, mediaEmbed)
if err != nil {
return ortfodb.Media{}, fmt.Errorf("while analyzing media: %w", err)
}
Expand All @@ -138,7 +140,7 @@ func startWebview() error {

return Writeback(settings, description, workID)
})
w.Bind("backend__writeTags", func(tags []ortfomk.Tag) error {
w.Bind("backend__writeTags", func(tags []ortfodb.Tag) error {
spew.Dump(tags)
tagsBytes, err := yaml.Marshal(tags)
if err != nil {
Expand All @@ -147,7 +149,7 @@ func startWebview() error {

return os.WriteFile(ConfigurationDirectory("portfolio-database", "tags.yaml"), tagsBytes, 0644)
})
w.Bind("backend__writeTechnologies", func(technologies []ortfomk.Technology) error {
w.Bind("backend__writeTechnologies", func(technologies []ortfodb.Technology) error {
tagsBytes, err := yaml.Marshal(technologies)
if err != nil {
ErrorToBrowser(fmt.Sprintf("while converting to YAML: %v", err))
Expand All @@ -156,16 +158,16 @@ func startWebview() error {

return os.WriteFile(ConfigurationDirectory("portfolio-database", "technologies.yaml"), tagsBytes, 0644)
})
w.Bind("backend__writeExternalSites", func(externalSites []ortfomk.ExternalSite) error {
w.Bind("backend__writeExternalSites", func(externalSites []ExternalSite) error {
sitesBytes, err := yaml.Marshal(externalSites)
if err != nil {
return fmt.Errorf("while converting to YAML: %w", err)
}

return os.WriteFile(ConfigurationDirectory("portfolio-database", "sites.yaml"), sitesBytes, 0644)
})
w.Bind("backend__writeCollection", func(collections []ortfomk.Collection) error {
collectionsByID := make(map[string]ortfomk.Collection)
w.Bind("backend__writeCollection", func(collections []Collection) error {
collectionsByID := make(map[string]Collection)
for _, c := range collections {
collectionsByID[c.ID] = c
}
Expand All @@ -191,7 +193,7 @@ func startWebview() error {

return settings.LoadUIState()
})
w.Bind("backend__getBuildProgress", func() ortfomk.ProgressFile {
w.Bind("backend__getBuildProgress", func() ortfodb.ProgressInfoEvent {
settings, _ := LoadSettings()
return settings.ProgressFile()
})
Expand Down Expand Up @@ -256,7 +258,7 @@ func startWebview() error {
if err != nil {
return "", fmt.Errorf("while loading settings: %w", err)
}
bytes, err := os.ReadFile(JoinPaths(settings.ProjectsFolder, workID, ".portfoliodb", "description.md"))
bytes, err := os.ReadFile(JoinPaths(settings.ProjectsFolder, workID, ".ortfo", "description.md"))
return string(bytes), err
})
w.Bind("backend__writeRawDescription", func(workID string, content string) error {
Expand All @@ -265,7 +267,7 @@ func startWebview() error {
return fmt.Errorf("while loading settigns: %w", err)
}

return os.WriteFile(JoinPaths(settings.ProjectsFolder, workID, ".portfoliodb", "description.md"), []byte(content), 0644)
return os.WriteFile(JoinPaths(settings.ProjectsFolder, workID, ".ortfo", "description.md"), []byte(content), 0644)
})
w.Bind("backend__clearThumbnails", func() error {
return os.RemoveAll(ConfigurationDirectory("portfolio-database", "media"))
Expand Down Expand Up @@ -320,21 +322,15 @@ func Initialize() error {
return fmt.Errorf("couldn't save default settings: %w", err)
}

_, err = settings.InitializeOtfodbConfig()
if err != nil {
return fmt.Errorf("couldn't initialize configuration file: %w", err)
}

err = settings.InitializeDatabase()
if err != nil {
return fmt.Errorf("couldn't initialize portfolio database: %w", err)
}

ortfomk.WarmUp(&ortfomk.GlobalData{
Flags: ortfomk.Flags{
ProgressFile: ConfigurationDirectory(".progress.json"),
Silent: true,
},
// TODO make configurable
OutputDirectory: ConfigurationDirectory("built"),
// TODO #4 make configurable
TemplatesDirectory: ConfigurationDirectory("templates"),
HTTPLinks: make(map[string][]string),
})
return nil
}
2 changes: 1 addition & 1 deletion backend/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (m mediaRoot) Open(name string) (http.File, error) {
name = strings.TrimPrefix(name, "/")
command, path, _ := strings.Cut(name, "/")
path = filepath.Clean(path)
println(path, name)
println(command, path)
switch command {
case "projects":
return http.Dir(m.projectsRoot).Open(path)
Expand Down
Loading

0 comments on commit 849e7e7

Please sign in to comment.