From 905e5ea847c3d6ce91cc4f1c5bde5f576f03d2b1 Mon Sep 17 00:00:00 2001 From: Tobias Hunger Date: Tue, 9 Jun 2015 16:57:58 +0200 Subject: [PATCH] JsonWizard: Allow Generated files without a source These are expected to be found in the target location. They will have the KeepExisting attribute set and their contents will not be touched. Having this is useful to specify which files to open. Change-Id: I958f271405ca780e0052f069e0df89bd402da6c4 Reviewed-by: Orgad Shaneh --- .../jsonwizard/jsonwizardfilegenerator.cpp | 45 ++++++++++++------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardfilegenerator.cpp b/src/plugins/projectexplorer/jsonwizard/jsonwizardfilegenerator.cpp index 832b7d4a465..9898501bfa5 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizardfilegenerator.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardfilegenerator.cpp @@ -109,9 +109,9 @@ bool JsonWizardFileGenerator::setup(const QVariant &data, QString *errorMessage) f.openInEditor = tmp.value(QLatin1String("openInEditor"), false); f.openAsProject = tmp.value(QLatin1String("openAsProject"), false); - if (f.source.isEmpty()) { + if (f.source.isEmpty() && f.target.isEmpty()) { *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage", - "No source given for file in file list."); + "Source and target are both empty."); return false; } @@ -139,31 +139,38 @@ Core::GeneratedFiles JsonWizardFileGenerator::fileList(Utils::MacroExpander *exp if (!JsonWizard::boolFromVariant(f.condition, expander)) continue; + const bool keepExisting = f.source.isEmpty(); + const QString targetPath = project.absoluteFilePath(expander->expand(f.target)); + const QString sourcePath + = keepExisting ? targetPath : wizard.absoluteFilePath(expander->expand(f.source)); + const bool isBinary = JsonWizard::boolFromVariant(f.isBinary, expander); + // Read contents of source file - const QString src = wizard.absoluteFilePath(expander->expand(f.source)); const QFile::OpenMode openMode - = JsonWizard::boolFromVariant(f.isBinary, expander) + = JsonWizard::boolFromVariant(isBinary, expander) ? QIODevice::ReadOnly : (QIODevice::ReadOnly|QIODevice::Text); Utils::FileReader reader; - if (!reader.fetch(src, openMode, errorMessage)) + if (!reader.fetch(sourcePath, openMode, errorMessage)) return Core::GeneratedFiles(); // Generate file information: Core::GeneratedFile gf; - gf.setPath(project.absoluteFilePath(expander->expand(f.target))); + gf.setPath(targetPath); - if (JsonWizard::boolFromVariant(f.isBinary, expander)) { - gf.setBinary(true); - gf.setBinaryContents(reader.data()); - } else { - // TODO: Document that input files are UTF8 encoded! - gf.setBinary(false); - gf.setContents(processTextFileContents(expander, QString::fromUtf8(reader.data()), errorMessage)); - if (!errorMessage->isEmpty()) { - *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard", "When processing \"%1\":
%2") - .arg(src, *errorMessage); - return Core::GeneratedFiles(); + if (!keepExisting) { + if (isBinary) { + gf.setBinary(true); + gf.setBinaryContents(reader.data()); + } else { + // TODO: Document that input files are UTF8 encoded! + gf.setBinary(false); + gf.setContents(processTextFileContents(expander, QString::fromUtf8(reader.data()), errorMessage)); + if (!errorMessage->isEmpty()) { + *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard", "When processing \"%1\":
%2") + .arg(sourcePath, *errorMessage); + return Core::GeneratedFiles(); + } } } @@ -174,6 +181,10 @@ Core::GeneratedFiles JsonWizardFileGenerator::fileList(Utils::MacroExpander *exp attributes |= Core::GeneratedFile::OpenProjectAttribute; if (JsonWizard::boolFromVariant(f.overwrite, expander)) attributes |= Core::GeneratedFile::ForceOverwrite; + + if (keepExisting) + attributes |= Core::GeneratedFile::KeepExistingFileAttribute; + gf.setAttributes(attributes); result.append(gf); }