public bool ExportFiles(IReturnPackage package, int encodingCode) { if (package is null) { _logger.Info("Return package was null"); return(false); } _logger.Info( $"Trying to create export package for Studio Project:{package.FileBasedProject?.GetProjectInfo()?.Name}"); if (!(package.SelectedTargetFilesForImport?.Count() > 0)) { _logger.Info("No selected target files for import."); return(false); } var taskSequence = package?.FileBasedProject?.RunAutomaticTasks( package.SelectedTargetFilesForImport?.GetIds(), new[] { AutomaticTaskTemplateIds.GenerateTargetTranslations }); CreateArchive(package, encodingCode); // Create transit tpf file anyway if (taskSequence?.Status == TaskStatus.Completed) { return(true); } _logger.Info($"Generate target translation task sequence status:{taskSequence?.Status}"); return(false); }
public ReturnPackageWindowViewModel(IReturnPackage returnPackage, IReturnPackageService returnPackageService, IDialogService dialogService, IEventAggregatorService eventAggregatorService) { _dialogService = dialogService; _returnPackageService = returnPackageService; _eventAggregatorService = eventAggregatorService; ReturnPackage = returnPackage; SelectAll = false; foreach (var returnFile in ReturnPackage.ReturnFilesDetails) { returnFile.PropertyChanged += ReturnFile_PropertyChanged; } LoadEncodingsOptions(); }
/// <summary> /// Creates an archive in the Return Package folder and add project files to it /// For the moment we add the files without running any task on them /// </summary> private void CreateArchive(IReturnPackage package, int encodingCode) { try { ChangeMetadataFile(package.PathToPrjFile); var prjFileName = Path.GetFileNameWithoutExtension(package.PathToPrjFile); var archivePath = Path.Combine(package.FolderLocation, prjFileName + ".tpf"); foreach (var targetFile in package.SelectedTargetFilesForImport) { var pathToTargetFileFolder = Path.GetDirectoryName(targetFile.LocalFilePath); if (!File.Exists(archivePath)) { //create the archive, and add files to it using (var archive = ZipFile.Open(archivePath, ZipArchiveMode.Create)) { archive.CreateEntryFromFile(package.PathToPrjFile, string.Concat(prjFileName, ".PRJ"), CompressionLevel.Optimal); foreach (var file in package.SelectedTargetFilesForImport) { pathToTargetFileFolder = Path.GetDirectoryName(file.LocalFilePath); var fileName = Path.GetFileNameWithoutExtension(file.LocalFilePath); var nameBytes = Encoding.Default.GetBytes(fileName); var encodedFileName = Encoding.GetEncoding(encodingCode).GetString(nameBytes); archive.CreateEntryFromFile(Path.Combine(pathToTargetFileFolder, fileName), encodedFileName, CompressionLevel.Optimal); } } } else { UpdateArchive(archivePath, prjFileName, package, pathToTargetFileFolder); } } } catch (Exception ex) { _logger.Error($"{ex.Message}\n {ex.StackTrace}"); } }
private void UpdateArchive(string archivePath, string prjFileName, IReturnPackage returnPackagePackage, string pathToTargetFileFolder) { try { // open the archive and delete old files // archive in update mode not overrides existing files using (var archive = ZipFile.Open(archivePath, ZipArchiveMode.Update)) { var entriesCollection = new ObservableCollection <ZipArchiveEntry>(archive.Entries); foreach (var entry in entriesCollection) { if (entry.Name.Equals(string.Concat(prjFileName, ".PRJ"))) { entry.Delete(); } foreach (var project in returnPackagePackage.SelectedTargetFilesForImport) { var projectFromArchiveToBeDeleted = archive.Entries.FirstOrDefault(n => n.Name.Equals(Path.GetFileNameWithoutExtension(project.Name))); projectFromArchiveToBeDeleted?.Delete(); } } } //add files to archive using (var archive = ZipFile.Open(archivePath, ZipArchiveMode.Update)) { archive.CreateEntryFromFile(returnPackagePackage.PathToPrjFile, string.Concat(prjFileName, ".PRJ"), CompressionLevel.Optimal); foreach (var file in returnPackagePackage.SelectedTargetFilesForImport) { var fileName = Path.GetFileNameWithoutExtension(file.LocalFilePath); pathToTargetFileFolder = Path.GetDirectoryName(file.LocalFilePath); archive.CreateEntryFromFile(Path.Combine(pathToTargetFileFolder, fileName), fileName, CompressionLevel.Optimal); } } } catch (Exception ex) { _logger.Error($"{ex.Message}\n {ex.StackTrace}"); } }