# Copyright (c) 2020, the Dart project authors.  Please see the AUTHORS file
# for details. All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
"""Presubmit script for dart_ci repository.

See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
for more details about the presubmit API built into git cl.
"""

import subprocess


def _NeedsFormat(path):
  return subprocess.call(['dartfmt', '--set-exit-if-changed', '-n', path]) != 0


def _CheckDartFormat(input_api, output_api):
  files = [
      git_file.AbsoluteLocalPath()
      for git_file in input_api.AffectedTextFiles()
  ]
  unformatted_files = [
      path for path in files
      if path.endswith('.dart') and _NeedsFormat(path)
  ]
  if unformatted_files:
    escapedNewline = ' \\\n'
    return [
        output_api.PresubmitError(
            'File output does not match dartfmt.\n'
            'Fix these issues with:\n'
            'dartfmt -w%s%s' %
            (escapedNewline, escapedNewline.join(unformatted_files)))
    ]
  return []


def CommonChecks(input_api, output_api):
  return _CheckDartFormat(input_api, output_api)


CheckChangeOnCommit = CommonChecks
CheckChangeOnUpload = CommonChecks

# Copyright (c) 2020, the Dart project authors.  Please see the AUTHORS file
# for details. All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
"""Presubmit script for dart_ci repository.

See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
for more details about the presubmit API built into git cl.
"""

import subprocess


def _NeedsFormat(path):
  return subprocess.call(['dart', 'format', '--set-exit-if-changed',
                          '--output','none', path]) != 0


def _CheckDartFormat(input_api, output_api):
  files = [
      git_file.AbsoluteLocalPath()
      for git_file in input_api.AffectedTextFiles()
  ]
  unformatted_files = [
      path for path in files
      if path.endswith('.dart') and _NeedsFormat(path)
  ]
  if unformatted_files:
    escapedNewline = ' \\\n'
    return [
        output_api.PresubmitError(
            'File output does not match dartfmt.\n'
            'Fix these issues with:\n'
            'dart format%s%s' %
            (escapedNewline, escapedNewline.join(unformatted_files)))
    ]
  return []


def CommonChecks(input_api, output_api):
  return _CheckDartFormat(input_api, output_api)


CheckChangeOnCommit = CommonChecks
CheckChangeOnUpload = CommonChecks

Update presubmit to use 'dart format' command
Update presubmit to use 'dart format' command

The presubmit checks will then work with the Flutter SDK in the path.
This is the case when working on the Flutter web app current_results_ui.

Change-Id: I8a8d5db4454b57bc7936197032460e877037b386
Reviewed-on: https://dart-review.googlesource.com/c/dart_ci/+/191921
Reviewed-by: Alexander Thomas <29642742b6693024c89de8232f2e2542cf7eedf7@google.com>
