Skip to content

Commit 28a3ff6

Browse files
committed
Finally got renzoku 【れんぞく】 to work
After spending most of the day trying to hack away at this code to get it to function in a most basic state, I have finally done it. :) renzoku is finally working! :D I'm so excited! From here on out, the priorities have shifted from "get it to work" to "get it to work well"! Yipee! My very first actual pet project that hasn't fallen by the wayside. Signed-off-by: Colton Wolkins (Ogre) <frostyfrog2@gmail.com>
1 parent 9990368 commit 28a3ff6

15 files changed

+320
-91
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CXXFLAGS+= -Wall -g -Isrc/ -lyaml-cpp
44
DEPS=
55
BIN_DIR = bin
66
OBJ_DIR = obj
7-
EXECUTABLE = builder
7+
EXECUTABLE = renzoku
88
TEST_EXECUTABLE = TestCode
99

1010
SOURCES:=config.cpp main.cpp signals.cpp tokenizer.cpp log.cpp process.cpp util.cpp exceptions.cpp watcher.cpp

renzoku.conf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
watch:
2+
Filter: ['*.cpp', '*.hpp', '*.c', '*.h', 'include/*.hxx']
3+
Include: Makefile
4+
Exclude: src/Config.cpp
5+
compile:
6+
Command: make
7+
Enabled: yes
8+
Test:
9+
command: make test
10+
IgnoreStatus: [2,3,4]
11+
Enabled: no
12+
program:
13+
command: ./bin/program -h
14+
Enabled: false

src/config.cpp

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <cstdio>
4242
#include <stdexcept>
4343
#include "util.hpp"
44+
#include "log.hpp"
4445

4546
Config::Config() :
4647
mWatch(),
@@ -63,21 +64,27 @@ Config::Config(std::istream& file) :
6364

6465
Config::~Config()
6566
{
67+
size_t i;
6668
if(mCompile.command != NULL) {
67-
if(mCompile.command[0] != NULL)
68-
delete [] mCompile.command[0];
69+
for(i = 0; mCompile.command[i]; i++)
70+
delete [] mCompile.command[i];
6971
delete [] mCompile.command;
7072
}
7173
if(mTest.command != NULL) {
72-
if(mTest.command[0] != NULL)
73-
delete [] mTest.command[0];
74+
for(i = 0; mTest.command[i]; i++)
75+
delete [] mTest.command[i];
7476
delete [] mTest.command;
7577
}
7678
if(mProgram.command != NULL) {
77-
if(mProgram.command[0] != NULL)
78-
delete [] mProgram.command[0];
79+
for(i = 0; mProgram.command[i]; i++)
80+
delete [] mProgram.command[i];
7981
delete [] mProgram.command;
8082
}
83+
for(Process* proc : processes)
84+
{
85+
if(proc)
86+
delete proc;
87+
}
8188
}
8289

8390
std::ifstream Config::findConfig()
@@ -102,7 +109,7 @@ void Config::parseConfig(std::istream& file)
102109
// Tester
103110
for (YAML::const_iterator it=config.begin();it!=config.end();++it) {
104111
assert(it->second.Type() == YAML::NodeType::Map);
105-
//std::cout << it->first << '-' << it->second << '\n';
112+
//Logger::getLogger()->log(DEBUG, "%s - %s\n", it->first.as<std::string>().c_str(), it->second.as<std::string>().c_str());
106113
std::string key = Util::lowercase_r(it->first.as<std::string>());
107114
if(key == "watch")
108115
{
@@ -111,17 +118,26 @@ void Config::parseConfig(std::istream& file)
111118
else if(key == "program")
112119
{
113120
this->parseCommand(it->second, mProgram);
121+
if(mProgram.command && mProgram.enabled)
122+
processes.push_back(new Process(mProgram.command, true));
114123
}
115124
else if(key == "test")
116125
{
117126
this->parseCommand(it->second, mTest);
127+
if(mTest.command && mTest.enabled)
128+
processes.push_back(new Process(mTest.command, true));
118129
}
119130
else if(key == "compile")
120131
{
121132
this->parseCommand(it->second, mCompile);
133+
if(mCompile.command && mCompile.enabled)
134+
processes.push_back(new Process(mCompile.command, true));
122135
}
123136
else
137+
{
124138
std::cerr << "Found an error! -" << key << '\n';
139+
throw std::runtime_error("Found a config error!");
140+
}
125141
}
126142
}
127143

@@ -139,6 +155,9 @@ void Config::parseCommand(const YAML::Node& node, iCommandConfig& config)
139155
{
140156
if(value.IsScalar())
141157
{
158+
Logger::getLogger()->log(DEBUG, "Proc: %s", value.as<std::string>().c_str());
159+
config.command = Util::parseCommand(value.as<std::string>());
160+
#if 0
142161
// TODO: We need to take quotes from the user and send them all as
143162
// a single argument instead of multiple arguments.
144163
std::string cnfval = value.as<std::string>();
@@ -164,6 +183,7 @@ void Config::parseCommand(const YAML::Node& node, iCommandConfig& config)
164183
}
165184
}
166185
}
186+
#endif
167187
}
168188
}
169189
else if( key == "enabled" ) {
@@ -175,14 +195,27 @@ void Config::parseCommand(const YAML::Node& node, iCommandConfig& config)
175195
// std::cout << "==" << config.command[i] << '\n';
176196
//}
177197
}
198+
199+
void Config::restartProcesses()
200+
{
201+
for(Process* proc : processes)
202+
{
203+
if(proc->kill())
204+
if(proc != processes.back())
205+
proc->runAndWait();
206+
else
207+
proc->run();
208+
}
209+
}
210+
178211
void Config::parseWatcher(const YAML::Node& node)
179212
{
213+
size_t asterisk_count;
180214
if(node.size() >= 1 && node.IsMap())
181215
for (YAML::const_iterator iter=node.begin();iter!=node.end();++iter) {
182216
std::string key = iter->first.as<std::string>();
183217
YAML::Node value = iter->second;
184218
Util::lowercase(key);
185-
//std::cout << key << std::endl;
186219
if(key == "filter")
187220
{
188221
if(!value.IsSequence())
@@ -191,8 +224,15 @@ void Config::parseWatcher(const YAML::Node& node)
191224
filter_iter!=value.end();
192225
++filter_iter)
193226
{
194-
//std::cout << "Filter: " << filter_iter->as<std::string>() << '\n';
195-
mWatch.filters.push_back(filter_iter->as<std::string>());
227+
asterisk_count = 0;
228+
std::string val = filter_iter->as<std::string>();
229+
for(size_t i = 0; i < val.length(); i++)
230+
if(val[i] == '*')
231+
asterisk_count++;
232+
Logger::getLogger()->log(DEBUG, "Filter: %s", val.c_str());
233+
if(asterisk_count > 1)
234+
throw std::runtime_error("Could not open file");
235+
mWatch.filters.push_back(val);
196236
}
197237
}
198238
else if(key == "include")
@@ -205,13 +245,13 @@ void Config::parseWatcher(const YAML::Node& node)
205245
filter_iter!=value.end();
206246
++filter_iter)
207247
{
208-
//std::cout << "Include: " << filter_iter->as<std::string>() << '\n';
248+
Logger::getLogger()->log(DEBUG, "Include: %s", filter_iter->as<std::string>().c_str());
209249
mWatch.include.push_back(filter_iter->as<std::string>());
210250
}
211251
}
212252
else if(value.IsScalar())
213253
{
214-
//std::cout << "Include: " << value.as<std::string>() << '\n';
254+
Logger::getLogger()->log(DEBUG, "Include: %s", value.as<std::string>().c_str());
215255
mWatch.include.push_back(value.as<std::string>());
216256
}
217257
}
@@ -225,18 +265,18 @@ void Config::parseWatcher(const YAML::Node& node)
225265
filter_iter!=value.end();
226266
++filter_iter)
227267
{
228-
//std::cout << "Exclude: " << filter_iter->as<std::string>() << '\n';
268+
Logger::getLogger()->log(DEBUG, "Exclude: %s", filter_iter->as<std::string>().c_str());
229269
mWatch.exclude.push_back(filter_iter->as<std::string>());
230270
}
231271
}
232272
else if(value.IsScalar())
233273
{
234-
//std::cout << "Exclude: " << value.as<std::string>() << '\n';
274+
Logger::getLogger()->log(DEBUG, "Exclude: %s", value.as<std::string>().c_str());
235275
mWatch.exclude.push_back(value.as<std::string>());
236276
}
237277
}
238278
else
239-
std::cout << "Value: " << value.as<std::string>() << '\n';
279+
Logger::getLogger()->log(DEBUG, "Value: %s\n", value.as<std::string>().c_str());
240280
}
241281
}
242282

src/config.hpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,20 @@
3838
#include <string>
3939
#include <istream>
4040
#include <yaml-cpp/yaml.h>
41+
#include "process.hpp"
4142

4243
#define CONFIG_NAME "renzoku.conf"
4344

44-
struct WatchConfig {
45+
struct iConfig {
46+
virtual ~iConfig() {}
47+
};
48+
49+
struct WatchConfig : public iConfig {
4550
std::vector<std::string> filters;
4651
std::vector<std::string> include;
4752
std::vector<std::string> exclude;
4853
};
49-
struct iCommandConfig {
54+
struct iCommandConfig : public iConfig {
5055
bool enabled = true;
5156
char** command = NULL;
5257
};
@@ -68,18 +73,17 @@ class Config {
6873
*/
6974
private:
7075
YAML::Node config;
71-
WatchConfig mWatch;
72-
CompileConfig mCompile;
73-
TestConfig mTest;
74-
ProgramConfig mProgram;
76+
#define CONFIG_SECTION(X) X##Config m##X;
77+
#include "config_sections.hpp"
78+
#undef CONFIG_SECTION
79+
std::vector<Process*> processes;
7580
void parseWatcher(const YAML::Node&);
7681
void parseCommand(const YAML::Node&, iCommandConfig&);
7782
void parseConfig(std::istream&);
7883
std::ifstream findConfig();
7984
public:
80-
std::vector<std::string> sections = {
81-
"test",
82-
#define CONFIG_SECTION(X) #X,
85+
std::vector<iConfig> sections = {
86+
#define CONFIG_SECTION(X) m##X,
8387
#include "config_sections.hpp"
8488
#undef CONFIG_SECTION
8589
};
@@ -90,4 +94,6 @@ class Config {
9094
inline CompileConfig getCompileConfig() { return mCompile; };
9195
inline TestConfig getTestConfig() { return mTest; };
9296
inline ProgramConfig getProgramConfig() { return mProgram; };
97+
inline std::vector<Process*> getProcesses() const { return processes; };
98+
void restartProcesses();
9399
};

src/config_sections.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ the fact that it needs to be included multiple times.
55
#ifndef CONFIG_SECTION
66
#error "CONFIG_SECTION macro hasn't been defined!"
77
#endif
8-
CONFIG_SECTION(Watcher)
9-
CONFIG_SECTION(Compiler)
10-
CONFIG_SECTION(Tester)
8+
9+
#ifndef COMPILE_STEP_POS
10+
#define COMPILE_STEP_POS 0
11+
#endif
12+
CONFIG_SECTION(Watch)
13+
CONFIG_SECTION(Compile)
14+
CONFIG_SECTION(Test)
1115
//CONFIG_SECTION(Coverage)
1216
//CONFIG_SECTION(BrowserRefresher)
13-
CONFIG_SECTION(Runner)
17+
CONFIG_SECTION(Program)

src/exceptions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "exceptions.hpp"
22

33
CException::CException(const std::string& message) :
4-
std::runtime_error(message + ": " + strerror(errno))
4+
std::runtime_error(message + ": " + strerror(errno) + " (" + std::to_string(errno) + ")")
55
{
66
}

src/log.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ Logger* Logger::removeLogger()
7272
return mLogger;
7373
}
7474

75-
Logger::Logger()
75+
Logger::Logger() :
76+
mMethod(STDHANDLE)
7677
{
78+
this->log(DEBUG, "Starting logging service for renzoku 【れんぞく】。");
7779
}
7880

7981
Logger::~Logger()

src/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "main.hpp"
3939
#include "signals.hpp"
4040
#include "config.hpp"
41+
#include "watcher.hpp"
4142

4243
volatile sig_atomic_t gRunning = 1;
4344

@@ -49,6 +50,9 @@ int main(int argc, char** argv) {
4950
//watcher();
5051
Config config;
5152
printf("%s\n", config.getCompileConfig().command[0]);
53+
Watcher w("./", config, true);
54+
while(gRunning)
55+
w.listen();
5256
return EXIT_SUCCESS;
5357
}
5458

0 commit comments

Comments
 (0)