writeToFile function
Implementation
Future<void> writeToFile() async {
final file = File('preview/lib/main.dart');
final parentPubspec = PubSpec.load();
final parent = parentPubspec.name.value;
previews.sort(
(a, b) {
return a.path.compareTo(b.path);
},
);
final imports = previews.indexed.map(
(element) {
final (index, preview) = element;
final relativePath = p.relative(preview.path, from: './lib');
return '''
import 'package:$parent/$relativePath' as p$index;
''';
},
).join('\n');
String? home;
final items = previews.indexed
.map(
(element) {
final (index, preview) = element;
return preview.builders
.map(
(builder) {
final buildername = builder.displayName;
final path =
'/${p.relative(preview.path, from: './lib')}#$buildername';
if (builder.parameters.where((p) => p.isNamed).isNotEmpty) {
alert(
'''
Named parameters for `preview` is not supported.
''',
);
return '';
}
final builderParams = builder.parameters.map((p) {
var defaultValue = p.defaultValueCode;
var type = p.type
.getDisplayString(withNullability: false)
.toVariableName();
var extra = '';
if (p.type.isDartCoreSet) {
type = 'set';
final subType =
(p.type as InterfaceType).typeArguments.first;
final values = valueSetAnnotation
.annotationsOf(p)
.firstOrNull
?.getField('values');
final set = values?.toSetValue();
final items = set
?.map((o) {
return o.toIntValue() ??
o.toBoolValue() ??
o.toDoubleValue() ??
o.toFunctionValue() ??
o.toStringValue() ??
o.toSymbolValue() ??
o.toTypeValue() ??
o.toString();
})
.join(',')
.replaceAllMapped(
RegExp(r'^(.+)$'),
(match) {
return '{${match[1]}}';
},
) ??
'p$index.$subType.values.toSet()';
extra = 'items: $items';
}
return '''
preview.input.\$$type(
${defaultValue == null || defaultValue.isEmpty ? '' : 'initialValue: $defaultValue,'}
title: '${p.displayName}',
$extra
)
''';
}).join(',');
home ??= path;
return '''
'$path': PreviewWidget(
build: (_, preview) => p$index.$buildername($builderParams),
)
''';
},
)
.where((s) => s.isNotEmpty)
.join(',');
},
)
.where((s) => s.isNotEmpty)
.join(',');
await file.create(recursive: true);
await file.writeAsString(
DartFormatter().format('''
// ignore_for_file: type=lint, unused_element, unnecessary_import, unused_import
// AUTO GENERATED BY hypen
// Do not modify this file.
import 'dart:html';
import 'package:flutter/material.dart';
import 'package:hypen/hypen.dart';
$imports
void main() {
runApp(const SaveScope(child: MainApp()));
}
final navKey = GlobalKey<NavigatorState>();
class MainApp extends StatefulWidget {
const MainApp({super.key});
@override
State<MainApp> createState() => _MainAppState();
}
class _MainAppState extends State<MainApp> {
String? initialPath;
@override
void initState() {
super.initState();
loadPreviousPreview();
}
void loadPreviousPreview() {
initialPath = window.localStorage['preview'];
}
void savePreview(String path) {
window.localStorage['preview'] = path;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Row(
children: [
SizedBox(
width: 250,
child: ListView.builder(
itemBuilder: (context, index) {
final path = items.keys.elementAt(index);
return ListTile(
onTap: () {
navKey.currentState?.pushReplacementNamed(path);
},
title: PreviewText(path),
);
},
itemCount: items.length,
),
),
VerticalDivider(width: 16),
Expanded(
child: Navigator(
key: navKey,
initialRoute: initialPath ?? '$home',
onGenerateRoute: (settings) {
savePreview(settings.name!);
return MaterialPageRoute(
builder: (context) {
return Center(
child: items[settings.name],
);
},
);
},
),
),
],
),
),
);
}
}
final items = <String, Widget>{
$items
};
'''),
);
}