/// <summary> /// Copies a folder recursively /// </summary> /// <param name="?"></param> private void CopyFolder(FileToCopy copyFileItem) { string sourcePath = copyFileItem.source; string destinyPath = copyFileItem.destiny; if (Directory.Exists(sourcePath)) { if (!Directory.Exists(destinyPath)) { Directory.CreateDirectory(destinyPath); } //Get the files inside the folder string[] files = Directory.GetFiles(sourcePath); if (files.Length > 0) { foreach (string file in files) { CopyFile(new FileToCopy(copyFileItem.source + @"\" + Path.GetFileName(file), copyFileItem.destiny + @"\" + Path.GetFileName(file))); } } //Get the folders inside string[] directories = Directory.GetDirectories(sourcePath); if (directories.Length > 0) { foreach (string directory in directories) { string origin = Path.Combine(copyFileItem.source, directory.Replace(Path.GetDirectoryName(directory) + "\\", string.Empty)); string destiny = Path.Combine(copyFileItem.destiny, directory.Replace(Path.GetDirectoryName(directory) + "\\", string.Empty)); CopyFolder(new FileToCopy(origin, destiny)); } } } }
/// <summary> /// Copies a specific file /// </summary> /// <param name="filePath"></param> private void CopyFile(FileToCopy copyFileItem) { string sourcePath = copyFileItem.source; string destinyPath = copyFileItem.destiny; try { if (System.IO.File.Exists(sourcePath)) { Directory.CreateDirectory(Path.GetDirectoryName(destinyPath)); if (System.IO.File.Exists(destinyPath)) { string suffix = ".copy"; System.IO.File.Copy(sourcePath, destinyPath + suffix, true); System.IO.File.Delete(destinyPath); System.IO.File.Copy(destinyPath + suffix, destinyPath, true); System.IO.File.Delete(destinyPath + suffix); Console.WriteLine("{0} {1}", destinyPath, " file overwritten"); } else { System.IO.File.Copy(sourcePath, destinyPath); Console.WriteLine("{0} {1}", destinyPath, " file copied"); } } } catch { } }
/// <summary> /// Copy files from OutputDirectory/Include/ForlderTypeName to SourceDirectory/Include/FolderTypeName /// </summary> private static bool CopyDirectory(string OutputDirectory, string SourceDirectory, string FolderTypeName, Logger log) { DirectoryInfo SourceDirectoryInfo = new DirectoryInfo(Path.Combine(Path.Combine(SourceDirectory, "Include"), FolderTypeName)); if (!SourceDirectoryInfo.Exists) { log.Error(Language.Message("DirectoryCouldNotBeFound", SourceDirectoryInfo.FullName)); return(false); } DirectoryInfo OutputDirectoryInfo = new DirectoryInfo(Path.Combine(Path.Combine(OutputDirectory, "Include"), FolderTypeName)); if (!OutputDirectoryInfo.Exists) { OutputDirectoryInfo.Create(); } foreach (FileInfo FileToCopy in SourceDirectoryInfo.GetFiles()) { FileInfo OutFile = new FileInfo(Path.Combine(OutputDirectoryInfo.FullName, FileToCopy.Name)); if (!OutFile.Exists || FileToCopy.LastWriteTimeUtc != OutFile.LastWriteTimeUtc) { try { SetFileAttributeForReplace(OutFile); FileToCopy.CopyTo(OutFile.FullName, true); } //These exception has occurred when spaming the markdownmode publish flag options. Unlikely to occur in proper use, just raise warning. catch (IOException) { log.Warn(Language.Message("FileCouldNotBeCopiedAlreadyBeingCopiedByAnotherProcess", OutFile.FullName)); } catch (UnauthorizedAccessException) { log.Warn(Language.Message("FileCouldNotBeCopiedAlreadyBeingCopiedByAnotherProcess", OutFile.FullName)); } } } return(true); }
/// <exclude /> public override IEnumerable<PackageFragmentValidationResult> Validate() { var validationResult = new List<PackageFragmentValidationResult>(); if (this.Configuration.Count(f => f.Name == "Files") > 1) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_OnlyOneFilesElement, this.ConfigurationParent); return validationResult; } if (this.Configuration.Count(f => f.Name == "Directories") > 1) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_OnlyOneDirectoriesElement, this.ConfigurationParent); return validationResult; } XElement filesElement = this.Configuration.SingleOrDefault(f => f.Name == "Files"); XElement directoriesElement = this.Configuration.SingleOrDefault(f => f.Name == "Directories"); _filesToCopy = new List<FileToCopy>(); _directoriesToDelete = new List<string>(); if (filesElement != null) { foreach (XElement fileElement in filesElement.Elements("File")) { XAttribute sourceFilenameAttribute = fileElement.Attribute("sourceFilename"); XAttribute targetFilenameAttribute = fileElement.Attribute("targetFilename"); if (sourceFilenameAttribute == null) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_MissingAttribute("sourceFilename"), fileElement); continue; } if (targetFilenameAttribute == null) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_MissingAttribute("targetFilename"), fileElement); continue; } XAttribute allowOverwriteAttribute = fileElement.Attribute("allowOverwrite"); XAttribute assemblyLoadAttribute = fileElement.Attribute("assemblyLoad"); XAttribute deleteTargetDirectoryAttribute = fileElement.Attribute("deleteTargetDirectory"); XAttribute onlyUpdateAttribute = fileElement.Attribute("onlyUpdate"); if (deleteTargetDirectoryAttribute != null) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_DeleteTargetDirectoryNotAllowed, fileElement); continue; } bool allowOverwrite = false; if (!ParseBoolAttribute(allowOverwriteAttribute, validationResult, ref allowOverwrite)) { continue; } bool loadAssembly = false; if (!ParseBoolAttribute(assemblyLoadAttribute, validationResult, ref loadAssembly)) { continue; } bool onlyUpdate = false; if (!ParseBoolAttribute(onlyUpdateAttribute, validationResult, ref onlyUpdate)) { continue; } string sourceFilename = sourceFilenameAttribute.Value; if (!this.InstallerContext.ZipFileSystem.ContainsFile(sourceFilename)) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_MissingFile(sourceFilename), sourceFilenameAttribute); continue; } if (loadAssembly && onlyUpdate) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_OnlyUpdateNotAllowedWithLoadAssemlby, onlyUpdateAttribute); continue; } string targetFilename = PathUtil.Resolve(targetFilenameAttribute.Value); if (C1File.Exists(targetFilename)) { if (!allowOverwrite && !onlyUpdate) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_FileExists(targetFilename), targetFilenameAttribute); continue; } if (((C1File.GetAttributes(targetFilename) & FileAttributes.ReadOnly) > 0) && !allowOverwrite) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_FileReadOnly(targetFilename), targetFilenameAttribute); continue; } } else if (onlyUpdate) { Log.LogVerbose(LogTitle, "Skipping updating of the file '{0}' because it does not exist", targetFilename); continue; // Target file does not, so skip this } var fileToCopy = new FileToCopy { SourceFilename = sourceFilename, TargetRelativeFilePath = targetFilenameAttribute.Value, TargetFilePath = targetFilename, Overwrite = allowOverwrite || onlyUpdate }; _filesToCopy.Add(fileToCopy); if (loadAssembly) { string tempFilename = Path.Combine(this.InstallerContext.TempDirectory, Path.GetFileName(targetFilename)); this.InstallerContext.ZipFileSystem.WriteFileToDisk(sourceFilename, tempFilename); PackageAssemblyHandler.AddAssembly(tempFilename); } } } if (directoriesElement != null) { foreach (XElement directoryElement in directoriesElement.Elements("Directory")) { XAttribute sourceDirectoryAttribute = directoryElement.Attribute("sourceDirectory"); XAttribute targetDirectoryAttribute = directoryElement.Attribute("targetDirectory"); if (sourceDirectoryAttribute == null) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_MissingAttribute("sourceDirectory"), directoryElement); continue; } if (targetDirectoryAttribute == null) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_MissingAttribute("targetDirectory"), directoryElement); continue; } XAttribute allowOverwriteAttribute = directoryElement.Attribute("allowOverwrite"); XAttribute assemblyLoadAttribute = directoryElement.Attribute("assemblyLoad"); XAttribute deleteTargetDirectoryAttribute = directoryElement.Attribute("deleteTargetDirectory"); XAttribute onlyUpdateAttribute = directoryElement.Attribute("onlyUpdate"); if (assemblyLoadAttribute != null) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_AssemblyLoadNotAllowed, directoryElement); continue; } if (onlyUpdateAttribute != null) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_OnlyUpdateNotAllowed, directoryElement); continue; } bool allowOverwrite = false; if (!ParseBoolAttribute(allowOverwriteAttribute, validationResult, ref allowOverwrite)) { continue; } bool deleteTargetDirectory = false; if (!ParseBoolAttribute(deleteTargetDirectoryAttribute, validationResult, ref deleteTargetDirectory)) { continue; } string sourceDirectory = sourceDirectoryAttribute.Value; if (!this.InstallerContext.ZipFileSystem.ContainsDirectory(sourceDirectory)) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_MissingDirectory(sourceDirectory), sourceDirectoryAttribute); continue; } string targetDirectory = PathUtil.Resolve(targetDirectoryAttribute.Value); if (deleteTargetDirectory) { if (C1Directory.Exists(targetDirectory)) { _directoriesToDelete.Add(targetDirectory); } } foreach (string sourceFilename in this.InstallerContext.ZipFileSystem.GetFilenames(sourceDirectory)) { string resolvedSourceFilename = sourceFilename.Remove(0, sourceDirectory.Length); if (resolvedSourceFilename.StartsWith("/")) { resolvedSourceFilename = resolvedSourceFilename.Remove(0, 1); } string targetFilename = Path.Combine(targetDirectory, resolvedSourceFilename); if (C1File.Exists(targetFilename) && !deleteTargetDirectory && !allowOverwrite) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_FileExists(targetFilename), targetDirectoryAttribute); continue; } var fileToCopy = new FileToCopy { SourceFilename = sourceFilename, TargetRelativeFilePath = Path.Combine(targetDirectoryAttribute.Value, resolvedSourceFilename), TargetFilePath = targetFilename, Overwrite = allowOverwrite }; _filesToCopy.Add(fileToCopy); } } } if (validationResult.Count > 0) { _filesToCopy = null; _directoriesToDelete = null; } return validationResult; }
/// <exclude /> public override IEnumerable <PackageFragmentValidationResult> Validate() { var validationResult = new List <PackageFragmentValidationResult>(); if (this.Configuration.Count(f => f.Name == "Files") > 1) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_OnlyOneFilesElement, this.ConfigurationParent); return(validationResult); } XElement filesElement = this.Configuration.SingleOrDefault(f => f.Name == "Files"); _filesToCopy = new List <FileToCopy>(); if (filesElement != null) { foreach (XElement fileElement in filesElement.Elements("File")) { XAttribute sourceFilenameAttribute = fileElement.Attribute("sourceFilename"); XAttribute targetFilenameAttribute = fileElement.Attribute("targetFilename"); if (sourceFilenameAttribute == null) { validationResult.AddFatal( Texts.FilePackageFragmentInstaller_MissingAttribute("sourceFilename"), fileElement); continue; } if (targetFilenameAttribute == null) { validationResult.AddFatal( Texts.FilePackageFragmentInstaller_MissingAttribute("targetFilename"), fileElement); continue; } XAttribute allowOverwriteAttribute = fileElement.Attribute("allowOverwrite"); XAttribute assemblyLoadAttribute = fileElement.Attribute("assemblyLoad"); XAttribute onlyUpdateAttribute = fileElement.Attribute("onlyUpdate"); XAttribute addAssemblyBindingAttribute = fileElement.Attribute("addAssemblyBinding"); bool allowOverwrite = false; if (!ParseBoolAttribute(allowOverwriteAttribute, validationResult, ref allowOverwrite)) { continue; } bool loadAssembly = false; if (!ParseBoolAttribute(assemblyLoadAttribute, validationResult, ref loadAssembly)) { continue; } bool onlyUpdate = false; if (!ParseBoolAttribute(onlyUpdateAttribute, validationResult, ref onlyUpdate)) { continue; } bool addAssemblyBinding = false; if (!ParseBoolAttribute(addAssemblyBindingAttribute, validationResult, ref addAssemblyBinding)) { continue; } string sourceFilename = sourceFilenameAttribute.Value; if (!this.InstallerContext.ZipFileSystem.ContainsFile(sourceFilename)) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_MissingFile(sourceFilename), sourceFilenameAttribute); continue; } if (loadAssembly && onlyUpdate) { validationResult.AddFatal( Texts.FilePackageFragmentInstaller_OnlyUpdateNotAllowedWithLoadAssemlby, onlyUpdateAttribute); continue; } string targetFilename = PathUtil.Resolve(targetFilenameAttribute.Value); if (C1File.Exists(targetFilename)) { if (!allowOverwrite && !onlyUpdate) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_FileExists(targetFilename), targetFilenameAttribute); continue; } if (((C1File.GetAttributes(targetFilename) & FileAttributes.ReadOnly) > 0) && !allowOverwrite) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_FileReadOnly(targetFilename), targetFilenameAttribute); continue; } } else if (onlyUpdate) { Log.LogVerbose(LogTitle, "Skipping updating of the file '{0}' because it does not exist", targetFilename); continue; // Target file does not, so skip this } var fileToCopy = new FileToCopy { SourceFilename = sourceFilename, TargetRelativeFilePath = targetFilenameAttribute.Value, TargetFilePath = targetFilename, Overwrite = allowOverwrite || onlyUpdate, AddAssemblyBinding = addAssemblyBinding }; _filesToCopy.Add(fileToCopy); if (loadAssembly) { string tempFilename = Path.Combine(this.InstallerContext.TempDirectory, Path.GetFileName(targetFilename)); this.InstallerContext.ZipFileSystem.WriteFileToDisk(sourceFilename, tempFilename); PackageAssemblyHandler.AddAssembly(tempFilename); } } } if (validationResult.Count > 0) { _filesToCopy = null; } return(validationResult); }
/// <exclude /> public override IEnumerable <PackageFragmentValidationResult> Validate() { var validationResult = new List <PackageFragmentValidationResult>(); if (this.Configuration.Count(f => f.Name == "Files") > 1) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_OnlyOneFilesElement, this.ConfigurationParent); return(validationResult); } if (this.Configuration.Count(f => f.Name == "Directories") > 1) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_OnlyOneDirectoriesElement, this.ConfigurationParent); return(validationResult); } XElement filesElement = this.Configuration.SingleOrDefault(f => f.Name == "Files"); XElement directoriesElement = this.Configuration.SingleOrDefault(f => f.Name == "Directories"); _filesToCopy = new List <FileToCopy>(); _directoriesToDelete = new List <string>(); if (filesElement != null) { foreach (XElement fileElement in filesElement.Elements("File")) { XAttribute sourceFilenameAttribute = fileElement.Attribute("sourceFilename"); XAttribute targetFilenameAttribute = fileElement.Attribute("targetFilename"); if (sourceFilenameAttribute == null) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_MissingAttribute("sourceFilename"), fileElement); continue; } if (targetFilenameAttribute == null) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_MissingAttribute("targetFilename"), fileElement); continue; } XAttribute allowOverwriteAttribute = fileElement.Attribute("allowOverwrite"); XAttribute assemblyLoadAttribute = fileElement.Attribute("assemblyLoad"); XAttribute deleteTargetDirectoryAttribute = fileElement.Attribute("deleteTargetDirectory"); XAttribute onlyUpdateAttribute = fileElement.Attribute("onlyUpdate"); XAttribute onlyAddAttribute = fileElement.Attribute("onlyAdd"); if (deleteTargetDirectoryAttribute != null) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_DeleteTargetDirectoryNotAllowed, fileElement); continue; } bool allowOverwrite = false; if (!ParseBoolAttribute(allowOverwriteAttribute, validationResult, ref allowOverwrite)) { continue; } bool loadAssembly = false; if (!ParseBoolAttribute(assemblyLoadAttribute, validationResult, ref loadAssembly)) { continue; } bool onlyUpdate = false; if (!ParseBoolAttribute(onlyUpdateAttribute, validationResult, ref onlyUpdate)) { continue; } bool onlyAdd = false; if (!ParseBoolAttribute(onlyAddAttribute, validationResult, ref onlyAdd)) { continue; } string sourceFilename = sourceFilenameAttribute.Value; if (!this.InstallerContext.ZipFileSystem.ContainsFile(sourceFilename)) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_MissingFile(sourceFilename), sourceFilenameAttribute); continue; } if (loadAssembly && onlyUpdate) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_OnlyUpdateNotAllowedWithLoadAssemlby, onlyUpdateAttribute); continue; } if (onlyAdd && onlyUpdate) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_OnlyUpdateAndOnlyAddNotAllowed, onlyUpdateAttribute); continue; } if (onlyAdd && allowOverwrite) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_OnlyAddAndAllowOverwriteNotAllowed, onlyAddAttribute); continue; } string targetFilename = PathUtil.Resolve(targetFilenameAttribute.Value); if (C1File.Exists(targetFilename)) { if (onlyAdd) { Log.LogVerbose(LogTitle, "Skipping adding of the file '{0}' because it already exist and is marked 'onlyAdd'", targetFilename); continue; // Target file does not, so skip this } if (!allowOverwrite && !onlyUpdate) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_FileExists(targetFilename), targetFilenameAttribute); continue; } if (((C1File.GetAttributes(targetFilename) & FileAttributes.ReadOnly) > 0) && !allowOverwrite) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_FileReadOnly(targetFilename), targetFilenameAttribute); continue; } } else if (onlyUpdate) { Log.LogVerbose(LogTitle, "Skipping updating of the file '{0}' because it does not exist", targetFilename); continue; // Target file does not, so skip this } var fileToCopy = new FileToCopy { SourceFilename = sourceFilename, TargetRelativeFilePath = targetFilenameAttribute.Value, TargetFilePath = targetFilename, Overwrite = allowOverwrite || onlyUpdate }; _filesToCopy.Add(fileToCopy); if (loadAssembly) { string tempFilename = Path.Combine(this.InstallerContext.TempDirectory, Path.GetFileName(targetFilename)); this.InstallerContext.ZipFileSystem.WriteFileToDisk(sourceFilename, tempFilename); PackageAssemblyHandler.AddAssembly(tempFilename); } } } if (directoriesElement != null) { foreach (XElement directoryElement in directoriesElement.Elements("Directory")) { XAttribute sourceDirectoryAttribute = directoryElement.Attribute("sourceDirectory"); XAttribute targetDirectoryAttribute = directoryElement.Attribute("targetDirectory"); if (sourceDirectoryAttribute == null) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_MissingAttribute("sourceDirectory"), directoryElement); continue; } if (targetDirectoryAttribute == null) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_MissingAttribute("targetDirectory"), directoryElement); continue; } XAttribute allowOverwriteAttribute = directoryElement.Attribute("allowOverwrite"); XAttribute assemblyLoadAttribute = directoryElement.Attribute("assemblyLoad"); XAttribute deleteTargetDirectoryAttribute = directoryElement.Attribute("deleteTargetDirectory"); XAttribute onlyUpdateAttribute = directoryElement.Attribute("onlyUpdate"); if (assemblyLoadAttribute != null) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_AssemblyLoadNotAllowed, directoryElement); continue; } if (onlyUpdateAttribute != null) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_OnlyUpdateNotAllowed, directoryElement); continue; } bool allowOverwrite = false; if (!ParseBoolAttribute(allowOverwriteAttribute, validationResult, ref allowOverwrite)) { continue; } bool deleteTargetDirectory = false; if (!ParseBoolAttribute(deleteTargetDirectoryAttribute, validationResult, ref deleteTargetDirectory)) { continue; } string sourceDirectory = sourceDirectoryAttribute.Value; if (!this.InstallerContext.ZipFileSystem.ContainsDirectory(sourceDirectory)) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_MissingDirectory(sourceDirectory), sourceDirectoryAttribute); continue; } string targetDirectory = PathUtil.Resolve(targetDirectoryAttribute.Value); if (deleteTargetDirectory) { if (C1Directory.Exists(targetDirectory)) { _directoriesToDelete.Add(targetDirectory); } } foreach (string sourceFilename in this.InstallerContext.ZipFileSystem.GetFilenames(sourceDirectory)) { string resolvedSourceFilename = sourceFilename.Remove(0, sourceDirectory.Length); if (resolvedSourceFilename.StartsWith("/")) { resolvedSourceFilename = resolvedSourceFilename.Remove(0, 1); } string targetFilename = Path.Combine(targetDirectory, resolvedSourceFilename); if (C1File.Exists(targetFilename) && !deleteTargetDirectory && !allowOverwrite) { validationResult.AddFatal(Texts.FilePackageFragmentInstaller_FileExists(targetFilename), targetDirectoryAttribute); continue; } var fileToCopy = new FileToCopy { SourceFilename = sourceFilename, TargetRelativeFilePath = Path.Combine(targetDirectoryAttribute.Value, resolvedSourceFilename), TargetFilePath = targetFilename, Overwrite = allowOverwrite }; _filesToCopy.Add(fileToCopy); } } } if (validationResult.Count > 0) { _filesToCopy = null; _directoriesToDelete = null; } return(validationResult); }