Arkanjo 0.2
A tool for find code duplicated functions in codebases
Loading...
Searching...
No Matches
preprocessor_build.cpp
Go to the documentation of this file.
2#include <cassert>
3#include <iomanip>
4#include <iostream>
5
7
8using fm = FormatterManager;
9
10tuple<string, double, bool> PreprocessorBuild::read_parameters() {
11 fm::write(INITIAL_MESSAGE);
12 string similarity_message;
13
14 fm::write(PROJECT_PATH_MESSAGE);
15 string path_str;
16 cin >> path_str;
17 fs::path path(path_str);
18
19 fm::write(MINIMUM_SIMILARITY_MESSAGE);
20 cin >> similarity_message;
21 double similarity = stod(similarity_message);
22
23 bool use_duplication_finder_by_tool = false;
24
25 while (true) {
26 fm::write(MESSAGE_DUPLICATION_FINDER_TYPE_1);
27 fm::write(MESSAGE_DUPLICATION_FINDER_TYPE_2);
28 fm::write(MESSAGE_DUPLICATION_FINDER_TYPE_3);
29 int x;
30 cin >> x;
31 if (x == 1) {
32 use_duplication_finder_by_tool = true;
33 } else if (x == 2) {
34 use_duplication_finder_by_tool = false;
35 } else {
36 std::cerr << INVALID_CODE_DUPLICATION_FINDER << '\n';
37 exit(0);
38 continue;
39 }
40 break;
41 }
42
43 return {path, similarity, use_duplication_finder_by_tool};
44}
45
46void PreprocessorBuild::preprocess(const fs::path& path, double similarity, bool use_duplication_finder_by_tool) {
47 fm::write(BREAKER_MESSAGE);
48
49 fs::path base_path = Config::config().base_path / Config::config().name_container;
50
51 if (fs::exists(base_path)) {
52 fs::remove_all(base_path);
53 }
54
55 FunctionBreaker function_breaker(path);
56
57 fm::write(DUPLICATION_MESSAGE);
58
59 if (use_duplication_finder_by_tool) {
60 DuplicationFinderTool duplicationFinder(base_path, similarity);
61 duplicationFinder.execute();
62 } else {
63 DuplicationFinderDiff duplicationFinder(base_path, similarity);
64 duplicationFinder.execute();
65 }
66
68
69 fm::write(END_MESSAGE);
70}
71
73
75 fs::path base_path = Config::config().base_path / Config::config().name_container;
76 if (force_preprocess || !std::filesystem::exists(base_path / CONFIG_PATH)) {
77 auto [path, similarity, use_duplication_finder_by_tool] = read_parameters();
78 preprocess(path, similarity, use_duplication_finder_by_tool);
79 }
80}
81
82PreprocessorBuild::PreprocessorBuild(bool force_preprocess, const fs::path& path, double similarity) {
83 fs::path base_path = Config::config().base_path / Config::config().name_container;
84 if (force_preprocess || !std::filesystem::exists(base_path / CONFIG_PATH)) {
85 preprocess(path, similarity, true);
86 }
87}
88
90 auto it_name = options.args.find("name");
91 if (it_name != options.args.end()) {
92 Config::config().name_container = it_name->second;
93 }
94 auto it_json = options.args.find("json");
95 if (it_json != options.args.end()) {
96 throw CLIError("--json is not supported in this command.");
97 return false;
98 }
99 return true;
100}
101
102bool PreprocessorBuild::run([[maybe_unused]] const ParsedOptions& options) {
103 fs::path base_path = Config::config().base_path / Config::config().name_container;
104 auto [path, similarity, use_duplication_finder_by_tool] = read_parameters();
105 preprocess(path, similarity, use_duplication_finder_by_tool);
106
107 return true;
108}
Base class for CLI-related errors.
Definition cli_error.hpp:10
const CliOption * options() const final
static Config & config()
Gets the singleton configuration instance.
Definition config.cpp:37
fs::path base_path
Default base path for temporary files.
Definition config.hpp:42
fs::path name_container
Name of the cache container.
Definition config.hpp:44
Code duplication preprocessor.
Main code duplication detection tool.
static void write(const std::string &template_str, const std::vector< T > &data, enum Format effective=Format::AUTO, RowColorFn color_fn=nullptr, std::ostream &out=std::cout)
Main function extraction processor.
bool validate(const ParsedOptions &options) override
Validate the arguments already analyzed.
bool run(const ParsedOptions &options) override
Executes the main logic of the command.
static constexpr const char * CONFIG_PATH
Configuration file path.
static void save_current_run_params(const fs::path &path)
Saves preprocessing parameters for future runs.
Codebase preprocessing interface.