Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Process.run not working on windows #1191

Closed
ljoly83 opened this issue Dec 16, 2023 · 3 comments
Closed

fix: Process.run not working on windows #1191

ljoly83 opened this issue Dec 16, 2023 · 3 comments
Assignees
Labels
question Further information is requested

Comments

@ljoly83
Copy link

ljoly83 commented Dec 16, 2023

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.

@ljoly83 ljoly83 added the bug Something isn't working label Dec 16, 2023
@felangel
Copy link
Owner

felangel commented Jan 3, 2024

Hi @ljoly83 👋
Thanks for opening an issue!

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

@felangel felangel added question Further information is requested waiting for response Waiting for additional information and removed bug Something isn't working labels Jan 3, 2024
@ljoly83
Copy link
Author

ljoly83 commented Jan 5, 2024

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';
  }
}

@PeterJ504
Copy link

PeterJ504 commented May 1, 2024

I don't think you need cmd.exe

import 'dart:io';

import 'package:mason/mason.dart';

void run(HookContext context) async {
  String output = 'Test_2';
  var result0 = await Process.run('', ['echo', 'Test', '0'], runInShell: true);
  var result1 = await Process.run('echo', ['Test 1'], runInShell: true);
  var result2 =
      await Process.run('echo', [output, '$output'], runInShell: true);
  stdout.write(result0.stdout);
  stdout.write(result1.stdout);
  stdout.write(result2.stdout);
}

*** Result ***
Test 0
"Test 1"
Test_2 Test_2

@felangel felangel removed the waiting for response Waiting for additional information label May 1, 2024
@felangel felangel closed this as completed May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants