12std::tuple<std::string, double, size_t, std::vector<std::string>,
Granularity>
13PreprocessorBuild::read_parameters(
const ParsedOptions& options) {
18 if (
auto path =
options.get(
"path")) {
21 if (!fs::exists(path_str)) {
22 std::cout << ERROR_PATH_MESSAGE <<
"\n";
27 if (path_str.empty()) {
31 fs::path path(path_str);
33 double similarity = 0.0;
35 if (
auto similarity_value =
options.get_as<
double>(
"similarity"))
36 similarity = *similarity_value;
38 if (similarity == 0.0) {
40 std::string similarity_message;
41 std::cin >> similarity_message;
42 similarity = stod(similarity_message);
45 size_t use_duplication_finder_index = 0;
46 bool method_from_option =
false;
48 if (
auto method =
options.get(
"method")) {
49 auto found = std::find_if(MethodsType.begin(), MethodsType.end(),
51 return std::to_string(mi.id) == *method || mi.name == *method;
53 if (found == MethodsType.end()) {
54 std::string valid_methods;
55 for (
size_t i = 0; i < MethodsType.size(); ++i) {
56 if (i > 0) valid_methods +=
", ";
57 valid_methods += std::to_string(MethodsType[i].
id);
59 valid_methods += MethodsType[i].name;
62 "Invalid --method value: '" +
63 std::string(*method) +
64 "'. Use " + valid_methods +
"."
67 use_duplication_finder_index = found->id - 1;
68 method_from_option =
true;
71 if (!method_from_option) {
73 fm::write(MESSAGE_DUPLICATION_FINDER_TYPE);
74 for (
size_t i = 0; i < MethodsType.size(); ++i) {
75 std::cout << i + 1 <<
") " << MethodsType[i].description <<
'\n';
77 std::cin >> use_duplication_finder_index;
79 use_duplication_finder_index == 0 ||
80 use_duplication_finder_index > MethodsType.size()
82 throw CLIError(INVALID_CODE_DUPLICATION_FINDER);
86 --use_duplication_finder_index;
91 if (
auto granularity_value =
options.get(
"granularity")) {
92 std::string_view granularity_option = *granularity_value;
93 if (granularity_option ==
"file") {
95 }
else if (granularity_option ==
"function") {
99 "Invalid --granularity value: '" +
100 std::string(granularity_option) +
101 "'. Use 'function' (default) or 'file'."
106 auto pass_through_args =
options.extra_args;
108 return {path, similarity, use_duplication_finder_index, pass_through_args, granularity};
111void PreprocessorBuild::preprocess(
const fs::path& path,
double similarity,
size_t use_duplication_finder_index,
112 const std::vector<std::string>& pass_through_args,
114 auto start = std::chrono::high_resolution_clock::now();
120 if (fs::exists(base_path)) {
121 fs::remove_all(base_path);
124 fm::write(
"Removed existing container folder");
127 fs::create_directories(base_path);
129 auto start_breaker = std::chrono::high_resolution_clock::now();
130 auto method = MethodsType[use_duplication_finder_index].create(
131 base_path, similarity, pass_through_args);
137 method->on_function(fd);
138 }, stats, minimum_lines, granularity);
141 fm::write(
"\tFound " + std::to_string(size_files) +
" files");
142 fm::write(
"\tSkipped " + std::to_string(stats.
errors) +
" parser errors");
143 fm::time(
"\tExecution time breaker:", start_breaker);
146 auto start_duplication = std::chrono::high_resolution_clock::now();
151 fm::time(
"\tExecution time duplication:", start_duplication);
162 if (force_preprocess || !std::filesystem::exists(base_path /
CONFIG_PATH)) {
163 preprocess(path, similarity, 0);
169 throw CLIError(
"--json is not supported in this command.");
172 if (
auto minimum_lines =
options.get_as<
int>(
"minimum-lines"))
173 this->minimum_lines = *minimum_lines;
174 }
catch (
const std::invalid_argument&) {
176 "--minimum-lines must be a valid number (passing " +
177 std::string(*
options.get(
"minimum-lines")) +
")"
179 }
catch (
const std::out_of_range&) {
180 throw CLIError(
"--minimum-lines outside the permitted range");
187 mode_verbose =
options.has(
"verbose");
190 auto [path, similarity, use_duplication_finder_index,
191 pass_through_args, granularity] = read_parameters(
options);
192 preprocess(path, similarity, use_duplication_finder_index,
193 pass_through_args, granularity);
Base class for CLI-related errors.
const CliOption * options() const final
static Config & config()
Gets the singleton configuration instance.
Main function extraction processor.
int process(const fs::path &folder_path, std::function< void(const FunctionData &)> on_function, SkipStats &stats, int minimum_lines=0, Granularity granularity=Granularity::Function)
Processes all files in directory.
static void save_current_run_params(const fs::path &path, const fs::path &cache_path)
Saves preprocessing parameters for future runs.
static constexpr const char * CONFIG_PATH
Configuration file path.
bool validate(const ParsedOptions &options) override
Validate the arguments already analyzed.
PreprocessorBuild()=default
bool run(const ParsedOptions &options) override
Executes the main logic of the command.
Configuration management interface.
Granularity
Comparison granularity used when breaking the codebase.
Codebase preprocessing interface.
std::filesystem::path base_path
Default base path for temporary files.
std::filesystem::path name_container
Name of the cache container.