Git Product home page Git Product logo

Comments (3)

PeterJ504 avatar PeterJ504 commented on June 4, 2024 1

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

from mason.

felangel avatar felangel commented on June 4, 2024

Hi @ljoly83 👋
Thanks for opening an issue!

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

from mason.

ljoly83 avatar ljoly83 commented on June 4, 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';
  }
}

from mason.

Related Issues (20)

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.