From 129ef2715582980883682661b7bc1daa52ee06c2 Mon Sep 17 00:00:00 2001 From: oyvang Date: Mon, 11 May 2015 11:38:03 +0200 Subject: [PATCH] Fiked Terminal compile and run functions --- juci/notebook.cc | 4 ++ juci/notebook.h | 1 + juci/terminal.cc | 96 ++++++++++++++++++++++++------------------------ juci/terminal.h | 9 +++-- juci/window.cc | 55 +++++++++++++++------------ 5 files changed, 91 insertions(+), 74 deletions(-) diff --git a/juci/notebook.cc b/juci/notebook.cc index 28a1e2e..4c6888e 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -564,6 +564,10 @@ void Notebook::Controller::PopupSetSize(Gtk::ScrolledWindow &scroll, } } +std::string Notebook::Controller::CurrentPagePath(){ + return text_vec_.at(CurrentPage())->path(); +} + void Notebook::Controller::FindPopupPosition(Gtk::TextView& textview, int popup_x, int popup_y, diff --git a/juci/notebook.h b/juci/notebook.h index 7784487..f3678cd 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -40,6 +40,7 @@ namespace Notebook { int CurrentPage(); Gtk::Box& entry_view(); Gtk::Notebook& Notebook(); + std::string CurrentPagePath(); void OnBufferChange(); void BufferChangeHandler(Glib::RefPtr buffer); diff --git a/juci/terminal.cc b/juci/terminal.cc index e512af0..9553dfe 100644 --- a/juci/terminal.cc +++ b/juci/terminal.cc @@ -2,14 +2,12 @@ #include #include + Terminal::View::View(){ scrolledwindow_.add(textview_); scrolledwindow_.set_size_request(-1,150); view_.add(scrolledwindow_); textview_.set_editable(false); - //Pango::TabArray tabsize; - //tabsize.set_tab(200,Pango::TAB_LEFT, 200); - //textview_.set_tabs(tabsize); } @@ -17,59 +15,61 @@ Terminal::Controller::Controller() { folder_command_ = ""; } -void Terminal::Controller::SetFolderCommand(std::string path) { - int pos = path.find_last_of("/\\"); - path.erase(path.begin()+pos,path.end()); - folder_command_ = "cd "+ path + "; "; +void Terminal::Controller::SetFolderCommand( boost::filesystem::path + CMake_path) { + path_ = CMake_path.string(); + folder_command_ = "cd "+ path_ + "; "; +} + +bool Terminal::Controller::Compile(){ + if (running.try_lock()) { + std::thread execute([=]() { + Terminal().get_buffer()->set_text(""); + ExecuteCommand("cmake ."); + if (ExistInConsole(cmake_sucsess)){ + ExecuteCommand("make"); + } + }); + execute.detach(); + running.unlock(); + if (ExistInConsole(make_built)) return true; + } + PrintMessage("juCi++ ERROR: Failed to compile project in directory" + + path + "\n"); + return false; } -void Terminal::Controller::CompileAndRun(std::string project_name) { - if (folder_command_=="") { - PrintMessage("juCi++ ERROR: Can not find project's CMakeList.txt\n"); - } else { - if (running.try_lock()) { - std::thread execute([=]() { - Terminal().get_buffer()->set_text(""); - ExecuteCommand("cmake ."); - if (ExistInConsole(cmake_sucsess)){ - ExecuteCommand("make"); - if (ExistInConsole(make_built)){ - if (FindExecutable(project_name)) { - ExecuteCommand("./"+project_name); - } else { - PrintMessage("juCi++ ERROR: Can not find Executable\n"); - } - } - } - }); - execute.detach(); - running.unlock(); - } - } +void Terminal::Controller::Run(std::string executable) { + if (running.try_lock()) { + std::thread execute([=]() { + ExecuteCommand("./"+executable); + }); + execute.detach(); + running.unlock(); + } } void Terminal::Controller::PrintMessage(std::string message){ Terminal().get_buffer()-> - insert(Terminal().get_buffer()-> end(),"> "+message); +y insert(Terminal().get_buffer()-> end(),"> "+message); } -bool Terminal::Controller::FindExecutable(std::string executable) { - std::string build = Terminal().get_buffer()->get_text(); - double pos = build.find(make_built); - Gtk::TextIter start = Terminal().get_buffer()->get_iter_at_offset(pos); - Gtk::TextIter end = Terminal().get_buffer()->get_iter_at_offset(pos); - while (!end.ends_line()) { - end.forward_char(); - } - build = Terminal().get_buffer()->get_text(start, end); - pos = build.find_last_of(" "); - std::cout << "FINNER NY POS" << std::endl; - build = build.substr(pos+1); - std::cout <<"BUILD TARGET = "<< build << std::endl; - std::cout << "EXECUTABLE FILE = "<< executable << std::endl; - if(build != executable) return false; - return true; -} +// bool Terminal::Controller::FindExecutable(std::string executable) { +// std::string build = Terminal().get_buffer()->get_text(); +// double pos = build.find(make_built); +// Gtk::TextIter start = Terminal().get_buffer()->get_iter_at_offset(pos); +// Gtk::TextIter end = Terminal().get_buffer()->get_iter_at_offset(pos); +// while (!end.ends_line()) { +// end.forward_char(); +// } +// build = Terminal().get_buffer()->get_text(start, end); +// pos = build.find_last_of(" "); +// build = build.substr(pos+1); +// std::cout << "DEBUG: BUILD TARGET = "<< build << std::endl; +// std::cout << "EDEBUG: ECUTABLE FILE = "<< executable << std::endl; +// if(build != executable) return false; +// return true; +// } bool Terminal::Controller::ExistInConsole(std::string string) { double pos = Terminal().get_buffer()-> diff --git a/juci/terminal.h b/juci/terminal.h index fe890fc..650758f 100644 --- a/juci/terminal.h +++ b/juci/terminal.h @@ -3,6 +3,7 @@ #include #include "gtkmm.h" +#include namespace Terminal { @@ -22,16 +23,18 @@ namespace Terminal { Controller(); Gtk::HBox& view() {return view_.view();} Gtk::TextView& Terminal(){return view_.textview();} - void SetFolderCommand(std::string path); - void CompileAndRun(std::string project_name); + void SetFolderCommand(boost::filesystem::path CMake_path); + void Run(std::string executable); + bool Compile(); private: void ExecuteCommand(std::string command); bool OnButtonRealeaseEvent(GdkEventKey* key); bool ExistInConsole(std::string string); - bool FindExecutable(std::string executable); + // bool FindExecutable(std::string executable); void PrintMessage(std::string message); Terminal::View view_; std::string folder_command_; + std::string path_; std::mutex running; const std::string cmake_sucsess = "Build files have been written to:"; const std::string make_built = "Built target"; diff --git a/juci/window.cc b/juci/window.cc index 5177995..537eea0 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -43,30 +43,39 @@ Window::Window() : notebook_.OnSaveFile(); }); - keybindings_.action_group_menu()->add(Gtk::Action::create("ProjectCompileAndRun", - "Compile And Run"), - Gtk::AccelKey(keybindings_.config_ - .key_map()["compile_and_run"]), - [this]() { - terminal_. - SetFolderCommand("/home/gm/ClionProjects/testi/CM.txt"); - std::string p = notebook_.directories().get_project_name("/home/gm/ClionProjects/testi"); - terminal_.CompileAndRun(p); - }); + keybindings_. + action_group_menu()-> + add(Gtk::Action::create("ProjectCompileAndRun", + "Compile And Run"), + Gtk::AccelKey(keybindings_.config_ + .key_map()["compile_and_run"]), + [this]() { + notebook_.OnSaveFile(); + std::string path = notebook_.CurrentPagePath(); + terminal_.SetFolderCommand(path); + if(terminal_.Compile()) { + std::string executable = notebook_.directories(). + getCmakeVarValue(path,"add_executable("); + terminal_.Run(executable) + } + }); - keybindings_.action_group_menu()->add(Gtk::Action::create("ProjectCompile", - "Compile"), - Gtk::AccelKey(keybindings_.config_ - .key_map()["compile"]), - [this]() { - terminal_. - SetFolderCommand("/home/gm/ClionProjects/testi/CM.txt"); - std::string p = notebook_.directories().get_project_name("/home/gm/ClionProjects/testi"); - terminal_.CompileAndRun(p); - }); - - this->signal_button_release_event(). - connect(sigc::mem_fun(*this,&Window::OnMouseRelease),false); + keybindings_. + action_group_menu()-> + add(Gtk::Action::create("ProjectCompile", + "Compile"), + Gtk::AccelKey(keybindings_.config_ + .key_map()["compile"]), + [this]() { + notebook_.OnSaveFile(); + std::string path = + notebook_.CurrentPagePath(); + terminal_.SetFolderCommand(path); + terminal_.Compile(); + }); + + this->signal_button_release_event(). + connect(sigc::mem_fun(*this,&Window::OnMouseRelease),false); terminal_.Terminal().signal_button_release_event(). connect(sigc::mem_fun(*this,&Window::OnMouseRelease),false);