The first one had the problem in that it only showed packages that were direct dependencies. It’s API was fundamentally incompatible with showing packages in the manifest since you can have multiple packages with the same name there and the key for installed was the package name. To have an API that gives information about all dependencies (including recursive) you pretty much must use the UUID as the key since that is what identifies the package.
But I am interested in just knowing if a package is already available for using or not.
If name collision is a (remote, i.e. not possible if you just use the standard repository) possibility one could have just a function that returns the number of available packages by name and then use it as:
if Pkg.availablecount("pkgName") == 0
elseif Pkg.availablecount("pkgName") > 1
@error "More than one package is available with the given name. Please use UUID"
isinstalled(pkg::String) = any(x -> x.name == pkg && x.is_direct_dep, values(Pkg.dependencies()))
pk = ["Knet","Colors","Images", "StatsPlots"]
using Pkg; for p in pk; isinstalled(p) || Pkg.add(p); end
using Knet, Colors, Images
Also I’d like to replace using Knet, Colors, Images
with for p in pk; using(p); end
but it doesn’t work bc pk is a vector of strings
Adding the function isinstalled(pkg::String) each time is a bit of a pain …
There is the Pkg.project() which contains a field named dependencies containing a dict with package names and UUID of direct dependencies. So keys(Pkg.project().dependencies) == keys(Pkg.installed()) if I’m not mistaken, and could for the purpose in this thread probably be used as a one line replacement.