From 4d81e799bcdc4b8af963328b4b336d225367882f Mon Sep 17 00:00:00 2001 From: Carl Gay Date: Sat, 23 Mar 2024 17:25:55 -0400 Subject: [PATCH] workspaces: choose a reasonable default library name Choose a default library to build, test, or use as the project for lsp-dylan if no default is specified in workspace.json. --- sources/workspaces/registry.dylan | 18 +++++++++++++----- sources/workspaces/workspaces.dylan | 17 +++++++++++++---- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/sources/workspaces/registry.dylan b/sources/workspaces/registry.dylan index c204b86..7458970 100644 --- a/sources/workspaces/registry.dylan +++ b/sources/workspaces/registry.dylan @@ -8,7 +8,6 @@ end; //// REGISTRY // Keys used to lookup values in a parsed LID file. -// TODO: use 'define enum' in uncommon-dylan define constant $platforms-key = #"platforms"; define constant $files-key = #"files"; define constant $library-key = #"library"; @@ -425,14 +424,23 @@ define function parse-lid-file lid end function; -define function find-library-names + +// Find the names of all libraries defined in `dir`, a directory or registry. +define generic find-library-names (dir) => (names :: ); + +define method find-library-names (dir :: ) => (names :: ) - let registry = make(, root-directory: dir); + find-library-names(make(, root-directory: dir)) +end method; + +define method find-library-names + (registry :: ) => (names :: ) // It's possible for a LID included via the LID: keyword to not have a library. remove(map(rcurry(lid-value, $library-key), - find-lids(registry, dir)), + find-lids(registry, registry.root-directory)), #f) -end function; +end method; + // Build a map from source file names (absolute pathname strings) to the names // of libraries they belong to (a sequence of strings). For now we only look at diff --git a/sources/workspaces/workspaces.dylan b/sources/workspaces/workspaces.dylan index 4f3c2a5..9d218d7 100644 --- a/sources/workspaces/workspaces.dylan +++ b/sources/workspaces/workspaces.dylan @@ -142,10 +142,19 @@ define function load-workspace let ws-json = ws-file & load-json-file(ws-file); let default-library = ws-json & element(ws-json, $default-library-key, default: #f); - if (~default-library & active-packages.size = 1) - // TODO: this isn't right. Should find the actual libraries, from the LID - // files, and if there's only one "*-test*" library, choose that. - default-library := pm/package-name(active-packages[0]); + if (~default-library) + let libs = find-library-names(registry); + if (~empty?(libs)) + local method match (suffix, lib) + ends-with?(lib, suffix) & lib + end; + // The assumption here is that (for small projects) there's usually one + // test library that you want to run. + default-library := (any?(curry(match, "-test-suite-app"), libs) + | any?(curry(match, "-test-suite"), libs) + | any?(curry(match, "-tests"), libs) + | libs[0]); + end; end; make(, active-packages: active-packages,