From ee2d2db8b092e4a46077863dc1b8dfce51231296 Mon Sep 17 00:00:00 2001 From: Colton Donnelly Date: Thu, 26 Oct 2023 16:36:54 +0000 Subject: [PATCH 1/6] add Alias field to backends, support at CLI and store levels --- internal/api/types.go | 4 +++ internal/backends/backends.go | 14 +++++++++- internal/backends/python/python.go | 1 + internal/store/store.go | 45 ++++++++++++++++++++---------- 4 files changed, 48 insertions(+), 16 deletions(-) diff --git a/internal/api/types.go b/internal/api/types.go index 0da08a3c..62e866ad 100644 --- a/internal/api/types.go +++ b/internal/api/types.go @@ -143,6 +143,10 @@ type LanguageBackend struct { // This field is mandatory. Name string + // An alias for the backend, useful for backwards compatibility + // when renaming backends. + Alias string + // The filename of the specfile, e.g. "pyproject.toml" for // Poetry. // diff --git a/internal/backends/backends.go b/internal/backends/backends.go index f62df3dc..ab2eee37 100644 --- a/internal/backends/backends.go +++ b/internal/backends/backends.go @@ -50,9 +50,21 @@ func matchesLanguage(b api.LanguageBackend, language string) bool { for _, bPart := range strings.Split(b.Name, "-") { bParts[bPart] = true } + checkAlias := false for _, lPart := range strings.Split(language, "-") { if !bParts[lPart] { - return false + checkAlias = true + } + } + if checkAlias { + bParts = map[string]bool{} + for _, bPart := range strings.Split(b.Name, "-") { + bParts[bPart] = true + } + for _, lPart := range strings.Split(language, "-") { + if !bParts[lPart] { + return false + } } } return true diff --git a/internal/backends/python/python.go b/internal/backends/python/python.go index d698604a..bf878d32 100644 --- a/internal/backends/python/python.go +++ b/internal/backends/python/python.go @@ -185,6 +185,7 @@ func add(pkgs map[api.PkgName]api.PkgSpec, projectName string) { func makePythonPoetryBackend(python string) api.LanguageBackend { return api.LanguageBackend{ Name: "python3-poetry", + Alias: "python-python3-poetry", Specfile: "pyproject.toml", Lockfile: "poetry.lock", FilenamePatterns: []string{"*.py"}, diff --git a/internal/store/store.go b/internal/store/store.go index 7da1ba82..b78b4570 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -66,15 +66,28 @@ func readMaybe() { // initLanguage creates an entry in the store for the given language, // if necessary. (A language is just the name of a backend.) -func initLanguage(language string) { +func initLanguage(language, languageAlias string) { if st.Languages == nil { st.Languages = map[string]*storeLanguage{} } - if st.Languages[language] == nil { + if st.Languages[language] == nil && languageAlias != "" && st.Languages[languageAlias] != nil { st.Languages[language] = &storeLanguage{} } } +func getLanguageCache(language, languageAlias string) *storeLanguage { + if st.Languages == nil { + return nil + } + if st.Languages[language] != nil { + return st.Languages[language] + } + if languageAlias != "" && st.Languages[languageAlias] != nil { + return st.Languages[languageAlias] + } + return nil +} + // Write writes the current contents of the store from memory back to // disk. If there is an error, it terminates the process. func Write() { @@ -105,8 +118,8 @@ func Write() { // returns true. func HasSpecfileChanged(b api.LanguageBackend) bool { readMaybe() - initLanguage(b.Name) - return hashFile(b.Specfile) != st.Languages[b.Name].SpecfileHash + initLanguage(b.Name, b.Alias) + return hashFile(b.Specfile) != getLanguageCache(b.Name, b.Alias).SpecfileHash } // HasLockfileChanged returns false if the lockfile exists and has not @@ -115,8 +128,8 @@ func HasSpecfileChanged(b api.LanguageBackend) bool { // returns true. func HasLockfileChanged(b api.LanguageBackend) bool { readMaybe() - initLanguage(b.Name) - return hashFile(b.Lockfile) != st.Languages[b.Name].LockfileHash + initLanguage(b.Name, b.Alias) + return hashFile(b.Lockfile) != getLanguageCache(b.Name, b.Alias).LockfileHash } // GuessWithCache returns b.Guess(), but re-uses a cached return value @@ -129,14 +142,15 @@ func HasLockfileChanged(b api.LanguageBackend) bool { // not read from the cache. func GuessWithCache(b api.LanguageBackend, forceGuess bool) map[api.PkgName]bool { readMaybe() - initLanguage(b.Name) - old := st.Languages[b.Name].GuessedImportsHash + initLanguage(b.Name, b.Alias) + cache := getLanguageCache(b.Name, b.Alias) + old := cache.GuessedImportsHash var new hash = "n/a" // If no regexps, then we can't hash imports. Skip reading and // writing the hash. if len(b.GuessRegexps) > 0 { new = hashImports(b) - st.Languages[b.Name].GuessedImportsHash = new + cache.GuessedImportsHash = new } if forceGuess || new != old { var pkgs map[api.PkgName]bool @@ -157,7 +171,7 @@ func GuessWithCache(b api.LanguageBackend, forceGuess bool) map[api.PkgName]bool // e.g. due to syntax error, then don't update // the hash. This will force the search to be // redone next time. - st.Languages[b.Name].GuessedImportsHash = old + cache.GuessedImportsHash = old } // Only cache result if we are going to use the cache, // and skip caching if bare imports search was not @@ -172,12 +186,12 @@ func GuessWithCache(b api.LanguageBackend, forceGuess bool) map[api.PkgName]bool for name := range pkgs { guessed = append(guessed, string(name)) } - st.Languages[b.Name].GuessedImports = guessed + cache.GuessedImports = guessed } return pkgs } else { pkgs := map[api.PkgName]bool{} - for _, name := range st.Languages[b.Name].GuessedImports { + for _, name := range cache.GuessedImports { pkgs[api.PkgName(name)] = true } return pkgs @@ -188,7 +202,8 @@ func GuessWithCache(b api.LanguageBackend, forceGuess bool) map[api.PkgName]bool // lockfile. Neither file need exist. func UpdateFileHashes(b api.LanguageBackend) { readMaybe() - initLanguage(b.Name) - st.Languages[b.Name].SpecfileHash = hashFile(b.Specfile) - st.Languages[b.Name].LockfileHash = hashFile(b.Lockfile) + initLanguage(b.Name, b.Alias) + cache := getLanguageCache(b.Name, b.Alias) + cache.SpecfileHash = hashFile(b.Specfile) + cache.LockfileHash = hashFile(b.Lockfile) } From 2ed93fa32cf6cf3e00c1318adfa72203fc2b798a Mon Sep 17 00:00:00 2001 From: Colton Donnelly Date: Thu, 26 Oct 2023 16:45:18 +0000 Subject: [PATCH 2/6] copy-paste mistake in matchesLanguage --- internal/backends/backends.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/backends/backends.go b/internal/backends/backends.go index ab2eee37..3d5729c8 100644 --- a/internal/backends/backends.go +++ b/internal/backends/backends.go @@ -58,7 +58,7 @@ func matchesLanguage(b api.LanguageBackend, language string) bool { } if checkAlias { bParts = map[string]bool{} - for _, bPart := range strings.Split(b.Name, "-") { + for _, bPart := range strings.Split(b.Alias, "-") { bParts[bPart] = true } for _, lPart := range strings.Split(language, "-") { From 6c9cddbad9c208aa21b4091f436b9e0f4c202a2b Mon Sep 17 00:00:00 2001 From: Colton Donnelly Date: Thu, 26 Oct 2023 17:00:27 +0000 Subject: [PATCH 3/6] == not != for nil checks --- internal/store/store.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/store/store.go b/internal/store/store.go index b78b4570..a198e60c 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -70,7 +70,7 @@ func initLanguage(language, languageAlias string) { if st.Languages == nil { st.Languages = map[string]*storeLanguage{} } - if st.Languages[language] == nil && languageAlias != "" && st.Languages[languageAlias] != nil { + if st.Languages[language] == nil && languageAlias != "" && st.Languages[languageAlias] == nil { st.Languages[language] = &storeLanguage{} } } From 1eb8434fb4b71186bbfe37880bec7611bc2c49aa Mon Sep 17 00:00:00 2001 From: Colton Donnelly Date: Thu, 26 Oct 2023 17:43:49 +0000 Subject: [PATCH 4/6] fix boolean logic again --- internal/store/store.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/store/store.go b/internal/store/store.go index a198e60c..3a62e21f 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -70,7 +70,7 @@ func initLanguage(language, languageAlias string) { if st.Languages == nil { st.Languages = map[string]*storeLanguage{} } - if st.Languages[language] == nil && languageAlias != "" && st.Languages[languageAlias] == nil { + if st.Languages[language] == nil && (languageAlias != "" || st.Languages[languageAlias] == nil) { st.Languages[language] = &storeLanguage{} } } From 6d6d4385db0fcc918a42274dccd0a15b18b28aff Mon Sep 17 00:00:00 2001 From: Colton Donnelly Date: Thu, 26 Oct 2023 17:47:35 +0000 Subject: [PATCH 5/6] fix boolean logic again again --- internal/store/store.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/store/store.go b/internal/store/store.go index 3a62e21f..3e2e4a5a 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -70,7 +70,7 @@ func initLanguage(language, languageAlias string) { if st.Languages == nil { st.Languages = map[string]*storeLanguage{} } - if st.Languages[language] == nil && (languageAlias != "" || st.Languages[languageAlias] == nil) { + if st.Languages[language] == nil && (languageAlias == "" || st.Languages[languageAlias] == nil) { st.Languages[language] = &storeLanguage{} } } From 79e8fb73c587c6bb8bdceb4ea3f9b3504e64ce3d Mon Sep 17 00:00:00 2001 From: Colton Donnelly Date: Thu, 26 Oct 2023 17:48:54 +0000 Subject: [PATCH 6/6] break if need to check alias --- internal/backends/backends.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/backends/backends.go b/internal/backends/backends.go index 3d5729c8..4fa37d5c 100644 --- a/internal/backends/backends.go +++ b/internal/backends/backends.go @@ -54,6 +54,7 @@ func matchesLanguage(b api.LanguageBackend, language string) bool { for _, lPart := range strings.Split(language, "-") { if !bParts[lPart] { checkAlias = true + break } } if checkAlias {