Skip to content

Commit

Permalink
feat/Adding support for which-language --all (#301)
Browse files Browse the repository at this point in the history
* Teach which-language about --all

* Add IsActive support to python and node

* Got migrated, which is cool. Also adding some other modules for testing

* Linter
  • Loading branch information
blast-hardcheese authored Oct 29, 2024
1 parent b5631d0 commit 01e96db
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 8 deletions.
7 changes: 3 additions & 4 deletions .replit
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
run = "make && echo \"UPM is built, can be found here: ./cmd/upm/upm\""

modules = ["go-1.20:v2-20230911-b5aa5df"]
run = "make upm && echo \"UPM is built, can be found here: ./cmd/upm/upm\""
modules = ["bash", "bun-1.1", "go-1.21", "python-3.12"]

[nix]
channel = "stable-23_05"
channel = "stable-24_05"
4 changes: 4 additions & 0 deletions internal/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ type LanguageBackend struct {
// Check to see if we think we can run at all
IsAvailable func() bool

// Check for signs that this backend has either been used or
// can be used to query
IsActive func() bool

// List of filename globs that match against files written in
// this programming language, e.g. "*.py" for Python. These
// should not include any slashes, because they may be matched
Expand Down
17 changes: 17 additions & 0 deletions internal/backends/nodejs/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,20 @@ var tsxPathGlobs = []string{
"*.tsx",
}

func commonIsActive(lockfile string) bool {
_, err := os.Stat(lockfile)
return !os.IsNotExist(err)
}

// NodejsYarnBackend is a UPM backend for Node.js that uses [Yarn](https://yarnpkg.com/).
var NodejsYarnBackend = api.LanguageBackend{
Name: "nodejs-yarn",
Specfile: "package.json",
Lockfile: "yarn.lock",
IsAvailable: yarnIsAvailable,
IsActive: func() bool {
return commonIsActive("yarn.lock")
},
FilenamePatterns: nodejsPatterns,
Quirks: api.QuirksAddRemoveAlsoLocks |
api.QuirksAddRemoveAlsoInstalls |
Expand Down Expand Up @@ -481,6 +489,9 @@ var NodejsPNPMBackend = api.LanguageBackend{
Specfile: "package.json",
Lockfile: "pnpm-lock.yaml",
IsAvailable: pnpmIsAvailable,
IsActive: func() bool {
return commonIsActive("pnpm-lock.yaml")
},
FilenamePatterns: nodejsPatterns,
Quirks: api.QuirksAddRemoveAlsoLocks |
api.QuirksAddRemoveAlsoInstalls |
Expand Down Expand Up @@ -581,6 +592,9 @@ var NodejsNPMBackend = api.LanguageBackend{
Specfile: "package.json",
Lockfile: "package-lock.json",
IsAvailable: npmIsAvailable,
IsActive: func() bool {
return commonIsActive("package-lock.json")
},
FilenamePatterns: nodejsPatterns,
Quirks: api.QuirksAddRemoveAlsoLocks |
api.QuirksAddRemoveAlsoInstalls |
Expand Down Expand Up @@ -670,6 +684,9 @@ var BunBackend = api.LanguageBackend{
Specfile: "package.json",
Lockfile: "bun.lockb",
IsAvailable: bunIsAvailable,
IsActive: func() bool {
return commonIsActive("bun.lockb")
},
FilenamePatterns: nodejsPatterns,
Quirks: api.QuirksAddRemoveAlsoLocks |
api.QuirksAddRemoveAlsoInstalls |
Expand Down
11 changes: 11 additions & 0 deletions internal/backends/python/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ func commonGuessPackageDir() string {
return ""
}

func commonIsActive(lockfile string) bool {
_, err := os.Stat(lockfile)
return !os.IsNotExist(err)
}

var pythonGuessRegexps = util.Regexps([]string{
// The (?:.|\\\n) subexpression allows us to
// match match multiple lines if
Expand Down Expand Up @@ -383,6 +388,9 @@ func makePythonPoetryBackend() api.LanguageBackend {
_, err := exec.LookPath("poetry")
return err == nil
},
IsActive: func() bool {
return commonIsActive("poetry.lock")
},
FilenamePatterns: []string{"*.py"},
Quirks: api.QuirksAddRemoveAlsoLocks |
api.QuirksAddRemoveAlsoInstalls,
Expand Down Expand Up @@ -754,6 +762,9 @@ func makePythonUvBackend() api.LanguageBackend {
_, err := exec.LookPath("uv")
return err == nil
},
IsActive: func() bool {
return commonIsActive("uv.lock")
},
Alias: "python-python3-uv",
FilenamePatterns: []string{"*.py"},
Quirks: api.QuirksAddRemoveAlsoInstalls | api.QuirksAddRemoveAlsoLocks,
Expand Down
5 changes: 4 additions & 1 deletion internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,12 @@ func DoCLI() {
Long: "Ask which language your project is autodetected as",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
runWhichLanguage(language)
runWhichLanguage(language, all)
},
}
cmdWhichLanguage.Flags().BoolVarP(
&all, "all", "a", false, "List all packages for all detected languages",
)
rootCmd.AddCommand(cmdWhichLanguage)

cmdListLanguages := &cobra.Command{
Expand Down
26 changes: 23 additions & 3 deletions internal/cli/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,29 @@ func (s *subroutineSilencer) restore() {
}

// runWhichLanguage implements 'upm which-language'.
func runWhichLanguage(language string) {
b := backends.GetBackend(context.Background(), language)
fmt.Println(b.Name)
func runWhichLanguage(language string, all bool) {
if !all {
b := backends.GetBackend(context.Background(), language)
fmt.Println(b.Name)
return
}
for _, bi := range backends.GetBackendNames() {
if !bi.Available {
continue
}
if language != "" && !strings.Contains(bi.Name, language) {
continue
}

b := backends.GetBackend(context.Background(), bi.Name)

// If we can't determine we are in use, don't report
if b.IsActive == nil || !b.IsActive() {
continue
}

fmt.Println(b.Name)
}
}

// runListLanguages implements 'upm list-languages'.
Expand Down

0 comments on commit 01e96db

Please sign in to comment.