Skip to content

Commit

Permalink
Remove devtools_shared dependency on package:io (flutter#6132)
Browse files Browse the repository at this point in the history
  • Loading branch information
kenzieschmoll authored Aug 1, 2023
1 parent d20816e commit 4f13092
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 3 deletions.
3 changes: 1 addition & 2 deletions packages/devtools_shared/lib/src/devtools_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ abstract class ExtensionsApi {
static const extensionsResultPropertyName = 'extensions';

/// Returns and optionally sets the enabled state for a DevTools extension.
static const apiExtensionEnabledState =
'${apiPrefix}extensionEnabledState';
static const apiExtensionEnabledState = '${apiPrefix}extensionEnabledState';

/// The property name for the query parameter passed along with
/// [apiExtensionEnabledState] requests to the server that describes the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import 'dart:io';

import 'package:io/io.dart';
import 'package:path/path.dart' as path;

import 'extension_model.dart';
Expand Down Expand Up @@ -131,6 +130,38 @@ class ExtensionsManager {
}
}

// NOTE: this code is copied from `package:io`:
// https://github.com/dart-lang/io/blob/master/lib/src/copy_path.dart.
/// Copies all of the files in the [from] directory to [to].
///
/// This is similar to `cp -R <from> <to>`:
/// * Symlinks are supported.
/// * Existing files are over-written, if any.
/// * If [to] is within [from], throws [ArgumentError] (an infinite operation).
/// * If [from] and [to] are canonically the same, no operation occurs.
///
/// Returns a future that completes when complete.
Future<void> copyPath(String from, String to) async {
if (path.canonicalize(from) == path.canonicalize(to)) {
return;
}
if (path.isWithin(from, to)) {
throw ArgumentError('Cannot copy from $from to $to');
}

await Directory(to).create(recursive: true);
await for (final file in Directory(from).list(recursive: true)) {
final copyTo = path.join(to, path.relative(file.path, from: from));
if (file is Directory) {
await Directory(copyTo).create(recursive: true);
} else if (file is File) {
await File(file.path).copy(copyTo);
} else if (file is Link) {
await Link(copyTo).create(await file.target(), recursive: true);
}
}
}

/// TODO(kenz): remove this class. This is copied from
/// package:extension_discovery, which is drafed here:
/// https://github.com/dart-lang/tools/pull/129. Remove this temporary copy once
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) 2023, 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.

@TestOn('vm')

import 'dart:io';

import 'package:devtools_shared/devtools_extensions.dart';
import 'package:path/path.dart' as p;
import 'package:test/test.dart';

void main() {
late Directory from;
late Directory to;

tearDown(() {
// Delete [to] first so that we do not hit a file system exception when [to]
// is a subdirectory of [from].
to.deleteSync(recursive: true);
from.deleteSync(recursive: true);
});

test('copyPath', () async {
from = _createFromDir();
to = _createToDir();

await copyPath(from.path, to.path);
const expected =
"[Directory: 'tmp/bar', File: 'tmp/bar/baz.txt', File: 'tmp/foo.txt']";
final fromContents = _contentAsOrderedString(from);
final toContents = _contentAsOrderedString(to);
expect(fromContents.toString(), expected);
expect(toContents.toString(), expected.replaceAll('tmp', 'tmp2'));
});

test('copy path throws for infinite operation', () async {
from = _createFromDir();
to = Directory(p.join(from.path, 'bar'));
expect(to.existsSync(), isTrue);
await expectLater(copyPath(from.path, to.path), throwsArgumentError);
});
}

Directory _createFromDir() {
final from = Directory('tmp')..createSync();
File.fromUri(Uri.parse(p.join(from.path, 'foo.txt')))..createSync();
final dir = Directory(p.join(from.path, 'bar'))..createSync();
File.fromUri(Uri.parse(p.join(dir.path, 'baz.txt')))..createSync();
final contents = _contentAsOrderedString(from);
expect(
contents,
"[Directory: 'tmp/bar', File: 'tmp/bar/baz.txt', File: 'tmp/foo.txt']",
);
return from;
}

Directory _createToDir() {
final to = Directory('tmp2')..createSync();
final contents = _contentAsOrderedString(to);
expect(contents, '[]');
return to;
}

String _contentAsOrderedString(Directory dir) {
final contents = dir.listSync(recursive: true)
..sort((a, b) => a.path.compareTo(b.path));
return contents.toString();
}

0 comments on commit 4f13092

Please sign in to comment.