Commit b4df5861 authored by hjk's avatar hjk
Browse files

qtpromaker: refactoring

parent a757c558
......@@ -8,6 +8,24 @@
#include <QTextStream>
#include <QVector>
//
// Command line options
//
// Put everything into a single project.
static bool treeDepth = 0;
// FIXME: Make file classes configurable on the command line.
static const char *defaultExtensions[][2] =
{
{ "SOURCES", "cpp,c,C,cxx,c++" },
{ "HEADERS", "hpp,h,H,hxx,h++" },
{ "TRANSLATIONS", "ts" },
{ "FORMS", "ui" },
};
typedef QHash<QByteArray, QByteArray> Extensions;
class FileClass
{
......@@ -56,34 +74,54 @@ private:
QSet<QString> m_files;
};
class Runner
class Project
{
public:
Runner() {}
void run();
Project() {}
void create();
void addPath(const QDir &dir);
void addFileClass(const FileClass &fc) { m_fileClasses.append(fc); }
void writeProFile(const QString &fileName);
void setPaths(const QStringList &paths);
void setOutputFileName(const QString &fileName) { m_outputFileName = fileName; }
void writeProFile();
void setExtensions(const Extensions &extensions);
private:
//void addFileClass(const FileClass &fc) { m_fileClasses.append(fc); }
void handleDir(const QDir &dir);
QList<QDir> m_dirs;
QVector<FileClass> m_fileClasses;
Extensions m_extensions;
QString m_outputFileName;
};
void Runner::addPath(const QDir &dir)
void Project::setPaths(const QStringList &paths)
{
foreach (const QString &path, paths)
addPath(QDir(path));
}
void Project::setExtensions(const Extensions &extensions)
{
m_fileClasses.clear();
m_extensions = extensions;
Extensions::ConstIterator it = extensions.begin();
for ( ; it != extensions.end(); ++it)
m_fileClasses.append(FileClass(it.value(), it.key()));
}
void Project::addPath(const QDir &dir)
{
m_dirs.append(dir);
}
void Runner::run()
void Project::create()
{
for (int i = 0; i != m_dirs.size(); ++i)
handleDir(m_dirs.at(i));
}
void Runner::handleDir(const QDir &dir)
void Project::handleDir(const QDir &dir)
{
QDirIterator it(dir.path());
while (it.hasNext()) {
......@@ -102,16 +140,16 @@ void Runner::handleDir(const QDir &dir)
}
}
void Runner::writeProFile(const QString &fileName)
void Project::writeProFile()
{
QFile file(fileName);
QFile file(m_outputFileName);
file.open(QIODevice::ReadWrite);
QTextStream ts(&file);
ts << "######################################################################\n";
ts << "# Automatically generated by qtpromaker\n";
ts << "######################################################################\n\n";
ts << "TEMPLATE = app\n";
ts << "TARGET = " << QFileInfo(fileName).baseName() << "\n";
ts << "TARGET = " << QFileInfo(m_outputFileName).baseName() << "\n";
foreach (const FileClass &fc, m_fileClasses)
fc.writeProBlock(ts);
ts << "\nPATHS *=";
......@@ -128,38 +166,46 @@ int main(int argc, char *argv[])
QCoreApplication app(argc, argv);
QStringList args = app.arguments();
// Set up default values.
QDir dir = QDir::currentPath();
QString outFile = dir.dirName() + ".pro";
QString outputFileName = dir.dirName() + ".pro";
QStringList paths;
Extensions extensions;
for (int i = 0; i != sizeof(defaultExtensions)/sizeof(defaultExtensions[0]); ++i)
extensions[defaultExtensions[i][0]] = extensions[defaultExtensions[i][1]];
// Override by command line.
for (int i = 1, n = args.size(); i < n; ++i) {
const QString arg = args.at(i);
if (arg == "-h" || arg == "--help" || arg == "-help") {
qWarning() << "Usage: " << qPrintable(args.at(0))
<< " [-o out.pro] [dir...]";
return 1;
} else if (arg == "-o" && i < n - 1) {
outFile = args.at(++i);
} else {
paths.append(args.at(i));
}
bool handled = true;
if (i < n - 1) {
if (arg == "-o" || arg == "--output" || arg == "-output")
outputFileName = args.at(++i);
else if (arg == "-d" || arg == "--depth" || arg == "-depth")
treeDepth = args.at(++i).toInt();
else
handled = false;
}
// Nothing know. Treat it as path.
if (!handled)
paths.append(args.at(i));
}
if (paths.isEmpty())
paths.append(".");
Runner r;
// FIXME: Make file classes configurable on the command line.
r.addFileClass(FileClass("cpp,c,C,cxx,c++", "SOURCES"));
r.addFileClass(FileClass("hpp,h,H,hxx,h++", "HEADERS"));
r.addFileClass(FileClass("ts", "TRANSLATIONS"));
r.addFileClass(FileClass("ui", "FORMS"));
foreach (const QString &path, paths)
r.addPath(QDir(path));
r.run();
r.writeProFile(outFile);
// Run the thing.
Project p;
p.setExtensions(extensions);
p.setPaths(paths);
p.setOutputFileName(outputFileName);
p.create();
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment