public void TestExecuteWithActionCopyWithoutExistingFileDifferentFileName([Values( FileOperationsShared.ExistsOption.DoNothing, FileOperationsShared.ExistsOption.IncrementFileName, FileOperationsShared.ExistsOption.OverwriteFile)] FileOperationsShared.ExistsOption fileExistsOption) { TestExecuteWithActionCopyWithoutExistingFile(fixtureSourceFilePath, false, Path.Combine(fixtureDestinationFolderPath, "DifferentFileName.txt"), fileExistsOption); }
public void TestExecuteWithActionMoveWithoutExistingFile([Values( FileOperationsShared.ExistsOption.DoNothing, FileOperationsShared.ExistsOption.IncrementFileName, FileOperationsShared.ExistsOption.OverwriteFile)] FileOperationsShared.ExistsOption fileExistsOption) { TestExecuteWithActionMoveWithoutExistingFile(fixtureSourceFilePath, true, fixtureDestinationFolderPath, fileExistsOption); }
public override void GenerateCode(IFunctionBuilder functionBuilder) { FileOperationsShared.ActionType actionType = FunctionData.Properties[FileOperationsShared.ActionPropertyName].GetValue <FileOperationsShared.ActionType>(); FileOperationsShared.ExistsOption existsOption = FunctionData.Properties[FileOperationsShared.FileExistsPropertyName].GetValue <FileOperationsShared.ExistsOption>(); if (actionType == FileOperationsShared.ActionType.FileExists) { functionBuilder.AddCode(string.Format( @"bool exists = Twenty57.Linx.Components.File.FileOperationsX.FileExists({0}); {1}.Log(System.String.Format(""File {{0}} {{1}}"", {0}, exists ? ""exists."" : ""does not exist."")); return exists;" , functionBuilder.GetParamName(FileOperationsShared.SourceFilePathPropertyName), functionBuilder.ContextParamName)); } else if (actionType == FileOperationsShared.ActionType.CreateTempFile) { functionBuilder.AddCode(string.Format( @"string tempFilePath = Twenty57.Linx.Components.File.FileOperationsX.CreateTempFile(); {0}.Log(System.String.Format(""Temp file created at {{0}}."", tempFilePath)); return tempFilePath;" , functionBuilder.ContextParamName)); } else { bool keepFileName = FunctionData.Properties.ContainsKey(FileOperationsShared.KeepFileNamePropertyName) ? FunctionData.Properties[FileOperationsShared.KeepFileNamePropertyName].GetValue <bool>() : true; functionBuilder.AddCode(string.Format(@"string destinationFilePath = Twenty57.Linx.Components.File.FileOperationsX.Execute({0}, {1}, {2}, {3}, {4}, message => {5}.Log(message));" , functionBuilder.GetParamName(FileOperationsShared.SourceFilePathPropertyName) , CSharpUtilities.BoolAsString(keepFileName) , functionBuilder.GetParamName(keepFileName ? FileOperationsShared.DestinationFolderPathPropertyName : FileOperationsShared.DestinationFilePathPropertyName) , CSharpUtilities.EnumAsString(actionType), CSharpUtilities.EnumAsString(existsOption) , functionBuilder.ContextParamName)); if (actionType != FileOperationsShared.ActionType.Delete) { functionBuilder.AddCode("return destinationFilePath;"); } } functionBuilder.AddAssemblyReference(typeof(FileOperations)); }
public static string Execute(string sourceFilePath, bool keepFileName, string destinationPath, FileOperationsShared.ActionType action, FileOperationsShared.ExistsOption fileExistsOption, Action <string> logger) { if (string.IsNullOrEmpty(sourceFilePath)) { throw new Exception("Source file path cannot be null or empty."); } if (action == FileOperationsShared.ActionType.Delete) { if (Directory.Exists(sourceFilePath)) { logger("Delete directory " + sourceFilePath); Directory.Delete(sourceFilePath, true); } else if (System.IO.File.Exists(sourceFilePath)) { logger("Delete file " + sourceFilePath); System.IO.File.Delete(sourceFilePath); } else { logger(string.Format("File {0} does not exist. Do nothing.", sourceFilePath)); } return(null); } else { if (!System.IO.File.Exists(sourceFilePath)) { if (!IsValidFilePath(sourceFilePath)) { throw new Exception(string.Format("Invalid source file path: {0}.", sourceFilePath)); } throw new FileNotFoundException(string.Format("Source file path [{0}] does not exist.", sourceFilePath)); } if (string.IsNullOrEmpty(destinationPath)) { throw new Exception("Destination path cannot be null or empty."); } if (!IsValidFilePath(destinationPath)) { throw new Exception(string.Format("Invalid path: {0}.", destinationPath)); } string destinationFileNamePath; if (keepFileName) { destinationFileNamePath = Path.Combine(destinationPath, Path.GetFileName(sourceFilePath)); logger("Create directory " + destinationPath); Directory.CreateDirectory(destinationPath); } else { destinationFileNamePath = destinationPath; string directoryName = Path.GetDirectoryName(destinationPath); logger("Create directory " + directoryName); Directory.CreateDirectory(directoryName); } if ((fileExistsOption == FileOperationsShared.ExistsOption.DoNothing) && (System.IO.File.Exists(destinationFileNamePath))) { logger(string.Format("File {0} exists. Do nothing.", destinationFileNamePath)); return(string.Empty); } if (fileExistsOption == FileOperationsShared.ExistsOption.IncrementFileName) { destinationFileNamePath = FileUtil.GetIncrementalFileName(destinationFileNamePath); } if (action == FileOperationsShared.ActionType.Copy) { logger(string.Format("Copy {0} to {1}.", sourceFilePath, destinationFileNamePath)); System.IO.File.Copy(sourceFilePath, destinationFileNamePath, true); } else { if (System.IO.File.Exists(destinationFileNamePath)) { logger("Delete file " + destinationFileNamePath); System.IO.File.Delete(destinationFileNamePath); } logger(string.Format("Move {0} to {1}.", sourceFilePath, destinationFileNamePath)); System.IO.File.Move(sourceFilePath, destinationFileNamePath); } return(destinationFileNamePath); } }
private FunctionResult Execute(string sourceFilePath, bool keepFileName, string destinationPath, FileOperationsShared.ActionType action, FileOperationsShared.ExistsOption fileExistsOption) { FunctionExecutor tester = (new FunctionTester <FileOperations>()).Compile( new PropertyValue(FileOperationsShared.ActionPropertyName, action), new PropertyValue(FileOperationsShared.KeepFileNamePropertyName, keepFileName), new PropertyValue(FileOperationsShared.FileExistsPropertyName, fileExistsOption)); return(tester.Execute( new ParameterValue(FileOperationsShared.SourceFilePathPropertyName, sourceFilePath), new ParameterValue(FileOperationsShared.DestinationFolderPathPropertyName, keepFileName ? destinationPath : null), new ParameterValue(FileOperationsShared.DestinationFilePathPropertyName, keepFileName ? null : destinationPath))); }
private void TestExecuteWithActionMoveWithoutExistingFile(string sourceFilePath, bool keepFileName, string destinationPath, FileOperationsShared.ExistsOption fileExistsOption) { string sourceFileContents = System.IO.File.ReadAllText(sourceFilePath); FunctionResult result = Execute(sourceFilePath, keepFileName, destinationPath, FileOperationsShared.ActionType.Move, fileExistsOption); string destinationFilePath = keepFileName ? Path.Combine(destinationPath, Path.GetFileName(sourceFilePath)) : destinationPath; Assert.AreEqual(result.Value, destinationFilePath); Assert.IsFalse(System.IO.File.Exists(sourceFilePath)); Assert.IsTrue(System.IO.File.Exists(destinationFilePath)); Assert.AreEqual(sourceFileContents, System.IO.File.ReadAllText(destinationFilePath)); }