Skip to content

Commit

Permalink
Move *SDK environments into a single ToolEnvironment class. (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Apr 13, 2018
1 parent ae07762 commit 9dabdef
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 119 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.11.0-dev

**Breaking changes:**

* `DartSdk`, `FlutterSdk` and `PubEnvironment` is replaced with `ToolEnvironment`.

## 0.10.6

* Enable Dart 2 Preview in analyzer options (including non-Flutter packages).
Expand Down
6 changes: 3 additions & 3 deletions lib/src/library_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ class LibraryScanner {
this._context, this._overrides);

factory LibraryScanner(
PubEnvironment pubEnv, String packagePath, bool useFlutter,
ToolEnvironment toolEnv, String packagePath, bool useFlutter,
{List<LibraryOverride> overrides}) {
// TODO: fail more clearly if this...fails
var sdkPath = pubEnv.dartSdk.sdkDir;
var sdkPath = toolEnv.dartSdkDir;

var resourceProvider = PhysicalResourceProvider.INSTANCE;
var sdk = new FolderBasedDartSdk(
Expand All @@ -65,7 +65,7 @@ class LibraryScanner {
RunPubList runPubList;
if (useFlutter) {
runPubList = (Folder folder) =>
pubEnv.listPackageDirsSync(folder.path, useFlutter);
toolEnv.listPackageDirsSync(folder.path, useFlutter);
}

var pubPackageMapProvider = new PubPackageMapProvider(
Expand Down
45 changes: 18 additions & 27 deletions lib/src/package_analyzer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,17 @@ import 'utils.dart';
import 'version.dart';

class PackageAnalyzer {
final DartSdk _dartSdk;
final FlutterSdk _flutterSdk;
final PubEnvironment _pubEnv;
final ToolEnvironment _toolEnv;

PackageAnalyzer._(this._dartSdk, this._flutterSdk, this._pubEnv);

factory PackageAnalyzer(DartSdk dartSdk,
{String flutterDir, String pubCacheDir}) {
var flutterSdk = new FlutterSdk(sdkDir: flutterDir);

var pubEnv = new PubEnvironment(dartSdk,
flutterSdk: flutterSdk, pubCacheDir: pubCacheDir);

return new PackageAnalyzer._(dartSdk, flutterSdk, pubEnv);
}
PackageAnalyzer(this._toolEnv);

static Future<PackageAnalyzer> create(
{String sdkDir, String flutterDir, String pubCacheDir}) async =>
new PackageAnalyzer(await DartSdk.create(sdkDir: sdkDir),
flutterDir: flutterDir, pubCacheDir: pubCacheDir);
{String sdkDir, String flutterDir, String pubCacheDir}) async {
return new PackageAnalyzer(await ToolEnvironment.create(
dartSdkDir: sdkDir,
flutterSdkDir: flutterDir,
pubCacheDir: pubCacheDir));
}

Future<Summary> inspectPackage(
String package, {
Expand All @@ -66,7 +57,7 @@ class PackageAnalyzer {
packageDir = tempDir?.path;
}
if (packageDir == null) {
var pkgInfo = await _pubEnv.getLocation(package, version: version);
var pkgInfo = await _toolEnv.getLocation(package, version: version);
packageDir = pkgInfo.location;
}
try {
Expand All @@ -88,9 +79,9 @@ class PackageAnalyzer {
}

Future<Summary> _inspect(String pkgDir, bool keepTransitiveLibs) async {
log.info("SDK: ${_dartSdk.version}");
if (_pubEnv.pubCacheDir != null) {
log.fine("Using .package-cache: ${_pubEnv.pubCacheDir}");
log.info("SDK: ${_toolEnv.dartSdkInfo.version}");
if (_toolEnv.pubCacheDir != null) {
log.fine("Using .package-cache: ${_toolEnv.pubCacheDir}");
}
log.fine('Inspecting package at $pkgDir');
final suggestions = <Suggestion>[];
Expand Down Expand Up @@ -120,7 +111,7 @@ class PackageAnalyzer {
Set<String> unformattedFiles;
try {
unformattedFiles = new SplayTreeSet<String>.from(
await _dartSdk.filesNeedingFormat(pkgDir));
await _toolEnv.filesNeedingFormat(pkgDir));

assert(unformattedFiles.every((f) => dartFiles.contains(f)),
'dartfmt should only return Dart files');
Expand All @@ -136,7 +127,7 @@ class PackageAnalyzer {
}

log.info("Pub upgrade...");
final upgrade = await _pubEnv.runUpgrade(pkgDir, usesFlutter);
final upgrade = await _toolEnv.runUpgrade(pkgDir, usesFlutter);

PkgResolution pkgResolution;
if (upgrade.exitCode == 0) {
Expand Down Expand Up @@ -194,7 +185,7 @@ class PackageAnalyzer {
'package:package_resolver/package_resolver.dart'),
];

libraryScanner = new LibraryScanner(_pubEnv, pkgDir, usesFlutter,
libraryScanner = new LibraryScanner(_toolEnv, pkgDir, usesFlutter,
overrides: overrides);
assert(libraryScanner.packageName == package);
} catch (e, stack) {
Expand Down Expand Up @@ -296,7 +287,7 @@ class PackageAnalyzer {

Map<String, Object> flutterVersion;
if (usesFlutter) {
flutterVersion = await _flutterSdk.getVersion();
flutterVersion = await _toolEnv.getFlutterVersion();
}

DartPlatform platform;
Expand Down Expand Up @@ -328,7 +319,7 @@ class PackageAnalyzer {

return new Summary(
panaPkgVersion,
_dartSdk.version,
_toolEnv.dartSdkInfo.version,
pubspec.name,
pubspec.version,
pubspec,
Expand All @@ -349,7 +340,7 @@ class PackageAnalyzer {
if (dirs.isEmpty) {
return null;
}
final proc = await _dartSdk.runAnalyzer(pkgPath, dirs, usesFlutter);
final proc = await _toolEnv.runAnalyzer(pkgPath, dirs, usesFlutter);

String output = proc.stderr;
if ('\n$output'.contains('\nUnhandled exception:\n')) {
Expand Down
166 changes: 79 additions & 87 deletions lib/src/sdk_env.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,87 @@ import 'dart:io';

import 'package:cli_util/cli_util.dart' as cli;
import 'package:path/path.dart' as p;
import 'package:pub_semver/pub_semver.dart';

import 'analysis_options.dart';
import 'logging.dart';
import 'pubspec.dart';
import 'sdk_info.dart';
import 'utils.dart';

class DartSdk implements DartSdkInfo {
final String sdkDir;
final Map<String, String> _environment;
class ToolEnvironment {
final String dartSdkDir;
final String pubCacheDir;
final String _dartCmd;
final String _pubCmd;
final String _dartAnalyzerCmd;
final String _dartfmtCmd;
final String _pubCmd;
final String dateString;
final String platform;
final Version version;

DartSdk._(this.sdkDir, this._environment, this.version, this.dateString,
this.platform)
: _dartAnalyzerCmd = _join(sdkDir, 'bin', 'dartanalyzer'),
_dartfmtCmd = _join(sdkDir, 'bin', 'dartfmt'),
_pubCmd = _join(sdkDir, 'bin', 'pub');

static Future<DartSdk> create(
{String sdkDir, Map<String, String> environment}) async {
sdkDir ??= cli.getSdkPath();
environment ??= {};
final String _flutterCmd;
final Map<String, String> _environment;
DartSdkInfo _dartSdkInfo;

ToolEnvironment._(
this.dartSdkDir,
this.pubCacheDir,
this._dartCmd,
this._pubCmd,
this._dartAnalyzerCmd,
this._dartfmtCmd,
this._flutterCmd,
this._environment,
);

DartSdkInfo get dartSdkInfo => _dartSdkInfo;

Future _init() async {
final dartVersionResult = handleProcessErrors(
await runProc(_dartCmd, ['--version'], environment: _environment));
final dartVersionString = dartVersionResult.stderr.toString().trim();
_dartSdkInfo = new DartSdkInfo.parse(dartVersionString);
}

var dartCmd = _join(sdkDir, 'bin', 'dart');
static Future<ToolEnvironment> create({
String dartSdkDir,
String flutterSdkDir,
String pubCacheDir,
Map<String, String> environment,
}) async {
Future<String> resolve(String dir) async {
if (dir == null) return null;
return new Directory(dir).resolveSymbolicLinks();
}

var r = handleProcessErrors(
await runProc(dartCmd, ['--version'], environment: environment));
var versionString = r.stderr.toString().trim();
dartSdkDir ??= cli.getSdkPath();
final resolvedDartSdk = await resolve(dartSdkDir);
final resolvedFlutterSdk = await resolve(flutterSdkDir);
final resolvedPubCache = await resolve(pubCacheDir);
final env = <String, String>{};
env.addAll(environment ?? const {});

var info = new DartSdkInfo.parse(versionString);
if (resolvedPubCache != null) {
env['PUB_CACHE'] = resolvedPubCache;
}

return new DartSdk._(
sdkDir, environment, info.version, info.dateString, info.platform);
final pubEnvValues = <String>[];
final origPubEnvValue = Platform.environment[_pubEnvironmentKey] ?? '';
pubEnvValues.addAll(origPubEnvValue
.split(':')
.map((s) => s.trim())
.where((s) => s.isNotEmpty));
pubEnvValues.add('bot.pkg_pana');
env[_pubEnvironmentKey] = pubEnvValues.join(':');

final toolEnv = new ToolEnvironment._(
resolvedDartSdk,
resolvedPubCache,
_join(resolvedDartSdk, 'bin', 'dart'),
_join(resolvedDartSdk, 'bin', 'pub'),
_join(resolvedDartSdk, 'bin', 'dartanalyzer'),
_join(resolvedDartSdk, 'bin', 'dartfmt'),
_join(resolvedFlutterSdk, 'bin', 'flutter'),
env,
);
await toolEnv._init();
return toolEnv;
}

Future<ProcessResult> runAnalyzer(
Expand Down Expand Up @@ -111,7 +153,7 @@ class DartSdk implements DartSdkInfo {
return files;
}

Future<ProcessResult> _execUpgrade(
Future<ProcessResult> _execPubUpgrade(
String packageDir, Map<String, String> environment) {
return runProc(
_pubCmd,
Expand All @@ -120,71 +162,21 @@ class DartSdk implements DartSdkInfo {
environment: environment,
);
}
}

class FlutterSdk {
final String _flutterBin;

FlutterSdk({String sdkDir}) : _flutterBin = _join(sdkDir, 'bin', 'flutter');

Future<ProcessResult> _execUpgrade(
Future<ProcessResult> _execFlutterUpgrade(
String packageDir, Map<String, String> environment) =>
runProc(
_flutterBin,
_flutterCmd,
['packages', 'pub', 'upgrade', '--verbosity', 'io', '--no-precompile'],
workingDirectory: packageDir,
environment: environment,
);

Future<Map<String, Object>> getVersion() async {
var result = await runProc(_flutterBin, ['--version', '--machine']);
Future<Map<String, Object>> getFlutterVersion() async {
var result = await runProc(_flutterCmd, ['--version', '--machine']);
assert(result.exitCode == 0);
return json.decode(result.stdout);
}
}

class PubEnvironment {
final DartSdk dartSdk;
final FlutterSdk flutterSdk;
final String pubCacheDir;
final Map<String, String> _environment = {};

PubEnvironment(this.dartSdk, {FlutterSdk flutterSdk, this.pubCacheDir})
: this.flutterSdk = flutterSdk ?? new FlutterSdk() {
_environment.addAll(this.dartSdk._environment);
if (!_environment.containsKey(_pubEnvironmentKey)) {
// Then do the standard behavior. Extract the current value, if any,
// and append the default value

var pubEnvironment = <String>[];

var currentEnv = Platform.environment[_pubEnvironmentKey];

if (currentEnv != null) {
pubEnvironment.addAll(currentEnv
.split(
':') // if there are many values, they should be separated by `:`
.map((v) => v.trim()) // don't want whitespace
.where((v) => v.isNotEmpty) // don't want empty values
);
}

pubEnvironment.add('bot.pkg_pana');

_environment[_pubEnvironmentKey] = pubEnvironment.join(':');
}
if (pubCacheDir != null) {
var resolvedDir = new Directory(pubCacheDir).resolveSymbolicLinksSync();
if (resolvedDir != pubCacheDir) {
throw new ArgumentError([
"Make sure you resolve symlinks:",
pubCacheDir,
resolvedDir
].join('\n'));
}
_environment['PUB_CACHE'] = pubCacheDir;
}
}

Future<bool> detectFlutterUse(String packageDir) async {
final pubspec = new Pubspec.parseFromDir(packageDir);
Expand All @@ -200,9 +192,9 @@ class PubEnvironment {
retryCount--;

if (usesFlutter) {
result = await flutterSdk._execUpgrade(packageDir, _environment);
result = await _execFlutterUpgrade(packageDir, _environment);
} else {
result = await dartSdk._execUpgrade(packageDir, _environment);
result = await _execPubUpgrade(packageDir, _environment);
}

if (result.exitCode > 0) {
Expand Down Expand Up @@ -234,7 +226,7 @@ class PubEnvironment {
args.add(package);

var result = handleProcessErrors(await runProc(
dartSdk._pubCmd,
_pubCmd,
args,
environment: _environment,
));
Expand All @@ -255,7 +247,7 @@ class PubEnvironment {

// now get all installed packages
result = handleProcessErrors(await runProc(
dartSdk._pubCmd,
_pubCmd,
['cache', 'list'],
environment: _environment,
));
Expand All @@ -276,15 +268,15 @@ class PubEnvironment {
if (usesFlutter) {
// flutter env
return runProcSync(
flutterSdk._flutterBin,
_flutterCmd,
['packages', 'pub', 'list-package-dirs'],
workingDirectory: packageDir,
environment: _environment,
);
} else {
// normal pub
return runProcSync(
dartSdk._pubCmd,
_pubCmd,
['list-package-dirs'],
workingDirectory: packageDir,
environment: _environment,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/version.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ part of pana.version;
// Generator: PackageVersionGenerator
// **************************************************************************

final _$panaPkgVersionPubSemverVersion = new Version.parse("0.10.6");
final _$panaPkgVersionPubSemverVersion = new Version.parse("0.11.0-dev");
Loading

0 comments on commit 9dabdef

Please sign in to comment.