Running a defined maintainence dart file when (or after) you call pub get

Issue

I have a dart command prompt file that is a boiler plate function that is required prior to development. I wanted to run it in when the user runs pub get.

I saw that https://www.dartlang.org/tools/pub/pubspec.html#executables “executables” might be exactly what i want. I noticed that it has to be in the bin though, so it seems that if i set up:

executables:
  admin_script:

it would expose admin_script to be able to be executed and calls: bin/admin_script.dart.

The issue is that 1) I wanted this script to be in lib/commmon/ for the time being and run…. and 2) executables exposes it, but doesnt necessarily execute it.

Is there something I am missing which would allow me to do something like:

pub get

and then it would at the end of get call, lib/common/admin_script.dart

Edit

As an edit, I was trying to see if there was a way to run this maintenance script either immediately following the pub get. Not sure if this is valued as being inside of the pubspec.yaml file or if this is something I would want to configure elsewhere in my script.

I was thinking that this would be a good script to run when you open the project, but im not sure if that is also an applicable option.

Solution

This isn’t possible from within pub. It doesn’t support any kind of hooks. This issue is tracking the ability for packages to provide hooks that are run when they’re installed, but I don’t think there’s an issue for hooks defined by the local package.

Probably the best way to do this right now would be to use a task runner like hop to define a task that both runs pub get and executes any additional code you need. This has the added benefit of being more explicit than having a hook that runs implicitly.

As an aside, executables stored bin/ and exposed through the pubspec are intended to be public-facing: they’re for users of your package to execute. If you want an executable that’s only run in your package, that should go in the tool/ directory. See the package layout conventions for more details.

Answered By – Natalie Weizenbaum

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.