miqt-docker: windows compatibility

This commit is contained in:
mappu 2025-04-30 16:29:50 +12:00
parent 6d176bc410
commit f0ce7984ce
2 changed files with 31 additions and 8 deletions

View File

@ -2,6 +2,7 @@ package main
import ( import (
"errors" "errors"
"fmt"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
@ -16,6 +17,8 @@ func highestCommonParent(paths []string) (string, error) {
parts := strings.Split(paths[0], string(filepath.Separator)) parts := strings.Split(paths[0], string(filepath.Separator))
caseSensitive := runtime.GOOS != "windows"
for _, check := range paths { for _, check := range paths {
checkn := strings.Split(check, string(filepath.Separator)) checkn := strings.Split(check, string(filepath.Separator))
@ -25,8 +28,15 @@ func highestCommonParent(paths []string) (string, error) {
} }
for i, checkpart := range checkn[0:len(parts)] { // len(parts) is now <= len(checkn) so this is safe for i, checkpart := range checkn[0:len(parts)] { // len(parts) is now <= len(checkn) so this is safe
if parts[i] == checkpart { if caseSensitive {
continue if parts[i] == checkpart {
continue
}
} else {
// case insensitive comparison
if strings.EqualFold(parts[i], checkpart) {
continue
}
} }
// Divergence from i: onwards // Divergence from i: onwards
@ -45,7 +55,7 @@ func highestCommonParent(paths []string) (string, error) {
if isEmpty { if isEmpty {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
return "", errors.New("Selected paths have no common ancestor") return "", fmt.Errorf("Selected paths have no common ancestor: %v", paths)
} }
return `/`, nil return `/`, nil
} }

View File

@ -9,6 +9,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"os/user" "os/user"
"path"
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime" "runtime"
@ -224,15 +225,27 @@ func getDockerRunArgsForGlob(dockerfiles []fs.DirEntry, containerNameGlob string
return nil, err return nil, err
} }
mountDir := `/src/` + filepath.Base(cwd) // Don't use /src directly, otherwise -android-build will not know the package name for top-level builds // Don't mount directly on /src , otherwise -android-build will not know
// the package name for top-level builds. Use a subfolder within it
mountDir := `/src/` + filepath.Base(cwd)
fullCommand = append(fullCommand, `-v`, basedir+`:`+mountDir, `-w`, filepath.Join(mountDir, relCwd)) if runtime.GOOS == "windows" {
// convert C:\foo\bar paths to /c/foo/bar that Docker understands
// Otherwise, you experience "invalid mode" when the : is parsed
basedir = `/` + strings.ToLower(string(basedir[0])) + `/` + strings.ReplaceAll(basedir[3:], `\`, `/`)
fullCommand = append(fullCommand, `-e`, `HOME=/tmp`) // Always forwardslashes for in-docker paths, even on Windows OS
mountDir = strings.ReplaceAll(mountDir, `\`, `/`)
}
// Final standard docker commands fullCommand = append(fullCommand,
`-v`, basedir+`:`+mountDir,
`-w`, path.Join(mountDir, relCwd),
fullCommand = append(fullCommand, containerName+`:`+dockerfileHash) // , `/bin/bash`, `-c`) // Final standard docker commands
`-e`, `HOME=/tmp`,
containerName+`:`+dockerfileHash,
)
return fullCommand, nil return fullCommand, nil
} }