| 188 | } |
| 189 | |
| 190 | static void generate_gitfile(const char *gitfile, const std::vector<std::string> &desired_lines) { |
| 191 | // Reference: https://github.com/github/linguist/blob/master/docs/overrides.md#summary |
| 192 | auto lines = desired_lines; |
| 193 | auto generate = [&lines](const char *newline) { |
| 194 | std::string generated; |
| 195 | generated += "# cmkr"; |
| 196 | generated += newline; |
| 197 | for (const auto &line : lines) { |
| 198 | generated += line; |
| 199 | generated += newline; |
| 200 | } |
| 201 | return generated; |
| 202 | }; |
| 203 | if (!fs::exists(gitfile)) { |
| 204 | create_file(gitfile, generate("\n")); |
| 205 | } else { |
| 206 | auto contents = read_file(gitfile); |
| 207 | std::string line; |
| 208 | auto cr = 0, lf = 0; |
| 209 | auto flush_line = [&line, &lines]() { |
| 210 | auto itr = std::find(lines.begin(), lines.end(), line); |
| 211 | if (itr != lines.end()) { |
| 212 | lines.erase(itr); |
| 213 | } |
| 214 | line.clear(); |
| 215 | }; |
| 216 | for (size_t i = 0; i < contents.length(); i++) { |
| 217 | if (contents[i] == '\r') { |
| 218 | cr++; |
| 219 | continue; |
| 220 | } |
| 221 | if (contents[i] == '\n') { |
| 222 | lf++; |
| 223 | flush_line(); |
| 224 | } else { |
| 225 | line += contents[i]; |
| 226 | } |
| 227 | } |
| 228 | if (!line.empty()) { |
| 229 | flush_line(); |
| 230 | } |
| 231 | |
| 232 | if (!lines.empty()) { |
| 233 | // Append the cmkr .gitattributes using the detected newline |
| 234 | auto newline = cr == lf ? "\r\n" : "\n"; |
| 235 | if (!contents.empty() && contents.back() != '\n') { |
| 236 | contents += newline; |
| 237 | } |
| 238 | contents += newline; |
| 239 | contents += generate(newline); |
| 240 | create_file(gitfile, contents); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | void generate_project(const std::string &type) { |
| 246 | const auto name = escape_project_name(fs::current_path().stem().string()); |
no test coverage detected