1 | |
---|
2 | #include "cmt_sequence.h" |
---|
3 | #include "cmt_system.h" |
---|
4 | |
---|
5 | SequenceRunner::SequenceRunner () |
---|
6 | { |
---|
7 | } |
---|
8 | |
---|
9 | void SequenceRunner::begin () |
---|
10 | { |
---|
11 | started = false; |
---|
12 | running = false; |
---|
13 | if (style == CmtSystem::getenv ("CMTSTRUCTURINGSTYLE")) |
---|
14 | { |
---|
15 | vdir = CmtSystem::file_separator (); |
---|
16 | vdir += "v1"; |
---|
17 | } |
---|
18 | else |
---|
19 | { |
---|
20 | vdir = ""; |
---|
21 | } |
---|
22 | package = ""; |
---|
23 | filename = ""; |
---|
24 | buffer = ""; |
---|
25 | pwd = ""; |
---|
26 | } |
---|
27 | |
---|
28 | void SequenceRunner::filter (const cmt_string& line) |
---|
29 | { |
---|
30 | //cout << "line=[" << line << "]" << endl; |
---|
31 | |
---|
32 | CmtSystem::cmt_string_vector words; |
---|
33 | |
---|
34 | CmtSystem::split (line, " \t", words); |
---|
35 | |
---|
36 | cmt_string verb; |
---|
37 | |
---|
38 | if (words.size () > 0) verb = words[0]; |
---|
39 | |
---|
40 | if (verb.substr (0, 2) == "%%") |
---|
41 | { |
---|
42 | if (verb.substr (0, 3) == "%%%") return; |
---|
43 | |
---|
44 | if (running) |
---|
45 | { |
---|
46 | buffer.write (filename); |
---|
47 | //cout << "%%Writing file " << filename << endl; |
---|
48 | //cout << buffer << endl; |
---|
49 | buffer = ""; |
---|
50 | |
---|
51 | running = false; |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | if (verb == "%%package") |
---|
56 | { |
---|
57 | package = words[1]; |
---|
58 | version = words[2]; |
---|
59 | |
---|
60 | if (version == "") version = "v1"; |
---|
61 | |
---|
62 | cmt_string command = "cmt create "; |
---|
63 | command += package; |
---|
64 | command += " "; |
---|
65 | command += version; |
---|
66 | |
---|
67 | CmtSystem::execute (command); |
---|
68 | //cout << "%% creating package " << package << " with command [" << command << "]" << endl; |
---|
69 | |
---|
70 | if (style == CmtSystem::getenv ("CMTSTRUCTURINGSTYLE")) |
---|
71 | { |
---|
72 | vdir = CmtSystem::file_separator (); |
---|
73 | vdir += version; |
---|
74 | } |
---|
75 | else |
---|
76 | { |
---|
77 | vdir = ""; |
---|
78 | } |
---|
79 | |
---|
80 | filename = package; |
---|
81 | filename += vdir; |
---|
82 | filename += CmtSystem::file_separator (); |
---|
83 | filename += "cmt"; |
---|
84 | filename += CmtSystem::file_separator (); |
---|
85 | filename += "requirements"; |
---|
86 | |
---|
87 | started = true; |
---|
88 | } |
---|
89 | else if (verb == "%%file") |
---|
90 | { |
---|
91 | cmt_string file = words[1]; |
---|
92 | |
---|
93 | cmt_string d = package; |
---|
94 | d += vdir; |
---|
95 | d += CmtSystem::file_separator (); |
---|
96 | d += file; |
---|
97 | CmtSystem::dirname (d, d); |
---|
98 | |
---|
99 | CmtSystem::mkdir (d); |
---|
100 | //cout << "%% creating directory " << d << endl; |
---|
101 | |
---|
102 | buffer = ""; |
---|
103 | |
---|
104 | filename = package; |
---|
105 | filename += vdir; |
---|
106 | filename += CmtSystem::file_separator (); |
---|
107 | filename += file; |
---|
108 | started = true; |
---|
109 | } |
---|
110 | else if (verb == "%%cdpackage") |
---|
111 | { |
---|
112 | package = words[1]; |
---|
113 | |
---|
114 | pwd = package; |
---|
115 | pwd += vdir; |
---|
116 | pwd += CmtSystem::file_separator (); |
---|
117 | pwd += "cmt"; |
---|
118 | } |
---|
119 | else if (verb == "%%cmt") |
---|
120 | { |
---|
121 | cmt_string command; |
---|
122 | |
---|
123 | if (pwd != "") |
---|
124 | { |
---|
125 | command = "cd "; |
---|
126 | command += pwd; |
---|
127 | command += " "; |
---|
128 | command += CmtSystem::command_separator (); |
---|
129 | command += " "; |
---|
130 | } |
---|
131 | |
---|
132 | command += line; |
---|
133 | command.replace ("%%", ""); |
---|
134 | |
---|
135 | CmtSystem::execute (command); |
---|
136 | } |
---|
137 | else |
---|
138 | { |
---|
139 | buffer += line; |
---|
140 | buffer += "\n"; |
---|
141 | } |
---|
142 | |
---|
143 | if (started) |
---|
144 | { |
---|
145 | started = false; |
---|
146 | running = true; |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | void SequenceRunner::end () |
---|
151 | { |
---|
152 | if (running) |
---|
153 | { |
---|
154 | if (buffer != "") |
---|
155 | { |
---|
156 | buffer.write (filename); |
---|
157 | //cout << "%%Writing file " << filename << endl; |
---|
158 | //cout << buffer << endl; |
---|
159 | buffer = ""; |
---|
160 | } |
---|
161 | running = false; |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | |
---|