Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
Re-implement type detection
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisWiegman committed Jul 21, 2024
1 parent 6de58cc commit f5accc0
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func handleTypeDetection(cmd *cobra.Command, consoleOutput *console.Console, kan
}
}

if kanaSettings.GetBool("TypeIsDetected") {
if kanaSettings.GetBool("typeDetected") {
consoleOutput.Printf(
"A %s was detected in the current site folder. Starting site as a %s\n",
kanaSettings.Get("type"),
Expand Down
5 changes: 5 additions & 0 deletions internal/settings/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ var defaults = []Setting{
Usage: "Set the type of the installation, `site`, `plugin` or `theme`.",
},
},
{
name: "typeDetected",
defaultValue: "false",
settingType: "bool",
},
{
name: "updateInterval",
defaultValue: "7",
Expand Down
92 changes: 92 additions & 0 deletions internal/settings/settings.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package settings

import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -67,9 +70,98 @@ func Load(kanaSettings *Settings, version string, cmd *cobra.Command) error {
return err
}

err = loadDetectedType(kanaSettings)
if err != nil {
return err
}

return processStartFlags(cmd, kanaSettings)
}

func loadDetectedType(settings *Settings) error {
var err error
var isSite bool
oldType := settings.Get("type")
workingDirectory := settings.Get("workingDirectory")

isSite, err = helpers.PathExists(filepath.Join(workingDirectory, "wp-includes", "version.php"))
if err != nil {
return err
}

if isSite {
return err
}

items, _ := os.ReadDir(workingDirectory)

for _, item := range items {
if item.IsDir() {
continue
}

if item.Name() == "style.css" || filepath.Ext(item.Name()) == ".php" {
var f *os.File
var line string

f, err = os.Open(filepath.Join(workingDirectory, item.Name()))
if err != nil {
return err
}

reader := bufio.NewReader(f)
line, err = helpers.ReadLine(reader)

for err == nil {
exp := regexp.MustCompile(`(Plugin|Theme) Name: .*`)

for _, match := range exp.FindAllStringSubmatch(line, -1) {
if match[1] == "Theme" {
err = settings.Set("type", "theme")
if err != nil {
return err
}
} else {
err = settings.Set("type", "plugin")
if err != nil {
return err
}
}

if oldType != settings.Get("type") {
err = settings.Set("typeDetected", true)
if err != nil {
return err
}
}

return err
}
line, err = helpers.ReadLine(reader)
}
}
}

// We don't care if it is an empty folder.
if err == io.EOF {
err = nil //nolint:ineffassign,wastedassign
}

err = settings.Set("type", "site")
if err != nil {
return err
}

if oldType != settings.Get("type") {
err = settings.Set("typeDetected", true)
if err != nil {
return err
}
}

return err
}

func (s *Settings) Get(name string) string {
for i := range s.settings {
if strings.EqualFold(s.settings[i].name, name) {
Expand Down

0 comments on commit f5accc0

Please sign in to comment.