Added logic to create the indenter calling script

Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
master
Michele Calgaro 1 year ago
parent 97a6a7874e
commit 48a09796d5
Signed by: MicheleC
GPG Key ID: 2A75B7CA8ADED5CF

@ -31,6 +31,7 @@ set( ${target}_SRCS
ToolBarWidget.ui
SettingsPaths.cpp
TemplateBatchScript.cpp
UiGuiErrorMessage.cpp
UiGuiIniFileParser.cpp
UiGuiVersion.cpp

@ -20,12 +20,14 @@
#include "config.h"
#include "IndentHandler.h"
#include <cstdlib>
#include "MainWindow.h"
#include "SettingsPaths.h"
#include "UiGuiErrorMessage.h"
#include "UiGuiIniFileParser.h"
#include "UiGuiSettings.h"
//--- #include "TemplateBatchScript.h"
#include "TemplateBatchScript.h"
#include <tqapplication.h>
#include <tqcheckbox.h>
@ -215,94 +217,94 @@ void IndentHandler::contextMenuEvent(TQContextMenuEvent *event)
}
}
//--- /*
//--- \brief Creates the content for a shell script that can be used as a external tool call
//--- to indent an as parameter defined file.
//--- */
//--- TQString IndentHandler::generateShellScript(const TQString &configFilename)
//--- {
//--- TQString indenterCompleteCallString;
//--- TQString parameterInputFile;
//--- TQString parameterOuputFile;
//--- TQString parameterParameterFile;
//--- TQString replaceInputFileCommand;
//---
//--- // Define the placeholder for parameter variables either in batch or bash programming.
//--- TQString shellParameterPlaceholder = "$1";
//---
//--- parameterInputFile = " " + m_inputFileParameter + "\"" + shellParameterPlaceholder + "\"";
//---
//--- if (m_outputFileParameter != "none" && m_outputFileParameter != "stdout")
//--- {
//--- if (m_outputFileName == m_inputFileName)
//--- {
//--- parameterOuputFile = " " + m_outputFileParameter + "\"" + shellParameterPlaceholder + "\"";
//--- }
//--- else
//--- {
//--- parameterOuputFile = " " + m_outputFileParameter + m_outputFileName + ".tmp";
//--- }
//--- }
//---
//--- // If the config file name is empty it is assumed that all parameters are sent via command line
//--- // call
//--- if (m_globalConfigFilename.isEmpty())
//--- {
//--- parameterParameterFile = " " + getParameterString();
//--- }
//--- // else if needed add the parameter to the indenter call string where the config file can be
//--- // found.
//--- else if (m_useCfgFileParameter != "none")
//--- {
//--- parameterParameterFile = " " + m_useCfgFileParameter + "\"./" + configFilename + "\"";
//--- }
//---
//--- // Assemble indenter call string for parameters according to the set order.
//--- if (m_parameterOrder == "ipo")
//--- {
//--- indenterCompleteCallString = parameterInputFile + parameterParameterFile + parameterOuputFile;
//--- }
//--- else if (m_parameterOrder == "pio")
//--- {
//--- indenterCompleteCallString = parameterParameterFile + parameterInputFile + parameterOuputFile;
//--- }
//--- else if (m_parameterOrder == "poi")
//--- {
//--- indenterCompleteCallString = parameterParameterFile + parameterOuputFile + parameterInputFile;
//--- }
//--- else
//--- {
//--- indenterCompleteCallString = parameterInputFile + parameterOuputFile + parameterParameterFile;
//--- }
//---
//--- // Generate the indenter call string either for win32 or other systems.
//--- indenterCompleteCallString = "#!/bin/bash\n" + m_indenterExecutableCallString +
//--- indenterCompleteCallString;
//---
//--- // If the indenter writes to stdout pipe the output into a file
//--- if (m_outputFileParameter == "stdout")
//--- {
//--- indenterCompleteCallString = indenterCompleteCallString + " >" + m_outputFileName + ".tmp";
//--- }
//---
//--- // If the output filename is not the same as the input filename copy the output over the input.
//--- if (m_outputFileName != m_inputFileName)
//--- {
//--- replaceInputFileCommand = "mv " + m_outputFileName + ".tmp \"" + shellParameterPlaceholder +
//--- "\"\n";
//--- }
//---
//--- TQString shellScript(TemplateBatchScript::getTemplateBatchScript());
//--- shellScript = shellScript.replace("__INDENTERCALLSTRING2__",
//--- indenterCompleteCallString + "\n" + replaceInputFileCommand);
//--- indenterCompleteCallString = indenterCompleteCallString.replace("$1", "$file2indent");
//--- replaceInputFileCommand = replaceInputFileCommand.replace("$1", "$file2indent");
//--- shellScript = shellScript.replace("__INDENTERCALLSTRING1__",
//--- indenterCompleteCallString + "\n" + replaceInputFileCommand);
//---
//--- return shellScript;
//--- }
//---
/*
\brief Creates the content for a shell script that can be used as a external tool call
to indent an as parameter defined file.
*/
TQString IndentHandler::generateShellScript(const TQString &configFilename)
{
TQString indenterCompleteCallString;
TQString parameterInputFile;
TQString parameterOuputFile;
TQString parameterParameterFile;
TQString replaceInputFileCommand;
// Define the placeholder for parameter variables either in batch or bash programming.
TQString shellParameterPlaceholder = "$1";
parameterInputFile = " " + m_inputFileParameter + "\"" + shellParameterPlaceholder + "\"";
if (m_outputFileParameter != "none" && m_outputFileParameter != "stdout")
{
if (m_outputFileName == m_inputFileName)
{
parameterOuputFile = " " + m_outputFileParameter + "\"" + shellParameterPlaceholder + "\"";
}
else
{
parameterOuputFile = " " + m_outputFileParameter + m_outputFileName + ".tmp";
}
}
// If the config file name is empty it is assumed that all parameters are sent via command line
// call
if (m_globalConfigFilename.isEmpty())
{
parameterParameterFile = " " + getParameterString();
}
// else if needed add the parameter to the indenter call string where the config file can be
// found.
else if (m_useCfgFileParameter != "none")
{
parameterParameterFile = " " + m_useCfgFileParameter + "\"./" + configFilename + "\"";
}
// Assemble indenter call string for parameters according to the set order.
if (m_parameterOrder == "ipo")
{
indenterCompleteCallString = parameterInputFile + parameterParameterFile + parameterOuputFile;
}
else if (m_parameterOrder == "pio")
{
indenterCompleteCallString = parameterParameterFile + parameterInputFile + parameterOuputFile;
}
else if (m_parameterOrder == "poi")
{
indenterCompleteCallString = parameterParameterFile + parameterOuputFile + parameterInputFile;
}
else
{
indenterCompleteCallString = parameterInputFile + parameterOuputFile + parameterParameterFile;
}
// Generate the indenter call string either for win32 or other systems.
indenterCompleteCallString = "#!/bin/bash\n" + m_indenterExecutableCallString +
indenterCompleteCallString;
// If the indenter writes to stdout pipe the output into a file
if (m_outputFileParameter == "stdout")
{
indenterCompleteCallString = indenterCompleteCallString + " >" + m_outputFileName + ".tmp";
}
// If the output filename is not the same as the input filename copy the output over the input.
if (m_outputFileName != m_inputFileName)
{
replaceInputFileCommand = "mv " + m_outputFileName + ".tmp \"" + shellParameterPlaceholder +
"\"\n";
}
TQString shellScript(TemplateBatchScript::getTemplateBatchScript());
shellScript = shellScript.replace("__INDENTERCALLSTRING2__",
indenterCompleteCallString + "\n" + replaceInputFileCommand);
indenterCompleteCallString = indenterCompleteCallString.replace("$1", "$file2indent");
replaceInputFileCommand = replaceInputFileCommand.replace("$1", "$file2indent");
shellScript = shellScript.replace("__INDENTERCALLSTRING1__",
indenterCompleteCallString + "\n" + replaceInputFileCommand);
return shellScript;
}
//--- /*
//--- \brief Format \a sourceCode by calling the indenter.
//---
@ -669,12 +671,13 @@ TQString IndentHandler::getParameterString()
*/
void IndentHandler::saveConfigFile(const TQString &filePathName, const TQString &paramString)
{
//--- TQFile::remove(filePathName);
//--- TQFile cfgFile(filePathName);
//---
//--- cfgFile.open(TQFile::ReadWrite | TQFile::Text);
//--- cfgFile.write(paramString.toAscii());
//--- cfgFile.close();
TQFile::remove(filePathName);
TQFile cfgFile(filePathName);
cfgFile.open(IO_ReadWrite | IO_Translate);
TQCString paramCString = paramString.local8Bit();
cfgFile.writeBlock(paramCString.data(), paramCString.length());
cfgFile.close();
}
/*
@ -1548,22 +1551,22 @@ void IndentHandler::showIndenterManual() const
//--- TQDesktopServices::openUrl(manualReference);
}
//--- /*
//--- \brief Returns the name of the currently selected indenter.
//--- */
//--- TQString IndentHandler::getCurrentIndenterName()
//--- {
//--- TQString currentIndenterName = m_indenterSelectionCombobox->currentText();
//---
//--- // Remove the supported programming languages from indenters name, which are set in braces.
//--- if (currentIndenterName.find("(") > 0)
//--- {
//--- // Using index-1 to also leave out the blank before the brace.
//--- currentIndenterName = currentIndenterName.left(currentIndenterName.find("(") - 1);
//--- }
//---
//--- return currentIndenterName;
//--- }
/*
\brief Returns the name of the currently selected indenter.
*/
TQString IndentHandler::getCurrentIndenterName()
{
TQString currentIndenterName = m_indenterSelectionCombobox->currentText();
// Remove the supported programming languages from indenters name, which are set in braces.
if (currentIndenterName.find("(") > 0)
{
// Using index-1 to also leave out the blank before the brace.
currentIndenterName = currentIndenterName.left(currentIndenterName.find("(") - 1);
}
return currentIndenterName;
}
/*
\brief Shows a file open dialog to open an existing config file for the current indenter.
@ -1612,55 +1615,54 @@ void IndentHandler::saveasIndentCfgFileDialog()
*/
void IndentHandler::createIndenterCallShellScript()
{
//--- TQString shellScriptExtension;
//--- shellScriptExtension = "sh";
//---
//--- TQString fileExtensions = tr("Shell Script") + " (*." + shellScriptExtension + ");;" + tr(
//--- "All files") + " (*.*)";
//---
//--- TQString currentIndenterName = getCurrentIndenterName();
//--- currentIndenterName = currentIndenterName.replace(" ", "_");
//---
//--- TQString shellScriptFileName = TQFileDialog::getSaveFileName(this, tr(
//--- "Save shell script"), "call_" + currentIndenterName + "." + shellScriptExtension,
//--- fileExtensions);
//---
//--- // Saving has been canceled if the filename is empty
//--- if (shellScriptFileName.isEmpty())
//--- {
//--- return;
//--- }
//---
//--- // Delete any old file, write the new contents and set executable permissions.
//--- TQFile::remove(shellScriptFileName);
//--- TQFile outSrcFile(shellScriptFileName);
//--- if (outSrcFile.open(TQFile::ReadWrite | TQFile::Text))
//--- {
//--- TQString shellScriptConfigFilename = TQFileInfo(shellScriptFileName).baseName() + "." +
//--- TQFileInfo(m_globalConfigFilename).suffix();
//---
//--- // Get the content of the shell/batch script.
//--- TQString indenterCallShellScript = generateShellScript(shellScriptConfigFilename);
//---
//--- // Replace placeholder for script name in script template.
//--- indenterCallShellScript = indenterCallShellScript.replace("__INDENTERCALLSTRINGSCRIPTNAME__", TQFileInfo(
//--- shellScriptFileName).fileName());
//---
//--- outSrcFile.write(indenterCallShellScript.toAscii());
//--- // For non Windows systems set the files executable flag
//--- outSrcFile.setPermissions(
//--- outSrcFile.permissions() | TQFile::ExeOwner | TQFile::ExeUser | TQFile::ExeGroup);
//--- outSrcFile.close();
//---
//--- // Save the indenter config file to the same directory, where the shell srcipt was saved to,
//--- // because the script will reference it there via "./".
//--- if (!m_globalConfigFilename.isEmpty())
//--- {
//--- saveConfigFile(TQFileInfo(
//--- shellScriptFileName).path() + "/" + shellScriptConfigFilename,
//--- getParameterString());
//--- }
//--- }
TQString shellScriptExtension;
shellScriptExtension = "sh";
TQString fileExtensions = tr("Shell Script") + " (*." + shellScriptExtension + ");;" + tr(
"All files") + " (*.*)";
TQString currentIndenterName = getCurrentIndenterName();
currentIndenterName = currentIndenterName.replace(" ", "_");
TQString indenterScriptName = "call_" + currentIndenterName + "." + shellScriptExtension;
TQString shellScriptFileName = TQFileDialog::getSaveFileName(indenterScriptName,
fileExtensions, nullptr, nullptr, tr("Save shell script"));
// Saving has been canceled if the filename is empty
if (shellScriptFileName.isEmpty())
{
return;
}
// Delete any old file, write the new contents and set executable permissions.
TQFile::remove(shellScriptFileName);
TQFile outSrcFile(shellScriptFileName);
if (outSrcFile.open(IO_ReadWrite | IO_Translate))
{
TQString shellScriptConfigFilename = TQFileInfo(shellScriptFileName).baseName() + "." +
TQFileInfo(m_globalConfigFilename).extension(false);
// Get the content of the shell/batch script.
TQString indenterCallShellScript = generateShellScript(shellScriptConfigFilename);
// Replace placeholder for script name in script template.
indenterCallShellScript = indenterCallShellScript.replace("__INDENTERCALLSTRINGSCRIPTNAME__",
TQFileInfo(shellScriptFileName).fileName());
TQCString indenterString = indenterCallShellScript.local8Bit();
outSrcFile.writeBlock(indenterString.data(), indenterString.length());
// For non Windows systems set the files executable flag
outSrcFile.close();
int _ = std::system("chmod a+x " + shellScriptFileName.local8Bit());
// Save the indenter config file to the same directory, where the shell srcipt was saved to,
// because the script will reference it there via "./".
if (!m_globalConfigFilename.isEmpty())
{
saveConfigFile(TQFileInfo(shellScriptFileName).dirPath(true) + "/" + shellScriptConfigFilename,
getParameterString());
}
}
}
/*

@ -46,7 +46,7 @@ class IndentHandler : public TQWidget
IndentHandler(int indenterID, MainWindow *mainWindow = nullptr, TQWidget *parent = nullptr);
~IndentHandler();
///-- TQString generateShellScript(const TQString &configFilename);
TQString generateShellScript(const TQString &configFilename);
///-- TQString callIndenter(TQString sourceCode, TQString inputFileExtension);
bool loadConfigFile(const TQString &filePathName);
void resetToDefaultValues();
@ -55,7 +55,7 @@ class IndentHandler : public TQWidget
TQString getParameterString();
TQString getIndenterCfgFile();
///-- TQString getManual();
///-- TQString getCurrentIndenterName();
TQString getCurrentIndenterName();
void contextMenuEvent(TQContextMenuEvent *event);
signals:

@ -20,7 +20,7 @@
#include "TemplateBatchScript.h"
/*
\brief The only and static function of this class returns a batch or shell script
\brief The only static function of this class returns a batch or shell script
as string that can be used to call an indenter with the current settings from
the command line.

@ -29,4 +29,4 @@ class TemplateBatchScript
static const char* getTemplateBatchScript();
};
#endif // TEMPLATEBATCHSCRIPT_H
#endif
Loading…
Cancel
Save