From d058f1f012b4c037a84dd77bc698d42734b8a7c5 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 29 Dec 2018 05:13:58 +0100 Subject: [PATCH] Simple files (without flags) work. --- CMakeLists.txt | 8 +++- src/main.cpp | 110 +++++++++++++++++++++++++++++++++++++++++++++++++ xdgcfg | 2 +- 3 files changed, 117 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b0ec709..11806fd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,12 +1,13 @@ cmake_minimum_required (VERSION 3.2) project(cppscript - VERSION 0.7.1 + VERSION 0 LANGUAGES CXX ) include(GNUInstallDirs) find_package(PkgConfig REQUIRED) pkg_check_modules(LIBXDG_BASEDIR REQUIRED libxdg-basedir) +pkg_check_modules(LIBCONFIG REQUIRED libconfig++) set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -17,8 +18,10 @@ set(CMAKE_CXX_FLAGS_DEBUG include_directories(${PROJECT_BINARY_DIR}) include_directories(${LIBXDG_BASEDIR_INCLUDE_DIRS}) +include_directories(${LIBCONFIG_INCLUDE_DIRS}) link_directories(${LIBXDG_BASEDIR_LIBRARY_DIRS}) +link_directories(${LIBCONFIG_LIBRARY_DIRS}) # Write version in header configure_file( @@ -29,6 +32,7 @@ configure_file( file(GLOB sources src/*.cpp) add_executable(${CMAKE_PROJECT_NAME} "${sources}") target_link_libraries(${CMAKE_PROJECT_NAME} - "${LIBXDG_BASEDIR_LDFLAGS} -lstdc++fs") + "${LIBXDG_BASEDIR_LDFLAGS} ${LIBCONFIG_LDFLAGS}" + "-lstdc++fs") install(TARGETS ${CMAKE_PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR}) #install(FILES README.md DESTINATION ${CMAKE_INSTALL_DOCDIR}) diff --git a/src/main.cpp b/src/main.cpp index 2b3a180..d8a85b3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,116 @@ +/* Public Domain / CC-0 + * Author: tastytea + */ + +#if __cplusplus >= 201703L + #include +#else + #include +#endif +#include +#include +#include +#include +#include +#include +#include +#include "xdgcfg.hpp" #include "version.hpp" +#if __cplusplus >= 201703L + namespace fs = std::filesystem; +#else + namespace fs = std::experimental::filesystem; +#endif +using std::cout; +using std::cerr; +using std::endl; + +bool need_save = false; +string compiler = "g++"; +fs::path cache_dir; + +void read_settings() +{ + xdgcfg config("cppscript.cfg"); + if (config.read() != 0) + { + config.write(); + } + libconfig::Setting &cfg = config.get_cfg().getRoot(); + + if (cfg.exists("compiler")) + { + compiler = cfg["compiler"].c_str(); + } + else + { + cfg.add("compiler", libconfig::Setting::TypeString) = compiler; + need_save = true; + } + + if (cfg.exists("cache_dir")) + { + cache_dir = cfg["cache_dir"].c_str(); + } + else + { + xdgHandle xdg; + xdgInitHandle(&xdg); + cache_dir = xdgCacheHome(&xdg); + cache_dir /= "cppscript"; + xdgWipeHandle(&xdg); + } + if (!fs::is_directory(cache_dir)) + { + fs::create_directories(cache_dir); + } + + if (need_save) + { + config.write(); + } +} + int main(int argc, char *argv[]) { + read_settings(); + + if (argc > 1) + { + const fs::path path(argv[1]); + const fs::path binary = cache_dir / path.stem(); + const fs::path source = binary.string() + ".cpp"; + + std::ifstream in(path); + if (in.is_open()) + { + std::ofstream out(source); + if (out.is_open()) + { + string buf; + std::getline(in, buf); + while (!in.eof()) + { + std::getline(in, buf); + out << buf << endl; + } + in.close(); + out.close(); + } + else + { + cerr << "ERROR: Could not open file: " << source << endl; + } + } + else + { + cerr << "ERROR: Could not open file: " << path << endl; + } + + std::system((compiler + " " + source.string() + + " -o " + binary.string()).c_str()); + execv(binary.c_str(), argv); + } return 0; } diff --git a/xdgcfg b/xdgcfg index 6c0976b..e22f82f 160000 --- a/xdgcfg +++ b/xdgcfg @@ -1 +1 @@ -Subproject commit 6c0976baa74f959ed3218af56e56ff58202a5a05 +Subproject commit e22f82fc6f1c40cda3d3ce5e671299f26f622528