Refactored code into a class.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
19a9388137
commit
6efea73391
200
src/main.cpp
200
src/main.cpp
|
@ -14,18 +14,20 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <experimental/filesystem>
|
#include "version.hpp"
|
||||||
#include <iostream>
|
#include "xdgcfg.hpp"
|
||||||
#include <string>
|
|
||||||
#include <fstream>
|
|
||||||
#include <chrono>
|
|
||||||
#include <system_error>
|
|
||||||
#include <cstdlib>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <libconfig.h++>
|
#include <libconfig.h++>
|
||||||
#include <basedir.h>
|
#include <basedir.h>
|
||||||
#include "xdgcfg.hpp"
|
#include <unistd.h>
|
||||||
#include "version.hpp"
|
#include <chrono>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <exception>
|
||||||
|
#include <experimental/filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <system_error>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace fs = std::experimental::filesystem;
|
namespace fs = std::experimental::filesystem;
|
||||||
using std::cout;
|
using std::cout;
|
||||||
|
@ -34,12 +36,32 @@ using std::endl;
|
||||||
using std::chrono::system_clock;
|
using std::chrono::system_clock;
|
||||||
using std::chrono::hours;
|
using std::chrono::hours;
|
||||||
using std::chrono::duration_cast;
|
using std::chrono::duration_cast;
|
||||||
|
using std::string;
|
||||||
|
using std::vector;
|
||||||
|
|
||||||
string compiler = "g++ -x c++";
|
class Compilescript
|
||||||
fs::path cache_dir;
|
{
|
||||||
int clean_after_hours = 30 * 24;
|
private:
|
||||||
|
string _compiler;
|
||||||
|
fs::path _cache_dir;
|
||||||
|
int _clean_after_hours;
|
||||||
|
|
||||||
bool read_settings()
|
public:
|
||||||
|
Compilescript();
|
||||||
|
|
||||||
|
void read_settings();
|
||||||
|
void cleanup();
|
||||||
|
bool compile(const string &filename, char *argv[]);
|
||||||
|
void print_version();
|
||||||
|
};
|
||||||
|
|
||||||
|
Compilescript::Compilescript()
|
||||||
|
: _compiler("g++ -x c++")
|
||||||
|
, _clean_after_hours(30 * 24)
|
||||||
|
{}
|
||||||
|
|
||||||
|
|
||||||
|
void Compilescript::read_settings()
|
||||||
{
|
{
|
||||||
bool need_save = false;
|
bool need_save = false;
|
||||||
xdgcfg config("compilescript.cfg");
|
xdgcfg config("compilescript.cfg");
|
||||||
|
@ -48,106 +70,79 @@ bool read_settings()
|
||||||
|
|
||||||
if (cfg.exists("compiler"))
|
if (cfg.exists("compiler"))
|
||||||
{
|
{
|
||||||
compiler = cfg["compiler"].c_str();
|
_compiler = cfg["compiler"].c_str();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
cfg.add("compiler", libconfig::Setting::TypeString) = compiler;
|
cfg.add("compiler", libconfig::Setting::TypeString) = _compiler;
|
||||||
need_save = true;
|
need_save = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cfg.exists("clean_after_hours"))
|
if (cfg.exists("clean_after_hours"))
|
||||||
{
|
{
|
||||||
cfg.lookupValue("clean_after_hours", clean_after_hours);
|
cfg.lookupValue("clean_after_hours", _clean_after_hours);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
cfg.add("clean_after_hours",
|
cfg.add("clean_after_hours",
|
||||||
libconfig::Setting::TypeInt) = clean_after_hours;
|
libconfig::Setting::TypeInt) = _clean_after_hours;
|
||||||
need_save = true;
|
need_save = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cfg.exists("cache_dir"))
|
if (cfg.exists("cache_dir"))
|
||||||
{
|
{
|
||||||
cache_dir = cfg["cache_dir"].c_str();
|
_cache_dir = cfg["cache_dir"].c_str();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
xdgHandle xdg;
|
xdgHandle xdg;
|
||||||
xdgInitHandle(&xdg);
|
xdgInitHandle(&xdg);
|
||||||
cache_dir = xdgCacheHome(&xdg);
|
_cache_dir = xdgCacheHome(&xdg);
|
||||||
cache_dir /= "compilescript";
|
_cache_dir /= "compilescript";
|
||||||
xdgWipeHandle(&xdg);
|
xdgWipeHandle(&xdg);
|
||||||
}
|
}
|
||||||
if (!fs::is_directory(cache_dir))
|
if (!fs::is_directory(_cache_dir))
|
||||||
{
|
{
|
||||||
fs::create_directories(cache_dir);
|
fs::create_directories(_cache_dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (need_save)
|
if (need_save)
|
||||||
{
|
{
|
||||||
config.write();
|
config.write();
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void cleanup()
|
void Compilescript::cleanup()
|
||||||
{
|
{
|
||||||
for (const fs::directory_entry &entry
|
for (const auto &entry : fs::recursive_directory_iterator(_cache_dir))
|
||||||
: fs::recursive_directory_iterator(cache_dir))
|
|
||||||
{
|
{
|
||||||
if (fs::is_regular_file(entry))
|
if (!fs::is_regular_file(entry))
|
||||||
{
|
{
|
||||||
auto diff = system_clock::now() - fs::last_write_time(entry);
|
continue;
|
||||||
if (duration_cast<hours>(diff).count() > clean_after_hours)
|
}
|
||||||
{
|
|
||||||
fs::path current_path = entry.path();
|
|
||||||
std::error_code e;
|
|
||||||
|
|
||||||
while (fs::remove(current_path, e))
|
const auto diff = system_clock::now() - fs::last_write_time(entry);
|
||||||
|
if (duration_cast<hours>(diff).count() > _clean_after_hours)
|
||||||
|
{
|
||||||
|
fs::path current_path = entry.path();
|
||||||
|
std::error_code e;
|
||||||
|
|
||||||
|
while (fs::remove(current_path, e))
|
||||||
|
{
|
||||||
|
current_path = current_path.parent_path();
|
||||||
|
if (current_path == _cache_dir)
|
||||||
{
|
{
|
||||||
current_path = current_path.parent_path();
|
break;
|
||||||
if (current_path == cache_dir)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
bool Compilescript::compile(const string &filename, char *argv[])
|
||||||
{
|
{
|
||||||
if (!read_settings())
|
const fs::path original = fs::canonical(filename);
|
||||||
{
|
const fs::path source = _cache_dir / original;
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (argc <= 1)
|
|
||||||
{
|
|
||||||
cerr << "usage: " << argv[0] <<
|
|
||||||
" [file|--cleanup|--version] [arguments]\n";
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if (string(argv[1]) == "--cleanup")
|
|
||||||
{
|
|
||||||
cleanup();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if (string(argv[1]) == "--version")
|
|
||||||
{
|
|
||||||
cout << "compilescript " << global::version << endl <<
|
|
||||||
"Copyright (C) 2018, 2019 tastytea <tastytea@tastytea.de>\n"
|
|
||||||
"License GPLv3: GNU GPL version 3 <https://www.gnu.org/licenses/gpl-3.0.html>.\n"
|
|
||||||
"This program comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
|
|
||||||
"and you are welcome to redistribute it under certain conditions.\n";
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
const fs::path original = fs::canonical(argv[1]);
|
|
||||||
const fs::path source = cache_dir / original;
|
|
||||||
const fs::path binary = (source.string() + ".bin");
|
const fs::path binary = (source.string() + ".bin");
|
||||||
string compiler_arguments;
|
string compiler_arguments;
|
||||||
fs::create_directories(binary.parent_path());
|
fs::create_directories(binary.parent_path());
|
||||||
|
@ -170,7 +165,11 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
std::getline(in, buf);
|
std::getline(in, buf);
|
||||||
if (buf.substr(0, 16) == "//compilescript:")
|
if (buf.substr(0, 17) == "// compilescript:")
|
||||||
|
{
|
||||||
|
compiler_arguments = buf.substr(17);
|
||||||
|
}
|
||||||
|
else if (buf.substr(0, 16) == "//compilescript:")
|
||||||
{
|
{
|
||||||
compiler_arguments = buf.substr(16);
|
compiler_arguments = buf.substr(16);
|
||||||
}
|
}
|
||||||
|
@ -196,26 +195,73 @@ int main(int argc, char *argv[])
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
cerr << "ERROR: Could not open file: " << source << endl;
|
cerr << "ERROR: Could not open file: " << source << endl;
|
||||||
return 1;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
cerr << "ERROR: Could not open file: " << original << endl;
|
cerr << "ERROR: Could not open file: " << original << endl;
|
||||||
return 1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ret = std::system((compiler + " " + source.string() + " " +
|
const string command = _compiler + " " + source.string() + " "
|
||||||
compiler_arguments +
|
+ compiler_arguments + " -o " + binary.string();
|
||||||
" -o " + binary.string()).c_str());
|
int ret = std::system(command.c_str()); // NOLINT Doesn't apply here.
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
{
|
{
|
||||||
cerr << "ERROR: Compilation failed.\n";
|
cerr << "ERROR: Compilation failed.\n";
|
||||||
return 1;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
execv(binary.c_str(), &argv[1]);
|
execvp(binary.c_str(), &argv[1]); // NOLINT We know that argv[1] exists.
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Compilescript::print_version()
|
||||||
|
{
|
||||||
|
cout << "compilescript " << global::version << '\n' <<
|
||||||
|
"Copyright (C) 2018, 2019 tastytea <tastytea@tastytea.de>\n"
|
||||||
|
"License GPLv3: GNU GPL version 3 "
|
||||||
|
"<https://www.gnu.org/licenses/gpl-3.0.html>.\n"
|
||||||
|
"This program comes with ABSOLUTELY NO WARRANTY. This is free software,"
|
||||||
|
"\nand you are welcome to redistribute it under certain conditions.\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
const vector<string> args(argv, argv + argc);
|
||||||
|
|
||||||
|
Compilescript App;
|
||||||
|
App.read_settings();
|
||||||
|
|
||||||
|
if (args.size() <= 1)
|
||||||
|
{
|
||||||
|
cerr << "usage: " << args[0]
|
||||||
|
<< " [file|--cleanup|--version] [arguments]\n";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (args[1] == "--cleanup")
|
||||||
|
{
|
||||||
|
App.cleanup();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (args[1] == "--version")
|
||||||
|
{
|
||||||
|
App.print_version();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
App.compile(args[1], argv);
|
||||||
|
}
|
||||||
|
catch (const std::exception &e)
|
||||||
|
{
|
||||||
|
cerr << "Error: " << e.what() << endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
namespace global
|
namespace global
|
||||||
{
|
{
|
||||||
static constexpr char version[] = "@PROJECT_VERSION@";
|
static constexpr char version[] = "@PROJECT_VERSION@";
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // VERSION_HPP
|
#endif // VERSION_HPP
|
||||||
|
|
Reference in New Issue