A framework for building convergent cross-platform Nextcloud clients using Flutter.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.5 KiB

import 'dart:io';
void main() {
final props = File('lib/src/webdav/props.csv').readAsLinesSync().map((final line) => line.split(','));
final valueProps = <String>[];
final findProps = <String>[];
final variables = <String>[];
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<String> generateClass(
final String name,
final String elementName,
final String namespace,
final List<String> props,
final List<String> 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"');
}
}