2017-02-28 01:23:38 +08:00
|
|
|
//
|
|
|
|
// Created by buda on 27/02/17.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <experimental/filesystem>
|
|
|
|
namespace fs = std::experimental::filesystem;
|
2017-04-11 20:55:57 +08:00
|
|
|
|
2017-06-21 17:29:13 +08:00
|
|
|
#include <glog/logging.h>
|
2017-07-06 19:53:39 +08:00
|
|
|
#include "gflags/gflags.h"
|
2017-06-21 17:29:13 +08:00
|
|
|
|
2017-06-13 22:25:08 +08:00
|
|
|
#include "query/frontend/stripped.hpp"
|
2017-04-19 21:52:20 +08:00
|
|
|
#include "utils/exceptions.hpp"
|
2017-04-11 20:55:57 +08:00
|
|
|
#include "utils/file.hpp"
|
2017-04-18 18:11:25 +08:00
|
|
|
#include "utils/string.hpp"
|
2017-04-11 20:55:57 +08:00
|
|
|
|
2017-04-18 18:11:25 +08:00
|
|
|
/**
|
|
|
|
* Reads a query from the file specified by the path argument.
|
|
|
|
* The first line of a query should start with "// Query: ". Query can be
|
|
|
|
* in more than one line but every line has to start with "//".
|
|
|
|
*
|
|
|
|
* @param path to the query file.
|
|
|
|
* @return query as a string.
|
|
|
|
*/
|
|
|
|
|
2017-07-06 19:53:39 +08:00
|
|
|
DEFINE_string(src, "tests/integration/hardcoded_queries",
|
|
|
|
"Path to sources of hardcoded queries.");
|
|
|
|
DEFINE_string(dst, "build/compiled/hardcode",
|
|
|
|
"Destination path of hardcoded queries");
|
|
|
|
|
2017-04-18 18:11:25 +08:00
|
|
|
std::string ExtractQuery(const fs::path &path) {
|
2017-04-11 20:55:57 +08:00
|
|
|
auto comment_mark = std::string("// ");
|
|
|
|
auto query_mark = comment_mark + std::string("Query: ");
|
2017-04-18 18:11:25 +08:00
|
|
|
auto lines = utils::ReadLines(path);
|
2017-04-11 20:55:57 +08:00
|
|
|
// find the line with a query (the query can be split across multiple
|
|
|
|
// lines)
|
2017-04-18 18:11:25 +08:00
|
|
|
for (int i = 0; i < static_cast<int>(lines.size()); ++i) {
|
2017-04-11 20:55:57 +08:00
|
|
|
// find query in the line
|
|
|
|
auto &line = lines[i];
|
|
|
|
auto pos = line.find(query_mark);
|
|
|
|
// if query doesn't exist pass
|
|
|
|
if (pos == std::string::npos) continue;
|
2017-04-18 18:11:25 +08:00
|
|
|
auto query = utils::Trim(line.substr(pos + query_mark.size()));
|
|
|
|
while (i + 1 < static_cast<int>(lines.size()) &&
|
2017-04-11 20:55:57 +08:00
|
|
|
lines[i + 1].find(comment_mark) != std::string::npos) {
|
|
|
|
query += lines[i + 1].substr(lines[i + 1].find(comment_mark) +
|
|
|
|
comment_mark.length());
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2017-04-19 21:52:20 +08:00
|
|
|
throw utils::BasicException("Unable to find query!");
|
2017-04-11 20:55:57 +08:00
|
|
|
}
|
2017-02-28 01:23:38 +08:00
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2017-07-06 19:53:39 +08:00
|
|
|
gflags::ParseCommandLineFlags(&argc, &argv, false);
|
2017-06-21 17:29:13 +08:00
|
|
|
google::InitGoogleLogging(argv[0]);
|
2017-02-28 01:23:38 +08:00
|
|
|
|
2017-07-06 19:53:39 +08:00
|
|
|
auto src_path = FLAGS_src;
|
2017-06-21 17:29:13 +08:00
|
|
|
LOG(INFO) << "Src path is: " << src_path;
|
2017-02-28 01:23:38 +08:00
|
|
|
permanent_assert(fs::exists(src_path), "src folder must exist");
|
|
|
|
|
2017-07-06 19:53:39 +08:00
|
|
|
auto dst_path = FLAGS_dst;
|
2017-06-21 17:29:13 +08:00
|
|
|
LOG(INFO) << "Dst path is: " << dst_path;
|
2017-02-28 01:23:38 +08:00
|
|
|
fs::create_directories(dst_path);
|
|
|
|
|
|
|
|
auto src_files = utils::LoadFilePaths(src_path, "cpp");
|
|
|
|
|
|
|
|
for (auto &src_file : src_files) {
|
2017-04-18 18:11:25 +08:00
|
|
|
auto query = ExtractQuery(src_file);
|
2017-06-08 00:28:31 +08:00
|
|
|
auto query_hash = query::StrippedQuery(query).hash();
|
2017-02-28 01:23:38 +08:00
|
|
|
auto dst_file = dst_path / fs::path(std::to_string(query_hash) + ".cpp");
|
|
|
|
fs::copy(src_file, dst_file, fs::copy_options::overwrite_existing);
|
2017-06-21 17:29:13 +08:00
|
|
|
LOG(INFO) << src_file << "- {(copy) -> " << dst_file;
|
2017-02-28 01:23:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
auto hpp_files = utils::LoadFilePaths(src_path, "hpp");
|
|
|
|
for (auto &hpp_file : hpp_files) {
|
|
|
|
fs::copy(hpp_file, dst_path / hpp_file.filename(),
|
|
|
|
fs::copy_options::overwrite_existing);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2017-04-11 20:55:57 +08:00
|
|
|
}
|