diff --git a/clang/compilerDb.go b/clang/compilerDb.go index e5d5801..7a48412 100644 --- a/clang/compilerDb.go +++ b/clang/compilerDb.go @@ -2,11 +2,12 @@ package clang import ( "encoding/json" - "errors" - "github.com/ejfitzgerald/clang-tidy-cache/utils" + "fmt" "io/ioutil" "os" "path/filepath" + + "github.com/ejfitzgerald/clang-tidy-cache/utils" ) type DatabaseEntry struct { @@ -32,6 +33,9 @@ func ExtractCompilationTarget(databaseRootPath string, target string) (*Database defer jsonFile.Close() bytes, err := ioutil.ReadAll(jsonFile) + if err != nil { + return nil, err + } var db Database err = json.Unmarshal(bytes, &db) @@ -39,11 +43,16 @@ func ExtractCompilationTarget(databaseRootPath string, target string) (*Database return nil, err } + // Find the entry that matches the target for _, entry := range db { + entry.File = utils.NormalizePath(entry.File) + entry.Directory = utils.NormalizePath(entry.Directory) + target = utils.NormalizePath(target) + if entry.File == target || entry.File == filepath.Join(entry.Directory, target) { return &entry, nil } } - return nil, errors.New("Unable to locate the compiler definition") + return nil, fmt.Errorf("unable to find the compiler definition for target %v", target) } diff --git a/main.go b/main.go index 5e9527f..e380fdf 100644 --- a/main.go +++ b/main.go @@ -3,14 +3,15 @@ package main import ( "encoding/json" "fmt" - "github.com/ejfitzgerald/clang-tidy-cache/caches" - "github.com/ejfitzgerald/clang-tidy-cache/clang" "io" "io/ioutil" "os" "os/exec" "os/user" "path" + + "github.com/ejfitzgerald/clang-tidy-cache/caches" + "github.com/ejfitzgerald/clang-tidy-cache/clang" ) const VERSION = "0.3.0" diff --git a/utils/fileSystem.go b/utils/fileSystem.go index ea5f3ac..aa2ce9d 100644 --- a/utils/fileSystem.go +++ b/utils/fileSystem.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path/filepath" + "strings" ) // FindInParents searches for a file named filename in searchDir or any of it's @@ -22,3 +23,32 @@ func FindInParents(searchDir string, filename string) (string, error) { } return FindInParents(parentDir, filename) } + +// Converts given path to Posix (replacing \ with /) +// +// @param {string} givenPath Path to convert +// +// @returns {string} Converted filepath +func PosixifyPath(givenPath string) string { + return strings.ReplaceAll(givenPath, "\\", "/") +} + +// NormalizePath normalizes the given path between Windows and POSIX +// and removes the leading "./" if present +// +// @param {string} path Path to normalize +// +// @returns {string} Normalized path +func NormalizePath(path string) string { + var normalizedPath string + + // Normalize the path to use POSIX separators + normalizedPath = PosixifyPath(path) + + // Remove the leading "./" if present + if normalizedPath[0:2] == "./" { + normalizedPath = normalizedPath[2:] + } + + return normalizedPath +}