invertase / melos

🌋 A tool for managing Dart projects with multiple packages. With IntelliJ and Vscode IDE support. Supports automated versioning, changelogs & publishing via Conventional Commits.
https://melos.invertase.dev/~melos-latest
Apache License 2.0
1.13k stars 201 forks source link

feat: add support for combining scripts #664

Closed jessicatarra closed 6 months ago

jessicatarra commented 6 months ago

Description

This PR adds the capability to combine multiple scripts into a single script definition. In the provided example, the pre-commit script is configured to invoke a simple command as echo 'hello world', and also individual scripts such as format and analyze.

YAML file

scripts:
  pre-commit:
    description: pre-commit git hook script
    steps:
      - echo 'hello world'
      - format
      - analyze
  format:
    description: Format Dart code.
    run: dart format .
    exec:
      concurrency: 5
  analyze:
    description: Analyze Dart code
    run: dart analyze .
    exec:
      concurrency: 5

Output

┌─(~/Development/melos)──────────────────────────────────────────────────────────────────────────────────────────────────────────(jessica@jessicas-MacBook-Air:s005)─┐
└─(21:14:01 on feature/multiple-steps-script ✹)──> melos run pre-commit                                                                                ──(Sat,Mar16)─┘
Building package executable... 
Built melos:melos.
melos run pre-commit
  └> echo 'hello world'
     └> RUNNING

hello world

melos run pre-commit
  └> echo 'hello world'
     └> SUCCESS

melos run pre-commit
  └> format
     └> RUNNING

melos run format
  └> melos exec --concurrency 5 -- "dart format ."
     └> RUNNING

$ melos exec
  └> dart format .
     └> RUNNING (in 3 packages)

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
[conventional_commit]: Formatted 3 files (0 changed) in 0.18 seconds.
[melos]: Formatted 85 files (0 changed) in 0.60 seconds.
[melos_workspace]: Formatted 90 files (0 changed) in 0.61 seconds.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

$ melos exec
  └> dart format .
     └> SUCCESS

melos run format
  └> melos exec --concurrency 5 -- "dart format ."
     └> SUCCESS

melos run pre-commit
  └> format
     └> SUCCESS

melos run pre-commit
  └> analyze
     └> RUNNING

melos run analyze
  └> melos exec --concurrency 5 -- "dart analyze ."
     └> RUNNING

$ melos exec
  └> dart analyze .
     └> RUNNING (in 3 packages)

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
[conventional_commit]: Analyzing ....
[melos]: Analyzing ....
[melos_workspace]: Analyzing ....
[conventional_commit]: No issues found!
[melos]: No issues found!
[melos_workspace]: No issues found!
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

$ melos exec
  └> dart analyze .
     └> SUCCESS

melos run analyze
  └> melos exec --concurrency 5 -- "dart analyze ."
     └> SUCCESS

melos run pre-commit
  └> analyze
     └> SUCCESS

As it stands, when using the multiple script definition, we are not allowed to use execution options like concurrency or packageFilter option, because that would increase the level of complexity and could cause several problems, so my suggestion for now is to be able to configure these things in the individual scripts.

Closes #653

Type of Change

spydon commented 6 months ago

As it stands, when using the multiple script definition, we are not allowed to use execution options like concurrency or packageFilter option, because that would increase the level of complexity and could cause several problems, so my suggestion for now is to be able to configure these things in the individual scripts.

Completely agree!

jessicatarra commented 6 months ago

Hey @spydon, thanks for your feedback! and regarding the test scenario you just mentioned in the first comment, I think it's worth including a validation to avoid an infinite loop, because that's actually what ends up happening, so I'm working on it.

MohiuddinM commented 6 months ago

Isn't this already possible like:

pre_commit:
 description: pre-commit git hook script
    run: |
      melos run format
      melos run analyze
      melos run test
  exec:
    concurrency: 3
    failFast: true
spydon commented 6 months ago

Isn't this already possible like:

pre_commit:
 description: pre-commit git hook script
    run: |
      melos run format
      melos run analyze
      melos run test
  exec:
    concurrency: 3
    failFast: true

Sure you can do that, but this is a much cleaner API! Especially since multi-line scripts can have some quirks, which even your example suffers from (missing &&). :smile:

provokateurin commented 6 months ago

Cool feature, can we also have this for hooks? I guess this would be a breaking change so I'd understand if you wouldn't want to add it, but I think it would be very useful.

spydon commented 6 months ago

Cool feature, can we also have this for hooks? I guess this would be a breaking change so I'd understand if you wouldn't want to add it, but I think it would be very useful.

You can open an enhancement issue regarding that if you want, so that we don't have the discussion about it on this PR. For now, you could just create a melos script with steps and call it from the hook instead.