#97 File is now Uri based

brian Tue 4 Jul 2006

I did some refactoring of the sys::File API to integrate it into the new Uri design. Files are now constructed and traversed solely through a Uri, not a Str. Common methods like name, path, ext are now just convenience methods on File.uri.

The File.make(Uri) method is still used to create a representation of a file on the local file system. However to deal with the blasted trailing slash issue, you must use a trailing slash in the Uri for a directory. If the file exists and you mix-up the trailing slash it fails fast with an IOErr. I decided that the best design is to stay faithful and consistent to how Uri normalization works.

So File.isDir() is now just a convenience on Uri.isDir() and is based on whether the Uri ends with a trailing slash (regardless of whether the file even exists yet). Because of this, I refactored File.createFile() and File.createDir() into a single method called File.create().

The File.join() method was replaced with File.plus(Uri) which uses operator overloading and normal Uri normalization - this is why it is so important to stay consistent with the trailing slash:

// before
f := File.make("base").join("dir", "file.txt")
// now
f := File.make(`base/`) + `dir/file.txt`

Note that everything is done with Uri, not Str. Also note that since we use Uri exclusively we won't ever worry about platform dependent separators like Windows.

As part of this rework, I now allow you to compile resource files into your pods and access them as sys::Files through the API. To include resource files in your pod, just put the files in a subdirectory called "res" as a peer to "fan", "test", "java". This is a temp solution until I get around to real build files. You can use the "-src" flag with fanc to include all the fan source files as resource files - they are flattened in the pod as "/src/<filename>.fan".

To access a resource file in a specific Pod you can use the Pod.files() method. For example to read a source file and pipe to stdout:

Sys.findPod("sys").files[`/src/Obj.fan`].in | Sys.out

I still need to do a bit more polish to handle the "file:" scheme and I want to do a "pod:" scheme. I'm also going to add a source() method to Type to get the Uri for the source file which when used in conjunction with Type.line() and Slot.line() will provide the infrastructure for fandoc source browsing.

brian Tue 4 Jul 2006

I decided what we have working with relative Uris is functional enough for the time being. I'm going to hold off on absolute Uris since their design hinges on having a working registry.

Login or Signup to reply.