创建和编写具有'execute'权限的文件?

  • 本文关键字:execute 权限 文件 创建 dart
  • 更新时间 :
  • 英文 :


我正在用dart编写一个bash脚本。

下面的代码创建一个文件。 但该文件没有"执行"权限。所以我无法通过执行./ex.sh.

new File('ex.sh').writeAsStringSync(script_str);

也许,我需要将文件统计设置为文件。 但我找不到任何 API。

还没有尝试过,但是如果您尝试一下呢:

new File('ex.sh').writeAsString(script_str).then((final File file) {
    return file.stat().then((final FileStat stat) => stat.mode = 777);
});

似乎此功能尚未实现。请参阅 code.google.com/p/dart/issues/detail?id=15078

作为解决方法,只需创建一个实用程序函数来运行chmod命令。

void _runBashCommandSync(GrinderContext context, String command, {String cwd, bool log: true}) {
  context.log(command);
  ProcessResult result =
    Process.runSync('/bin/bash', ['-c', command], workingDirectory: cwd);
  if (!log) return;
  if (result.stdout.isNotEmpty) {
    context.log(result.stdout);
  }
  if (result.stderr.isNotEmpty) {
    context.log(result.stderr);
  }
  if (result.exitCode > 0) {
    context.fail("exit code ${result.exitCode}");
  }
}
_runBashCommandSync(context, 'chmod 777 ex.sh');

最新更新