Beispiel #1
0
void MainWindow::generateOutput()
{
  m_tabWidget->clear();

  QString outputType = m_designWidget->outputType();
  m_loader->setTheme(outputType);

  if (outputType == "cpp")
    return generateCpp();

  Grantlee::Template classTemplate
      = m_engine->loadByName("class." + outputType);

  if (classTemplate->error()) {
    createOutputTab("Class", classTemplate->errorString());
    return;
  }

  Grantlee::Context c = m_designWidget->getContext();

  QString output;
  QTextStream textStream(&output);
  NoEscapeOutputStream stream(&textStream);

  classTemplate->render(&stream, &c);
  createOutputTab("Class", classTemplate->error() ? classTemplate->errorString()
                                                  : output);
}
void TestRunner::compile()
{
    // Check when is the last time that the test source has been modified
    const QString filePath = inDir_.filePath(fileName_);
    const QFileInfo fileInfo(filePath);
    const QDateTime lastModified = fileInfo.lastModified();

    // Compile if necessary
    Status s = status();
    bool notCompiledYet = (s == Status::NotCompiledYet) || (s == Status::CompileError);
    bool modified       = (lastCompiled_ < lastModified);
    bool processing     = (s == Status::Compiling) || (s == Status::Running);
    if (notCompiledYet || (modified && !processing))
    {
         setStatus_(Status::Compiling);
        compileOutput_.clear();
        lastCompiled_ = lastModified;

        // -------- Go to folder where to compile test --------

        QString compileDirPath = outDir_.absoluteFilePath(testName_);
        compileDir_ = outDir_;
        if (!compileDir_.cd(testName_))
        {
            // Create folder since it doesn't exist
            if (!outDir_.mkdir(testName_))
            {
                failCompilation_("Can't create build folder " + compileDirPath);
                return;
            }

            // Go to folder where to compile test
            // This is hard to reach (if we manage to create the folder, surely
            // we can cd to it), but doesn't hurt to check.
            if (!compileDir_.cd(testName_))
            {
                failCompilation_("Can't create build folder " + compileDirPath);
                return;
            }
        }


        // -------- Open all files for reading or writing --------

        const QString hFileName   = testName_ + ".gen.h";
        const QString cppFileName = testName_ + ".gen.cpp";
        const QString proFileName = testName_ + ".gen.pro";

        const QString hFilePath   = compileDir_.filePath(hFileName);
        const QString cppFilePath = compileDir_.filePath(cppFileName);
        const QString proFilePath = compileDir_.filePath(proFileName);

        QFile sourceFile(filePath);
        QFile hFile(hFilePath);
        QFile cppFile(cppFilePath);
        QFile proFile(proFilePath);

        if (!sourceFile.open(QFile::ReadOnly | QFile::Text))
        {
            failCompilation_("Can't open " + filePath);
            return;
        }

        if (!hFile.open(QFile::WriteOnly | QFile::Text))
        {
            failCompilation_("Can't write " + hFilePath);
            return;
        }

        if (!cppFile.open(QFile::WriteOnly | QFile::Text))
        {
            failCompilation_("Can't write " + cppFilePath);
            return;
        }

        if (!proFile.open(QFile::WriteOnly | QFile::Text))
        {
            failCompilation_("Can't write " + proFilePath);
            return;
        }


        // -------- Read source file --------

        QTextStream sourceStream(&sourceFile);
        const QString testSource = sourceStream.readAll();


        // -------- Generate and write test gen files --------

        QTextStream hStream(&hFile);
        QTextStream cppStream(&cppFile);
        QTextStream proStream(&proFile);

        hStream   << generateH(testName_, testSource);
        cppStream << generateCpp(testName_, testSource);
        proStream << generatePro(testName_, testSource);


        // -------- Run qmake --------

        QString program = QMAKE_QMAKE_QMAKE;
        QStringList arguments;
        arguments << "-spec" << QMAKE_QMAKESPEC << proFilePath;

        compileOutput_ +=
                getCurrentTime() +
                ": Starting: \"" +
                program + "\" " +
                arguments.join(' ') + "\n";

        process_->setWorkingDirectory(compileDirPath);
        connect(process_,
                static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
                this,
                &TestRunner::compile_onQmakeFinished_);

        emit outputChanged();

        process_->start(program, arguments);

        // -> go read qMakeFinished_(int exitCode) now.
    }
    else
    {
        emit compileFinished(true);
    }
}