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

feat(composer): add check for bin-dir #17

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
93 changes: 83 additions & 10 deletions drushlauncher/drushlauncher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,103 @@ import (
"fmt"
"os"
"path/filepath"
"encoding/json"
"io/ioutil"
)

type ComposerJson struct {
Config struct {
BinDir string `json:"bin-dir"`
} `json:"config"`
}

func FindDrupalRoot(path string) (string, error) {
var drushDir string

// Check if the vendor/bin/drush directory exists in the current directory
drushDir := filepath.Join(path, "vendor", "bin", "drush")
if _, err := os.Stat(drushDir); err == nil {
// Drupal root found, return the current directory
return path, nil
}
binDir := GetComposerBinDir(path)

if (binDir != "") {
drushDir := filepath.Join(path, binDir, "drush")
if _, err := os.Stat(drushDir); err == nil {
// Drupal root found, return the current directory
return path, nil
}
}
// Move one level up the directory tree
parentDir := filepath.Dir(path)
if parentDir == path {
// If we reached the root directory, stop traversing
return "", fmt.Errorf("Drupal root not found")
}

// Check if the vendor/bin/drush directory exists in the parent directory
drushDir = filepath.Join(parentDir, "vendor", "bin", "drush")
if _, err := os.Stat(drushDir); err == nil {
// Drupal root found in the parent directory, return the parent directory
return parentDir, nil
// Check if the drush exec file exists in the parent directory
binDir = GetComposerBinDir(parentDir)
if (binDir != "") {
drushDir = filepath.Join(path, binDir, "drush")
if _, err := os.Stat(drushDir); err == nil {
// Drupal root found in the parent directory, return the parent directory
return parentDir, nil
}
}

// Recursively continue searching in the parent directory
return FindDrupalRoot(parentDir)
}

func GetComposerBinDir(path string) string {
var dirPath = "";
var composerJsonPath = filepath.Join(path, dirPath, "composer.json")
var composerFound = false

// Check if composer.json exist
if _, err := os.Stat(composerJsonPath); os.IsNotExist(err) {
// Check one directory up in case Drupal webroot is in a subdirectory.
composerFound = false;
} else {
composerFound = true;
}

// Check the parent directory.
if !composerFound {
dirPath = ".."
composerJsonPath = filepath.Join(path, dirPath, "composer.json")
if _, err := os.Stat(composerJsonPath); os.IsNotExist(err) {
// No way to find the bin-dir flag, return the default vendor/bin
return filepath.Join("vendor", "bin")
} else {
composerFound = true;
}
}

composerJsonFile, err := os.Open(composerJsonPath)

// Check if we can open composer.json
if err != nil {
fmt.Println("Error opening composer.json:", err)
// Here we return blank because we know there's a file but we can't read
// it, so we can't check for the the bin-dir flag.
return ""
}

// Close the file when we are done.
defer composerJsonFile.Close()

// Read the file into a byte array
byteValue, _ := ioutil.ReadAll(composerJsonFile)

// Parse composer.json
var composerJsonValues ComposerJson
json.Unmarshal(byteValue, &composerJsonValues)

// Check if the bin-dir flag exists
if composerJsonValues.Config.BinDir == "" {
// If the bin-dir flag does not exist, use the default vendor/bin
return filepath.Join(dirPath, "vendor", "bin")
}

// If the bin-dir flag exists, use the value of the flag
binDir := filepath.Join(dirPath, composerJsonValues.Config.BinDir)

return binDir
}
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ func main() {
}

// Construct the full path to the drush executable
drushExec := filepath.Join(drupalRoot, "vendor", "bin", "drush")
// Parse the composer.json to get the bin-dir flag.
// If no bin-dir flag is found, or no composer.json file use the default vendor/bin
var drushRoot = filepath.Join("vendor", "bin")
if (drushlauncher.GetComposerBinDir(drupalRoot) != "") {
drushRoot = drushlauncher.GetComposerBinDir(drupalRoot)
}
drushExec := filepath.Join(drupalRoot, drushRoot, "drush")

// Check if the drush executable exists
if _, err := os.Stat(drushExec); os.IsNotExist(err) {
Expand Down