platformio / platformio-core

Your Gateway to Embedded Software Development Excellence :alien:
https://platformio.org
Apache License 2.0
7.84k stars 789 forks source link

Hook after Clean #4456

Open Wiz-IO opened 1 year ago

Wiz-IO commented 1 year ago

I use BUILD_DIR to compile boot2_stage boot loader of Raspberry Pi Pico but Clean clear only board folder...

I didn't find a solution for hook Clean (after) and I can offer you a small patch:

PlatformBase / base.py

    def on_clean(self, dir):            # <---
        pass

builder / tools / piotarget.py

    print("Done cleaning")
    return build_dir                    # <---

builder / main.py

if env.GetOption("clean") or is_clean_all:
    dir = env.PioClean(is_clean_all)    # <---
    env.PioPlatform().on_clean(dir)     # <---
    env.Exit(0)

platform.py

class MyPlatform(PlatformBase):
    def on_clean(self, dir):
        print('ON_CLEAN', dir) 
# RESULT: 
#   ON_CLEAN .....\.pio\build\test-board
ivankravets commented 1 year ago

You can override the run method and catch the "clean" target.

We also extended PlatformIO Build API and it is possible to catch "clean" target in the PRE script. See https://github.com/platformio/platformio-core/commit/7e6cb84c879a3d5080bd53183f80575d10a45cd5

Wiz-IO commented 1 year ago

PRE script will work in next version ?

ivankravets commented 1 year ago

Yes, you can test it with pio upgrade --dev.

Wiz-IO commented 1 year ago

nice ! Thanks

Wiz-IO commented 1 year ago

Ivan, am i doing something wrong? pre-script is executed with Build, but not with Clean

Import("env")

def on_clean_pre(source, target, env):
    print('ON CLEAN PRE')

def on_clean_post(source, target, env):
    print('ON CLEAN POST')

env.AddPreAction("clean", on_clean_pre)
env.AddPostAction("clean", on_clean_post)

print('--------SCRIPT-----------')

PlatformIO has been successfully upgraded to 6.1.6a3

sarthaknimbalkar commented 1 year ago

Hey @ivankravets, I'm new to open source and would like to make a contribution on this issue. Could I possibly give it a try?

zeel01 commented 10 months ago

This seems to remain unavailable. There are generated files in my project that I would like to delete when I run the clean or fullclean action, but as far as I can tell these actions still don't trigger pre/post hooks. This seems like it should be a pretty common need - cleaning up additional stuff that PIO doesn't normally know about.

dbarwacz commented 2 months ago

It seems that adding

env.AddPreAction("clean", on_clean_pre)
env.AddPostAction("clean", on_clean_post)

has no effect despite looking ok. The workaround I found is to create custom target that does the clean and remembering not to use the default one, although it's not ideal.