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

add Alias field to backends, support at CLI and store levels #150

Merged
merged 6 commits into from
Oct 26, 2023
Merged
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
4 changes: 4 additions & 0 deletions internal/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down
15 changes: 14 additions & 1 deletion internal/backends/backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,22 @@ 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
cdmistman marked this conversation as resolved.
Show resolved Hide resolved
break
}
}
if checkAlias {
bParts = map[string]bool{}
for _, bPart := range strings.Split(b.Alias, "-") {
bParts[bPart] = true
}
for _, lPart := range strings.Split(language, "-") {
if !bParts[lPart] {
return false
}
}
}
return true
Expand Down
1 change: 1 addition & 0 deletions internal/backends/python/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
45 changes: 30 additions & 15 deletions internal/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
}