File indexing completed on 2024-05-19 05:42:15

0001 // ct_lvtprj_create_prj_from_db.m.cpp                               -*-C++-*--
0002 
0003 /*
0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk>
0005 // SPDX-License-Identifier: Apache-2.0
0006 //
0007 // Licensed under the Apache License, Version 2.0 (the "License");
0008 // you may not use this file except in compliance with the License.
0009 // You may obtain a copy of the License at
0010 //
0011 //     http://www.apache.org/licenses/LICENSE-2.0
0012 //
0013 // Unless required by applicable law or agreed to in writing, software
0014 // distributed under the License is distributed on an "AS IS" BASIS,
0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016 // See the License for the specific language governing permissions and
0017 // limitations under the License.
0018 */
0019 
0020 #include <ct_lvtldr_nodestorage.h>
0021 #include <ct_lvtprj_projectfile.h>
0022 
0023 #include <cstdlib>
0024 #include <filesystem>
0025 #include <iostream>
0026 #include <vector>
0027 
0028 #include <QCommandLineOption>
0029 #include <QCommandLineParser>
0030 #include <QCoreApplication>
0031 #include <QString>
0032 #include <QStringList>
0033 
0034 using namespace Codethink::lvtprj;
0035 using namespace Codethink::lvtldr;
0036 
0037 namespace {
0038 
0039 struct CommandLineArgs {
0040     std::filesystem::path sourcePath;
0041     std::filesystem::path dbPath;
0042     std::filesystem::path output;
0043 };
0044 
0045 enum class CommandLineParseResult {
0046     Ok,
0047     Error,
0048     Help,
0049 };
0050 
0051 CommandLineParseResult parseCommandLine(QCommandLineParser& parser, CommandLineArgs& args)
0052 {
0053     parser.setApplicationDescription("Create a project file from a database file");
0054     parser.addPositionalArgument("DATABASE", "Path to the database (required)");
0055     parser.addPositionalArgument("OUTPUT", "Path to the output project file (required)");
0056     const QCommandLineOption helpOption = parser.addHelpOption();
0057 
0058     const QCommandLineOption sourcePath("source-path", "Path for source code", "SOURCE_PATH", "");
0059     parser.addOptions({sourcePath});
0060 
0061     if (!parser.parse(QCoreApplication::arguments())) {
0062         return CommandLineParseResult::Error;
0063     }
0064 
0065     if (parser.isSet(helpOption)) {
0066         return CommandLineParseResult::Help;
0067     }
0068 
0069     // positional arguments
0070     const QStringList posArgs = parser.positionalArguments();
0071     if (posArgs.size() < 2) {
0072         return CommandLineParseResult::Error;
0073     }
0074     args.dbPath = posArgs.at(0).toStdString();
0075     args.output = posArgs.at(1).toStdString();
0076     args.sourcePath = parser.value(sourcePath).toStdString();
0077     return CommandLineParseResult::Ok;
0078 }
0079 
0080 } // namespace
0081 
0082 int main(int argc, char **argv)
0083 {
0084     QCoreApplication app(argc, argv);
0085     QCommandLineParser parser;
0086     CommandLineArgs args;
0087 
0088     switch (parseCommandLine(parser, args)) {
0089     case CommandLineParseResult::Ok:
0090         break;
0091     case CommandLineParseResult::Error:
0092         parser.showHelp();
0093         Q_UNREACHABLE();
0094     case CommandLineParseResult::Help:
0095         parser.showHelp();
0096         Q_UNREACHABLE();
0097     }
0098 
0099     auto projectFile = ProjectFile{};
0100     projectFile.createEmpty().expect("Unexpected error preparing new project file");
0101 
0102     // TODO: Properly create project from Code Database file instead of manually copying them
0103     std::filesystem::remove(projectFile.codeDatabasePath());
0104     std::filesystem::remove(projectFile.cadDatabasePath());
0105     std::filesystem::copy(args.dbPath, projectFile.codeDatabasePath());
0106     // TODO: Create missing tables for CAD database
0107     std::filesystem::copy(args.dbPath, projectFile.cadDatabasePath());
0108 
0109     projectFile.saveAs(args.output, ProjectFile::BackupFileBehavior::Keep)
0110         .expect("Unexpected error saving the file to disk");
0111 
0112     return EXIT_SUCCESS;
0113 }