-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathMakefile
191 lines (161 loc) · 5.78 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
SHELL := /bin/bash
S3_BUCKET := openai.lawrencemcdaniel.com
CLOUDFRONT_DISTRIBUTION_ID := E3AIBM1KMSJOP1
ifeq ($(OS),Windows_NT)
PYTHON := python.exe
ACTIVATE_VENV := venv\Scripts\activate
else
PYTHON := python3.11
ACTIVATE_VENV := source venv/bin/activate
endif
PIP := $(PYTHON) -m pip
ifneq ("$(wildcard .env)","")
include .env
else
$(shell cp ./doc/example-dot-env .env)
endif
.PHONY: analyze pre-commit api-init api-activate api-lint api-clean api-test client-init client-lint client-update client-run client-build client-release
# Default target executed when no arguments are given to make.
all: help
clean:
make api-clean
make client-clean
lint:
make api-lint
make client-lint
init:
make api-init
make client-init
build:
make api-build
make client-build
run:
make client-run
analyze:
cloc . --exclude-ext=svg,json,zip --vcs=git
coverage:
coverage run --source=api/terraform/python/openai_api \
-m unittest discover -s api/terraform/python/openai_api/
coverage report -m
coverage html
release:
git commit -m "fix: force a new release" --allow-empty && git push
# -------------------------------------------------------------------------
# Install and run pre-commit hooks
# -------------------------------------------------------------------------
pre-commit:
pre-commit install
pre-commit autoupdate
pre-commit run --all-files
# ---------------------------------------------------------
# create python virtual environments for dev as well
# as for the Lambda layer.
# ---------------------------------------------------------
api-init:
make api-clean
npm install && \
$(PYTHON) -m venv venv && \
$(ACTIVATE_VENV) && \
$(PIP) install --upgrade pip && \
$(PIP) install -r requirements.txt && \
deactivate && \
cd ./api/terraform/python/layer_genai/ && \
$(PYTHON) -m venv venv && \
$(ACTIVATE_VENV) && \
$(PIP) install --upgrade pip && \
$(PIP) install -r requirements.txt && \
$(PYTHON) -m spacy download en_core_web_sm
deactivate && \
pre-commit install
api-build:
cd ./api/terraform
terraform init
terraform apply
api-test:
python -m unittest discover -s api/terraform/python/openai_api/
api-lint:
terraform fmt -recursive
pre-commit run --all-files
black ./api/terraform/python/
flake8 api/terraform/python/
pylint api/terraform/python/openai_api/**/*.py
api-clean:
rm -rf venv
rm -rf ./api/terraform/python/layer_genai/venv
rm -rf ./api/terraform/build/
mkdir -p ./api/terraform/build/
find ./api/terraform/python/ -name __pycache__ -type d -exec rm -rf {} +
######################
# React app
######################
client-clean:
rm -rf node_modules
rm -rf client/node_modules
rm -rf client/dist
client-init:
make client-clean
npm install
cd ./client && npm install && npm init @eslint/config
client-lint:
cd ./client && npm run lint
# npx prettier --write "src/**/*.{js,cjs,jsx,ts,tsx,json,css,scss,md}"
client-update:
npm install -g npm
npm install -g npm-check-updates
ncu --upgrade --packageFile ./package.json
ncu --upgrade --packageFile ./client/package.json
npm update -g
npm install ./client/
client-run:
cd ./client && npm run dev
client-build:
cd ./client && npm run build
client-release:
#---------------------------------------------------------
# usage: deploy prouduction build of the React
# app to AWS S3 bucket.
#
# 1. Build the React application
# 2. Upload to AWS S3
# 3. Invalidate all items in the AWS Cloudfront CDN.
#---------------------------------------------------------
npm run build --prefix ./client/
# ------------------------
# add all built files to the S3 bucket.
# ------------------------
aws s3 sync ./client/dist/ s3://$(S3_BUCKET) \
--acl public-read \
--delete --cache-control max-age=31536000,public \
--expires '31 Dec 2050 00:00:01 GMT'
# ------------------------
# remove the cache-control header created above with a "no-cache" header so that browsers never cache this page
# ------------------------
aws s3 cp s3://$(S3_BUCKET)/index.html s3://$(S3_BUCKET)/index.html --metadata-directive REPLACE --cache-control max-age=0,no-cache,no-store,must-revalidate --content-type text/html --acl public-read
# invalidate the Cloudfront cache
aws cloudfront create-invalidation --distribution-id $(CLOUDFRONT_DISTRIBUTION_ID) --paths "/*" "/index.html"
######################
# HELP
######################
help:
@echo '===================================================================='
@echo 'clean - remove all build, test, coverage and Python artifacts'
@echo 'lint - run all code linters and formatters'
@echo 'init - create environments for Python, NPM and pre-commit and install dependencies'
@echo 'build - create and configure AWS infrastructure resources and build the React app'
@echo 'run - run the web app in development mode'
@echo 'analyze - generate code analysis report'
@echo 'coverage - generate code coverage analysis report'
@echo 'release - force a new release'
@echo '-- AWS API Gateway + Lambda --'
@echo 'api-init - create a Python virtual environment and install dependencies'
@echo 'api-test - run Python unit tests'
@echo 'api-lint - run Python linting'
@echo 'api-clean - destroy the Python virtual environment'
@echo '-- React App --'
@echo 'client-clean - destroy npm environment'
@echo 'client-init - run npm install'
@echo 'client-lint - run npm lint'
@echo 'client-update - update npm packages'
@echo 'client-run - run the React app in development mode'
@echo 'client-build - build the React app for production'
@echo 'client-release - deploy the React app to AWS S3 and invalidate the Cloudfront CDN'