1+ // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+ // for details. All rights reserved. Use of this source code is governed by a
3+ // BSD-style license that can be found in the LICENSE file.
4+
5+ import 'dart:convert' ;
16import 'dart:io' ;
27
3- import 'package:http/http.dart' as http;
48import 'package:path/path.dart' ;
59
610import '../logging/logging.dart' ;
@@ -35,7 +39,7 @@ class GradleTools {
3539
3640 static Future <void > _runGradleCommand (
3741 List <MavenDependency > deps, String targetDir,
38- {bool extractSources = false }) async {
42+ {String taskName = 'copyJars' }) async {
3943 final gradleWrapper = await getGradleWExecutable ();
4044 // Paths in Gradle files on Windows get improperly escaped
4145 final targetPath = Platform .isWindows
@@ -47,13 +51,14 @@ class GradleTools {
4751 );
4852 final tempDir = await currentDir.createTemp ('maven_temp_' );
4953 await createStubProject (tempDir);
54+
5055 final tempGradle = join (tempDir.path, 'temp_build.gradle.kts' );
5156 log.finer ('using Gradle stub:\n $gradle ' );
5257 await File (tempGradle).writeAsString (gradle);
5358 final gradleArgs = [
5459 '-b' , // specify gradle file to run
5560 tempGradle,
56- extractSources ? 'extractSourceJars' : 'copyJars' ,
61+ taskName ,
5762 '-q' // quiet mode
5863 ];
5964 await _runCmd (gradleWrapper! .toFilePath (), gradleArgs);
@@ -68,16 +73,8 @@ class GradleTools {
6873 /// Downloads and unpacks source files of [deps] into [targetDir] .
6974 static Future <void > downloadMavenSources (
7075 List <MavenDependency > deps, String targetDir) async {
71- // TODO(https://github.com/dart-lang/native/issues/2579): Make this use
72- // gradle as well, instead of manually downloading deps via http.
73- for (final dep in deps) {
74- final targetFile = File (join (targetDir, dep.filename ()));
75- await targetFile.parent.create (recursive: true );
76- final sourceJarLocation = dep.toURLString (repoLocation);
77- await targetFile
78- .writeAsBytes (await http.readBytes (Uri .parse (sourceJarLocation)));
79- }
80- await _runGradleCommand (deps, extractSources: true , targetDir);
76+ await _runGradleCommand (deps, taskName: 'downloadSources' , targetDir);
77+ await _runGradleCommand (deps, taskName: 'extractSourceJars' , targetDir);
8178 }
8279
8380 static Future <void > createStubProject (Directory rootTempDir) async {
@@ -103,19 +100,23 @@ class GradleTools {
103100 /// Downloads JAR files of all [deps] transitively into [targetDir] .
104101 static Future <void > downloadMavenJars (
105102 List <MavenDependency > deps, String targetDir) async {
106- await _runGradleCommand (deps, targetDir);
103+ await _runGradleCommand (deps, taskName: 'copyJars' , targetDir);
104+ await _runGradleCommand (deps, taskName: 'extractSourceJars' , targetDir);
107105 }
108106
109107 static String _getStubGradle (List <MavenDependency > deps, String targetDir,
110108 {String javaVersion = '11' }) {
111109 final depDecls = < String > [];
110+ final sourceDecls = < String > [];
112111 // Use implementation configuration
113112 for (var dep in deps) {
114113 depDecls.add (dep.toGradleDependency ('implementation' ));
114+ sourceDecls.add (dep.toURLString (repoLocation));
115115 }
116116 return '''
117117 plugins {
118118 java
119+ id("de.undercouch.download") version "5.7.0"
119120 }
120121
121122 repositories {
@@ -146,6 +147,14 @@ class GradleTools {
146147 into("$targetDir ")
147148 }
148149
150+ tasks.register<de.undercouch.gradle.tasks.download.Download>("downloadSources") {
151+ src(listOf(
152+ ${jsonEncode (sourceDecls ).replaceAll ("[" , "" ).replaceAll ("]" , "" )}
153+ ))
154+ dest("$targetDir ")
155+ overwrite(true)
156+ }
157+
149158 dependencies {
150159 ${depDecls .join ("\n " )}
151160 }''' ;
0 commit comments