smf_contribution_engine 0.3.0 copy "smf_contribution_engine: ^0.3.0" to clipboard
smf_contribution_engine: ^0.3.0 copied to clipboard

Engine for structured code transformations in Dart/Flutter (insert/replace widgets, modify arguments, lists and classes).

example/smf_contribution_engine_example.dart

// Patches the lib/main.dart of a Flutter project: adds an import and a call
// before runApp(). Run it with
// `dart run example/smf_contribution_engine_example.dart`.
//
// It prints:
//
//   import 'package:flutter/material.dart';
//   import 'package:shop_app/logging.dart';
//
//   void main() {
//     setUpLogging();
//     runApp(const MaterialApp(home: Placeholder()));
//   }
import 'dart:io';

import 'package:smf_contribution_engine/smf_contribution_engine.dart';

const _main = '''
import 'package:flutter/material.dart';

void main() {
  runApp(const MaterialApp(home: Placeholder()));
}
''';

Future<void> main() async {
  final project = Directory.systemTemp.createTempSync('smf_engine_example');
  try {
    final file = File('${project.path}/lib/main.dart')
      ..createSync(recursive: true)
      ..writeAsStringSync(_main);

    // Each contribution parses the file and finds its target by the names
    // in the source. The engine renders the placeholders in the text that a
    // contribution inserts: {{app_name_sc}} is app_name in snake_case.
    await PatchEngine(
      const [
        InsertImport(
          file: 'lib/main.dart',
          import: "import 'package:{{app_name_sc}}/logging.dart';",
        ),
        InsertIntoFunction(
          file: 'lib/main.dart',
          function: 'main',
          beforeStatement: 'runApp(',
          insert: 'setUpLogging();',
        ),
      ],
      projectRoot: project.path,
      mustacheVariables: const {'app_name': 'shop app'},
    ).applyAll();

    stdout.write(file.readAsStringSync());
  } finally {
    project.deleteSync(recursive: true);
  }
}
0
likes
160
points
278
downloads

Documentation

API reference

Publisher

verified publishersaymyframe.com

Weekly Downloads

Engine for structured code transformations in Dart/Flutter (insert/replace widgets, modify arguments, lists and classes).

Homepage
Repository (GitHub)
View/report issues
Contributing

Topics

#codemod #refactoring #ast #analysis #codegen

License

Apache-2.0 (license)

Dependencies

analyzer, dart_style, mason_logger, mustachex, path

More

Packages that depend on smf_contribution_engine