From 1a35f98a360e602807c48df9d0a363f64ffde9bf Mon Sep 17 00:00:00 2001 From: Mathew Winstone Date: Fri, 19 Jan 2024 10:31:00 -0500 Subject: [PATCH 1/7] feat(composer): add check for bin-dir --- drushlauncher/drushlauncher.go | 73 +++++++++++++++++++++++++++++----- main.go | 4 +- 2 files changed, 66 insertions(+), 11 deletions(-) diff --git a/drushlauncher/drushlauncher.go b/drushlauncher/drushlauncher.go index eaf74b2..1f75ebd 100644 --- a/drushlauncher/drushlauncher.go +++ b/drushlauncher/drushlauncher.go @@ -4,16 +4,29 @@ 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 { @@ -21,13 +34,53 @@ func FindDrupalRoot(path string) (string, error) { 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 { + composerJsonPath := filepath.Join(path, "composer.json") + + // Check if composer.json and composer.json exist + if _, err := os.Stat(composerJsonPath); os.IsNotExist(err) { + return "" + } + composerJsonFile, err := os.Open(composerJsonPath) + + // Check if we can open composer.json + if err != nil { + fmt.Println("Error opening composer.json:", err) + 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("vendor", "bin") + } + + // If the bin-dir flag exists, use the value of the flag + binDir := filepath.Join(composerJsonValues.Config.BinDir) + + return binDir +} \ No newline at end of file diff --git a/main.go b/main.go index 5a253a5..c08176e 100644 --- a/main.go +++ b/main.go @@ -48,7 +48,9 @@ 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, use the default vendor/bin + drushExec := filepath.Join(drupalRoot, drushlauncher.GetComposerBinDir(drupalRoot), "drush") // Check if the drush executable exists if _, err := os.Stat(drushExec); os.IsNotExist(err) { From 303847e7bff37c1fbc466283e3767a0cff1a2a49 Mon Sep 17 00:00:00 2001 From: Mathew Winstone Date: Mon, 29 Jan 2024 11:20:46 -0500 Subject: [PATCH 2/7] fix(defaults): add fallback to vendor/bin --- drushlauncher/drushlauncher.go | 2 +- main.go | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/drushlauncher/drushlauncher.go b/drushlauncher/drushlauncher.go index 1f75ebd..f5f8725 100644 --- a/drushlauncher/drushlauncher.go +++ b/drushlauncher/drushlauncher.go @@ -51,7 +51,7 @@ func FindDrupalRoot(path string) (string, error) { func GetComposerBinDir(path string) string { composerJsonPath := filepath.Join(path, "composer.json") - // Check if composer.json and composer.json exist + // Check if composer.json exist if _, err := os.Stat(composerJsonPath); os.IsNotExist(err) { return "" } diff --git a/main.go b/main.go index 02707a8..606d2d7 100644 --- a/main.go +++ b/main.go @@ -56,8 +56,9 @@ func main() { // Construct the full path to the drush executable // Parse the composer.json to get the bin-dir flag. - // If no bin-dir flag is found, use the default vendor/bin - drushExec := filepath.Join(drupalRoot, drushlauncher.GetComposerBinDir(drupalRoot), "drush") + // If no bin-dir flag is found, or no composer.json file use the default vendor/bin + var drushRoot = drushlauncher.GetComposerBinDir(drupalRoot) ? drushlauncher.GetComposerBinDir(drupalRoot) : filepath.Join("vendor", "bin") + drushExec := filepath.Join(drupalRoot, drushRoot, "drush") // Check if the drush executable exists if _, err := os.Stat(drushExec); os.IsNotExist(err) { From 7bad3fb65cbb08c08c38ec30b1f7b5c071e3acaa Mon Sep 17 00:00:00 2001 From: Mathew Winstone Date: Mon, 29 Jan 2024 11:22:46 -0500 Subject: [PATCH 3/7] fix(defaults): add fallback to vendor/bin --- main.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 606d2d7..f75dbf5 100644 --- a/main.go +++ b/main.go @@ -57,7 +57,12 @@ func main() { // Construct the full path to the drush executable // 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 = drushlauncher.GetComposerBinDir(drupalRoot) ? drushlauncher.GetComposerBinDir(drupalRoot) : filepath.Join("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 From 20bd888cf2a504a5802b8155c091949f14273916 Mon Sep 17 00:00:00 2001 From: Mathew Winstone Date: Mon, 29 Jan 2024 11:23:13 -0500 Subject: [PATCH 4/7] chore(cleanup): remove whitespace --- main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/main.go b/main.go index f75dbf5..f9875d9 100644 --- a/main.go +++ b/main.go @@ -58,7 +58,6 @@ func main() { // 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) } From e82855f29e08d93e2fb2702ea7bd6d857f4df46e Mon Sep 17 00:00:00 2001 From: Mathew Winstone Date: Mon, 29 Jan 2024 12:50:57 -0500 Subject: [PATCH 5/7] fix(composer): add more fallback cases --- drushlauncher/drushlauncher.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/drushlauncher/drushlauncher.go b/drushlauncher/drushlauncher.go index f5f8725..47d9636 100644 --- a/drushlauncher/drushlauncher.go +++ b/drushlauncher/drushlauncher.go @@ -53,13 +53,16 @@ func GetComposerBinDir(path string) string { // Check if composer.json exist if _, err := os.Stat(composerJsonPath); os.IsNotExist(err) { - return "" + // No way to find the bin-dir flag, return the default vendor/bin + return filepath.Join("vendor", "bin") } 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 "" } From 0f6c67432faa9153c3f5e8c8507ba76cc9fa9719 Mon Sep 17 00:00:00 2001 From: Mathew Winstone Date: Thu, 8 Feb 2024 10:56:17 -0500 Subject: [PATCH 6/7] fix(composer): update logic When a root is passed, we need to check the parent in case web dir is not the same a composer dir --- drushlauncher/drushlauncher.go | 25 +++++++++++++++++++++---- main.go | 1 - 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/drushlauncher/drushlauncher.go b/drushlauncher/drushlauncher.go index 47d9636..59417dd 100644 --- a/drushlauncher/drushlauncher.go +++ b/drushlauncher/drushlauncher.go @@ -49,13 +49,30 @@ func FindDrupalRoot(path string) (string, error) { } func GetComposerBinDir(path string) string { - composerJsonPath := filepath.Join(path, "composer.json") + 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) { - // No way to find the bin-dir flag, return the default vendor/bin - return filepath.Join("vendor", "bin") + // 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 @@ -83,7 +100,7 @@ func GetComposerBinDir(path string) string { } // If the bin-dir flag exists, use the value of the flag - binDir := filepath.Join(composerJsonValues.Config.BinDir) + binDir := filepath.Join(dirPath, composerJsonValues.Config.BinDir) return binDir } \ No newline at end of file diff --git a/main.go b/main.go index f9875d9..9bc192a 100644 --- a/main.go +++ b/main.go @@ -61,7 +61,6 @@ func main() { if (drushlauncher.GetComposerBinDir(drupalRoot) != "") { drushRoot = drushlauncher.GetComposerBinDir(drupalRoot) } - drushExec := filepath.Join(drupalRoot, drushRoot, "drush") // Check if the drush executable exists From 410ed6c85d7f46caee044cccd568e185976ce10a Mon Sep 17 00:00:00 2001 From: Mathew Winstone Date: Thu, 8 Feb 2024 10:56:40 -0500 Subject: [PATCH 7/7] fix(composer): update logic When a root is passed, we need to check the parent in case web dir is not the same a composer dir --- drushlauncher/drushlauncher.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drushlauncher/drushlauncher.go b/drushlauncher/drushlauncher.go index 59417dd..0b506c3 100644 --- a/drushlauncher/drushlauncher.go +++ b/drushlauncher/drushlauncher.go @@ -96,7 +96,7 @@ func GetComposerBinDir(path string) string { // 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("vendor", "bin") + return filepath.Join(dirPath, "vendor", "bin") } // If the bin-dir flag exists, use the value of the flag