getRawReleaseNotes method

Future<Iterable<Entry>> getRawReleaseNotes(
  1. int port,
  2. String changeLog,
  3. Map<String, String> promptByLang
)

Implementation

Future<Iterable<Entry>> getRawReleaseNotes(
  int port,
  String changeLog,
  Map<String, String> promptByLang,
) async {
  final server = await HttpServer.bind(InternetAddress.loopbackIPv4, port);
  final ciConfig = await CiConfig.getConfig(_CIPath.configPath);
  final langs = ciConfig.localizationLanguageList;

  final entries = {for (final ln in langs) ln: await _createEntry(ln)};

  final completer = Completer<Iterable<Entry>>();

  await for (final HttpRequest request in server) {
    // handle only requests to the root
    if (request.uri.path != '/') continue;

    try {
      final response = request.response;

      printInfo("Request [${request.uri.toString()}]");

      // TODO: check for max length

      entries.values.forEach((entry) {
        entry.clear();
      });

      final data = request.uri.queryParameters;
      final formSubmitted = data.containsKey('submit');
      for (final kv in data.entries) {
        final id = kv.key;
        final value = kv.value;

        entries.values.any((entry) => entry.update(id, value));
      }

      entries.values.forEach((entry) {
        final lang = entry.lang;
        final prompt = promptByLang[lang];
        if (prompt != null) {
          entry.values
              .updateAll((key, value) => value.isEmpty ? prompt : value);
        }
      });

      // TODO: error if default and stores values are set
      if (formSubmitted &&
          entries.values.every((entry) => entry.isAllRequiredValuesSet())) {
        completer.complete(entries.values);
        response
            .writeln("Succeed. Close the page and return to the console.");
        await response.close();
        break;
      } else {
        final noteTemplate = await _readTemplate("release_note");
        final entryTemplate = await _readTemplate("release_note_entry");
        final formTemplate = await _readTemplate("release_notes");

        final items = entries.values.map((entry) {
          return buildNote(
              noteTemplate,
              entry.map((type, id, value) => buildEntry(
                    entryTemplate,
                    id,
                    value,
                    entry.lang,
                    type,
                    isRequired: entry.isRequired,
                  )));
        }).join("\n");

        final text = formTemplate
            .replaceAll("%change-log%", changeLog)
            .replaceAll("%items%", items);

        response.headers.contentType = ContentType.html;
        response.statusCode = HttpStatus.ok;
        response.writeln(text);
        await response.close();
      }
    } catch (e, s) {
      printError('Handle request error: $e\n$s');
    }
  }

  return completer.future;
}