Skip to content

Commit ccb223c

Browse files
committed
refactor(makefile): split makefile into modular files for better readability and maintainability
- Extracted environment variables into `makefile.env.mk` - Moved reusable functions to `makefile.functions.mk` - Separated cleaning rules into `makefile.clean.mk` - Grouped PXT-specific commands in `makefile.pxt.mk` - Updated `Makefile` to include these new modular files This refactoring improves the organization, readability, and modularity of the Makefile, facilitating easier maintenance and scalability.
1 parent 612a452 commit ccb223c

7 files changed

Lines changed: 294 additions & 187 deletions

File tree

.devcontainer/devcontainer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
// Use 'forwardPorts' to make a list of ports inside the container available locally.
119119
"forwardPorts": [3232, 3233],
120120
// Use 'postCreateCommand' to run commands after the container is created.
121-
"postAttachCommand": "make setup build",
121+
"postAttachCommand": "make build",
122122
"postCreateCommand": "sudo /lib/systemd/systemd-udevd --daemon",
123123

124124
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.

Makefile

Lines changed: 33 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -1,224 +1,72 @@
1-
.DEFAULT_GOAL = build
1+
.DEFAULT_GOAL = default
22

33
default : build
44
all : setup build
55

6-
.ONESHELL: # Applies to every targets in the file!
7-
8-
export PATH := $(shell pwd)/node_modules/.bin:$(PATH)
9-
10-
export PXT_FORCE_LOCAL := 1
11-
export PXT_RUNTIME_DEV := 1
12-
export PXT_ASMDEBUG := 1
13-
export PXT_NODOCKER := 1
14-
15-
PXT_LIBRARIES := pxt pxt-common-packages pxt-steami pxt-steami-backend
16-
PXT_COMMANDS := add buildcss buildjres buildsimjs buildsprites buildtarget bump checkdocs checkpkgcfg ci console deploy extract help init install npminstallnative run serve staticpkg tag testghpkgs update usedblocks
17-
# Arguments par défaut pour chaque commande PXT
18-
PXT_ADD_ARGS ?=
19-
PXT_BUILDCSS_ARGS ?=
20-
PXT_BUILDJRES_ARGS ?=
21-
PXT_BUILDSIMJS_ARGS ?=
22-
PXT_BUILDSPRITES_ARGS ?=
23-
PXT_BUILDTARGET_ARGS ?= --localbuild --force
24-
PXT_BUMP_ARGS ?=
25-
PXT_CHECKDOCS_ARGS ?=
26-
PXT_CHECKPKGCFG_ARGS ?=
27-
PXT_CI_ARGS ?=
28-
PXT_CONSOLE_ARGS ?=
29-
PXT_DEPLOY_ARGS ?=
30-
PXT_EXTRACT_ARGS ?=
31-
PXT_HELP_ARGS ?=
32-
PXT_INIT_ARGS ?=
33-
PXT_INSTALL_ARGS ?=
34-
PXT_NPMINSTALLNATIVE_ARGS ?=
35-
PXT_RUN_ARGS ?=
36-
PXT_SERVE_ARGS ?= --localbuild --rebundle --noauth --no-browser --no-serial -h '0.0.0.0'
37-
PXT_STATICPKG_ARGS ?= -o ../static/ --localbuild
38-
PXT_TAG_ARGS ?=
39-
PXT_TESTGHPKGS_ARGS ?=
40-
PXT_UPDATE_ARGS ?=
41-
PXT_USEDBLOCKS_ARGS ?=
42-
43-
PXT="/workspaces/makecode-steami/node_modules/.bin/pxt"
44-
45-
define install_node_package
46-
echo "Installing $1 ..."
47-
cd $1 || exit
48-
npm install --link
49-
endef
50-
51-
define _remove_file_if_exist
52-
if [ -f $1 ] ; then
53-
echo "Remove $1"
54-
rm -f $1
55-
fi
56-
endef
57-
58-
define _remove_directory_if_exist
59-
if [ -d $1 ] ; then
60-
echo "Remove directory $1"
61-
rm -Rf $1
62-
fi
63-
endef
64-
65-
define deepclean_node_package
66-
echo "Deep cleanning $1 .."
67-
$(call _remove_file_if_exist,$1/package-lock.json)
68-
$(call _remove_directory_if_exist,$1/node_modules)
69-
endef
70-
71-
define pxt_command
72-
echo "PXT = $(PXT)"
73-
echo "Call pxt $1"
74-
if ! [ -x $(PXT) ]; then
75-
echo "pxt is not installed ! Please run make setup first"
76-
exit
77-
fi
78-
cd pxt-steami || exit
79-
pxt $1
80-
endef
81-
82-
define _clean_static
83-
echo "Clean static build"
84-
$(call _remove_directory_if_exist,static)
85-
endef
86-
87-
define _clean_pxt_steami
88-
echo "Clean pxt-steami build"
89-
90-
if [ -x "$(PXT)" ]; then
91-
echo "pxt found ! \n Automatic cleanning"
92-
if [ -d pxt-steami/built ]; then
93-
cd pxt-steami || exit
94-
pxt clean
95-
fi
96-
else
97-
echo "pxt not found ! \n Manual cleanning"
98-
$(call _remove_directory_if_exist,pxt-steami/built)
99-
$(call _remove_directory_if_exist,pxt-steami/libs/core/built)
100-
$(call _remove_directory_if_exist,pxt-steami/libs/blocksprj/built)
101-
fi
102-
endef
103-
104-
define _clean_pxt_core
105-
echo "Clean pxt-core build"
106-
$(call _remove_directory_if_exist,pxt/built)
107-
endef
108-
109-
define _clean_pxt_common_packages
110-
echo "Clean pxt-common-packages build"
111-
$(call _remove_directory_if_exist,pxt-common-packages/built)
112-
endef
113-
114-
define _clean_pxt_steami_backend_certificates
115-
echo "Clean pxt-steami-backend certificates ..."
116-
$(call _remove_file_if_exist,pxt-steami-backend/https/fastify.cert)
117-
$(call _remove_file_if_exist,pxt-steami-backend/https/fastify.key)
118-
$(call _remove_file_if_exist,pxt-steami-backend/https/rootCA.pem)
119-
endef
120-
121-
define _clean
122-
$(call _clean_static)
123-
$(call _clean_pxt_steami)
124-
endef
125-
126-
define _deepclean
127-
$(call _clean)
128-
$(call _clean_pxt_common_packages)
129-
$(call _clean_pxt_core)
130-
$(call deepclean_node_package,pxt)
131-
$(call deepclean_node_package,pxt-common-packages)
132-
$(call deepclean_node_package,pxt-steami)
133-
$(call deepclean_node_package,pxt-steami-backend)
134-
$(call deepclean_node_package,.)
135-
endef
136-
137-
define _generate_localcertificates
138-
export CAROOT=$(PWD)/pxt-steami-backend/https
139-
mkcert -install
140-
mkcert -cert-file pxt-steami-backend/https/fastify.cert -key-file pxt-steami-backend/https/fastify.key 'makecode.local' localhost 127.0.0.1 ::1
141-
endef
142-
143-
define _install_cert_for_local_dev
144-
if [ -f /.dockerenv ]; then
145-
echo "This rule work only for the host system"
146-
else
147-
sudo cp pxt-steami-backend/https/rootCA.pem /usr/local/share/ca-certificates/rootCA.crt
148-
sudo update-ca-certificates
149-
sudo sh -c "echo 127.0.0.1 makecode.local>>/etc/hosts"
150-
fi
151-
endef
152-
153-
.PHONY : prepare
154-
prepare :
6+
.ONESHELL: # Applies to every targets
7+
8+
include env.mk
9+
include commons.mk
10+
include pxt.mk
11+
12+
# Setup commands
13+
.PHONY: prepare
14+
prepare:
15515
@echo "Install Git hooks"
15616
git config core.hooksPath .hooks
15717

158-
.PHONY : setup
159-
setup : prepare clean $(PXT) install-makecode-steami $(addprefix install-,$(PXT_LIBRARIES))
18+
.PHONY: setup
19+
setup: prepare clean $(PXT)
16020

16121
.PHONY : clean
16222
clean : ;@$(call _clean)
16323

24+
.PHONY : clean-local-certificates
25+
clean-local-certificates:;@$(call _clean_pxt_steami_backend_certificates)
26+
16427
.PHONY : deepclean
16528
deepclean : ;@$(call _deepclean)
16629

167-
# Création des cibles de build pour chaque package pxt
168-
define _install_node_package_template
169-
.PHONY: install-$1
170-
install-$1: $2/node_modules/.package-lock.json $2/package-lock.json
171-
172-
$2/node_modules/.package-lock.json $2/package-lock.json: $2/package.json
173-
@$$(call install_node_package,$$(<D))
174-
175-
endef
30+
# Create make rule for each PXT command
31+
$(foreach command,$(PXT_COMMANDS),$(eval $(call _call_pxt_command_template,$(command),$(command),$(PXT))))
17632

177-
# Création de la target install-makecode-steami
33+
# Create install rules for each node package
17834
$(eval $(call _install_node_package_template,makecode-steami,.))
179-
180-
# Création des targets install-pxt-XXX
18135
$(foreach target,$(PXT_LIBRARIES),$(eval $(call _install_node_package_template,$(target),$(target))))
18236

183-
$(PXT) : pxt/built/target.json pxt-common-packages/node_modules/.package-lock.json node_modules/.package-lock.json
37+
# Install pxt CLI
38+
$(PXT) : | install-makecode-steami $(addprefix install-,$(PXT_LIBRARIES)) pxt/built/target.json
18439

40+
# Build pxt cli
18541
pxt/built/target.json : pxt/node_modules/.package-lock.json
42+
@echo "Build pxt core"
18643
cd pxt || exit
18744
npm run build
18845

189-
define _call_pxt_command_template
190-
.PHONY: $1
191-
$1: $3
192-
@$$(call pxt_command,$2 $$(PXT_$(shell echo $2 | tr '[:lower:]' '[:upper:]')_ARGS))
193-
194-
endef
195-
196-
$(eval $(call _call_pxt_command_template,build,buildtarget,$(PXT)))
197-
198-
$(foreach command,$(PXT_COMMANDS),$(eval $(call _call_pxt_command_template,$(command),$(command),$(PXT))))
46+
# Create build rule by aliasing pxt buildtarget command
47+
.PHONY : build
48+
build : buildtarget
19949

50+
# Create package rule by aliasing pxt staticpkg command
20051
.PHONY : package
201-
package : static/target.json
52+
package : staticpkg
20253

203-
static/target.json : $(PXT)
204-
@$(call pxt_command,staticpkg $(PXT_STATICPKG_ARGS))
54+
static/target.json : staticpkg
20555

20656
.PHONY : staticserve
20757
staticserve : static/target.json pxt-steami-backend/https/fastify.cert pxt-steami-backend/https/fastify.key pxt-steami-backend/node_modules/.package-lock.json
58+
@echo "Serve static editor"
20859
nodemon pxt-steami-backend/server.js
20960

210-
.PHONY : clean_localcertificates
211-
clean_localcertificates:;@$(call _clean_pxt_steami_backend_certificates)
212-
21361
.PHONY : localcertificates
21462
localcertificates: pxt-steami-backend/https/fastify.cert pxt-steami-backend/https/fastify.key pxt-steami-backend/https/rootCA.pem
21563

216-
pxt-steami-backend/https/fastify.cert pxt-steami-backend/https/fastify.key pxt-steami-backend/https/rootCA.pem & : ;@$(call _generate_localcertificates)
217-
218-
.PHONY : install_cert_for_local_dev
219-
install_cert_for_local_dev : pxt-steami-backend/https/rootCA.pem
64+
.PHONY : install-cert-for-local-dev
65+
install-cert-for-local-dev : localcertificates
22066
@$(call _install_cert_for_local_dev)
22167

68+
pxt-steami-backend/https/fastify.cert pxt-steami-backend/https/fastify.key pxt-steami-backend/https/rootCA.pem & : ;@$(call _generate_localcertificates)
69+
22270
# A useful debug Make Target - found from
22371
# http://lists.gnu.org/archive/html/help-make/2005-08/msg00137.html
22472
.PHONY: printvars

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,78 @@ To run the MakeCode editor locally:
6565
```
6666
7. Open the editor in your browser at `http://localhost:3000`.
6767

68+
## PXT Commands and Default Parameters
69+
70+
This project uses a Makefile to handle various PXT commands such as `serve`, `run`, `update`, and more. Each command can be run by simply invoking `make <command>`. For instance:
71+
72+
```sh
73+
make serve
74+
```
75+
76+
By default, each PXT command comes with predefined arguments, which can be customized according to your needs. Below is a list of the default arguments for each command:
77+
78+
- **add**: _(No default arguments)_
79+
- **buildcss**: _(No default arguments)_
80+
- **buildjres**: _(No default arguments)_
81+
- **buildsimjs**: _(No default arguments)_
82+
- **buildsprites**: _(No default arguments)_
83+
- **buildtarget**: --localbuild --force
84+
- **bump**: _(No default arguments)_
85+
- **checkdocs**: _(No default arguments)_
86+
- **checkpkgcfg**: _(No default arguments)_
87+
- **ci**: _(No default arguments)_
88+
- **console**: _(No default arguments)_
89+
- **deploy**: _(No default arguments)_
90+
- **extract**: _(No default arguments)_
91+
- **init**: _(No default arguments)_
92+
- **install**: _(No default arguments)_
93+
- **npminstallnative**: _(No default arguments)_
94+
- **run**: _(No default arguments)_
95+
- **serve**: --localbuild --rebundle --noauth --no-browser --no-serial -h '0.0.0.0'
96+
- **staticpkg**: -o ../static/ --localbuild
97+
- **tag**: _(No default arguments)_
98+
- **testghpkgs**: _(No default arguments)_
99+
- **update**: _(No default arguments)_
100+
- **usedblocks**: _(No default arguments)_
101+
102+
### Default Parameters Variable Naming Pattern
103+
104+
The naming of the variables containing the default parameters follows a consistent pattern. Each variable that holds default arguments is named as:
105+
106+
**`PXT_<COMMAND>_ARGS`**
107+
108+
Where:
109+
110+
- **`PXT`**: This prefix indicates that the variable is related to PXT commands.
111+
- **`<COMMAND>`**: The name of the specific PXT command, written in uppercase. For example, `DEPLOY` for the `deploy` command.
112+
- **`_ARGS`**: This suffix denotes that the variable contains arguments to be passed to the specified command.
113+
114+
### Examples
115+
116+
- **`PXT_DEPLOY_ARGS`**: Contains the default arguments for the `deploy` command.
117+
- **`PXT_RUN_ARGS`**: Contains the default arguments for the `run` command.
118+
- **`PXT_UPDATE_ARGS`**: Contains the default arguments for the `update` command.
119+
120+
### How to Modify Default Parameters
121+
122+
If you need to override the default arguments, you can do so by specifying them directly in the command line when invoking `make`. To modify the default parameters for any PXT command, you can override the variable directly when calling `make`. For instance:
123+
124+
```sh
125+
make run PXT_RUN_ARGS="--hardware --verbose"
126+
```
127+
128+
This command will run the `run` target with custom arguments (`--hardware --verbose`), overriding the default value set in `PXT_RUN_ARGS`.
129+
130+
This naming convention makes it easy to customize arguments for each command. This flexibility allows you to tailor the PXT commands to match your specific development or deployment requirements without modifying the Makefile itself.
131+
132+
### Example Usage
133+
134+
To run the `serve` command with custom arguments:
135+
136+
```sh
137+
make serve PXT_SERVE_ARGS="--port 8080 --no-browser"
138+
```
139+
68140
## Using Dev Container
69141

70142
This repository is configured to be used with a development container (Dev Container), which allows for an easy and consistent development environment.

0 commit comments

Comments
 (0)