<commit_before>// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#define FML_USED_ON_EMBEDDER

#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterHeadlessDartRunner.h"

#include <functional>
#include <memory>

#include "flutter/fml/message_loop.h"
#include "flutter/shell/common/engine.h"
#include "flutter/shell/common/rasterizer.h"
#include "flutter/shell/common/run_configuration.h"
#include "flutter/shell/common/shell.h"
#include "flutter/shell/common/switches.h"
#include "flutter/shell/common/thread_host.h"
#include "flutter/shell/platform/darwin/common/command_line.h"
#include "lib/fxl/functional/make_copyable.h"

static std::unique_ptr<shell::PlatformView> CreateHeadlessPlatformView(shell::Shell& shell) {
  return std::make_unique<shell::PlatformView>(shell, shell.GetTaskRunners());
}

static std::unique_ptr<shell::Rasterizer> CreateHeadlessRasterizer(shell::Shell& shell) {
  return std::make_unique<shell::Rasterizer>(shell.GetTaskRunners());
}

@implementation FlutterHeadlessDartRunner {
  shell::ThreadHost _threadHost;
  std::unique_ptr<shell::Shell> _shell;
}

- (void)runWithEntrypoint:(NSString*)entrypoint {
  if (_shell != nullptr || entrypoint.length == 0) {
    FXL_LOG(ERROR) << "This headless dart runner was already used to run some code.";
    return;
  }

  const auto label = "io.flutter.headless";

  // Create the threads to run the shell on.
  _threadHost = {
      label,                       // native thread label
      shell::ThreadHost::Type::UI  // managed threads to create
  };

  // Configure shell task runners.
  auto current_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
  auto single_task_runner = _threadHost.ui_thread->GetTaskRunner();
  blink::TaskRunners task_runners(label,                // dart thread label
                                  current_task_runner,  // platform
                                  single_task_runner,   // gpu
                                  single_task_runner,   // ui
                                  single_task_runner    // io
  );

  auto settings = shell::SettingsFromCommandLine(shell::CommandLineFromNSProcessInfo());

  // Create the shell. This is a blocking operation.
  _shell = shell::Shell::Create(
      std::move(task_runners),                                        // task runners
      std::move(settings),                                            // settings
      std::bind(&CreateHeadlessPlatformView, std::placeholders::_1),  // platform view creation
      std::bind(&CreateHeadlessRasterizer, std::placeholders::_1)     // rasterzier creation
  );

  if (_shell == nullptr) {
    FXL_LOG(ERROR) << "Could not start a shell for the headless dart runner with entrypoint: "
                   << entrypoint.UTF8String;
    return;
  }

  // Override the default run configuration with the specified entrypoint.
  _shell->GetTaskRunners().GetUITaskRunner()->PostTask(
      fxl::MakeCopyable([engine = _shell->GetEngine(),
                         config = shell::RunConfiguration::InferFromSettings(settings)]() mutable {
        if (!engine || !engine->Run(std::move(config))) {
          FXL_LOG(ERROR) << "Could not launch engine with configuration.";
        }
      }));
}

@end<commit_msg>Fix Dart project configuration for headless Dart runners. (#5461)<commit_after>// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#define FML_USED_ON_EMBEDDER

#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterHeadlessDartRunner.h"

#include <functional>
#include <memory>

#include "flutter/fml/message_loop.h"
#include "flutter/shell/common/engine.h"
#include "flutter/shell/common/rasterizer.h"
#include "flutter/shell/common/run_configuration.h"
#include "flutter/shell/common/shell.h"
#include "flutter/shell/common/switches.h"
#include "flutter/shell/common/thread_host.h"
#include "flutter/shell/platform/darwin/common/command_line.h"
#include "flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject_Internal.h"
#include "lib/fxl/functional/make_copyable.h"

static std::unique_ptr<shell::PlatformView> CreateHeadlessPlatformView(shell::Shell& shell) {
  return std::make_unique<shell::PlatformView>(shell, shell.GetTaskRunners());
}

static std::unique_ptr<shell::Rasterizer> CreateHeadlessRasterizer(shell::Shell& shell) {
  return std::make_unique<shell::Rasterizer>(shell.GetTaskRunners());
}

@implementation FlutterHeadlessDartRunner {
  shell::ThreadHost _threadHost;
  std::unique_ptr<shell::Shell> _shell;
}

- (void)runWithEntrypoint:(NSString*)entrypoint {
  if (_shell != nullptr || entrypoint.length == 0) {
    FXL_LOG(ERROR) << "This headless dart runner was already used to run some code.";
    return;
  }

  const auto label = "io.flutter.headless";

  // Create the threads to run the shell on.
  _threadHost = {
      label,                       // native thread label
      shell::ThreadHost::Type::UI  // managed threads to create
  };

  // Configure shell task runners.
  auto current_task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
  auto single_task_runner = _threadHost.ui_thread->GetTaskRunner();
  blink::TaskRunners task_runners(label,                // dart thread label
                                  current_task_runner,  // platform
                                  single_task_runner,   // gpu
                                  single_task_runner,   // ui
                                  single_task_runner    // io
  );

  auto settings = shell::SettingsFromCommandLine(shell::CommandLineFromNSProcessInfo());

  // Create the shell. This is a blocking operation.
  _shell = shell::Shell::Create(
      std::move(task_runners),                                        // task runners
      std::move(settings),                                            // settings
      std::bind(&CreateHeadlessPlatformView, std::placeholders::_1),  // platform view creation
      std::bind(&CreateHeadlessRasterizer, std::placeholders::_1)     // rasterzier creation
  );

  if (_shell == nullptr) {
    FXL_LOG(ERROR) << "Could not start a shell for the headless dart runner with entrypoint: "
                   << entrypoint.UTF8String;
    return;
  }

  FlutterDartProject* project =
      [[[FlutterDartProject alloc] initFromDefaultSourceForConfiguration] autorelease];

  auto config = project.runConfiguration;

  config.SetEntrypoint(entrypoint.UTF8String);

  // Override the default run configuration with the specified entrypoint.
  _shell->GetTaskRunners().GetUITaskRunner()->PostTask(
      fxl::MakeCopyable([engine = _shell->GetEngine(), config = std::move(config)]() mutable {
        if (!engine || !engine->Run(std::move(config))) {
          FXL_LOG(ERROR) << "Could not launch engine with configuration.";
        }
      }));
}

@end
{'fraction_non_alphanumeric': 0.11662389506700883, 'fraction_numerical': 0.0029940119760479044, 'mean_word_length': 3.1386430678466075, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5937647', 'n_tokens_mistral': 2065, 'n_tokens_neox': 1984, 'n_words': 435}