Skip to content

Instantly share code, notes, and snippets.

@datio
Created March 2, 2017 06:44
Show Gist options
  • Save datio/eafb33dffdeea2823371247087c6d5d6 to your computer and use it in GitHub Desktop.
Save datio/eafb33dffdeea2823371247087c6d5d6 to your computer and use it in GitHub Desktop.
WIP
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"unicode"
)
// todo: Move to a separate file for readability.
const classExtensionContents = `<?php
namespace {addOnNamespace}\{originalNamespace};
class {className} extends XFCP_{className}
{
{methods}
}
// ******************** FOR IDE AUTO COMPLETE ********************
if (false)
{
class XFCP_{className} extends \{originalNamespace}\{className} {}
}`
// todo: Set values though the CLI or a config file.
const (
sourceDir = "/var/xf2/src/"
destinationDir = "/var/xf2/src/addons/datio/TestAddOn/"
addOnNamespace = "Datio/TestAddOn"
// originalNamespace = "XF\ConnectedAccount\Storage"
)
var matches = []string{}
func main() {
// todo: Move to a another file for readability.
_ = fmt.Sprintln(sourceDir, destinationDir, addOnNamespace, classExtensionContents)
if err := filepath.Walk(sourceDir+"XF/", getFireableDefinitions); err != nil {
fmt.Println(err.Error())
return
}
sort.Sort(sort.StringSlice(matches))
for index, match := range matches {
if index > 0 && match == matches[index-1] {
continue
}
// Generate here struct to be inserted into the type collection
fmt.Println(match)
}
}
var definitionRe = regexp.MustCompile(`(?:\$this->|\\XF::)extendClass\(['"]\\?(.*?)['"]\);|stringToClass\(.*?['"]\\?(.*?)['"].*?\);`)
var (
adminType = strings.NewReplacer(`%s\%s`, `%s\Admin`)
cliType = strings.NewReplacer(`%s\%s`, `%s\Cli`)
pubType = strings.NewReplacer(`%s\%s`, `%s\Pub`)
)
func getFireableDefinitions(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
// fileContents, err := ioutil.ReadFile(sourceDir + "XF/App.php")
fileContents, err := ioutil.ReadFile(path)
if err != nil {
return err
}
results := definitionRe.FindAllStringSubmatch(string(fileContents), -1)
if len(results) == 0 {
return nil
}
for _, submatch := range results {
if len(submatch[1]) > 0 {
matches = append(matches, submatch[1])
continue
} else if len(submatch[2]) == 0 {
continue
}
var first rune
for _, c := range submatch[2] {
first = c
break
}
if unicode.IsLower(first) {
continue
}
if strings.HasPrefix(submatch[2], `%s\%s`) {
matches = append(matches, adminType.Replace(submatch[2]))
matches = append(matches, cliType.Replace(submatch[2]))
matches = append(matches, pubType.Replace(submatch[2]))
continue
}
matches = append(matches, submatch[2])
}
return err
}
// todo:...
// func findWildcardPath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment