Skip to content

Instantly share code, notes, and snippets.

@wilsonsilva
Created June 9, 2023 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilsonsilva/9399e55d89bd37f926e376d09eeb0311 to your computer and use it in GitHub Desktop.
Save wilsonsilva/9399e55d89bd37f926e376d09eeb0311 to your computer and use it in GitHub Desktop.
Renames MyApp and MainApp to App in the Flutter app templates
#!/bin/bash
# Get the location of the Flutter SDK
flutter_path=$(which flutter)
# Check if Flutter SDK is found
if [[ -z "$flutter_path" ]]; then
echo "Flutter SDK not found. Please install Flutter and add it to your PATH."
exit 1
fi
flutter_path=${flutter_path%/*/*}
# Get the path to the file to be patched
file_to_patch="$flutter_path/packages/flutter_tools/templates/app/lib/main.dart.tmpl"
# Check if the file exists
if [[ ! -f "$file_to_patch" ]]; then
echo "File not found!"
exit 1
fi
# Use awk to replace all instances of MyApp and MainApp to App
echo "Creating patch for ${file_to_patch}"
awk '
{ gsub("MyApp","App"); gsub("MainApp","App"); print }
' "$file_to_patch" > temp
# Show a diff before patching
echo "Here is the proposed change:"
diff --color -u "$file_to_patch" temp
# Ask the user if they want to proceed
read -p "Do you want to apply these changes? (y/n) " answer
case ${answer:0:1} in
y|Y )
echo "Applying changes..."
mv temp "$file_to_patch"
;;
* )
echo "Aborting..."
rm temp
;;
esac
@wilsonsilva
Copy link
Author

Run this every time you update the SDK. Use it with flutter create -e myapp to generate a minimal lib/main.dart.

Before:

import 'package:flutter/material.dart';

void main() {
  runApp(const MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: Center(
          child: Text('Hello World!'),
        ),
      ),
    );
  }
}

After:

import 'package:flutter/material.dart';

void main() {
  runApp(const App());
}

class App extends StatelessWidget {
  const App({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: Center(
          child: Text('Hello World!'),
        ),
      ),
    );
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment