From 8ccc9a45c9269cb627a4ad339bc27002c7f112c0 Mon Sep 17 00:00:00 2001 From: Danny Tuppeny Date: Wed, 7 Feb 2018 18:49:54 +0000 Subject: [PATCH] Add flutter doctor support for VS Code (#14463) Looks in default install locations on Mac, Linux and Windows for VS Code. If found, looks in default extension location to see if Dart Code is installed. If VS Code is not installed, nothing is reported. If VS Code is installed without Dart Code, a warning is shown. --- packages/flutter_tools/lib/src/doctor.dart | 2 + .../flutter_tools/lib/src/vscode/vscode.dart | 180 ++++++++++++++++++ .../lib/src/vscode/vscode_validator.dart | 46 +++++ .../test/commands/doctor_test.dart | 48 ++++- .../application/resources/app/package.json | 4 + .../fake-extension.txt | 1 + 6 files changed, 280 insertions(+), 1 deletion(-) create mode 100644 packages/flutter_tools/lib/src/vscode/vscode.dart create mode 100644 packages/flutter_tools/lib/src/vscode/vscode_validator.dart create mode 100644 packages/flutter_tools/test/data/vscode/application/resources/app/package.json create mode 100644 packages/flutter_tools/test/data/vscode/extensions/Dart-Code.dart-code-4.5.6/fake-extension.txt diff --git a/packages/flutter_tools/lib/src/doctor.dart b/packages/flutter_tools/lib/src/doctor.dart index 7b8cb07872..2df8bdad6c 100644 --- a/packages/flutter_tools/lib/src/doctor.dart +++ b/packages/flutter_tools/lib/src/doctor.dart @@ -23,6 +23,7 @@ import 'globals.dart'; import 'ios/ios_workflow.dart'; import 'ios/plist_utils.dart'; import 'version.dart'; +import 'vscode/vscode_validator.dart'; Doctor get doctor => context[Doctor]; @@ -43,6 +44,7 @@ class Doctor { final List ideValidators = []; ideValidators.addAll(AndroidStudioValidator.allValidators); ideValidators.addAll(IntelliJValidator.installedValidators); + ideValidators.addAll(VsCodeValidator.installedValidators); if (ideValidators.isNotEmpty) _validators.addAll(ideValidators); else diff --git a/packages/flutter_tools/lib/src/vscode/vscode.dart b/packages/flutter_tools/lib/src/vscode/vscode.dart new file mode 100644 index 0000000000..b8b8395d7c --- /dev/null +++ b/packages/flutter_tools/lib/src/vscode/vscode.dart @@ -0,0 +1,180 @@ +// Copyright 2018 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. + +import 'dart:convert'; + +import '../base/common.dart'; +import '../base/file_system.dart'; +import '../base/platform.dart'; +import '../base/version.dart'; + +// Include VS Code insiders (useful for debugging). +const bool _includeInsiders = false; + +class VsCode { + static const String extensionIdentifier = 'Dart-Code.dart-code'; + + VsCode._(this.directory, this.extensionDirectory, { Version version }) + : this.version = version ?? Version.unknown { + + if (!fs.isDirectorySync(directory)) { + _validationMessages.add('VS Code not found at $directory'); + return; + } + + // If the extensions directory doesn't exist at all, the listSync() + // below will fail, so just bail out early. + if (!fs.isDirectorySync(extensionDirectory)) { + return; + } + + // Check for presence of extension. + final Iterable extensionDirs = fs + .directory(extensionDirectory) + .listSync() + .where((FileSystemEntity d) => d is Directory) + .where( + (FileSystemEntity d) => d.basename.startsWith(extensionIdentifier)); + + if (extensionDirs.isNotEmpty) { + final FileSystemEntity extensionDir = extensionDirs.first; + + _isValid = true; + _extensionVersion = new Version.parse( + extensionDir.basename.substring('$extensionIdentifier-'.length)); + _validationMessages.add('Dart Code extension version $_extensionVersion'); + } + } + + final String directory; + final String extensionDirectory; + final Version version; + + bool _isValid = false; + Version _extensionVersion; + final List _validationMessages = []; + + factory VsCode.fromDirectory(String installPath, String extensionDirectory) { + final String packageJsonPath = + fs.path.join(installPath, 'resources', 'app', 'package.json'); + final String versionString = _getVersionFromPackageJson(packageJsonPath); + Version version; + if (versionString != null) + version = new Version.parse(versionString); + return new VsCode._(installPath, extensionDirectory, version: version); + } + + bool get isValid => _isValid; + + Iterable get validationMessages => _validationMessages; + + static List allInstalled() { + if (platform.isMacOS) + return _installedMacOS(); + else if (platform.isWindows) + return _installedWindows(); + else if (platform.isLinux) + return _installedLinux(); + else + // VS Code isn't supported on the other platforms. + return []; + } + + // macOS: + // /Applications/Visual Studio Code.app/Contents/ + // /Applications/Visual Studio Code - Insiders.app/Contents/ + // $HOME/Applications/Visual Studio Code.app/Contents/ + // $HOME/Applications/Visual Studio Code - Insiders.app/Contents/ + // macOS Extensions: + // $HOME/.vscode/extensions + // $HOME/.vscode-insiders/extensions + static List _installedMacOS() { + final Map stable = { + fs.path.join('/Applications', 'Visual Studio Code.app', 'Contents'): + '.vscode', + fs.path.join(homeDirPath, 'Applications', 'Visual Studio Code.app', + 'Contents'): '.vscode' + }; + final Map insiders = { + fs.path.join( + '/Applications', 'Visual Studio Code - Insiders.app', 'Contents'): + '.vscode-insiders', + fs.path.join(homeDirPath, 'Applications', + 'Visual Studio Code - Insiders.app', 'Contents'): '.vscode-insiders' + }; + + return _findInstalled(stable, insiders); + } + + // Windows: + // $programfiles(x86)\Microsoft VS Code + // $programfiles(x86)\Microsoft VS Code Insiders + // TODO: Confirm these are correct for 64bit + // $programfiles\Microsoft VS Code + // $programfiles\Microsoft VS Code Insiders + // Windows Extensions: + // $HOME/.vscode/extensions + // $HOME/.vscode-insiders/extensions + static List _installedWindows() { + final String progFiles86 = platform.environment['programfiles(x86)']; + final String progFiles = platform.environment['programfiles']; + + final Map stable = { + fs.path.join(progFiles86, 'Microsoft VS Code'): '.vscode', + fs.path.join(progFiles, 'Microsoft VS Code'): '.vscode' + }; + final Map insiders = { + fs.path.join(progFiles86, 'Microsoft VS Code Insiders'): + '.vscode-insiders', + fs.path.join(progFiles, 'Microsoft VS Code Insiders'): '.vscode-insiders' + }; + + return _findInstalled(stable, insiders); + } + + // Linux: + // /usr/share/code/bin/code + // /usr/share/code-insiders/bin/code-insiders + // Linux Extensions: + // $HOME/.vscode/extensions + // $HOME/.vscode-insiders/extensions + static List _installedLinux() { + return _findInstalled( + {'/usr/share/code': '.vscode'}, + {'/usr/share/code-insiders': '.vscode-insiders'} + ); + } + + static List _findInstalled( + Map stable, Map insiders) { + final Map allPaths = {}; + allPaths.addAll(stable); + if (_includeInsiders) + allPaths.addAll(insiders); + + final List results = []; + + for (String directory in allPaths.keys) { + if (fs.directory(directory).existsSync()) { + final String extensionDirectory = + fs.path.join(homeDirPath, allPaths[directory], 'extensions'); + results.add(new VsCode.fromDirectory(directory, extensionDirectory)); + } + } + + return results; + } + + @override + String toString() => + 'VS Code ($version)${(_extensionVersion != Version.unknown ? ', Dart Code ($_extensionVersion)' : '')}'; + + static String _getVersionFromPackageJson(String packageJsonPath) { + if (!fs.isFileSync(packageJsonPath)) + return null; + final String jsonString = fs.file(packageJsonPath).readAsStringSync(); + final Map json = JSON.decode(jsonString); + return json['version']; + } +} diff --git a/packages/flutter_tools/lib/src/vscode/vscode_validator.dart b/packages/flutter_tools/lib/src/vscode/vscode_validator.dart new file mode 100644 index 0000000000..d27dcc2c04 --- /dev/null +++ b/packages/flutter_tools/lib/src/vscode/vscode_validator.dart @@ -0,0 +1,46 @@ +// Copyright 2018 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. + +import 'dart:async'; + +import '../base/version.dart'; +import '../doctor.dart'; +import 'vscode.dart'; + +class VsCodeValidator extends DoctorValidator { + static const String extensionMarketplaceUrl = + 'https://marketplace.visualstudio.com/items?itemName=Dart-Code.dart-code'; + final VsCode _vsCode; + + VsCodeValidator(this._vsCode) : super('VS Code'); + + static Iterable get installedValidators { + return VsCode + .allInstalled() + .map((VsCode vsCode) => new VsCodeValidator(vsCode)); + } + + @override + Future validate() async { + final List messages = []; + ValidationType type = ValidationType.missing; + final String vsCodeVersionText = _vsCode.version == Version.unknown + ? null + : 'version ${_vsCode.version}'; + messages.add(new ValidationMessage('VS Code at ${_vsCode.directory}')); + if (_vsCode.isValid) { + type = ValidationType.installed; + messages.addAll(_vsCode.validationMessages + .map((String m) => new ValidationMessage(m))); + } else { + type = ValidationType.partial; + messages.addAll(_vsCode.validationMessages + .map((String m) => new ValidationMessage.error(m))); + messages.add(new ValidationMessage( + 'Dart Code extension not installed; install from\n$extensionMarketplaceUrl')); + } + + return new ValidationResult(type, messages, statusInfo: vsCodeVersionText); + } +} diff --git a/packages/flutter_tools/test/commands/doctor_test.dart b/packages/flutter_tools/test/commands/doctor_test.dart index 9427ac9759..69405a2b72 100644 --- a/packages/flutter_tools/test/commands/doctor_test.dart +++ b/packages/flutter_tools/test/commands/doctor_test.dart @@ -6,6 +6,8 @@ import 'dart:async'; import 'package:flutter_tools/src/base/file_system.dart'; import 'package:flutter_tools/src/doctor.dart'; +import 'package:flutter_tools/src/vscode/vscode.dart'; +import 'package:flutter_tools/src/vscode/vscode_validator.dart'; import 'package:test/test.dart'; import '../src/context.dart'; @@ -27,6 +29,36 @@ void main() { expect(message.message, contains('Flutter plugin version 0.1.3')); expect(message.message, contains('recommended minimum version')); }); + + testUsingContext('vs code validator when both installed', () async { + final ValidationResult result = await VsCodeValidatorTestTargets.installedWithExtension.validate(); + expect(result.type, ValidationType.installed); + expect(result.statusInfo, 'version 1.2.3'); + expect(result.messages, hasLength(2)); + + ValidationMessage message = result.messages + .firstWhere((ValidationMessage m) => m.message.startsWith('VS Code ')); + expect(message.message, 'VS Code at ${VsCodeValidatorTestTargets.validInstall}'); + + message = result.messages + .firstWhere((ValidationMessage m) => m.message.startsWith('Dart Code ')); + expect(message.message, 'Dart Code extension version 4.5.6'); + }); + + testUsingContext('vs code validator when extension missing', () async { + final ValidationResult result = await VsCodeValidatorTestTargets.installedWithoutExtension.validate(); + expect(result.type, ValidationType.partial); + expect(result.statusInfo, 'version 1.2.3'); + expect(result.messages, hasLength(2)); + + ValidationMessage message = result.messages + .firstWhere((ValidationMessage m) => m.message.startsWith('VS Code ')); + expect(message.message, 'VS Code at ${VsCodeValidatorTestTargets.validInstall}'); + + message = result.messages + .firstWhere((ValidationMessage m) => m.message.startsWith('Dart Code ')); + expect(message.message, startsWith('Dart Code extension not installed')); + }); }); group('doctor with fake validators', () { @@ -236,4 +268,18 @@ class FakeQuietDoctor extends Doctor { } return _validators; } -} \ No newline at end of file +} + +class VsCodeValidatorTestTargets extends VsCodeValidator { + static final String validInstall = fs.path.join('test', 'data', 'vscode', 'application'); + static final String validExtensions = fs.path.join('test', 'data', 'vscode', 'extensions'); + static final String missingExtensions = fs.path.join('test', 'data', 'vscode', 'notExtensions'); + VsCodeValidatorTestTargets._(String installDirectory, String extensionDirectory) + : super(new VsCode.fromDirectory(installDirectory, extensionDirectory)); + + static VsCodeValidatorTestTargets get installedWithExtension => + new VsCodeValidatorTestTargets._(validInstall, validExtensions); + + static VsCodeValidatorTestTargets get installedWithoutExtension => + new VsCodeValidatorTestTargets._(validInstall, missingExtensions); +} diff --git a/packages/flutter_tools/test/data/vscode/application/resources/app/package.json b/packages/flutter_tools/test/data/vscode/application/resources/app/package.json new file mode 100644 index 0000000000..e4e72b6e0b --- /dev/null +++ b/packages/flutter_tools/test/data/vscode/application/resources/app/package.json @@ -0,0 +1,4 @@ +{ + "name": "fake-vs-code-install-for-tests", + "version": "1.2.3" +} diff --git a/packages/flutter_tools/test/data/vscode/extensions/Dart-Code.dart-code-4.5.6/fake-extension.txt b/packages/flutter_tools/test/data/vscode/extensions/Dart-Code.dart-code-4.5.6/fake-extension.txt new file mode 100644 index 0000000000..0c73c382e8 --- /dev/null +++ b/packages/flutter_tools/test/data/vscode/extensions/Dart-Code.dart-code-4.5.6/fake-extension.txt @@ -0,0 +1 @@ +This file is here only to ensure the parent folder is stored in Git to act as a fake extension folder for tests.