analysis_server_client 1.1.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 36

analysis_server_client #

analysis_server_client is a client wrapper over Analysis Server.

Overview #

  • Instances of Server manage a connection to an analysis server process, and facilitate communication to and from the server.

  • The Protocol library provides constants and classes to build requests for the server and decode responses and notifications from the server.

Example #

The example uses the Server to launch the analysis server, analyze all *.dart files in the specified directory, display the results, and shutdown the analysis server.

References #

For more about the analysis server, see the Analysis Server page.

1.1.1 #

  • Update ConnectionHandler to call checkServerProtocolVersion

1.1.0 #

  • Add analysis server protocol consts and classes
  • Overhaul the Server class
  • Add an example showing analysis of *.dart files in a directory
  • Update the required SDK

1.0.1 #

  • Update path on homepage

1.0.0 #

  • Initial version

example/example.dart

// Copyright (c) 2018, 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.

import 'dart:async';
import 'dart:io' show Directory, Platform, ProcessSignal, exit;

import 'package:analysis_server_client/handler/notification_handler.dart';
import 'package:analysis_server_client/handler/connection_handler.dart';
import 'package:analysis_server_client/protocol.dart';
import 'package:analysis_server_client/server.dart';
import 'package:path/path.dart' as path;
import 'package:pub_semver/pub_semver.dart';

/// A simple application that uses the analysis server to analyze a package.
main(List<String> args) async {
  String target = await parseArgs(args);
  print('Analyzing $target');

  // Launch the server
  Server server = new Server();
  await server.start();

  // Connect to the server
  _Handler handler = new _Handler(server);
  server.listenToOutput(notificationProcessor: handler.handleEvent);
  if (!await handler.serverConnected(timeLimit: const Duration(seconds: 15))) {
    exit(1);
  }

  // Request analysis
  await server.send(SERVER_REQUEST_SET_SUBSCRIPTIONS,
      new ServerSetSubscriptionsParams([ServerService.STATUS]).toJson());
  await server.send(ANALYSIS_REQUEST_SET_ANALYSIS_ROOTS,
      new AnalysisSetAnalysisRootsParams([target], const []).toJson());

  // Continue to watch for analysis until the user presses Ctrl-C
  StreamSubscription<ProcessSignal> subscription;
  subscription = ProcessSignal.sigint.watch().listen((_) async {
    print('Exiting...');
    subscription.cancel();
    await server.stop();
  });
}

class _Handler with NotificationHandler, ConnectionHandler {
  final Server server;
  int errorCount = 0;

  _Handler(this.server);

  @override
  void onAnalysisErrors(AnalysisErrorsParams params) {
    List<AnalysisError> errors = params.errors;
    bool first = true;
    for (AnalysisError error in errors) {
      if (error.type.name == 'TODO') {
        // Ignore these types of "errors"
        continue;
      }
      if (first) {
        first = false;
        print('${params.file}:');
      }
      Location loc = error.location;
      print('  ${error.message} • ${loc.startLine}:${loc.startColumn}');
      ++errorCount;
    }
  }

  @override
  void onFailedToConnect() {
    print('Failed to connect to server');
  }

  @override
  void onProtocolNotSupported(Version version) {
    print('Expected protocol version $PROTOCOL_VERSION, but found $version');
  }

  @override
  void onServerError(ServerErrorParams params) {
    if (params.isFatal) {
      print('Fatal Server Error: ${params.message}');
    } else {
      print('Server Error: ${params.message}');
    }
    if (params.stackTrace != null) {
      print(params.stackTrace);
    }
    super.onServerError(params);
  }

  @override
  void onServerStatus(ServerStatusParams params) {
    if (!params.analysis.isAnalyzing) {
      // Whenever the server stops analyzing,
      // print a brief summary of what issues have been found.
      if (errorCount == 0) {
        print('No issues found.');
      } else {
        print('Found ${errorCount} errors/warnings/hints');
      }
      errorCount = 0;
      print('--------- ctrl-c to exit ---------');
    }
  }
}

Future<String> parseArgs(List<String> args) async {
  if (args.length != 1) {
    printUsageAndExit('Expected exactly one directory');
  }
  final dir = new Directory(path.normalize(path.absolute(args[0])));
  if (!(await dir.exists())) {
    printUsageAndExit('Could not find directory ${dir.path}');
  }
  return dir.path;
}

void printUsageAndExit(String errorMessage) {
  print(errorMessage);
  print('');
  var appName = path.basename(Platform.script.toFilePath());
  print('Usage: $appName <directory path>');
  print('  Analyze the *.dart source files in <directory path>');
  exit(1);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  analysis_server_client: ^1.1.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:analysis_server_client/handler/connection_handler.dart';
import 'package:analysis_server_client/handler/notification_handler.dart';
import 'package:analysis_server_client/listener/server_listener.dart';
import 'package:analysis_server_client/protocol.dart';
import 'package:analysis_server_client/server.dart';
  
Version Uploaded Documentation Archive
1.1.1 Nov 20, 2018 Go to the documentation of analysis_server_client 1.1.1 Download analysis_server_client 1.1.1 archive
1.1.0 Nov 16, 2018 Go to the documentation of analysis_server_client 1.1.0 Download analysis_server_client 1.1.0 archive
1.0.1 Aug 8, 2017 Go to the documentation of analysis_server_client 1.0.1 Download analysis_server_client 1.0.1 archive
1.0.0 Aug 8, 2017 Go to the documentation of analysis_server_client 1.0.0 Download analysis_server_client 1.0.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
31
Health:
Code health derived from static analysis. [more]
0
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
36
Learn more about scoring.

We analyzed this package on Apr 12, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.2.0
  • pana: 0.12.14

Platforms

Detected platforms: Flutter, other

Platform components identified in package: io.

Health suggestions

Fix lib/src/protocol/protocol_generated.dart. (-99.81 points)

Analysis of lib/src/protocol/protocol_generated.dart reported 1255 hints, including:

line 16 col 1: Prefer using /// for doc comments.

line 28 col 3: Prefer using /// for doc comments.

line 33 col 3: Prefer using /// for doc comments.

line 36 col 3: Avoid return types on setters.

line 102 col 1: Prefer using /// for doc comments.

Fix lib/src/protocol/protocol_common.dart. (-89.57 points)

Analysis of lib/src/protocol/protocol_common.dart reported 451 hints, including:

line 15 col 1: Prefer using /// for doc comments.

line 28 col 3: Prefer using /// for doc comments.

line 33 col 3: Prefer using /// for doc comments.

line 36 col 3: Avoid return types on setters.

line 95 col 1: Prefer using /// for doc comments.

Fix lib/src/protocol/protocol_base.dart. (-27.44 points)

Analysis of lib/src/protocol/protocol_base.dart reported 64 hints, including:

line 14 col 1: Prefer using /// for doc comments.

line 20 col 3: Prefer using /// for doc comments.

line 27 col 1: Prefer using /// for doc comments.

line 33 col 3: Prefer using /// for doc comments.

line 39 col 3: Prefer using /// for doc comments.

Fix lib/src/protocol/protocol_internal.dart. (-13.96 points)

Analysis of lib/src/protocol/protocol_internal.dart reported 30 hints, including:

line 15 col 1: Prefer using /// for doc comments.

line 23 col 1: Prefer using /// for doc comments.

line 35 col 1: Prefer using /// for doc comments.

line 48 col 1: Prefer using /// for doc comments.

line 59 col 1: Prefer using /// for doc comments.

Fix lib/server.dart. (-1.99 points)

Analysis of lib/server.dart reported 4 hints:

line 162 col 23: Use = to separate a named parameter from its default value.

line 166 col 27: Use = to separate a named parameter from its default value.

line 167 col 31: Use = to separate a named parameter from its default value.

line 240 col 5: Future results in async function bodies must be awaited or marked unawaited using package:pedantic.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0-dev.9.0 <3.0.0
path ^1.6.2 1.6.2
pub_semver ^1.4.2 1.4.2
Transitive dependencies
collection 1.14.11
Dev dependencies
test ^1.3.4