<commit_before>#include "cmake.h"
#include "filesystem.h"
#include "dialogs.h"
#include "config.h"
#include "terminal.h"
#include <boost/regex.hpp>

CMake::CMake(const boost::filesystem::path &path) {
  const auto find_cmake_project=[this](const boost::filesystem::path &cmake_path) {
    for(auto &line: filesystem::read_lines(cmake_path)) {
      const boost::regex project_regex("^ *project *\\(.*$");
      boost::smatch sm;
      if(boost::regex_match(line, sm, project_regex))
        return true;
    }
    return false;
  };
  
  auto search_path=path;
  auto search_cmake_path=search_path;
  search_cmake_path+="/CMakeLists.txt";
  if(boost::filesystem::exists(search_cmake_path))
    paths.emplace(paths.begin(), search_cmake_path);
  if(find_cmake_project(search_cmake_path))
    project_path=search_path;
  else {
    do {
      search_path=search_path.parent_path();
      search_cmake_path=search_path;
      search_cmake_path+="/CMakeLists.txt";
      if(boost::filesystem::exists(search_cmake_path))
        paths.emplace(paths.begin(), search_cmake_path);
      if(find_cmake_project(search_cmake_path)) {
        project_path=search_path;
        break;
      }
    } while(search_path!=search_path.root_directory());
  }
  if(!project_path.empty()) {
    if(boost::filesystem::exists(project_path/"CMakeLists.txt") && !boost::filesystem::exists(project_path/"compile_commands.json"))
      create_compile_commands(project_path);
  }
}

bool CMake::create_compile_commands(const boost::filesystem::path &path) {
  Dialog::Message message("Creating "+path.string()+"/compile_commands.json");
  auto exit_status=Terminal::get().process(Config::get().terminal.cmake_command+" . -DCMAKE_EXPORT_COMPILE_COMMANDS=ON", path);
  message.hide();
  if(exit_status==EXIT_SUCCESS) {
#ifdef _WIN32 //Temporary fix to MSYS2's libclang
    auto compile_commands_path=path;
    compile_commands_path+="/compile_commands.json";
    auto compile_commands_file=filesystem::read(compile_commands_path);<commit_msg>Cleanup of cmake project search, no longer tried to search for cmakelists if starting/reaching a directory without cmakelists<commit_after>#include "cmake.h"
#include "filesystem.h"
#include "dialogs.h"
#include "config.h"
#include "terminal.h"
#include <boost/regex.hpp>

CMake::CMake(const boost::filesystem::path &path) {
  const auto find_cmake_project=[this](const boost::filesystem::path &cmake_path) {
    for(auto &line: filesystem::read_lines(cmake_path)) {
      const boost::regex project_regex("^ *project *\\(.*$");
      boost::smatch sm;
      if(boost::regex_match(line, sm, project_regex))
        return true;
    }
    return false;
  };
  
  auto search_path=path;
  while(true) {
    auto search_cmake_path=search_path/"CMakeLists.txt";
    if(boost::filesystem::exists(search_cmake_path))
      paths.emplace(paths.begin(), search_cmake_path);
    else
      break;
    if(find_cmake_project(search_cmake_path)) {
      project_path=search_path;
      break;
    }
    if(search_path==search_path.root_directory())
      break;
    search_path=search_path.parent_path();
  }
  
  if(!project_path.empty()) {
    if(boost::filesystem::exists(project_path/"CMakeLists.txt") && !boost::filesystem::exists(project_path/"compile_commands.json"))
      create_compile_commands(project_path);
  }
}

bool CMake::create_compile_commands(const boost::filesystem::path &path) {
  Dialog::Message message("Creating "+path.string()+"/compile_commands.json");
  auto exit_status=Terminal::get().process(Config::get().terminal.cmake_command+" . -DCMAKE_EXPORT_COMPILE_COMMANDS=ON", path);
  message.hide();
  if(exit_status==EXIT_SUCCESS) {
#ifdef _WIN32 //Temporary fix to MSYS2's libclang
    auto compile_commands_path=path;
    compile_commands_path+="/compile_commands.json";
    auto compile_commands_file=filesystem::read(compile_commands_path);
{'fraction_non_alphanumeric': 0.14023760330578514, 'fraction_numerical': 0.0015495867768595042, 'mean_word_length': 5.328431372549019, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '563232', 'n_tokens_mistral': 1303, 'n_tokens_neox': 1286, 'n_words': 184}