public virtual string GetOutputDirectory(string fileNameWithPath)
{
string outputDir;
string fileDirectory;
// Some files are given to us without a PATH but should should
// still be written to the output directory in the relative path of
// the output directory. The file directory is either the set of sub directories
// or just or the relative path recorded for the parent grammar. This means
// that when we write the tokens files, or the .java files for imported grammars
// taht we will write them in the correct place.
if (fileNameWithPath.LastIndexOfAny(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }) == -1)
{
// No path is included in the file name, so make the file
// directory the same as the parent grammar (which might sitll be just ""
// but when it is not, we will write the file in the correct place.
fileDirectory = ".";
}
else
{
fileDirectory = fileNameWithPath.Substring(0, fileNameWithPath.LastIndexOfAny(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }));
}
if (haveOutputDir)
{
// -o /tmp /var/lib/t.g4 => /tmp/T.java
// -o subdir/output /usr/lib/t.g4 => subdir/output/T.java
// -o . /usr/lib/t.g4 => ./T.java
if (fileDirectory != null &&
(Path.IsPathRooted(fileDirectory) ||
fileDirectory.StartsWith("~")))
{ // isAbsolute doesn't count this :(
// somebody set the dir, it takes precedence; write new file there
outputDir = outputDirectory;
}
else
{
// -o /tmp subdir/t.g4 => /tmp/subdir/t.g4
if (fileDirectory != null)
{
outputDir = Path.Combine(outputDirectory, fileDirectory);
}
else
{
outputDir = outputDirectory;
}
}
}
else
{
// they didn't specify a -o dir so just write to location
// where grammar is, absolute or relative, this will only happen
// with command line invocation as build tools will always
// supply an output directory.
outputDir = fileDirectory;
}
return outputDir;
}