Opened 3 months ago

Last modified 3 months ago

#73863 new enhancement

Proposal: `port fmt`

Reported by: herbygillot (Herby Gillot) Owned by:
Priority: Normal Milestone:
Component: base Version:
Keywords: Cc: neverpanic (Clemens Lang)
Port:

Description

port is way overdue for a fmt command, as you would find in most modern toolkits and development frameworks, such as Go (go fmt) or Rust (cargo fmt).

A port fmt command would automatically do the following to a Portfile:

  • add a modeline if it's missing
  • ensure spacing is multiples of 4
  • ensure spacing is consistently aligned
  • maybe ensure we try to keep to 80 char column width?
  • ensure ordering of PortSystem, the PortGroups cluster, and then everything else?
  • ensure checksums alignment

...any additional thoughts?

Change History (8)

comment:1 Changed 3 months ago by herbygillot (Herby Gillot)

There could be some overlap between port fmt and port lint, though fmt probably wouldn't fix every single thing that lint might point out, such as reccommended checksum types.

comment:2 Changed 3 months ago by Dave-Allured (Dave Allured)

  • Convert tabs to spaces?
  • Warn UTF-8? I think UTF-8 is valid in some text fields, but not everywhere?
  • Convert common UTF-8 mistakes into ASCII?
  • What is "checksums alignment"?
  • This is only for a development tool, right?
  • How would the targeting work? No file argument on this command, and targets the Portfile in the current directory?
  • Renames the original file as a backup? Or what?

comment:3 Changed 3 months ago by markemer (Mark Anderson)

I like this idea, since I'm constantly missing crap when formatting.

comment:4 Changed 3 months ago by jmroot (Joshua Root)

Please ensure this is implemented in a way that doesn't add overhead when it's not being used, either by making it a separate executable or at least a separate package that is only loaded when needed.

Convert tabs to spaces?

Should follow the modeline if one exists.

comment:5 Changed 3 months ago by barracuda156

Alphabetizing deps and portgroups would be nice, doing it manually can be pretty annoying.

comment:6 Changed 3 months ago by neverpanic (Clemens Lang)

Cc: neverpanic added

comment:7 Changed 3 months ago by neverpanic (Clemens Lang)

This would be nice, just to save some time, and to avoid discussions on formatting. We do have some quite complicated Portfiles, though, so I wonder how it would fare on those — I reckon we'd probably best starting off with an existing Tcl source code formatter (if that's something that already exists).

Note that we'll be changing to Tcl 9 soon and that'll enforce that input files are valid UTF-8 and otherwise crash out, so there will probably be no need for any encoding-specific tricks.

comment:8 in reply to:  2 Changed 3 months ago by herbygillot (Herby Gillot)

So we'll definitely need something that can parse Tcl. So to that end, I'm introducing tclparse and portparse packages that can help serve as the foundation for port fmt (and to also perhaps improve port lint).

I've got a WIP branch here: https://github.com/herbygillot/macports-base/tree/tclparse

Replying to jmroot:

Please ensure this is implemented in a way that doesn't add overhead when it's not being used, either by making it a separate executable or at least a separate package that is only loaded when needed.

This is addressed by having these 2 packages be standalone packages in src/tclparse1.0 and src/portparse1.0. Those only get imported by the port fmt code path (and port lint later on).

Should follow the modeline if one exists.

I think we should standardize on a standard modeline used across the entire codebase to simplify things.

Replying to Dave-Allured:

  • What is "checksums alignment"?

In my mind, it's making sure the checksum value fields are aligned:

checksums           rmd160  ec065cd934007fdfab0ef003f6209314d92324db \
                    sha256  f90c3b096af568438be7da52336784635a962c9822f10f98e5ad11ae8c7f5c64 \
                    size    334136

vs.

checksums           rmd160 ec065cd934007fdfab0ef003f6209314d92324db \
                    sha256 f90c3b096af568438be7da52336784635a962c9822f10f98e5ad11ae8c7f5c64 \
                    size 334136
  • This is only for a development tool, right?

Yeah, I see this as occupying the same space as port bump, for folks who work on Portfiles.

  • How would the targeting work? No file argument on this command, and targets the Portfile in the current directory?

I think similar to how port bump works. If you issue a port bump in a directory where a Portfile is present, it uses that, but if not, treats the argument as a port name. I'm open to suggestions.

  • Renames the original file as a backup? Or what?

I guess we can provide a flag to keep the original in a backup file, but my initial inkling was to have it format everything in place as most other fmt commands do.

Note: See TracTickets for help on using tickets.