Skip to content

Commit

Permalink
Support for Maketile.{js,coffee} exported rules (#99)
Browse files Browse the repository at this point in the history
Existing files or matching globs still take priority over rules,
to avoid loading Maketile when possible.
  • Loading branch information
edemaine committed Oct 15, 2022
1 parent 0e5df49 commit 8b92a91
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 19 deletions.
9 changes: 9 additions & 0 deletions examples/anim/Maketile.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
for lang in ['css', 'styl']
exports[lang] = do (lang) -> ->
svgtiler """
-f css-anim.#{lang}
( shapes.coffee css-anim.csv )
( ascii.coffee ascii.asc )
"""

export default exports.css
11 changes: 11 additions & 0 deletions examples/chess/Maketile.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export coffee = ->
svgtiler '-f map.coffee *.asc'
export js = ->
svgtiler '-f map.jsx *.asc'
export graph = ->
svgtiler '-f -O graph-\* map.coffee graph.coffee *.asc'

export default ->
coffee()
js()
graph()
27 changes: 12 additions & 15 deletions examples/mario/Maketile.coffee
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
palettes = ['castle', 'overworld', 'underground', 'underwater']

## Define individual palette rules and a default rule that builds them all.
for palette in palettes
exports[palette] = do (palette) -> ->
svgtiler "-f -s palette=#{palette} -O *_#{palette} mario.coffee door.tsv"

export default ->
for palette in palettes
exports[palette]()

## Example definition of just the "everything" rule.
export simple = ->
for palette in palettes
svgtiler "-f -s palette=#{palette} -O *_#{palette} mario.coffee door.tsv"

export singleRule = ->
## Example definition using just s single call to svgtiler()
export singleCall = ->
svgtiler '''
-f
( -s palette=castle -O *_castle mario.coffee door.tsv )
( -s palette=overworld -O *_overworld mario.coffee door.tsv )
( -s palette=underground -O *_underground mario.coffee door.tsv )
( -s palette=underwater -O *_underwater mario.coffee door.tsv )
'''

###
castle:
svgtiler -f -s palette=castle -O \*_castle mario.coffee door.tsv
overworld:
svgtiler -f -s palette=overworld -O \*_overworld mario.coffee door.tsv
underground:
svgtiler -f -s palette=underground -O \*_underground mario.coffee door.tsv
underwater:
svgtiler -f -s palette=underwater -O \*_underwater mario.coffee door.tsv
###
9 changes: 9 additions & 0 deletions examples/polyomino/Maketile.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
langs = ['cjsx', 'jsx', 'coffee']

for lang in langs
exports[lang] = do (lang) -> ->
svgtiler "-f outlines.#{lang} *.asc"

export default ->
for lang in langs
exports[lang]()
9 changes: 9 additions & 0 deletions examples/tetris/Maketile.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export txt = ->
svgtiler '-f -P --bg black NES_level7.txt example.asc'

export coffee = ->
svgtiler '-f -P NES_level7.coffee example.asc'

export default ->
txt()
coffee()
9 changes: 9 additions & 0 deletions examples/tilt/Maketile.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
langs = ['coffee', 'txt']

for lang in langs
exports[lang] = do (lang) -> ->
svgtiler "-f --tw 50 --th 50 tilt.#{lang} *.asc *.csv"

export default ->
for lang in langs
exports[lang]()
1 change: 1 addition & 0 deletions examples/witness/Maketile.args
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-f witness.coffee *.ssv *.asc
18 changes: 14 additions & 4 deletions src/svgtiler.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2385,7 +2385,7 @@ main = (args = process.argv[2..]) ->
showHelp = 'No filename arguments and no Maketile to run; showing --help'
maketile = null
ranMaketile = false
numFiles = 0
numFileArgs = 0
formats = []
settings = cloneSettings defaultSettings, true
inits = [] # array of objects to call doInit() on
Expand All @@ -2395,7 +2395,7 @@ main = (args = process.argv[2..]) ->
loop
## Automatically run Maketile if we're out of arguments
## and haven't processed any files yet.
if i >= args.length and not numFiles and not ranMaketile and
if i >= args.length and not numFileArgs and not ranMaketile and
(maketile ?= loadMaketile settings)?
ranMaketile = true
if maketile instanceof Args
Expand Down Expand Up @@ -2516,17 +2516,27 @@ main = (args = process.argv[2..]) ->
init.doInit() for init in inits
else
showHelp = false
numFileArgs++
## If argument is not a string (e.g. Mapping or Style),
## or is a string that corresponds to an existing filename.
## process it directly.
exists = typeof arg != 'string' or inputCache.has arg
unless exists
try
exists = fs.statSync arg
if exists
files = [arg]
## Otherwise, try matching as a glob pattern.
else if (files = glob arg).length
## Otherwise, check for a matching rule in the Maketile.
else
files = glob arg
maketile ?= loadMaketile settings
if maketile?.module.hasOwnProperty arg
maketile.module[arg]()
else
console.warn "No files or Maketile rules match '#{arg}'"
append = i+1 # where to append Args
for file in files
numFiles++
if typeof file == 'string'
cached = inputCache.has file
console.log '*', file, if cached then '(cached)' else ''
Expand Down

0 comments on commit 8b92a91

Please sign in to comment.