-
-
Notifications
You must be signed in to change notification settings - Fork 269
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 support for package extensions #3264
Merged
Merged
Changes from 17 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
e0954b9
add support for glue packages
KristofferC 2d6adbb
add some general explanation comments
IanButterworth 83ad655
precompile glue packages too
IanButterworth 1a69736
add support for glue packages
KristofferC 8ae5d35
Merge pull request #3265 from IanButterworth/ib/glue_precompile
KristofferC 041e9f0
fix order of sections in Project file
KristofferC f2376f8
tweak printing of glue pkgs in precompilation
KristofferC 6eb01e1
add docs for backwards compat
KristofferC b591e39
add a status option for glue
KristofferC e23c49e
fixup precompile
KristofferC 941998b
add tests for glue packages retrieved from a registry
KristofferC 2bfd2ab
add small note
KristofferC abc9e21
Update docs/src/creating-packages.md
KristofferC 92bed4c
Update src/API.jl
KristofferC 49cd28a
Update src/API.jl
KristofferC a9e5ffa
fixup fixup_glue
KristofferC c52bf27
weak -> glue
KristofferC f1a47ed
fix tests passing when dep warnings are errors (as they are on Julia CI)
KristofferC 169e128
gluedeps -> weakdeps
KristofferC fa6dbfe
ignore packages both in weakdeps and deps for backwards compat
KristofferC abe7000
glue -> extension
KristofferC 5085eb7
fix REPL for extension option
644a176
Clarify RegistrySpec docstring (#3155)
LilithHafner 50a0938
slightly simplify API internals (#3156)
LilithHafner 0c2bafd
REPLMode: Remove constructor for `APIOptions`
barucden acefa5f
REPLMode: Remove constructor for `PackageToken`
barucden a2aef89
review fixes
KristofferC File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -245,6 +245,111 @@ using Test | |||||
Every dependency should in general have a compatibility constraint on it. | ||||||
This is an important topic so there is a separate chapter about it: [Compatibility](@ref Compatibility). | ||||||
|
||||||
## Conditional loading of code in packages (Glue packages) | ||||||
|
||||||
!!! note | ||||||
This is a somewhat advanced section which can be skipped for people new to Julia and Julia packages. | ||||||
|
||||||
It is sometimes desirable to be able to extend some functionality of a package without having to | ||||||
unconditionally take on the cost (in terms of e.g. load time) of adding an extra dependency. | ||||||
A *glue package* is a file that gets automatically loaded when some other set of packages are | ||||||
loaded into the Julia session. This is very similar to functionality that the external package | ||||||
Requires.jl used to provide, but which is now available directly through Julia. | ||||||
|
||||||
A useful application of glue packages could be for a plotting package that should be able to plot | ||||||
objects from a wide variety of different Julia packages. | ||||||
Adding all those different Julia packages as dependencies | ||||||
could be expensive since they would end up getting loaded even if they were never used. | ||||||
Instead, these code required to plot objects for specific packages can be put into separate files | ||||||
(glue packages) which is only loaded when | ||||||
KristofferC marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
Below is an example of how the code can be structured for a use case as outlined above. | ||||||
|
||||||
`Project.toml`: | ||||||
```toml | ||||||
name = "Plotting" | ||||||
version = "0.1.0" | ||||||
uuid = "..." | ||||||
|
||||||
[gluedeps] | ||||||
Contour = "d38c429a-6771-53c6-b99e-75d170b6e991" | ||||||
|
||||||
[gluepkgs] | ||||||
# name of glue package to the left | ||||||
# glue dependencies required to load the glue pkg to the right | ||||||
# use a list for multiple glue dependencies | ||||||
GlueContour = "Contour" | ||||||
|
||||||
[compat] # compat can also be given on glue dependencies | ||||||
Contour = "0.6.2" | ||||||
``` | ||||||
|
||||||
`src/Plotting.jl`: | ||||||
```julia | ||||||
module Plotting | ||||||
|
||||||
function plot(x::Vector) | ||||||
# Some functionality for plotting a vector here | ||||||
end | ||||||
|
||||||
end # module | ||||||
``` | ||||||
|
||||||
`glue/GlueContour.jl` (can also be in `glue/GlueContour/GlueContour.jl`): | ||||||
```julia | ||||||
module GlueContour # Should be same name as the file (just like a normal package) | ||||||
|
||||||
using Plotting, Contour | ||||||
|
||||||
function Plotting.plot(c::Contour.ContourCollection) | ||||||
# Some functionality for plotting a contour here | ||||||
end | ||||||
|
||||||
end # module | ||||||
``` | ||||||
|
||||||
A user that depends on `Plotting` will not pay the cost of the "glue code" inside the `GlueContour` module. | ||||||
It is only when the `Contour` package actually gets loaded that the `GlueCountour` glue package is loaded | ||||||
and provide the new functionality. | ||||||
KristofferC marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
Compatibility can be set on glue dependencies just like normal dependencies and they can be used in the `test` | ||||||
target to make them available when testing. | ||||||
|
||||||
A glue package will only be loaded if the glue dependencies are loaded from the same environment or environments higher in the environment stack than the package itself. | ||||||
|
||||||
|
||||||
!!! compat | ||||||
In order to be compatible with earlier versions of Julia, some extra steps have to be taken. | ||||||
These are: Duplicate the packages under `[gluedeps]` into `[extras]`. This is an unfortunate | ||||||
duplication but without doing this the project verifier under older Julia versions will complain (error). | ||||||
|
||||||
### Backwards compatibility with Requires.jl | ||||||
|
||||||
Since glue packages and Requires.jl are solving the same problem, | ||||||
it is useful to know how to use Requires.jl on older Julia versions while seamlessly | ||||||
transitioning into using glue packages on newer Julia versions that support it. | ||||||
This is done by making the following changes (using the example above): | ||||||
|
||||||
- Add the following to the package file. This makes it so that Requires.jl loads and inserts the | ||||||
callback only when glue packages are not supported | ||||||
```julia | ||||||
# This symbol is only defined on Julia versions that support glue packages | ||||||
if isdefined(Base, :get_gluepkg) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
If I understand correctly? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, thanks :) |
||||||
using Requires | ||||||
function __init__() | ||||||
@require Contour = "d38c429a-6771-53c6-b99e-75d170b6e991" include("../glue/GlueContour.jl) | ||||||
end | ||||||
end | ||||||
``` | ||||||
|
||||||
- Do the following change in the glue packages for loading the glue dependency: | ||||||
```julia | ||||||
isdefined(Base, :get_gluepkg) ? (using Contour) : (using ..Contour) | ||||||
``` | ||||||
|
||||||
The package should now work with Requires.jl on Julia versions before glue packages were introduced | ||||||
and with glue packages afterward. | ||||||
|
||||||
## Package naming guidelines | ||||||
|
||||||
Package names should be sensible to most Julia users, *even to those who are not domain experts*. | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.