Pre Actions not triggered

Dear PIO Team,I wanted to trigger some pre action for an ESP32 build to prepare some Web files.I did the following:

1.) create a pre_build.py file like:

# Custom actions when building program/firmware

def before_build(source, target, env):
  print("Prepare headers before build")
  # do stuff before build

env.AddPreAction("build", before_build)

2.) add pre:prebuild_sript.py to my env

[env]
platform = https://github.com/pioarduino/platform-espressif32/releases/download/stable/platform-espressif32.zip
framework = arduino
extra_scripts = pre:pre_build.py

But “print Prepare headers before build does not apear when I build.

What doe I make wrong?

The complete File looks like:

Same with buildprog instead of build?

Might also be related to

Hi MaxGerhardt,

yes same with “buildprog” or “build”.
The strange thing is the post programm action works but the pre actions does not occur.

I think it would be vaoid to immediately execute the “pre build” function directly in the script, without it being attached to a pre action. Like, a direct function call to whetever you want to do.

You mean directly after line 3 ?
The print of the
“Current CLI…” and “Current Build targets” occur as empty array.

Can i call a bash script here in this file?
Do i need some subprocess stuff then?

No, instead of

do

before_build(None, None, env)

Wait a minute….

I saw actually the first time the output of the preaction but it is not repoducable

Clean and build?

pio run -t clean
pio run

on CLI.

Here see my image it appears at last after build…

see last line in pic.

This works like:

Any Idea to run a shell script in the pre_build.py script?

import subprocess
import shlex
subprocess.call(shlex.split('./test.sh param1 param2'))

Tried already but get permission denied.

  1. Script path may need to be absolute
  2. May need chmod +x test.sh
  3. test.sh must exist
  4. maybe try it with shell=True as additional parameter in subprocess.call()
  5. otherwise explicitly /bin/sh /test.sh instead of ./test.sh.
1 Like

Great Stuff!

can now build webpack header files before build.

Thank you so much !

:grinning_face:

If it’s just about GZipping, you may find it better to use the filesystem approach for the ESP32: You can just place the files verbatim in data/, build a filesystem and upload that. Then the [Question] ESP32: Compress files in Data to GZip before upload possible to SPIFFS? - #15 by szerintedmi can auto GZIP compress the files before upload, so the ESP32’s webserver serves compressed files.

Hi, no its not about gzip only.

1.) compile the Webfiles as minified files

2.) copy to data folder

3.) gzip files

4.) use xxd to create hexdump c-strings

5.) Add PROGMEM to it

6.) create include header files

=> to send from flash not from filesystem, it is said its faster than filesystem.