sormuras / bach

🎼 Bach Builds (on(ly)) Modules
https://bach.run
Universal Permissive License v1.0
234 stars 51 forks source link

Add event handler support #227

Closed sormuras closed 3 years ago

sormuras commented 3 years ago

Introduce extension points for "interesting" events using Java's Service Loader API.

In order to alter (extend or override) Bach's default build program (lifecycle), users have to extends Bach and provide their implementation via:

https://github.com/sormuras/bach/blob/7109652ba70b31ac9c2379ecc309aabe86dce875/.bach/bach.info/module-info.java#L130-L131

This implies the following initial setup:

public class CustomBach extends Bach {

  public static Provider<CustomBach> provider() {
    return CustomBach::new;
  }

  private CustomBach(Options options) {
    super(options);
  }

  // Here be overrides...
}  

Instead, users will be enabled to extend Bach's default build program by implementing one (or more) service:

interface OnTestsFailed { void onTestsFailed(Bach bach, List<Throwable> throwables); }
interface OnTestsSuccessful { void onTestsSuccessful(Bach bach); }

For example:

public class TestsHandler implements OnTestsFailed, OnTestsSuccessful {
  public void onTestsFailed(Bach bach, List<Throwable> throwables) {
    SystemTray...displayMessage(...);
  }
  public void onTestsSuccessful(Bach bach) {
    bach.log("Call more tools");
  }
}  
sormuras commented 3 years ago

If a module declares more than one provider then the providers are located in the order that its module descriptor lists the providers.

Copied from ServiceLoader.load(ModuleLayer, Class)

https://github.com/sormuras/bach/blob/f7d11ad8032fc249704c78bf61f84a2d0fe77ed8/.bach/bach.info/module-info.java#L133-L135

Thus, Teal is first, Green second.