Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline modules #2442

Open
iloveitaly opened this issue Oct 29, 2024 · 11 comments
Open

Inline modules #2442

iloveitaly opened this issue Oct 29, 2024 · 11 comments

Comments

@iloveitaly
Copy link
Contributor

From this discord conversation.

I have a bunch of related tasks:

    db-generate-migration
    db-migrate
    db-nuke
    db-reset
    db-seed
    db-up
    db-destroy

What I'd like to do is have a more clear separator (like rake):

    db:generate-migration
    db:migrate

This is possible with modules, but I cannot create multiple modules in a single Justfile (creating separate Justfiles would be messy in my case).

Two ideas I can think of to solve this problem:

  1. Would it be possible to support :, . in the name grammar?
  2. Could we define multiple modules within a single Justfile?
@casey casey changed the title Modules Without Separate Files Support inline modules Oct 30, 2024
@casey casey changed the title Support inline modules Inline modules Oct 30, 2024
@casey
Copy link
Owner

casey commented Oct 30, 2024

Thanks for opening this issue!

I would like to eventually support inline modules, which would allow submodules to be defined in a single justfile.

We could allow :: in recipe names, but modules do more than just provide namespaces for recipes, since they can have their own settings, variables, and submodules, so I'm more keen to just do full blown inline modules, which would support all of that.

The syntax I've had in mind is:

bar::
  foo:
    echo 'recipe foo inside module bar'

But it could also be:

mod bar::
  foo:
    echo 'recipe foo inside module bar'

just uses indentation to delimit recipe bodies, since recipe bodies are arbitrary text, and just is descended from make, which does the same. So it would be natural to also use indentation to delimit modules bodies, as in the examples above.

However, I do personally find indentation to be annoying and fiddly, so we could consider using {…} for modules, which would allow for indenting the contents:

bar {
  foo:
    echo 'recipe foo inside module bar'
}

Or not, depending on preference:

bar {
foo:
  echo 'recipe foo inside module bar'
}

@iloveitaly
Copy link
Contributor Author

I really like the :: indentation structure:

bar::
  foo:
    echo 'recipe foo inside module bar'

Feels really natural and easy to read.

Nested indentation isn't awesome, but { syntax wouldn't seem to align with the rest of the Justfile grammar IMHO. However, this is a departure from Makefile syntax in a bigger way so maybe it makes sense to do something very different?

Either way sounds great to me! I love this idea.

@casey
Copy link
Owner

casey commented Oct 31, 2024

Yah, honestly I like it too, especially in terms of being easy to read. My preference for avoiding indentation-based syntax is mostly when it comes to writing and editing. For example, copy-pasting a recipe from an inline module, where it's indented, to a dedicated module file, where it won't be indented, would require fixing the indentation manually.

But it's probably important that the syntax all feels consistent, and not randomly have {…} delimited constructs when everything else is based on indentation.

@iloveitaly
Copy link
Contributor Author

But it's probably important that the syntax all feels consistent

I'd agree here!

Looking forward to kicking the tires on this whenever it's implemented.

Thanks for the excellent tool.

@gechelberger
Copy link

Seconding. I was going to file this same issue.

Is the intent for these to be just logical groupings or more like clap sub commands?

I would lean more towards the subcmd option, though that is certainly more complicated:

  • Can modules be nested recursively? If you have the machinery to do it at a single level, I would expect that to be most of the complexity.
  • Can each module define it's own "default:" like a top level justfile
  • Can a parent module have variables that are available in a child scope
msrv := "1.65.0"

ci opts=""::
  default: core
  core:
    cargo test {{opts}}
  msrv:
    cargo +{{msrv}} test {{opts}}
  features:
    cargo test --all-features {{opts}}
  all: features msrv
> just ci
> just ci --verbose
> just ci::features --verbose
> just ci::all --no-run

@casey
Copy link
Owner

casey commented Nov 9, 2024

They would be like subcommands, since an inline module is the same as a normal module, and normal modules create subcommands.

It should be possible to nest modules recursively, although the parsing code will likely be horrible, so it's okay if only one level of nesting is supported at first.

Every module can have its own default, like existing modules.

Modules can't share anything yet, and are a little gimped at the moment 😅, see #2252.

@iloveitaly
Copy link
Contributor Author

After tinkering around with an ever-growing Justfile, I'm going to put in my vote for:

  • Allowing a single : in the recipe name grammar. This would still be helpful/cleaner when not using modules.
  • Keeping :: for modules

@casey
Copy link
Owner

casey commented Nov 20, 2024

Allowing : in the recipe name grammar would lead to some issues.

For example, should this:

foo:bar:baz

Be interpreted as recipe foo with dependency bar:baz, or recipe foo:bar with dependency baz. You could work around this by whitespace to disambiguate it, but that feels kinda bad.

@iloveitaly
Copy link
Contributor Author

Ah I see, that makes sense. Any workarounds would make the syntax ugly.

Another idea here is allowing dot-syntax. Mix does this and it works pretty well.

command.with.dots: other.command

@casey
Copy link
Owner

casey commented Nov 24, 2024

Yah, similar issues with . syntax. . is used so common for property access, and we might want that later, i.e. object.method().

@iloveitaly
Copy link
Contributor Author

Yup yup makes sense especially if you aim to expand the language and syntax over time. Love how thoughtful you've been with the tool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants