Exemple #1
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Add a file to the list of files to copy. The filename is built from the assembly
        /// name and the new extension.
        /// </summary>
        /// <param name="srcName">Path and name of file</param>
        /// <param name="newExtension">New extension</param>
        /// ------------------------------------------------------------------------------------
        private void AddFile(string srcName, string newExtension)
        {
            var newSrcInfo = new FileInfo(Path.ChangeExtension(srcName, newExtension));

            if (newSrcInfo.Exists)
            {
                string dstFile = Path.Combine(m_dstBaseInfo.FullName,
                                              Path.GetFileName(newSrcInfo.FullName));
                string newSrcInfoName = IsUnix ? newSrcInfo.FullName : newSrcInfo.FullName.ToLower();
                string dstFileName    = IsUnix ? dstFile : dstFile.ToLower();
                if (newSrcInfoName != dstFileName)
                {
                    FileCopyMap[dstFile] =
                        new FileDateInfo(newSrcInfoName, newSrcInfo.LastWriteTime);
                }
                else
                {
                    Log(Level.Debug, "Reference file {0} has same source and destination", newSrcInfo.FullName);
                }
            }
            else
            {
                Log(Level.Debug, "Reference file {0} does not exist", newSrcInfo.FullName);
            }
        }
Exemple #2
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Resolve references of a reference
        /// </summary>
        /// <param name="referencePath">Path to the referenced assembly</param>
        /// ------------------------------------------------------------------------------------
        private void ResolveReferences(string referencePath)
        {
            string      referenceName = Path.GetFileName(referencePath);
            XmlAssembly assembly      = m_cache[referenceName];

            if (assembly != null)
            {                   // we have cached references for this assembly
                foreach (Reference reference in assembly.References)
                {
                    string srcFilePath = Path.Combine(ToDirectory.FullName, reference.Name);

                    string dstFilePath = Path.Combine(m_dstBaseInfo.FullName,
                                                      Path.GetFileName(reference.Name));
                    if ((IsUnix && srcFilePath != dstFilePath) ||
                        (!IsUnix && srcFilePath.ToLower() != dstFilePath.ToLower()))
                    {
                        FileCopyMap[dstFilePath] =
                            new FileDateInfo(IsUnix ? srcFilePath : srcFilePath.ToLower(), DateTime.MinValue);
                        AddFile(srcFilePath, "xml");
                        AddFile(srcFilePath, "pdb");
                        ResolveReferences(srcFilePath);
                    }
                }
            }
        }
Exemple #3
0
        /*
         * Lists the files and their dates
         */
        public void ReadFiles()
        {
            DateTime dateTime = DateTime.MinValue;

            _dicFiles.Clear();
            _errorFiles.Clear();

            FileDateInfo fileDateInfo = new FileDateInfo();

            string[] filters = Source.Filter.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string fltr in filters)
            {
                string filter = fltr.Trim();
                Debug.Print($"-------------\r\nFilter: {filter}\r\n---------------");
                string[] fileEntries = Directory.GetFiles(Source.Path, filter);
                foreach (string file in fileEntries)
                {
                    if (!_dicFiles.ContainsKey(file) && !_errorFiles.Contains(file))
                    {
                        if (fileDateInfo.GetFileDate(file, out dateTime))
                        {
                            _dicFiles.Add(file, dateTime);
                        }
                        else
                        {
                            _errorFiles.Add(file);
                        }
                    }
                    //Debug.Print($"{Path.GetFileName(file)} - {_dicFiles[file].ToShortDateString()}");
                }
            }
            Debug.Print($"Found {_dicFiles.Count()} files");
            if (_errorFiles.Count() > 0)
            {
                Debug.Print($"ERRORS: Couldn't get dates from {_errorFiles.Count()} files:");
                foreach (string file in _errorFiles)
                {
                    Debug.Print($"\t{Path.GetFileName(file)}");
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// Executes the Copy task.
        /// </summary>
        /// <exception cref="BuildException">A file that has to be copied does not exist or could not be copied.</exception>
        protected override void ExecuteTask()
        {
            // ensure base directory is set, even if fileset was not initialized
            // from XML
            if (CopyFileSet.BaseDirectory == null) {
                CopyFileSet.BaseDirectory = new DirectoryInfo(Project.BaseDirectory);
            }

            // Clear previous copied files
            _fileCopyMap = new Hashtable();

            // copy a single file.
            if (SourceFile != null) {
                if (SourceFile.Exists) {
                    FileInfo dstInfo = null;
                    if (ToFile != null) {
                        dstInfo = ToFile;
                    } else {
                        string dstFilePath = Path.Combine(ToDirectory.FullName,
                            SourceFile.Name);
                        dstInfo = new FileInfo(dstFilePath);
                    }

                    // do the outdated check
                    bool outdated = (!dstInfo.Exists) || (SourceFile.LastWriteTime > dstInfo.LastWriteTime);

                    if (Overwrite || outdated) {
                        // add to a copy map of absolute verified paths
                        FileCopyMap.Add(dstInfo.FullName, new FileDateInfo(SourceFile.FullName, SourceFile.LastWriteTime));
                        if (dstInfo.Exists && dstInfo.Attributes != FileAttributes.Normal) {
                            File.SetAttributes(dstInfo.FullName, FileAttributes.Normal);
                        }
                    }
                } else {
                    throw CreateSourceFileNotFoundException (SourceFile.FullName);
                }
            } else { // copy file set contents.
                // get the complete path of the base directory of the fileset, ie, c:\work\nant\src
                DirectoryInfo srcBaseInfo = CopyFileSet.BaseDirectory;

                // if source file not specified use fileset
                foreach (string pathname in CopyFileSet.FileNames) {
                    FileInfo srcInfo = new FileInfo(pathname);
                    if (srcInfo.Exists) {
                        // will holds the full path to the destination file
                        string dstFilePath;

                        if (Flatten) {
                            dstFilePath = Path.Combine(ToDirectory.FullName,
                                srcInfo.Name);
                        } else {
                            // Gets the relative path and file info from the full
                            // source filepath
                            // pathname = C:\f2\f3\file1, srcBaseInfo=C:\f2, then
                            // dstRelFilePath=f3\file1
                            string dstRelFilePath = "";
                            if (srcInfo.FullName.IndexOf(srcBaseInfo.FullName, 0) != -1) {
                                dstRelFilePath = srcInfo.FullName.Substring(
                                    srcBaseInfo.FullName.Length);
                            } else {
                                dstRelFilePath = srcInfo.Name;
                            }

                            if (dstRelFilePath[0] == Path.DirectorySeparatorChar) {
                                dstRelFilePath = dstRelFilePath.Substring(1);
                            }

                            // The full filepath to copy to.
                            dstFilePath = Path.Combine(ToDirectory.FullName,
                                dstRelFilePath);
                        }

                        // do the outdated check
                        FileInfo dstInfo = new FileInfo(dstFilePath);
                        bool outdated = (!dstInfo.Exists) || (srcInfo.LastWriteTime > dstInfo.LastWriteTime);

                        if (Overwrite || outdated) {
                            // construct FileDateInfo for current file
                            FileDateInfo newFile = new FileDateInfo(srcInfo.FullName,
                                srcInfo.LastWriteTime);
                            // if multiple source files are selected to be copied
                            // to the same destination file, then only the last
                            // updated source should actually be copied
                            FileDateInfo oldFile = (FileDateInfo) FileCopyMap[dstInfo.FullName];
                            if (oldFile != null) {
                                // if current file was updated after scheduled file,
                                // then replace it
                                if (newFile.LastWriteTime > oldFile.LastWriteTime) {
                                    FileCopyMap[dstInfo.FullName] = newFile;
                                }
                            } else {
                                FileCopyMap.Add(dstInfo.FullName, newFile);
                                if (dstInfo.Exists && dstInfo.Attributes != FileAttributes.Normal) {
                                    File.SetAttributes(dstInfo.FullName, FileAttributes.Normal);
                                }
                            }
                        }
                    } else {
                        throw CreateSourceFileNotFoundException (srcInfo.FullName);
                    }
                }

                if (IncludeEmptyDirs && !Flatten) {
                    // create any specified directories that weren't created during the copy (ie: empty directories)
                    foreach (string pathname in CopyFileSet.DirectoryNames) {
                        DirectoryInfo srcInfo = new DirectoryInfo(pathname);
                        // skip directory if not relative to base dir of fileset
                        if (srcInfo.FullName.IndexOf(srcBaseInfo.FullName) == -1) {
                            continue;
                        }
                        string dstRelPath = srcInfo.FullName.Substring(srcBaseInfo.FullName.Length);
                        if (dstRelPath.Length > 0 && dstRelPath[0] == Path.DirectorySeparatorChar) {
                            dstRelPath = dstRelPath.Substring(1);
                        }

                        // The full filepath to copy to.
                        string destinationDirectory = Path.Combine(ToDirectory.FullName, dstRelPath);
                        if (!Directory.Exists(destinationDirectory)) {
                            try {
                                Directory.CreateDirectory(destinationDirectory);
                            } catch (Exception ex) {
                                throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                                "Failed to create directory '{0}'.", destinationDirectory ),
                                 Location, ex);
                            }
                            Log(Level.Verbose, "Created directory '{0}'.", destinationDirectory);
                        }
                    }
                }
            }

            // do all the actual copy operations now
            DoFileOperations();
        }
Exemple #5
0
        /// <summary>
        /// Executes the Copy task.
        /// </summary>
        /// <exception cref="BuildException">A file that has to be copied does not exist or could not be copied.</exception>
        protected override void ExecuteTask()
        {
            // ensure base directory is set, even if fileset was not initialized
            // from XML
            if (CopyFileSet.BaseDirectory == null)
            {
                CopyFileSet.BaseDirectory = new DirectoryInfo(Project.BaseDirectory);
            }

            // Clear previous copied files
            _fileCopyMap.Clear();

            // if the source file is specified, check to see whether it is a file or directory before proceeding
            if (SourceFile != null)
            {
                // copy a single file.
                if (SourceFile.Exists)
                {
                    FileInfo dstInfo = null;
                    if (ToFile != null)
                    {
                        dstInfo = ToFile;
                    }
                    else
                    {
                        string dstFilePath = Path.Combine(ToDirectory.FullName,
                                                          SourceFile.Name);
                        dstInfo = new FileInfo(dstFilePath);
                    }

                    // do the outdated check
                    bool outdated = (!dstInfo.Exists) || (SourceFile.LastWriteTime > dstInfo.LastWriteTime);

                    if (Overwrite || outdated)
                    {
                        // add to a copy map of absolute verified paths
                        FileCopyMap.Add(dstInfo.FullName, new FileDateInfo(SourceFile.FullName, SourceFile.LastWriteTime));
                        _fileCount++;

                        if (dstInfo.Exists && dstInfo.Attributes != FileAttributes.Normal)
                        {
                            File.SetAttributes(dstInfo.FullName, FileAttributes.Normal);
                        }
                    }
                }
                // If SourceFile exists as a directory, proceed with moving the specified directory
                else if (!SourceFile.Exists && Directory.Exists(SourceFile.FullName))
                {
                    // Stage the directory names
                    string sourceDirName = SourceFile.FullName;
                    string destDirName;

                    // If ToFile was specified, make sure the specified filename does not exist
                    // as a file or a directory.
                    if (ToFile != null)
                    {
                        if (ToFile.Exists)
                        {
                            throw new BuildException(String.Format(CultureInfo.InvariantCulture,
                                                                   "Cannot move directory '{0}' to an existing file '{1}'",
                                                                   SourceFile.FullName, ToFile.FullName), Location);
                        }
                        if (Directory.Exists(ToFile.FullName))
                        {
                            throw new BuildException(String.Format(CultureInfo.InvariantCulture,
                                                                   "Cannot move directory '{0}' to an existing directory '{1}'",
                                                                   SourceFile.FullName, ToFile.FullName), Location);
                        }
                        destDirName = ToFile.FullName;
                    }
                    // If ToDirectory was specified, make sure the specified directory does not
                    // exist.
                    else if (ToDirectory != null)
                    {
                        if (ToDirectory.Exists)
                        {
                            throw new BuildException(String.Format(CultureInfo.InvariantCulture,
                                                                   "Cannot move directory '{0}' to an existing directory '{1}'",
                                                                   SourceFile.FullName, ToDirectory.FullName), Location);
                        }
                        destDirName = ToDirectory.FullName;
                    }
                    // Else, throw an exception
                    else
                    {
                        throw new BuildException("Target directory name not specified",
                                                 Location);
                    }
                    FileCopyMap.Add(destDirName, new FileDateInfo(sourceDirName, SourceFile.LastWriteTime, true));
                    _dirCount++;
                }
                else
                {
                    throw CreateSourceFileNotFoundException(SourceFile.FullName);
                }
            }

            // copy file set contents.
            else
            {
                // get the complete path of the base directory of the fileset, ie, c:\work\nant\src
                DirectoryInfo srcBaseInfo = CopyFileSet.BaseDirectory;

                // Check to see if the file operation is a straight pass through (ie: no file or
                // directory modifications) before proceeding.
                bool completeDir = true;

                // completeDir criteria
                bool[] dirCheck = new bool[8];
                dirCheck[0] = CopyFileSet.IsEverythingIncluded;
                dirCheck[1] = !Flatten;
                dirCheck[2] = IncludeEmptyDirs || !CopyFileSet.HasEmptyDirectories;
                dirCheck[3] = FilterChain.IsNullOrEmpty(Filters);
                dirCheck[4] = _inputEncoding == null;
                dirCheck[5] = _outputEncoding == null;
                dirCheck[6] = srcBaseInfo != null && srcBaseInfo.Exists;
                dirCheck[7] = !ToDirectory.Exists ||
                              srcBaseInfo.FullName.Equals(ToDirectory.FullName,
                                                          StringComparison.InvariantCultureIgnoreCase);

                for (int b = 0; b < dirCheck.Length; b++)
                {
                    completeDir &= dirCheck[b];
                }

                if (completeDir)
                {
                    FileCopyMap.Add(ToDirectory.FullName,
                                    new FileDateInfo(srcBaseInfo.FullName, srcBaseInfo.LastWriteTime, true));
                    _dirCount++;
                }
                else
                {
                    // if source file not specified use fileset
                    foreach (string pathname in CopyFileSet.FileNames)
                    {
                        FileInfo srcInfo = new FileInfo(pathname);
                        if (srcInfo.Exists)
                        {
                            // will holds the full path to the destination file
                            string dstFilePath;

                            if (Flatten)
                            {
                                dstFilePath = Path.Combine(ToDirectory.FullName,
                                                           srcInfo.Name);
                            }
                            else
                            {
                                // Gets the relative path and file info from the full
                                // source filepath
                                // pathname = C:\f2\f3\file1, srcBaseInfo=C:\f2, then
                                // dstRelFilePath=f3\file1
                                string dstRelFilePath = "";
                                if (srcInfo.FullName.IndexOf(srcBaseInfo.FullName, 0) != -1)
                                {
                                    dstRelFilePath = srcInfo.FullName.Substring(
                                        srcBaseInfo.FullName.Length);
                                }
                                else
                                {
                                    dstRelFilePath = srcInfo.Name;
                                }

                                if (dstRelFilePath[0] == Path.DirectorySeparatorChar)
                                {
                                    dstRelFilePath = dstRelFilePath.Substring(1);
                                }

                                // The full filepath to copy to.
                                dstFilePath = Path.Combine(ToDirectory.FullName,
                                                           dstRelFilePath);
                            }

                            // do the outdated check
                            FileInfo dstInfo  = new FileInfo(dstFilePath);
                            bool     outdated = (!dstInfo.Exists) || (srcInfo.LastWriteTime > dstInfo.LastWriteTime);

                            if (Overwrite || outdated)
                            {
                                // construct FileDateInfo for current file
                                FileDateInfo newFile = new FileDateInfo(srcInfo.FullName,
                                                                        srcInfo.LastWriteTime);
                                // if multiple source files are selected to be copied
                                // to the same destination file, then only the last
                                // updated source should actually be copied
                                FileDateInfo oldFile = (FileDateInfo)FileCopyMap[dstInfo.FullName];
                                if (oldFile != null)
                                {
                                    // if current file was updated after scheduled file,
                                    // then replace it
                                    if (newFile.LastWriteTime > oldFile.LastWriteTime)
                                    {
                                        FileCopyMap[dstInfo.FullName] = newFile;
                                    }
                                }
                                else
                                {
                                    FileCopyMap.Add(dstInfo.FullName, newFile);
                                    _fileCount++;

                                    if (dstInfo.Exists && dstInfo.Attributes != FileAttributes.Normal)
                                    {
                                        File.SetAttributes(dstInfo.FullName, FileAttributes.Normal);
                                    }
                                }
                            }
                        }
                        else
                        {
                            throw CreateSourceFileNotFoundException(srcInfo.FullName);
                        }
                    }

                    if (IncludeEmptyDirs && !Flatten)
                    {
                        // create any specified directories that weren't created during the copy (ie: empty directories)
                        foreach (string pathname in CopyFileSet.DirectoryNames)
                        {
                            DirectoryInfo srcInfo = new DirectoryInfo(pathname);
                            // skip directory if not relative to base dir of fileset
                            if (srcInfo.FullName.IndexOf(srcBaseInfo.FullName) == -1)
                            {
                                continue;
                            }
                            string dstRelPath = srcInfo.FullName.Substring(srcBaseInfo.FullName.Length);
                            if (dstRelPath.Length > 0 && dstRelPath[0] == Path.DirectorySeparatorChar)
                            {
                                dstRelPath = dstRelPath.Substring(1);
                            }

                            // The full filepath to copy to.
                            string destinationDirectory = Path.Combine(ToDirectory.FullName, dstRelPath);
                            if (!Directory.Exists(destinationDirectory))
                            {
                                try {
                                    Directory.CreateDirectory(destinationDirectory);
                                } catch (Exception ex) {
                                    throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                                                                           "Failed to create directory '{0}'.", destinationDirectory),
                                                             Location, ex);
                                }
                                Log(Level.Verbose, "Created directory '{0}'.", destinationDirectory);
                            }
                        }
                    }
                }
            }

            // do all the actual copy operations now
            DoFileOperations();
        }
Exemple #6
0
        /// <summary>
        /// Executes the Copy task.
        /// </summary>
        /// <exception cref="BuildException">A file that has to be copied does not exist or could not be copied.</exception>
        protected override void ExecuteTask()
        {
            // ensure base directory is set, even if fileset was not initialized
            // from XML
            if (CopyFileSet.BaseDirectory == null)
            {
                CopyFileSet.BaseDirectory = new DirectoryInfo(Project.BaseDirectory);
            }

            // Clear previous copied files
            _fileCopyMap = new Hashtable();

            // copy a single file.
            if (SourceFile != null)
            {
                if (SourceFile.Exists)
                {
                    FileInfo dstInfo = null;
                    if (ToFile != null)
                    {
                        dstInfo = ToFile;
                    }
                    else
                    {
                        string dstFilePath = Path.Combine(ToDirectory.FullName,
                                                          SourceFile.Name);
                        dstInfo = new FileInfo(dstFilePath);
                    }

                    // do the outdated check
                    bool outdated = (!dstInfo.Exists) || (SourceFile.LastWriteTime > dstInfo.LastWriteTime);

                    if (Overwrite || outdated)
                    {
                        // add to a copy map of absolute verified paths
                        FileCopyMap.Add(dstInfo.FullName, new FileDateInfo(SourceFile.FullName, SourceFile.LastWriteTime));
                        if (dstInfo.Exists && dstInfo.Attributes != FileAttributes.Normal)
                        {
                            File.SetAttributes(dstInfo.FullName, FileAttributes.Normal);
                        }
                    }
                }
                else
                {
                    throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                                                           "Could not find file '{0}' to copy.", SourceFile.FullName),
                                             Location);
                }
            }
            else     // copy file set contents.
            // get the complete path of the base directory of the fileset, ie, c:\work\nant\src
            {
                DirectoryInfo srcBaseInfo = CopyFileSet.BaseDirectory;

                // if source file not specified use fileset
                foreach (string pathname in CopyFileSet.FileNames)
                {
                    FileInfo srcInfo = new FileInfo(pathname);
                    if (srcInfo.Exists)
                    {
                        // will holds the full path to the destination file
                        string dstFilePath;

                        if (Flatten)
                        {
                            dstFilePath = Path.Combine(ToDirectory.FullName,
                                                       srcInfo.Name);
                        }
                        else
                        {
                            // Gets the relative path and file info from the full
                            // source filepath
                            // pathname = C:\f2\f3\file1, srcBaseInfo=C:\f2, then
                            // dstRelFilePath=f3\file1
                            string dstRelFilePath = "";
                            if (srcInfo.FullName.IndexOf(srcBaseInfo.FullName, 0) != -1)
                            {
                                dstRelFilePath = srcInfo.FullName.Substring(
                                    srcBaseInfo.FullName.Length);
                            }
                            else
                            {
                                dstRelFilePath = srcInfo.Name;
                            }

                            if (dstRelFilePath[0] == Path.DirectorySeparatorChar)
                            {
                                dstRelFilePath = dstRelFilePath.Substring(1);
                            }

                            // The full filepath to copy to.
                            dstFilePath = Path.Combine(ToDirectory.FullName,
                                                       dstRelFilePath);
                        }

                        // do the outdated check
                        FileInfo dstInfo  = new FileInfo(dstFilePath);
                        bool     outdated = (!dstInfo.Exists) || (srcInfo.LastWriteTime > dstInfo.LastWriteTime);

                        if (Overwrite || outdated)
                        {
                            // construct FileDateInfo for current file
                            FileDateInfo newFile = new FileDateInfo(srcInfo.FullName,
                                                                    srcInfo.LastWriteTime);
                            // if multiple source files are selected to be copied
                            // to the same destination file, then only the last
                            // updated source should actually be copied
                            FileDateInfo oldFile = (FileDateInfo)FileCopyMap[dstInfo.FullName];
                            if (oldFile != null)
                            {
                                // if current file was updated after scheduled file,
                                // then replace it
                                if (newFile.LastWriteTime > oldFile.LastWriteTime)
                                {
                                    FileCopyMap[dstInfo.FullName] = newFile;
                                }
                            }
                            else
                            {
                                FileCopyMap.Add(dstInfo.FullName, newFile);
                                if (dstInfo.Exists && dstInfo.Attributes != FileAttributes.Normal)
                                {
                                    File.SetAttributes(dstInfo.FullName, FileAttributes.Normal);
                                }
                            }
                        }
                    }
                    else
                    {
                        throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                                                               "Could not find file '{0}' to copy.", srcInfo.FullName),
                                                 Location);
                    }
                }

                if (IncludeEmptyDirs && !Flatten)
                {
                    // create any specified directories that weren't created during the copy (ie: empty directories)
                    foreach (string pathname in CopyFileSet.DirectoryNames)
                    {
                        DirectoryInfo srcInfo = new DirectoryInfo(pathname);
                        // skip directory if not relative to base dir of fileset
                        if (srcInfo.FullName.IndexOf(srcBaseInfo.FullName) == -1)
                        {
                            continue;
                        }
                        string dstRelPath = srcInfo.FullName.Substring(srcBaseInfo.FullName.Length);
                        if (dstRelPath.Length > 0 && dstRelPath[0] == Path.DirectorySeparatorChar)
                        {
                            dstRelPath = dstRelPath.Substring(1);
                        }

                        // The full filepath to copy to.
                        string destinationDirectory = Path.Combine(ToDirectory.FullName, dstRelPath);
                        if (!Directory.Exists(destinationDirectory))
                        {
                            try {
                                Directory.CreateDirectory(destinationDirectory);
                            } catch (Exception ex) {
                                throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                                                                       "Failed to create directory '{0}'.", destinationDirectory),
                                                         Location, ex);
                            }
                            Log(Level.Verbose, "Created directory '{0}'.", destinationDirectory);
                        }
                    }
                }
            }

            // do all the actual copy operations now
            DoFileOperations();
        }
Exemple #7
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Executes the Copy task.
        /// </summary>
        /// <exception cref="BuildException">A file that has to be copied does not exist or
        /// could not be copied.</exception>
        /// ------------------------------------------------------------------------------------
        protected override void ExecuteTask()
        {
            if (SourceFile != null)
            {
                // Copy single file.
                base.ExecuteTask();
            }
            else
            {
                // Copy file set contents.

                if (m_fResolveAssemblies)
                {
                    LoadReferences();
                }

                // get the complete path of the base directory of the fileset, ie, c:/work/nant/src
                m_dstBaseInfo = ToDirectory;

                XmlAssembly xmlAssembly;
                if (Append)
                {
                    xmlAssembly = m_cache[Project.ProjectName];
                }
                else
                {
                    xmlAssembly = new XmlAssembly(Project.ProjectName);
                    m_cache.Add(xmlAssembly);
                }

                // if source file is not specified use fileset
                foreach (var pathname in CopyFileSet.FileNames)
                {
                    var srcInfo = new FileInfo(pathname);
                    Log(Level.Debug, "Checking file {0}", pathname);

                    if (srcInfo.Exists || m_force)
                    {
                        // The full filepath to copy to.
                        var dstFilePath = Path.Combine(m_dstBaseInfo.FullName, Path.GetFileName(srcInfo.FullName));

                        // do the outdated check
                        var dstInfo = new FileInfo(dstFilePath);
                        Log(Level.Debug, "Comparing with destination file {0}", dstFilePath);
                        var fOutdated = (!dstInfo.Exists) || (srcInfo.LastWriteTime > dstInfo.LastWriteTime);

                        if (Overwrite || fOutdated || m_force)
                        {
                            Log(Level.Debug, "Need to process: {0}", Overwrite ? "Overwrite is true" :
                                fOutdated ? "src is newer than dst" : "force is true");
                            if (!IsInGAC(srcInfo))
                            {
                                FileCopyMap[dstFilePath] =
                                    new FileDateInfo(IsUnix ? srcInfo.FullName : srcInfo.FullName.ToLower(), srcInfo.LastWriteTime);
                                if (dstInfo.Exists && dstInfo.Attributes != FileAttributes.Normal)
                                {
                                    File.SetAttributes(dstInfo.FullName, FileAttributes.Normal);
                                }
                                AddAssemblyAndRelatedFiles(xmlAssembly, srcInfo.FullName);
                            }
                            else
                            {
                                Log(Level.Verbose, "Reference file {0} skipped", srcInfo.FullName);
                            }
                        }
                        else
                        {
                            if (!IsInGAC(srcInfo))
                            {
                                AddAssemblyAndRelatedFiles(xmlAssembly, srcInfo.FullName);
                            }
                        }
                    }
                    else
                    {
                        if (FailOnError)
                        {
                            var msg = String.Format(CultureInfo.InvariantCulture,
                                                    "Could not find file {0} to copy.", srcInfo.FullName);
                            throw new BuildException(msg, Location);
                        }
                        Log(Level.Error, "Reference file {0} does not exist (ignored)", pathname);
                    }
                }

                CreateDirectories(CopyFileSet.BaseDirectory);

                try
                {
                    // do all the actual copy operations now...
                    DoFileOperations();
                }
                catch
                {
                    if (FailOnError)
                    {
                        throw;
                    }
                }
                finally
                {
                    // save the references of this project in our settings file
                    if (m_fResolveAssemblies)
                    {
                        SaveReferences();
                    }
                }
            }
        }
		public virtual void Execute()
		{
			fileCopyMap = new Hashtable();

			if (FromSingleFile != null)
			{
				if (FromSingleFile.Exists)
				{
					FileInfo dstInfo;

					if (ToSingleFile != null)
						dstInfo = ToSingleFile;
					else
					{
						var dstFilePath = Path.Combine(ToDirectory.FullName, FromSingleFile.Name);
						dstInfo = new FileInfo(dstFilePath);
					}

					var outdated = (!dstInfo.Exists) || (FromSingleFile.LastWriteTime > dstInfo.LastWriteTime);

					if (overwrite || outdated)
					{
						fileCopyMap.Add(dstInfo.FullName, new FileDateInfo(FromSingleFile.FullName, FromSingleFile.LastWriteTime));

						if (dstInfo.Exists && dstInfo.Attributes != FileAttributes.Normal)
							File.SetAttributes(dstInfo.FullName, FileAttributes.Normal);
					}
				}
				else
					throw new Exception(FromSingleFile.FullName);
			}
			else
			{
				var srcBaseInfo = new DirectoryInfo(FileSet.BaseDirectory);

				foreach (var pathname in FileSet.IncludedFiles)
				{
					var srcInfo = new FileInfo(pathname);

					if (!srcInfo.Exists)
						continue;

					string dstFilePath;

					if (flatten)
						dstFilePath = Path.Combine(ToDirectory.FullName, srcInfo.Name);
					else
					{
						var dstRelFilePath =
							(srcInfo.FullName.IndexOf(srcBaseInfo.FullName, 0, StringComparison.InvariantCultureIgnoreCase) != -1)
								? srcInfo.FullName.Substring(srcBaseInfo.FullName.Length)
								: srcInfo.Name;

						if (dstRelFilePath[0] == Path.DirectorySeparatorChar)
							dstRelFilePath = dstRelFilePath.Substring(1);

						dstFilePath = Path.Combine(ToDirectory.FullName, dstRelFilePath);
					}

					var dstInfo = new FileInfo(dstFilePath);
					var outdated = (!dstInfo.Exists) || (srcInfo.LastWriteTime > dstInfo.LastWriteTime);

					if (overwrite || outdated)
					{
						var newFile = new FileDateInfo(srcInfo.FullName, srcInfo.LastWriteTime);
						var oldFile = (FileDateInfo) fileCopyMap[dstInfo.FullName];

						if (oldFile != null && newFile.LastWriteTime > oldFile.LastWriteTime)
							fileCopyMap[dstInfo.FullName] = newFile;
						else
						{
							try
							{
								fileCopyMap.Add(dstInfo.FullName, newFile);
							}
							catch (ArgumentException argumentException)
							{
								if (!argumentException.Message.ToLower().Contains("item has already been added"))
									throw;
							}

							if (dstInfo.Exists && dstInfo.Attributes != FileAttributes.Normal)
								File.SetAttributes(dstInfo.FullName, FileAttributes.Normal);
						}
					}
					else
						throw new Exception(srcInfo.FullName);
				}

				if (includeEmpty && !flatten)
				{
					foreach (var pathname in FileSet.IncludedDirectories)
					{
						var srcInfo = new DirectoryInfo(pathname);

						if (srcInfo.FullName.IndexOf(srcBaseInfo.FullName) == -1)
							continue;

						var dstRelPath = srcInfo.FullName.Substring(srcBaseInfo.FullName.Length);

						if (dstRelPath.Length > 0 && dstRelPath[0] == Path.DirectorySeparatorChar)
							dstRelPath = dstRelPath.Substring(1);

						var destinationDirectory = Path.Combine(ToDirectory.FullName, dstRelPath);

						if (Directory.Exists(destinationDirectory))
							continue;

						try
						{
							Directory.CreateDirectory(destinationDirectory);
						}
						catch (Exception)
						{
							throw new Exception(string.Format("Failed to create directory '{0}'.", destinationDirectory));
						}
					}
				}
			}

			DoFileOperations();
		}
Exemple #9
0
        /// <summary>
        /// Executes the Copy task.
        /// </summary>
        /// <exception cref="BuildException">A file that has to be copied does not exist or could not be copied.</exception>
        protected override void ExecuteTask() {
            // ensure base directory is set, even if fileset was not initialized
            // from XML
            if (CopyFileSet.BaseDirectory == null) {
                CopyFileSet.BaseDirectory = new DirectoryInfo(Project.BaseDirectory);
            }

            // Clear previous copied files
            _fileCopyMap.Clear();

            // if the source file is specified, check to see whether it is a file or directory before proceeding
            if (SourceFile != null)
            {
                // copy a single file.
                if (SourceFile.Exists)
                {
                    FileInfo dstInfo = null;
                    if (ToFile != null) 
                    {
                        dstInfo = ToFile;
                    } 
                    else 
                    {
                        string dstFilePath = Path.Combine(ToDirectory.FullName, 
                            SourceFile.Name);
                        dstInfo = new FileInfo(dstFilePath);
                    }

                    // do the outdated check
                    bool outdated = (!dstInfo.Exists) || (SourceFile.LastWriteTime > dstInfo.LastWriteTime);

                    if (Overwrite || outdated) 
                    {
                        // add to a copy map of absolute verified paths
                        FileCopyMap.Add(dstInfo.FullName, new FileDateInfo(SourceFile.FullName, SourceFile.LastWriteTime));
                        _fileCount++;

                        if (dstInfo.Exists && dstInfo.Attributes != FileAttributes.Normal)
                        {
                            File.SetAttributes(dstInfo.FullName, FileAttributes.Normal);
                        }
                    }
                }
                // If SourceFile exists as a directory, proceed with moving the specified directory
                else if (!SourceFile.Exists && Directory.Exists(SourceFile.FullName))
                {
                    // Stage the directory names
                    string sourceDirName = SourceFile.FullName;
                    string destDirName;
                    
                    // If ToFile was specified, make sure the specified filename does not exist
                    // as a file or a directory.
                    if (ToFile != null)
                    {
                        if (ToFile.Exists)
                        {
                            throw new BuildException(String.Format(CultureInfo.InvariantCulture,
                                "Cannot move directory '{0}' to an existing file '{1}'", 
                                SourceFile.FullName, ToFile.FullName), Location);
                        }
                        if (Directory.Exists(ToFile.FullName))
                        {
                            throw new BuildException(String.Format(CultureInfo.InvariantCulture,
                                "Cannot move directory '{0}' to an existing directory '{1}'",
                                SourceFile.FullName, ToFile.FullName), Location);
                        }
                        destDirName = ToFile.FullName;
                    }
                    // If ToDirectory was specified, make sure the specified directory does not
                    // exist.
                    else if (ToDirectory != null)
                    {
                        if (ToDirectory.Exists)
                        {
                            throw new BuildException(String.Format(CultureInfo.InvariantCulture,
                                "Cannot move directory '{0}' to an existing directory '{1}'",
                                SourceFile.FullName, ToDirectory.FullName), Location);
                        }
                        destDirName = ToDirectory.FullName;
                    }
                    // Else, throw an exception
                    else
                    {
                        throw new BuildException("Target directory name not specified",
                            Location);
                    }
                    FileCopyMap.Add(destDirName, new FileDateInfo(sourceDirName, SourceFile.LastWriteTime, true));
                    _dirCount++;
                }
                else 
                {
                    throw CreateSourceFileNotFoundException (SourceFile.FullName);
                }
            }

            // copy file set contents.
            else
            {
                // get the complete path of the base directory of the fileset, ie, c:\work\nant\src
                DirectoryInfo srcBaseInfo = CopyFileSet.BaseDirectory;

                // Check to see if the file operation is a straight pass through (ie: no file or
                // directory modifications) before proceeding.
                bool completeDir = true;

                // completeDir criteria
                bool[] dirCheck = new bool[8];
                dirCheck[0] = CopyFileSet.IsEverythingIncluded;
                dirCheck[1] = !Flatten;
                dirCheck[2] = IncludeEmptyDirs || !CopyFileSet.HasEmptyDirectories;
                dirCheck[3] = FilterChain.IsNullOrEmpty(Filters);
                dirCheck[4] = _inputEncoding == null;
                dirCheck[5] = _outputEncoding == null;
                dirCheck[6] = srcBaseInfo != null && srcBaseInfo.Exists;
                dirCheck[7] = !ToDirectory.Exists ||
                    srcBaseInfo.FullName.Equals(ToDirectory.FullName,
                        StringComparison.InvariantCultureIgnoreCase);

                for (int b = 0; b < dirCheck.Length; b++) completeDir &= dirCheck[b];
                    
                if (completeDir)
                {
                    FileCopyMap.Add(ToDirectory.FullName, 
                        new FileDateInfo(srcBaseInfo.FullName, srcBaseInfo.LastWriteTime, true));
                    _dirCount++;
                }
                else
                {
                    // if source file not specified use fileset
                    foreach (string pathname in CopyFileSet.FileNames) 
                    {
                        FileInfo srcInfo = new FileInfo(pathname);
                        if (srcInfo.Exists) {
                            // will holds the full path to the destination file
                            string dstFilePath;

                            if (Flatten) {
                                dstFilePath = Path.Combine(ToDirectory.FullName, 
                                    srcInfo.Name);
                            } else {
                                // Gets the relative path and file info from the full 
                                // source filepath
                                // pathname = C:\f2\f3\file1, srcBaseInfo=C:\f2, then 
                                // dstRelFilePath=f3\file1
                                string dstRelFilePath = "";
                                if (srcInfo.FullName.IndexOf(srcBaseInfo.FullName, 0) != -1) {
                                    dstRelFilePath = srcInfo.FullName.Substring(
                                        srcBaseInfo.FullName.Length);
                                } else {
                                    dstRelFilePath = srcInfo.Name;
                                }
                            
                                if (dstRelFilePath[0] == Path.DirectorySeparatorChar) {
                                    dstRelFilePath = dstRelFilePath.Substring(1);
                                }
                            
                                // The full filepath to copy to.
                                dstFilePath = Path.Combine(ToDirectory.FullName, 
                                    dstRelFilePath);
                            }
                            
                            // do the outdated check
                            FileInfo dstInfo = new FileInfo(dstFilePath);
                            bool outdated = (!dstInfo.Exists) || (srcInfo.LastWriteTime > dstInfo.LastWriteTime);

                            if (Overwrite || outdated) {
                                // construct FileDateInfo for current file
                                FileDateInfo newFile = new FileDateInfo(srcInfo.FullName, 
                                    srcInfo.LastWriteTime);
                                // if multiple source files are selected to be copied 
                                // to the same destination file, then only the last
                                // updated source should actually be copied
                                FileDateInfo oldFile = (FileDateInfo) FileCopyMap[dstInfo.FullName];
                                if (oldFile != null) {
                                    // if current file was updated after scheduled file,
                                    // then replace it
                                    if (newFile.LastWriteTime > oldFile.LastWriteTime) {
                                        FileCopyMap[dstInfo.FullName] = newFile;
                                    }
                                } else {
                                    FileCopyMap.Add(dstInfo.FullName, newFile);
                                    _fileCount++;

                                    if (dstInfo.Exists && dstInfo.Attributes != FileAttributes.Normal) {
                                        File.SetAttributes(dstInfo.FullName, FileAttributes.Normal);
                                    }
                                }
                            }
                        } else {
                            throw CreateSourceFileNotFoundException (srcInfo.FullName);
                        }
                    }
                    
                    if (IncludeEmptyDirs && !Flatten) {
                        // create any specified directories that weren't created during the copy (ie: empty directories)
                        foreach (string pathname in CopyFileSet.DirectoryNames) {
                            DirectoryInfo srcInfo = new DirectoryInfo(pathname);
                            // skip directory if not relative to base dir of fileset
                            if (srcInfo.FullName.IndexOf(srcBaseInfo.FullName) == -1) {
                                continue;
                            }
                            string dstRelPath = srcInfo.FullName.Substring(srcBaseInfo.FullName.Length);
                            if (dstRelPath.Length > 0 && dstRelPath[0] == Path.DirectorySeparatorChar) {
                                dstRelPath = dstRelPath.Substring(1);
                            }

                            // The full filepath to copy to.
                            string destinationDirectory = Path.Combine(ToDirectory.FullName, dstRelPath);
                            if (!Directory.Exists(destinationDirectory)) {
                                try {
                                    Directory.CreateDirectory(destinationDirectory);
                                } catch (Exception ex) {
                                    throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                                    "Failed to create directory '{0}'.", destinationDirectory ), 
                                     Location, ex);
                                }
                                Log(Level.Verbose, "Created directory '{0}'.", destinationDirectory);
                            }
                        }
                    }
                }
            }

            // do all the actual copy operations now
            DoFileOperations();
        }
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Executes the Copy task.
		/// </summary>
		/// <exception cref="BuildException">A file that has to be copied does not exist or
		/// could not be copied.</exception>
		/// ------------------------------------------------------------------------------------
		protected override void ExecuteTask()
		{
			if (SourceFile != null)
			{
				// Copy single file.
				base.ExecuteTask();
			}
			else
			{
				// Copy file set contents.

				if (m_fResolveAssemblies)
					LoadReferences();

				// get the complete path of the base directory of the fileset, ie, c:/work/nant/src
				m_dstBaseInfo = ToDirectory;

				XmlAssembly xmlAssembly;
				if (Append)
					xmlAssembly = m_Cache[Project.ProjectName];
				else
				{
					xmlAssembly = new XmlAssembly(Project.ProjectName);
					m_Cache.Add(xmlAssembly);
				}

				// if source file is not specified use fileset
				foreach (string pathname in CopyFileSet.FileNames)
				{
					FileInfo srcInfo = new FileInfo(pathname);
					if (srcInfo.Exists || m_fForce)
					{
						// The full filepath to copy to.
						string dstFilePath = Path.Combine(m_dstBaseInfo.FullName, Path.GetFileName(srcInfo.FullName));

						// do the outdated check
						FileInfo dstInfo = new FileInfo(dstFilePath);
						bool fOutdated = (!dstInfo.Exists) || (srcInfo.LastWriteTime > dstInfo.LastWriteTime);

						if (Overwrite || fOutdated || m_fForce)
						{
							if (!IsInGAC(srcInfo))
							{
								FileCopyMap[dstFilePath] =
									new FileDateInfo(srcInfo.FullName.ToLower(), srcInfo.LastWriteTime);
								if (dstInfo.Exists && dstInfo.Attributes != FileAttributes.Normal)
									File.SetAttributes( dstInfo.FullName, FileAttributes.Normal );
								AddAssemblyAndRelatedFiles(xmlAssembly, srcInfo.FullName);
							}
							else
								Log(Level.Verbose, "Reference file {0} skipped", srcInfo.FullName);

						}
						else
						{
							if (!IsInGAC(srcInfo))
								AddAssemblyAndRelatedFiles(xmlAssembly, srcInfo.FullName);
						}
					}
					else
					{
						if (FailOnError)
						{
							string msg = String.Format(CultureInfo.InvariantCulture, "Could not find file {0} to copy.", srcInfo.FullName);
							throw new BuildException(msg, Location);
						}
						else
							Log(Level.Error, "Reference file {0} does not exist", pathname);
					}
				}

				CreateDirectories(CopyFileSet.BaseDirectory);

				try
				{
					// do all the actual copy operations now...
					DoFileOperations();
				}
				catch
				{
					if (FailOnError)
						throw;
				}
				finally
				{
					// save the references of this project in our settings file
					if (m_fResolveAssemblies)
						SaveReferences();
				}
			}
		}
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Add a file to the list of files to copy. The filename is built from the assembly
		/// name and the new extension.
		/// </summary>
		/// <param name="srcName">Path and name of file</param>
		/// <param name="newExtension">New extension</param>
		/// ------------------------------------------------------------------------------------
		private void AddFile(string srcName, string newExtension)
		{
			FileInfo newSrcInfo = new FileInfo(Path.ChangeExtension(srcName, newExtension));
			if (newSrcInfo.Exists)
			{
				string dstFile = Path.Combine(m_dstBaseInfo.FullName,
					Path.GetFileName(newSrcInfo.FullName));
				if (newSrcInfo.FullName.ToLower() != dstFile.ToLower())
					FileCopyMap[dstFile] =
						new FileDateInfo(newSrcInfo.FullName.ToLower(), newSrcInfo.LastWriteTime);
				else
					Log(Level.Debug, "Reference file {0} has same source and destination", newSrcInfo.FullName);
			}
			else
				Log(Level.Debug, "Reference file {0} does not exist", newSrcInfo.FullName);

		}
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Resolve references of a reference
		/// </summary>
		/// <param name="referencePath">Path to the referenced assembly</param>
		/// ------------------------------------------------------------------------------------
		private void ResolveReferences(string referencePath)
		{
			string referenceName = Path.GetFileName(referencePath);
			XmlAssembly assembly = m_Cache[referenceName];
			if (assembly != null)
			{	// we have cached references for this assembly
				foreach(Reference reference in assembly.References)
				{
					string srcFilePath = Path.Combine(ToDirectory.FullName, reference.Name);

					string dstFilePath = Path.Combine(m_dstBaseInfo.FullName,
						Path.GetFileName(reference.Name));
					if (srcFilePath.ToLower() != dstFilePath.ToLower())
					{
						FileCopyMap[dstFilePath] =
							new FileDateInfo(srcFilePath.ToLower(), DateTime.MinValue);
						AddFile(srcFilePath, "xml");
						AddFile(srcFilePath, "pdb");
						ResolveReferences(srcFilePath);
					}
				}
			}
		}