felangel / mason

Tools which allow developers to create and consume reusable templates called bricks.

Home Page:https://docs.brickhub.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

fix: Process.run not working on windows

ljoly83 opened this issue · comments

Description

I want to use Process.run on Windows, but I can't manage to make it work.

Steps To Reproduce
In my post_gen.dart:
var result = await Process.run('cmd.exe', ['echo test'], runInShell: true);
stdout.write(result.stdout);
stderr.write(result.stderr);

Expected Behavior
echo the test

** Result **
Microsoft Windows [version 10.0.22621.2861]
(c) Microsoft Corporation. Tous droits r‚serv‚s.

Hi @ljoly83 👋
Thanks for opening an issue!

Does your same code work outside the context of a mason hook?

Hi @felangel
Not sure to understand.
Here it is a simple DOS command echo and off course it works.
I try to use this postgen with windows:
https://github.com/hadiyaaamir/mason-bricks/blob/main/bricks/flutter_app_template/hooks/post_gen.dart
Here is my modified code:

import 'dart:io';

import 'package:mason/mason.dart';

Future<void> run(HookContext context) async {
  // final appName = context.vars['appName'].toString().snakeCase;
  // final appDirectory = Directory('${Directory.current.path}/$appName');
  final appDirectory = Directory('${Directory.current.path}');

  print('Running post_gen hook for ${appDirectory.path}');
  // Test command
  var result = await Process.run('cmd.exe', ['echo test'], runInShell: true);
  stdout.write(result.stdout);
  stderr.write(result.stderr);
  
  await Future.wait([
    // main app pubspec
    addAllDependencies(
      path: appDirectory.path,
      dependencies: [
        'dio',
        'intl',
        'equatable',
        'provider',
        'get_it',
        'package_info_plus',
        'path_provider',
        'flutter_animate',
        'animations',
        'page_transition',
        'flutter_spinkit'
        // 'authentication_repository --path="packages/authentication_repository"',
        // 'cache_client --path="packages/cache_client"',
        // 'localization --path="packages/localization"',
      ],
      devDependencies: ['mocktail'],
    ),

    // localization package
    addAllDependencies(
      path: '${appDirectory.path}/packages/localization',
      dependencies: ['easy_localization'],
      devDependencies: ['mocktail', 'test'],
    ),
  ]);

  String cdCommand = 'cd ${appDirectory.path}';
  String cleanAndGetCommand = 'flutter clean && flutter pub get';

  await Process.run(
      getOsCommand(), ['-c', '$cdCommand && $cleanAndGetCommand']);

  // await Process.run(getOsCommand(), ['-c', '$cleanAndGetCommand']);
  // // await Process.run('bash', ['-c', '$cdCommand/ios && pod install']);
  // await Process.start('flutter', ['clean'], runInShell: true);
  // await Process.start('flutter', ['get'], runInShell: true);

  // Delete gitkeep files
  if (!appDirectory.existsSync()) return;

  final gitKeepFiles = appDirectory
      .listSync(recursive: true, followLinks: false)
      .where((entity) => entity is File && entity.path.endsWith('.gitkeep'));

  for (final file in gitKeepFiles) {
    file.deleteSync();
  }
}

Future<void> addAllDependencies({
  required String path,
  required List<String> dependencies,
  required List<String> devDependencies,
}) async {
  await Future.wait([
    addDependencies(path: path, dependencies: dependencies),
    addDevDependencies(path: path, devDependencies: devDependencies),
  ]);
}

Future<void> addDependencies({
  required String path,
  required List<String> dependencies,
  bool devDependency = false,
}) async {
  final cdCommand = 'cd $path';
  final pubAddCommand = 'flutter pub add${devDependency ? ' -d' : ''}';

  print('Nb dependencies: ${dependencies.length}');
  await Future.wait(
    List.generate(
      dependencies.length,
      (index) {
        final command = '$cdCommand && $pubAddCommand ${dependencies[index]}';
        // print('Running: $command');
        // return Process.run(getOsCommand(), ['-c', command]);

        return Process.run(getOsCommand(), [command], runInShell: true);
        // var result = Process.run(getOsCommand(), [command], runInShell: true);
        // stdout.write(result.stdout);
        // stderr.write(result.stderr);
        // return result;
      },
    ),
  );

  // await Process.run('bash', ['-c', '$cdCommand && flutter pub get']);
  await Process.run(getOsCommand(), ['$cdCommand && flutter pub get'],
      runInShell: true);
}

Future<void> addDevDependencies({
  required String path,
  required List<String> devDependencies,
}) async {
  await addDependencies(
    path: path,
    dependencies: devDependencies,
    devDependency: true,
  );
}

String getOsCommand() {
  if (Platform.isWindows) {
    return 'cmd.exe';
  } else {
    return 'bash';
  }
}