Recursive directories with FileList

21 views
Skip to first unread message

Mikael Henriksson

unread,
Jan 12, 2010, 7:13:08 AM1/12/10
to phantom...@googlegroups.com
I have a tiny improvement for one the WrappedFileInfo class. It's not complete but it does do one more directory than it used to. I only needed this for an MVC project :)


public class WrappedFileInfo : WrappedFileSystemInfo {
        public WrappedFileInfo(string baseDir, string path, bool flatten) : base(baseDir, path, new FileInfo(path), flatten) {
        }
 
        public override void CopyToDirectory(string path) {
            if (!Directory.Exists(path)) {
                Directory.CreateDirectory(path);
            }
 
            if (Flatten) {
                var combinedPath = Path.Combine(path, Name);
                File.Copy(FullName, combinedPath, true);
            }
            else {
                var combinedPath = Path.Combine(path, PathWithoutBaseDirectory);
 
                var newPath = Path.GetDirectoryName(combinedPath);
                if (!Directory.Exists(newPath))
                {
                    Directory.CreateDirectory(newPath);
                }
 
                File.Copy(FullName, combinedPath, true);
            }
            
            //ensure all segments of "path" exist
            //all subdirectories in current file's path
 
            //baseDir -> "SubDirectory"
            //path -> SubDirectory/SubDirectory2/Foo.txt
            //FullPath....
            //pathWithoutBaseDir -> SubDirectory2/Foo.txt
 
        }
    }

Mikael Henriksson

unread,
Jan 12, 2010, 7:34:55 AM1/12/10
to phantom...@googlegroups.com
Also deleting directories in IOFunctions needs to change. There is a build in function for recursively deleting all children of a DirectoryInfo:

static void DeleteDirectory(string path) {
  var dirInfo = new DirectoryInfo(path);
  dirInfo.Delete(true);
  //foreach (var dir in dirInfo.GetDirectories()) {
  //    DeleteDirectory(dir.FullName);
  //}

  //foreach (var file in dirInfo.GetFiles()) {
  //    DeleteFile(file.FullName);
  //}

  //Directory.Delete(path);
}

Plus  as I mentioned below the top if statement needs to be changed to just get the path:
            if (!Directory.Exists(Path.GetDirectoryName(path)))
            {
Directory.CreateDirectory(Path.GetDirectoryName(path));
}

Jeremy Skinner

unread,
Jan 12, 2010, 7:36:43 AM1/12/10
to phantom...@googlegroups.com
I seem to remember that I tried this, but there were some issues due to timing that caused the next instruction to execute before the directory was deleted. I'll give it another try.

Jeremy


2010/1/12 Mikael Henriksson <mik...@zoolutions.se>

Mikael Henriksson

unread,
Jan 12, 2010, 7:45:01 AM1/12/10
to phantom...@googlegroups.com
I do get these errors occasionally but this should not be a problem in case of continuous integration. I have not had any problems deleting the files but there seems to be some issue with the copy functions. I'll see if there is something to do with the file system rights that can be taken care of. The build and artifacts folders I use I don't really care about. Maybe it's possible to use less locking. I know that it's mostly a timing issu. Sometimes I perform an operation through Phantom it seems like I have to wait a couple of minutes to be sure that I can delete / overwrite whatever I was doing. This must be some sort of standard that can be overridden :)

I'll see what I can dig up.

Jeremy Skinner

unread,
Jan 13, 2010, 4:32:13 AM1/13/10
to phantom...@googlegroups.com
I'd be great if you could create some tests that reproduce the problems that you're seeing and submit them via a pull request on github.

Thanks

Mikael Henriksson

unread,
Jan 13, 2010, 4:55:14 AM1/13/10
to phantom...@googlegroups.com
Will do Jeremy, I was at least able to create a proper patch with git. I am not a git-master yet but it's on my todo-list...
Do I have to clone you using github to do the pull request or is it enough if I clone to my machine directly?

Jeremy Skinner

unread,
Jan 13, 2010, 4:57:58 AM1/13/10
to phantom...@googlegroups.com
Fork the repository on github first and then clone your fork locally. You can then commit to your local clone and push up to your fork. I can then pull the changes in from your fork.

I wrote a guide on how to contribute patches which you might find useful: http://mvccontrib.github.com/MvcContrib/ The focus here is on the MvcContrib project, but all of the concepts apply to Phantom too.

Jeremy

2010/1/13 Mikael Henriksson <mik...@zoolutions.se>

Mikael Henriksson

unread,
Jan 13, 2010, 5:44:06 AM1/13/10
to phantom...@googlegroups.com
Thank you, great guide! Should be a breeze!

Mikael Henriksson

unread,
Jan 13, 2010, 3:14:06 PM1/13/10
to phantom...@googlegroups.com
It's harder than I thought to reproduce the problem. I can't get the function to fail within VS2008 but as soon as I run it from the console it fails with the following exception:

System.IO.DirectoryNotFoundException: Could not find a part of the path 'build/webui\Views/Web.config'.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite)
   at Phantom.Core.WrappedFileInfo.CopyToDirectory(String path)
   at build.$$Execute$closure$23$closure$25.Invoke(WrappedFileSystemInfo file) in D:\Projects\build.boo:line 82
   at Phantom.Core.Builtins.UtilityFunctions.ForEach[T](IEnumerable`1 source, Action`1 action)
   at build.$Execute$closure$23.Invoke() in D:\Projects\build.boo:line 85
   at Phantom.Core.Target.Execute()
   at Phantom.Core.ScriptModel.ExecuteTargets(String[] targetNames)
   at Phantom.Program.Execute(String[] args)

Running the build with the debug symbols I get:

System.IO.DirectoryNotFoundException: Could not find a part of the path 'build/webui\Views/Web.config'.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite)
   at Phantom.Core.WrappedFileInfo.CopyToDirectory(String path) in d:\OpenSource\Build\Phantom2\Phantom\src\Phantom.Core\WrappedFileInfo.cs:line 75
   at build.$$Execute$closure$23$closure$25.Invoke(WrappedFileSystemInfo file)
   at Phantom.Core.Builtins.UtilityFunctions.ForEach[T](IEnumerable`1 source, Action`1 action) in d:\OpenSource\Build\Phantom2\Phantom\src\Phantom.Core\Builtins\UtilityFunctions.cs:line 34
   at build.$Execute$closure$23.Invoke()
   at Phantom.Core.Target.Execute() in d:\OpenSource\Build\Phantom2\Phantom\src\Phantom.Core\Target.cs:line 81
   at Phantom.Core.ScriptModel.ExecuteTargets(String[] targetNames) in d:\OpenSource\Build\Phantom2\Phantom\src\Phantom.Core\ScriptModel.cs:line 73
   at Phantom.Program.Execute(String[] args) in d:\OpenSource\Build\Phantom2\Phantom\src\Phantom\Program.cs:line 57

I am pretty sure this is one of those reason like why people use IIS to debug websites instead of visual studio. Either visual studio is covering up my mistakes or the cmd prompt / console causes problems. Either way I am not sure I can reproduce the problem. For some reason the console seems to think that web.config is a folder. I assume this is also the reason why your recursive delete refused to work for me from the console.

I'll give it another shot tomorrow.

Jeremy Skinner

unread,
Feb 12, 2010, 4:08:44 PM2/12/10
to phantom...@googlegroups.com
I've finally got round to looking at your patch - sorry it took so long. It's now all merged into the master branch. In future, if you contribute any other patches it'd be helpful if you could preserve the existing whitespace settings (tabs, not spaces). It's difficult to diff two files if the editor thinks every line has changed :)

I've also added you to the contributions list in the acknowledgements section of the readme.

Jeremy

Mikael Henriksson

unread,
Feb 13, 2010, 7:09:14 AM2/13/10
to phantom...@googlegroups.com
Thank you Jeremy and sorry about tabs thing! I did "git apply --ignore-whitespace" and I set the tab settings to preserve tabs in Visual Studio. I have been meaning to do that for a couple of months but never got around to it. I really hate those space-replaced tabs and whitespace problems. It is almost as infuriating as a tree conflict in svn :)

Did I miss something? line ending style maybe? Should I set that to something specific?
Reply all
Reply to author
Forward
0 new messages