AddFile() public method

public AddFile ( string sourcePath, string destinationPath ) : void
sourcePath string
destinationPath string
return void
Esempio n. 1
0
        private void ProcessNugetFiles(View filesView, string srcFilesRoot, string currentCondition)
        {
            currentCondition = Pivots.NormalizeExpression(currentCondition ?? "");

            foreach (var containerName in filesView.GetChildPropertyNames().ToArray())
            {
                View container = filesView.GetProperty(containerName);
                if (containerName == "condition" || containerName == "*")
                {
                    foreach (var condition in container.GetChildPropertyNames())
                    {
                        ProcessNugetFiles(container.GetElement(condition), srcFilesRoot, condition);
                    }
                    continue;
                }

                // GS01 Workaround for bug in Values not caching the output set when a collection is added to ?
                var filemasks     = container.Values.Distinct().ToArray();
                var relativePaths = new Dictionary <string, string>();

                var optionExcludes = container.GetMetadataValues("exclude", container, false).Union(container.GetMetadataValues("excludes", container, false)).ToArray();

                foreach (var mask in filemasks)
                {
                    if (string.IsNullOrEmpty(mask))
                    {
                        continue;
                    }
                    var fileset = mask.FindFilesSmarterComplex(srcFilesRoot, optionExcludes).GetMinimalPathsToDictionary();

                    if (!fileset.Any())
                    {
                        Event <Warning> .Raise("ProcessNugetFiles", "WARNING: file selection '{0}' failed to find any files ", mask);

                        continue;
                    }
                    foreach (var key in fileset.Keys)
                    {
                        relativePaths.Add(key, fileset[key]);
                    }
                }

                var optionRenames = container.GetMetadataValues("rename", container, false).Union(container.GetMetadataValues("renames", container, false)).Select(each => {
                    var s = each.Split('/', '\\');
                    if (s.Length == 2)
                    {
                        return(new {
                            search = new Regex(s[0]),
                            replace = s[1]
                        });
                    }
                    return(null);
                }).Where(each => each != null).ToArray();

                // determine the destination location in the target package
                var optionDestination = container.GetMetadataValueHarder("destination", currentCondition);
                var destinationFolder = string.IsNullOrEmpty(optionDestination) ? (filesView.GetSingleMacroValue("d_" + containerName) ?? "\\") : optionDestination;

                var optionFlatten   = container.GetMetadataValueHarder("flatten", currentCondition).IsPositive();
                var optionNoOverlay = container.GetMetadataValueHarder("overlay", currentCondition).IsNegative();

                var addEachFiles = container.GetMetadataValuesHarder("add-each-file", currentCondition).ToArray();
                var addFolders   = container.GetMetadataValuesHarder("add-folder", currentCondition).ToArray();
                var addAllFiles  = container.GetMetadataValuesHarder("add-all-files", currentCondition).ToArray();


                // add the folder to an Collection somewhere else in the PropertySheet
                if (addFolders.Length > 0 && relativePaths.Any())
                {
                    foreach (var addFolder in addFolders)
                    {
                        var folderView = filesView.GetProperty(addFolder, lastMinuteReplacementFunction: s => s.Replace("${condition}", currentCondition));
                        if (folderView != null)
                        {
                            folderView.AddValue((filesView.GetSingleMacroValue("pkg_root") + destinationFolder).FixSlashes());
                        }
                    }
                }

                // add the folder+/** to an Collection somewhere else in the PropertySheet (useful for making <ItemGroup>s)
                if (addAllFiles.Length > 0 && relativePaths.Any())
                {
                    foreach (var addAllFile in addAllFiles)
                    {
                        var folderView = filesView.GetProperty(addAllFile, lastMinuteReplacementFunction: s => s.Replace("${condition}", currentCondition));

                        if (folderView != null)
                        {
                            // add the /** suffix on the path
                            // so it can be used in an ItemGroup
                            folderView.AddValue(((filesView.GetSingleMacroValue("pkg_root") + destinationFolder) + "/**").FixSlashes());
                        }
                    }
                }

                foreach (var srcf in relativePaths.Keys)
                {
                    var src = srcf;

                    if (optionExcludes.HasWildcardMatch(src))
                    {
                        continue;
                    }

                    Event <Verbose> .Raise("ProcessNugetFiles (adding file)", "'{0}' + '{1}'", destinationFolder, relativePaths[src]);

                    string target = Path.Combine(destinationFolder, optionFlatten ? Path.GetFileName(relativePaths[src]) : relativePaths[src]).Replace("${condition}", currentCondition).FixSlashes();

                    if (optionRenames.Length > 0)
                    {
                        // process rename commands
                        var dir      = Path.GetDirectoryName(target);
                        var filename = Path.GetFileName(target);

                        foreach (var rename in optionRenames)
                        {
                            var newFilename = rename.search.Replace(filename, rename.replace);

                            if (newFilename != filename)
                            {
                                // generate the new location for the renamed file
                                var tmpFile = Path.Combine(FilesystemExtensions.TempPath, "renamedFiles", Guid.NewGuid().ToString(), newFilename);

                                // derp. gotta create the target dir first. *sigh*
                                Directory.CreateDirectory(Path.GetDirectoryName(tmpFile));

                                //copy the src file to the tmpFile location
                                File.Copy(src, tmpFile);

                                // remove it when we're done packaging
                                _tempFiles.Add(tmpFile);

                                // switch the src to use the temp file
                                src = tmpFile;

                                // and just use the dir as the target (instead of the whole path)
                                target = dir;
                                break;
                            }
                        }
                    }

                    // add the file under the configuration (unless it's marked for no overlay, in which case just put it in the base.)
                    NugetPackage.AddFile(src, target, optionNoOverlay ? string.Empty : currentCondition);

                    if (addEachFiles.Length > 0)
                    {
                        foreach (var addEachFile in addEachFiles)
                        {
                            var fileListView = filesView.GetProperty(addEachFile, lastMinuteReplacementFunction: s => s.Replace("${condition}", currentCondition));
                            if (fileListView != null)
                            {
                                fileListView.AddValue((filesView.GetSingleMacroValue("pkg_root") + target).Replace("${condition}", currentCondition).FixSlashes());
                            }
                        }
                    }
                }
            }
        }