<commit_before>/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Build Suite.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "commandlineparser.h"

#include "command.h"
#include "commandlineoption.h"
#include "commandlineoptionpool.h"
#include "commandpool.h"
#include "../qbstool.h"
#include "../../shared/logging/consolelogger.h"

#include <logging/translator.h>
#include <tools/buildoptions.h>
#include <tools/cleanoptions.h>
#include <tools/error.h>
#include <tools/hostosinfo.h>
#include <tools/installoptions.h>
#include <tools/preferences.h>

#include <QCoreApplication>
#include <QDir>
#include <QTextStream>

#ifdef Q_OS_UNIX
#include <unistd.h>
#endif

namespace qbs {
using Internal::Tr;

class CommandLineParser::CommandLineParserPrivate
{
public:
    CommandLineParserPrivate();

    void doParse();
    Command *commandFromString(const QString &commandString) const;
    QList<Command *> allCommands() const;
    QString generalHelp() const;

    void setupProjectFile();
    void setupProgress();
    void setupLogLevel();
    void setupBuildOptions();

    QString propertyName(const QString &aCommandLineName) const;

    QStringList commandLine;
    Settings *settings;
    Command *command;
    QString projectFilePath;
    BuildOptions buildOptions;
    bool showProgress;
    CommandLineOptionPool optionPool;
    CommandPool commandPool;
};

CommandLineParser::CommandLineParser() : d(0)
{
}

CommandLineParser::~CommandLineParser()
{
    delete d;
}

void CommandLineParser::printHelp() const
{
    QTextStream stream(stdout);

    Q_ASSERT(d->command == d->commandPool.getCommand(HelpCommandType));
    const HelpCommand * const helpCommand = static_cast<HelpCommand *>(d->command);
    if (helpCommand->commandToDescribe().isEmpty()) {
        stream << "qbs " QBS_VERSION "\n";
        stream << d->generalHelp();
    } else {
        try {
            const Command * const commandToDescribe
                    = d->commandFromString(helpCommand->commandToDescribe());
            stream << commandToDescribe->longDescription();
        } catch (const Error &) {
            if (!QbsTool::tryToRunTool(helpCommand->commandToDescribe(),
                    QStringList(QLatin1String("--help")))) {
                throw;
            }
        }
    }
}

CommandType CommandLineParser::command() const
{
    return d->command->type();
}

QString CommandLineParser::projectFilePath() const
{
    return d->projectFilePath;
}

BuildOptions CommandLineParser::buildOptions() const
{
    return d->buildOptions;
}

CleanOptions CommandLineParser::cleanOptions() const
{
    Q_ASSERT(command() == CleanCommandType);
    CleanOptions options;
    options.cleanType = d->optionPool.allArtifactsOption()->enabled()
            ? CleanOptions::CleanupAll : CleanOptions::CleanupTemporaries;
    options.dryRun = buildOptions().dryRun;
    options.keepGoing = buildOptions().keepGoing;
    return options;
}

InstallOptions CommandLineParser::installOptions() const
{
    Q_ASSERT(command() == InstallCommandType || command() == RunCommandType);
    InstallOptions options;
    options.removeFirst = d->optionPool.removeFirstoption()->enabled();
    options.installRoot = d->optionPool.installRootOption()->installRoot();
    options.dryRun = buildOptions().dryRun;
    options.keepGoing = buildOptions().keepGoing;
    return options;
}

bool CommandLineParser::force() const
{
    return d->optionPool.forceOption()->enabled();
}

QStringList CommandLineParser::runArgs() const
{
    Q_ASSERT(d->command->type() == RunCommandType);
    return static_cast<RunCommand *>(d->command)->targetParameters();
}

QStringList CommandLineParser::products() const
{
    return d->optionPool.productsOption()->arguments();
}

bool CommandLineParser::showProgress() const
{
    return d->showProgress;
}

QString CommandLineParser::commandName() const
{
    return d->command->representation();
}

QString CommandLineParser::commandDescription() const
{
    return d->command->longDescription();
}

QList<QVariantMap> CommandLineParser::buildConfigurations() const
{
    // Key: build variant, value: properties. Empty key used for global properties.
    typedef QMap<QString, QVariantMap> PropertyMaps;
    PropertyMaps properties;

    const QString buildVariantKey = QLatin1String("qbs.buildVariant");
    QString currentKey = QString();
    QVariantMap currentProperties;
    foreach (const QString &arg, d->command->additionalArguments()) {
        const int sepPos = arg.indexOf(QLatin1Char(':'));
        if (sepPos == -1) { // New build variant found.
            properties.insert(currentKey, currentProperties);
            currentKey = arg;
            currentProperties.clear();
            continue;
        }
        const QString property = d->propertyName(arg.left(sepPos));
        if (property.isEmpty())
            qbsWarning() << Tr::tr("Ignoring empty property.");
        else if (property == buildVariantKey)
            qbsWarning() << Tr::tr("Refusing to overwrite special property '%1'.").arg(buildVariantKey);
        else<commit_msg>remove pointless assignment<commit_after>/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Build Suite.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "commandlineparser.h"

#include "command.h"
#include "commandlineoption.h"
#include "commandlineoptionpool.h"
#include "commandpool.h"
#include "../qbstool.h"
#include "../../shared/logging/consolelogger.h"

#include <logging/translator.h>
#include <tools/buildoptions.h>
#include <tools/cleanoptions.h>
#include <tools/error.h>
#include <tools/hostosinfo.h>
#include <tools/installoptions.h>
#include <tools/preferences.h>

#include <QCoreApplication>
#include <QDir>
#include <QTextStream>

#ifdef Q_OS_UNIX
#include <unistd.h>
#endif

namespace qbs {
using Internal::Tr;

class CommandLineParser::CommandLineParserPrivate
{
public:
    CommandLineParserPrivate();

    void doParse();
    Command *commandFromString(const QString &commandString) const;
    QList<Command *> allCommands() const;
    QString generalHelp() const;

    void setupProjectFile();
    void setupProgress();
    void setupLogLevel();
    void setupBuildOptions();

    QString propertyName(const QString &aCommandLineName) const;

    QStringList commandLine;
    Settings *settings;
    Command *command;
    QString projectFilePath;
    BuildOptions buildOptions;
    bool showProgress;
    CommandLineOptionPool optionPool;
    CommandPool commandPool;
};

CommandLineParser::CommandLineParser() : d(0)
{
}

CommandLineParser::~CommandLineParser()
{
    delete d;
}

void CommandLineParser::printHelp() const
{
    QTextStream stream(stdout);

    Q_ASSERT(d->command == d->commandPool.getCommand(HelpCommandType));
    const HelpCommand * const helpCommand = static_cast<HelpCommand *>(d->command);
    if (helpCommand->commandToDescribe().isEmpty()) {
        stream << "qbs " QBS_VERSION "\n";
        stream << d->generalHelp();
    } else {
        try {
            const Command * const commandToDescribe
                    = d->commandFromString(helpCommand->commandToDescribe());
            stream << commandToDescribe->longDescription();
        } catch (const Error &) {
            if (!QbsTool::tryToRunTool(helpCommand->commandToDescribe(),
                    QStringList(QLatin1String("--help")))) {
                throw;
            }
        }
    }
}

CommandType CommandLineParser::command() const
{
    return d->command->type();
}

QString CommandLineParser::projectFilePath() const
{
    return d->projectFilePath;
}

BuildOptions CommandLineParser::buildOptions() const
{
    return d->buildOptions;
}

CleanOptions CommandLineParser::cleanOptions() const
{
    Q_ASSERT(command() == CleanCommandType);
    CleanOptions options;
    options.cleanType = d->optionPool.allArtifactsOption()->enabled()
            ? CleanOptions::CleanupAll : CleanOptions::CleanupTemporaries;
    options.dryRun = buildOptions().dryRun;
    options.keepGoing = buildOptions().keepGoing;
    return options;
}

InstallOptions CommandLineParser::installOptions() const
{
    Q_ASSERT(command() == InstallCommandType || command() == RunCommandType);
    InstallOptions options;
    options.removeFirst = d->optionPool.removeFirstoption()->enabled();
    options.installRoot = d->optionPool.installRootOption()->installRoot();
    options.dryRun = buildOptions().dryRun;
    options.keepGoing = buildOptions().keepGoing;
    return options;
}

bool CommandLineParser::force() const
{
    return d->optionPool.forceOption()->enabled();
}

QStringList CommandLineParser::runArgs() const
{
    Q_ASSERT(d->command->type() == RunCommandType);
    return static_cast<RunCommand *>(d->command)->targetParameters();
}

QStringList CommandLineParser::products() const
{
    return d->optionPool.productsOption()->arguments();
}

bool CommandLineParser::showProgress() const
{
    return d->showProgress;
}

QString CommandLineParser::commandName() const
{
    return d->command->representation();
}

QString CommandLineParser::commandDescription() const
{
    return d->command->longDescription();
}

QList<QVariantMap> CommandLineParser::buildConfigurations() const
{
    // Key: build variant, value: properties. Empty key used for global properties.
    typedef QMap<QString, QVariantMap> PropertyMaps;
    PropertyMaps properties;

    const QString buildVariantKey = QLatin1String("qbs.buildVariant");
    QString currentKey;
    QVariantMap currentProperties;
    foreach (const QString &arg, d->command->additionalArguments()) {
        const int sepPos = arg.indexOf(QLatin1Char(':'));
        if (sepPos == -1) { // New build variant found.
            properties.insert(currentKey, currentProperties);
            currentKey = arg;
            currentProperties.clear();
            continue;
        }
        const QString property = d->propertyName(arg.left(sepPos));
        if (property.isEmpty())
            qbsWarning() << Tr::tr("Ignoring empty property.");
        else if (property == buildVariantKey)
            qbsWarning() << Tr::tr("Refusing to overwrite special property '%1'.").arg(buildVariantKey);
        else
{'fraction_non_alphanumeric': 0.1380463524653912, 'fraction_numerical': 0.0027998133457769483, 'mean_word_length': 4.116991643454039, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1445933', 'n_tokens_mistral': 3478, 'n_tokens_neox': 3408, 'n_words': 920}