import 'dart:io'; void main() { final props = File('lib/src/webdav/props.csv').readAsLinesSync().map((final line) => line.split(',')); final valueProps = []; final findProps = []; final variables = []; for (final prop in props) { final namespacePrefix = prop[0]; final namespaceVariable = convertNamespace(namespacePrefix); final type = prop[2]; final name = prop[1]; final variable = namespacePrefix + name.toLowerCase().replaceAll(RegExp('[^a-z]'), ''); valueProps.addAll([ "@annotation.XmlElement(name: '$name', namespace: $namespaceVariable, includeIfNull: false)", '$type? $variable;', ]); findProps.addAll([ "@annotation.XmlElement(name: '$name', namespace: $namespaceVariable, includeIfNull: false)", 'bool? $variable;', ]); variables.add(variable); } File('lib/src/webdav/props.dart').writeAsStringSync( [ '// ignore_for_file: public_member_api_docs', "import 'package:nextcloud/src/webdav/webdav.dart';", "import 'package:xml/xml.dart';", "import 'package:xml_annotation/xml_annotation.dart' as annotation;", "part 'props.g.dart';", '', ...generateClass('WebDavPropfindProp', 'prop', 'namespaceDav', findProps, variables), ...generateClass('WebDavProp', 'prop', 'namespaceDav', valueProps, variables), ...generateClass('WebDavOcFilterRules', 'filter-rules', 'namespaceOwncloud', valueProps, variables), ].join('\n'), ); } List generateClass( final String name, final String elementName, final String namespace, final List props, final List variables, ) => [ '@annotation.XmlSerializable(createMixin: true)', "@annotation.XmlRootElement(name: '$elementName', namespace: $namespace)", 'class $name with _\$${name}XmlSerializableMixin {', ' $name({', ...variables.map((final variable) => ' this.$variable,'), ' });', ' factory $name.fromXmlElement(final XmlElement element) => _\$${name}FromXmlElement(element);', ...props.map((final prop) => ' $prop'), '}', ]; String convertNamespace(final String namespacePrefix) { switch (namespacePrefix) { case 'dav': return 'namespaceDav'; case 'oc': return 'namespaceOwncloud'; case 'nc': return 'namespaceNextcloud'; case 'ocs': return 'namespaceOpenCollaborationServices'; case 'ocm': return 'namespaceOpenCloudMesh'; default: throw Exception('Unknown namespace prefix "$namespacePrefix"'); } }