Code Monkey home page Code Monkey logo

pub-scaff's Introduction

Introduction

Scaffold Generator for Dart and Flutter.

scaff is a simple command-line utility for generating Dart and Flutter components from template files. It is a very tedious job to keep replicating the boilerplate codes every time you try to add a new component in your app. Using scaff, you can generate dart or flutter components from the custom-defined templates. You can even include template variables in the component files and directories name for easy and flexible scaffolding.

scaff uses 'Mustache templating library' variable schemes for defining and processing the template files.

Installation/Upgrade

$ dart pub global activate scaff

Usage

$ dart pub global run scaff

Example

Let us create a simple component. First of all, we need to create a working directory and it should contain a scaff.setup.json file. The scaff.setup.json file should contain all the template variables used in the working directory. The component subdirectories and files should be included inside the working directory. The files and directories name may contain template variables as well.

Template variable examples: {{var1}}, {{className}}Base, {{fileName}}_store

The example template directory structure:

component_templates
│   └── general_store_architecture
│       ├── scaff.setup.json
│       └── {{componentName}}
│           ├── {{componentName}}.dart
│           └── {{componentName}}_store.dart
  1. Create a new directory in the project root
$ mkdir -p component_templates/general_store_architecture
$ cd component_templates/general_store_architecture
  1. Create the component directory
$ mkdir {{componentName}}
$ cd {{componentName}}
  1. Create the component template file
$ touch {{componentName}}.dart
  1. Add the code to {{componentName}}.dart file
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../data/mobx/{{componentName}}_store.dart';

class {{className}}Screen extends StatelessWidget {
  {{className}}Screen({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final {{componentName}} = Provider.of<{{className}}Store>(context);

    return Scaffold(
      body: Center(
        child: Column(),
      ),
    );
  }
}
  1. Create the store template file
$ touch {{componentName}}_store.dart
  1. Add the code to {{componentName}}_store.dart file
import 'package:mobx/mobx.dart';

abstract class {{className}}StoreBase with Store {
  @observable
  bool dummyValue = false;
}
  1. Create the scaff.setup.json file
$ cd ..
$ touch scaff.setup.json
  1. Add all the template variables used in the working directory to scaff.setup.json file as
{
  "variables": [
	"componentName",
	"className"
  ],
  "mappedVariables": {
	"componentName": "login",
	"className": "LoginScreen"
  }
}
  • variables holds a list of template variables. The CLI will prompt for the user input.
  • mappedVariables holds the values for the template variables. The generator will pick values from the mappedVariables automatically, if required.
  • You may use either of the one or in combination. CLI will skip the prompt if the value for a template variable is already available inside the mappedVariables.
  1. cd into general_store_architecture folder.
$ pwd # it should be pointing to =>  /path/component_templates/general_store_architecture
  1. Run scaff globally
$ dart pub global run scaff
  1. You will be prompted to:
Enter source directory (/path/component_templates/general_store_architecture) »
Enter destination directory (/path/component_templates/general_store_architecture/__component__) »
Enter template extension (dart) » 
Enter 'componentName' variable value » login
Enter 'className' variable value » Login
  1. The destination directory will have the newly generated component. The destination directory structure:
└── login
    ├── login.dart
    └── login_store.dart
  • Development and debugging
dart pub global activate --source path /path/to/scaff
dart pub global run scaff

Buy me a coffee

Help me keep the app FREE and open for all. Paypal me: paypal.me/ganeshrvel

Contacts

Please feel free to contact me at [email protected]

About

License

scaff | Scaffold Generator for Dart and Flutter. MIT License.

Copyright © 2018-Present Ganesh Rathinavel

pub-scaff's People

Contributors

ganeshrvel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pub-scaff's Issues

Can't execute pub global run scaff successfully

When I try to run pub global run scaff every time I get an error:

Unhandled exception:
StdinException: Error getting terminal echo mode, OS Error: Inappropriate ioctl for device, errno = 25
#0      Stdin.echoMode (dart:io-patch/stdio_patch.dart:71:7)
#1      get (package:prompts/prompts.dart:60:27)
#2      CliParser.getCliStream (package:scaff/src/classes/cli_parser.dart:51:23)
#3      main (file:///Library/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/scaff-1.2.0+2/lib/src/main.dart:22:31)
#4      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
#5      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
pub finished with exit code 255

What should I do for avoiding this problem?

Scaff version: 1.2.0+2
OS: BigSur 11.2.1 (20D74)

Flutter 1.22.6 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 9b2d32b605 (6 weeks ago) • 2021-01-22 14:36:39 -0800
Engine • revision 2f0af37152
Tools • Dart 2.10.5

The folder structure is exactly the same as in your example.
Full regards.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.