2

Flutter: Speed up your workflow with a custom project creation script

 2 years ago
source link: https://blog.gskinner.com/archives/2022/02/flutter-speed-up-your-workflow-with-a-custom-project-creation-script.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Flutter: Speed up your workflow with a custom project creation script

For some reason, starting a new project always brings a small feeling of inherent joy and excitement. There is something about a clean slate that is just very comforting, the lack of constraints, the promise of the unknown!

However, one thing that can tend drain this joy a bit are the rote tasks involved in setting up a new project. This typically includes a number of steps:

  • remembering to properly configure the name and package of your project when running create
  • add your usual set of plugins to pubspec.yaml, one by one, either looking up versions on pub.dev, or using the flutter pub add cli
  • open main.dart and delete the starting boilerplate

While this is not much work at all (~5 minutes or less), it does get really boring after a few dozen times. One solution to this problem is to write a script that can take care of this stuff for us. So we went ahead and wrote one! It’s worth noting that, while we chose to use python, you could do this with nodejs, dart or any number of cli-based languages.

How does it work?

For the most part, the script just generates a massive command line string, and executes it. It then generates a new main.dart file at the end. We’ll walk through it below, or you could just read the code, it’s pretty self-explanatory.

First, create a new project with a proper name and package:

org = "com.example"
name = "app_name"
cmd = "flutter create --org " + org + " --project-name " + name + " .";
  1. org = "com.example"
  2. name = "app_name"
  3. cmd = "flutter create --org " + org + " --project-name " + name + " .";
org = "com.example"
name = "app_name"
cmd = "flutter create --org " + org + " --project-name " + name + " .";

Next, add a series of flutter pub add commands, adding the latest versions of all default dependencies to your pubspec:

# dependencies
packages = [
 "cached_network_image",
 ... etc
];
for p in packages: 
	cmd += " && flutter pub add " + packages[packages.index(p)]

# dev-dependencies
packages = [
 "build_runner",
 ... etc
];

for p in packages : 
	cmd += " && flutter pub add --dev " + packages [packages .index(p)]
  1. # dependencies
  2. packages = [
  3. "cached_network_image",
  4. ... etc
  5. for p in packages:
  6. cmd += " && flutter pub add " + packages[packages.index(p)]
  7. # dev-dependencies
  8. packages = [
  9. "build_runner",
  10. ... etc
  11. for p in packages :
  12. cmd += " && flutter pub add --dev " + packages [packages .index(p)]
# dependencies
packages = [
 "cached_network_image",
 ... etc
];
for p in packages: 
	cmd += " && flutter pub add " + packages[packages.index(p)]

# dev-dependencies
packages = [
 "build_runner",
 ... etc
];

for p in packages : 
	cmd += " && flutter pub add --dev " + packages [packages .index(p)]

Then run the command which will create the project and add any dependencies:

os.system(cmd);
  1. os.system(cmd);
os.system(cmd);

The final step is to replace the default main.dart with a minimalist version:

main = """import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Center(child: FlutterLogo()),
    );
  }
}"""

mainPath = "lib/main.dart"; 
os.remove(mainPath);
f = open(mainPath, "a")
f.write(main)
f.close()
  1. main = """import 'package:flutter/material.dart';
  2. void main() => runApp(const MyApp());
  3. class MyApp extends StatelessWidget {
  4. const MyApp({Key? key}) : super(key: key);
  5. @override
  6. Widget build(BuildContext context) {
  7. return const MaterialApp(
  8. home: Center(child: FlutterLogo()),
  9. mainPath = "lib/main.dart";
  10. os.remove(mainPath);
  11. f = open(mainPath, "a")
  12. f.write(main)
  13. f.close()
main = """import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Center(child: FlutterLogo()),
    );
  }
}"""

mainPath = "lib/main.dart"; 
os.remove(mainPath);
f = open(mainPath, "a")
f.write(main)
f.close()

Done!

Usage

Once the file is created, usage is simple:

  • copy the file into an empty folder
  • change the org and name values
  • run it with ./filename.py

After a few seconds you will have a clean project, fully set up and ready to go! No boilerplate to delete, and all dependencies are installed and set to the latest versions.

You will likely want to update the packages to the ones that you use the most, and tweak the main.dart to your style. This script is just one example of a set of defaults.

Note: On macOs and Linux, you will need to run chmod on the file in order to grant it execute permissions: chmod +x filename.py

Happy coding!


Need help building something cool in Flutter? We’d love to chat.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK