edemaine / svgtiler

Tool for drawing diagrams on a grid, combining grids of SVGs into a big SVG figure
MIT License
61 stars 6 forks source link

Export redesign #100

Closed edemaine closed 2 years ago

edemaine commented 2 years ago

Currently exports are used in two ways:

I'd like to propose instead that export has a finite set of intended names. export default will remain ambiguous between Maketiles and mappings, for convenience, but you can choose to be unambiguous via export map or export make.

Maketiles (Maketile.{js,coffee})

The result is that a Maketile.coffee could be written much closer to an actual Makefile, via implicit export default:

drawing1: -> svgtiler 'mapping.coffee drawing1.asc'
drawing2: -> svgtiler 'mapping.coffee drawing2.asc'

And programmatically generated rules could be written reasonably nicely and in ESM:

export make = {}
for lang in ['coffee', 'js']
  make[lang] = do (lang) -> -> svgtiler "mapping.#{lang}"

Or totally generic rule names, something we couldn't do at all with the export approach:

export make = (lang) ->
  svgtiler "mapping.#{lang}"

# Example with default too:
export make = (lang) ->
  if lang
    svgtiler "mapping.#{lang}"
  else # default
    svgtiler 'coffee js'  # recursively call rules
    #or
    for lang in ['coffee', 'js']
      make lang

One limitation here is there's no way to tell, given an argument, whether it's a Maketile rule or a filename. That's fine with the current design: currently, filenames take priority over Maketile rules, so if you do svgtiler foo and foo is a filename, then it gets treated as a filename; indeed, the Maketile only gets loaded if the argument doesn't match as a filename. I think this is fine, as filenames should have .s in them, while rules should not, so there's no real conflict.

Mappings

This is the second half of #95. In the end, I think the svgtiler.{onInit,preprocess,postprocess} side effects are dangerous and hard to use:

So instead the proposal is:

Cool features of this approach:

We will have to change the situations in which svgtiler.background is allowed; it can no longer be at the top level of a mapping file.

diomidov commented 2 years ago

Array maybe means to run the rules in sequence (or in parallel?)

Potentially we could allow arrays of functions in the above exports to make it easy to combine multiple steps.

We already have an easy way of calling functions in sequence :stuck_out_tongue_closed_eyes:. You can always add that shorthand later if there's demand for it, but you can never remove it because of backwards compatibility.

Other than your insistence on always having more than one obvious way to do everything, the proposal looks great! :tada: