Skip to content

Commit 476883b

Browse files
authored
remove infinite spam from QUACKTIVATE and add @quickactivate (#110)
* remove infinite spam * docstring typo * version up * Add quickactivate macro * update docs and versions
1 parent 87a0a3b commit 476883b

File tree

5 files changed

+57
-25
lines changed

5 files changed

+57
-25
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.6.0
2+
* `quickactivate` doesn't do anything anymore if you try to activate to already active project.
3+
* New macro `@quickactivate`
4+
15
# 1.5.0
26
* Started to add support for overloading save/load for custom files. See the updated docs around `wsave` and `wload`.
37

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DrWatson"
22
uuid = "634d3b9d-ee7a-5ddf-bec9-22491ea816e1"
33
repo = "https://github.com/JuliaDynamics/DrWatson.jl.git"
4-
version = "1.5.0"
4+
version = "1.6.0"
55

66

77

docs/src/project.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,9 @@ This can be done in multiple ways:
6363

6464
We recommend the fourth approach, although it does come with a caveat (see the docstring of [`quickactivate`](@ref)).
6565

66-
Here is how it works: the function [`quickactivate`](@ref) activates a project given some path by recursively searching the path and its parents for a valid `Project.toml` file. Typically you put this function in your script files like so:
67-
```julia
68-
using DrWatson # DONT USE OTHER PACKAGES HERE!
69-
quickactivate(@__DIR__, "Best project in the WORLLDD")
70-
# Now you should start using other packages
71-
```
72-
where the second optional argument can assert if the activated project matches the name you provided. If not the function will throw an error.
73-
7466
```@docs
7567
quickactivate
68+
@quickactivate
7669
findproject
7770
```
7871

src/project_setup.jl

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
##########################################################################################
44
export projectdir, datadir, srcdir, plotsdir, scriptsdir, papersdir
55
export projectname
6-
export findproject, quickactivate
6+
export findproject, quickactivate, @quickactivate
77

88
@deprecate scriptdir scriptsdir #TODO: remove in next release
99

@@ -91,14 +91,20 @@ end
9191

9292
"""
9393
quickactivate(path [, name::String])
94-
Activate the project found by [`findproject`](@ref) of the `path`, which
95-
recursively searches the `path` and its parents for a valid Julia
96-
project file.
97-
94+
Activate the project found by recursively searching the `path`
95+
and its parents for a valid Julia project file.
9896
Optionally check if `name` is the same as the activated project's name.
99-
If it is not, throw an error.
97+
If it is not, throw an error. See also [`@quickactivate`].
98+
Do nothing if the project found is already active, or if no
99+
project file is found.
100100
101-
This function is _first_ activating the project and _then_ checking if
101+
Example:
102+
```julia
103+
using DrWatson
104+
quickactivate("path/to/project", "Best project in the WOLRLDD")
105+
```
106+
107+
Notice that this function is _first_ activating the project and _then_ checking if
102108
it matches the `name`.
103109
104110
!!! warning
@@ -108,17 +114,17 @@ it matches the `name`.
108114
will be the one of the global environment, and not of the activated project.
109115
To avoid unexpected behavior take care so that these two versions coincide.
110116
111-
**In addition please be very careful to not write:**
117+
**In addition please be very careful to write:**
112118
```julia
113-
using DrWatson, Package1, Package2
119+
using DrWatson
114120
quickactivate(@__DIR__)
121+
using Package1, Package2
115122
# do stuff
116123
```
117-
**but instead load packages after activating the project:**
124+
**instead of the erroneous:**
118125
```julia
119-
using DrWatson
126+
using DrWatson, Package1, Package2
120127
quickactivate(@__DIR__)
121-
using Package1, Package2
122128
# do stuff
123129
```
124130
This ensures that the packages you use will all have the versions dictated
@@ -127,7 +133,9 @@ it matches the `name`.
127133
"""
128134
function quickactivate(path, name = nothing)
129135
projectpath = findproject(path)
130-
projectpath === nothing && return nothing
136+
if projectpath === nothing || projectpath == dirname(Base.active_project())
137+
return nothing
138+
end
131139
Pkg.activate(projectpath)
132140
if !(name === nothing) && projectname() != name
133141
error(
@@ -138,8 +146,22 @@ function quickactivate(path, name = nothing)
138146
return nothing
139147
end
140148

149+
"""
150+
@quickactivate
151+
Equivalent with `quickactivate(@__DIR__)`.
141152
142-
153+
@quickactivate name
154+
Equivalent with `quickactivate(@__DIR__, name)`.
155+
"""
156+
macro quickactivate(name = nothing)
157+
if __source__.file === nothing
158+
dir = nothing
159+
else
160+
_dirname = dirname(String(__source__.file))
161+
dir = isempty(_dirname) ? pwd() : abspath(_dirname)
162+
end
163+
:(quickactivate($dir,$name))
164+
end
143165

144166
##########################################################################################
145167
# Project setup
@@ -213,7 +235,7 @@ function initialize_project(path, name = basename(path);
213235
# chmod is needed, as the file permissions are not set correctly when adding the package with `add`.
214236
cp(joinpath(@__DIR__, "defaults", "gitignore.txt"), joinpath(path, ".gitignore"))
215237
chmod(joinpath(path, ".gitignore"),0o644)
216-
238+
217239
cp(joinpath(@__DIR__, "defaults", "intro.jl"), joinpath(path, "scripts", "intro.jl"))
218240
chmod(joinpath(path, "scripts", "intro.jl"),0o644)
219241

test/update_results_tests.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,22 @@ subfolders = true, special_list=special_list, black_list = black_list)
9898
@test sort(names(cres3)) == sort(names(cres2))
9999
@test size(cres3) == size(cres2)
100100

101+
###############################################################################
102+
# Quickactivate macro #
103+
###############################################################################
104+
105+
cd(@__DIR__)
106+
isdir("testdir") && rm("testdir", recursive=true)
107+
initialize_project("testdir"; git = false)
108+
open(joinpath("testdir","testinclude.jl"),"w") do f
109+
write(f,"@quickactivate\n")
110+
end
111+
include(joinpath("testdir", "testinclude.jl"))
112+
@test Base.active_project() == abspath(joinpath("testdir","Project.toml"))
113+
101114
###############################################################################
102115
# Delete Folders #
103116
###############################################################################
104117

105118
cd(@__DIR__)
106-
rm("testdir", recursive=true)
119+
rm("testdir", recursive=true)

0 commit comments

Comments
 (0)