R.I.P. FileDirectory

30 April 2013

At heart, SmallCMS1, this blog's content management system, walks directories and processes files. Distilled, this code, originally written interactively in a workspace, is what SmallCMS1 does:

| startDir fileWalker dirWalker |

startDir := FileDirectory on: '/the/content/directory'.

fileWalker := [ :fileNames |
  fileNames do: [ :fname |
    Transcript show: '...', fname; cr ]].

dirWalker := [ :dir |
  Transcript show: dir localName; cr.
  fileWalker value: dir fileNames.
  dir directoryNames do: [ :dname |
    | subDir |
    subDir := dir directoryNamed: dname.
    dirWalker value: subDir ]].

dirWalker value: startDir.

As of Pharo 2.0, FileDirectory has been replaced by FileSystem. The above code still works using FileSystem, with minor changes:

| startDir fileWalker dirWalker |

startDir := FileSystem / 'the' / 'content' / 'directory'.

fileWalker := [ :fileNames |
  fileNames do: [ :fname |
    Transcript show: '...', fname; cr ]].

dirWalker := [ :dir |
  Transcript show: dir basename; cr.
  fileWalker value: dir fileNames.
  dir directoryNames do: [ :dname |
    | subDir |
    subDir := dir / dname.
    dirWalker value: subDir ]].

dirWalker value: startDir.

FileSystem also implements the visitor pattern, which performs enumeration and traversal on behalf of application code:

| fs |
fs := FileSystem / 'the' / 'content' / 'directory'.
CollectVisitor preorder: fs collect: [ :e |
  e isFile ifTrue: [ Transcript show: '...' ].
  Transcript show: e basename; cr ].