@@ -98,23 +98,14 @@ Future<void> main(List<String> args) async {
9898 retryOptions: RetryOptions (maxAttempts: 3 ),
9999 );
100100 final pkgDir = Directory (p.join (pkgDownloadDir.path, '$package -$version ' ));
101- final detected = await _detectSdks (pkgDir.path);
102-
103- final toolEnv = await ToolEnvironment .create (
104- dartSdkConfig: SdkConfig (
105- rootPath: detected.dartSdkPath,
106- configHomePath: _configHomePath ('dart' , detected.configKind),
107- ),
108- flutterSdkConfig: SdkConfig (
109- rootPath: detected.flutterSdkPath,
110- configHomePath: _configHomePath ('flutter' , detected.configKind),
101+ final toolEnv = await _setupSdksForPackage (
102+ pkgDir.path,
103+ toolFn: (detected) => _createToolEnvironment (
104+ detected: detected,
105+ pubCache: pubCache,
111106 ),
112- pubCacheDir: pubCache,
113- dartdocVersion: _dartdocVersion,
114107 );
115108
116- //final dartdocOutputDir =
117- // await Directory(p.join(outputFolder, 'doc')).create();
118109 final resourcesOutputDir =
119110 await Directory (p.join (outputFolder, 'resources' )).create ();
120111 final pana = PackageAnalyzer (toolEnv);
@@ -192,11 +183,38 @@ String? _configHomePath(String sdk, String kind) {
192183 return path;
193184}
194185
195- Future <({String configKind, String ? dartSdkPath, String ? flutterSdkPath})>
196- _detectSdks (String pkgDir) async {
186+ Future <ToolEnvironment > _createToolEnvironment ({
187+ required _DetectedSdks detected,
188+ required String ? pubCache,
189+ }) async {
190+ return await ToolEnvironment .create (
191+ dartSdkConfig: SdkConfig (
192+ rootPath: detected.dartSdkPath,
193+ configHomePath: _configHomePath ('dart' , detected.configKind),
194+ ),
195+ flutterSdkConfig: SdkConfig (
196+ rootPath: detected.flutterSdkPath,
197+ configHomePath: _configHomePath ('flutter' , detected.configKind),
198+ ),
199+ pubCacheDir: pubCache,
200+ dartdocVersion: _dartdocVersion,
201+ );
202+ }
203+
204+ typedef _DetectedSdks = ({
205+ String configKind,
206+ String ? dartSdkPath,
207+ String ? flutterSdkPath
208+ });
209+
210+ Future <ToolEnvironment > _setupSdksForPackage (
211+ String pkgDir, {
212+ required Future <ToolEnvironment > Function (_DetectedSdks detected) toolFn,
213+ }) async {
197214 // Load the pubspec so we detect which SDK to use for analysis
198215 final pubspecFile = File (p.join (pkgDir, 'pubspec.yaml' ));
199216 final pubspec = Pubspec .parseYaml (await pubspecFile.readAsString ());
217+ final usesFlutter = pubspec.usesFlutter;
200218
201219 // Discover installed Dart and Flutter SDKs.
202220 // This reads sibling folders to the Dart and Flutter SDK.
@@ -254,6 +272,16 @@ Future<({String configKind, String? dartSdkPath, String? flutterSdkPath})>
254272 return true ;
255273 }
256274
275+ ToolEnvironment ? stableToolEnv;
276+ Future <ToolEnvironment > createOrGetStableToolEnv () async {
277+ stableToolEnv ?? = await toolFn ((
278+ configKind: 'stable' ,
279+ dartSdkPath: installedDartSdk? .path,
280+ flutterSdkPath: installedFlutterSdk? .path,
281+ ));
282+ return stableToolEnv! ;
283+ }
284+
257285 // try to use the latest SDKs in the docker image
258286 final matchesInstalledSdks = matchesSdks (
259287 dart: installedDartSdk? .version,
@@ -262,13 +290,15 @@ Future<({String configKind, String? dartSdkPath, String? flutterSdkPath})>
262290 allowsMissingVersion: true ,
263291 );
264292 if (matchesInstalledSdks) {
265- // TODO(https://github.com/dart-lang/pub-dev/issues/7268): Also use `pub get` for better SDK selection.
266-
267- return (
268- configKind: 'stable' ,
269- dartSdkPath: installedDartSdk? .path,
270- flutterSdkPath: installedFlutterSdk? .path,
293+ final env = await createOrGetStableToolEnv ();
294+ final pr = await env.runPub (
295+ pkgDir,
296+ usesFlutter: usesFlutter,
297+ command: 'get' ,
271298 );
299+ if (! pr.wasError) {
300+ return env;
301+ }
272302 }
273303
274304 // try to use the latest stable downloadable SDKs, or
@@ -296,20 +326,24 @@ Future<({String configKind, String? dartSdkPath, String? flutterSdkPath})>
296326 channel: bundle.channel,
297327 );
298328
299- return (
329+ final env = await toolFn ( (
300330 configKind: bundle.configKind,
301331 dartSdkPath: dartSdkPath,
302332 flutterSdkPath: flutterSdkPath,
333+ ));
334+ final pr = await env.runPub (
335+ pkgDir,
336+ usesFlutter: usesFlutter,
337+ command: 'get' ,
303338 );
339+ if (! pr.wasError) {
340+ return env;
341+ }
304342 }
305343 }
306344
307- // should not happen, but instead of failing, let's return the installed SDKs
308- return (
309- configKind: 'stable' ,
310- dartSdkPath: installedDartSdk? .path,
311- flutterSdkPath: installedFlutterSdk? .path,
312- );
345+ // As a fallback, returning the installed tool environment (which may have been created already).
346+ return await createOrGetStableToolEnv ();
313347}
314348
315349Future <String ?> _installSdk ({
0 commit comments