createController method

Future<void> createController(
  1. String name, {
  2. String withArgument = '',
  3. String onCommand = '',
})

Implementation

Future<void> createController(String name,
    {String withArgument = '', String onCommand = ''}) async {
  var sample = ControllerSample('', name, PubspecUtils.isServerProject);
  if (withArgument.isNotEmpty) {
    if (isURL(withArgument)) {
      var res = await get(Uri.parse(withArgument));
      if (res.statusCode == 200) {
        var content = res.body;
        sample.customContent = replaceVars(content, name);
      } else {
        throw CliException(
            LocaleKeys.error_failed_to_connect.trArgs([withArgument]));
      }
    } else {
      var file = File(withArgument);
      if (file.existsSync()) {
        var content = file.readAsStringSync();
        sample.customContent = replaceVars(content, name);
      } else {
        throw CliException(
            LocaleKeys.error_no_valid_file_or_url.trArgs([withArgument]));
      }
    }
  }
  var controllerFile = handleFileCreate(
    name,
    'controller',
    onCommand,
    true,
    sample,
    'controllers',
  );

  var binindingPath =
      findBindingFromName(controllerFile.path, basename(onCommand));
  var pathSplit = Structure.safeSplitPath(controllerFile.path);
  pathSplit.remove('.');
  pathSplit.remove('lib');
  if (binindingPath.isNotEmpty) {
    addDependencyToBinding(binindingPath, name, pathSplit.join('/'));
  }
}