-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
65 lines (55 loc) · 2.22 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
.PHONY: create_env remove_env create_kernel remove_kernel jupyter
#################################################################################
# GLOBALS #
#################################################################################
## Change this variable
PROJECT_NAME=My Project Name
## Update these variables if needed
LANG=Python
CONDA_FILE=./environment.yml
PYTHON_FILE=./requirements.txt
R_FILE=./install.R
## Avoid changing these variables
PROJECT_SLUG=$(notdir $(shell pwd))
PROJECT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
ENV=$(PROJECT_DIR)/env
#################################################################################
# COMMANDS #
#################################################################################
## Create the project's conda environment and install packages from files
create_env:
ifeq (,$(shell mamba info --envs | grep $(ENV)))
mamba create -p $(ENV)
ifneq (,$(wildcard $(CONDA_FILE)))
mamba env update -p $(ENV) --file $(CONDA_FILE)
endif
ifneq (,$(wildcard $(PYTHON_FILE)))
mamba run -p $(ENV) python -m pip install -r $(PYTHON_FILE)
endif
ifneq (,$(wildcard $(R_FILE)))
mamba run -p $(ENV) Rscript -e "chooseCRANmirror(graphics=FALSE,ind=1);source('$(R_FILE)')"
endif
else
@echo Environment already exists
endif
## Remove the project's conda environment
remove_env:
mamba env remove -p $(ENV)
mamba clean -afy
## Create a new Jupyter kernel with using the project environment
create_kernel: create_env
ifeq ($(LANG),R)
mamba install -p $(ENV) r-irkernel -y
mamba run -p $(ENV) Rscript -e "IRkernel::installspec(name='$(PROJECT_SLUG)',displayname='R ($(PROJECT_NAME))')"
else
mamba run -p $(ENV) python -m pip install ipykernel
mamba run -p $(ENV) python -m ipykernel install --user --name "$(PROJECT_SLUG)" --display-name "Python ($(PROJECT_NAME))"
endif
## Remove the kernel from Jupyter
remove_kernel: remove_env
jupyter kernelspec remove $(PROJECT_SLUG) -f
## Run the kernel in a local Jupyter environment
jupyter: create_env
mamba install -p $(ENV) jupyter -y
$(MAKE) create_kernel
mamba run -p $(ENV) --no-capture-output jupyter lab -y