|
2 | 2 | // for details. All rights reserved. Use of this source code is governed by a
|
3 | 3 | // BSD-style license that can be found in the LICENSE file.
|
4 | 4 |
|
| 5 | +import 'package:path/path.dart' as p; |
| 6 | +import 'package:pub/src/source/path.dart'; |
5 | 7 | import 'package:pub_semver/pub_semver.dart';
|
6 | 8 | import 'package:yaml/yaml.dart';
|
7 | 9 |
|
@@ -313,25 +315,45 @@ class AddCommand extends PubCommand {
|
313 | 315 | if (gitUrl == null) {
|
314 | 316 | usageException('The `--git-url` is required for git dependencies.');
|
315 | 317 | }
|
| 318 | + Uri parsed; |
| 319 | + try { |
| 320 | + parsed = Uri.parse(gitUrl); |
| 321 | + } on FormatException catch (e) { |
| 322 | + usageException('The --git-url must be a valid url: ${e.message}.'); |
| 323 | + } |
| 324 | + final urlRelativeToEntrypoint = parsed.isAbsolute |
| 325 | + ? parsed.toString() |
| 326 | + : |
| 327 | + // Turn the relative url from current working directory into a relative |
| 328 | + // url from the entrypoint. |
| 329 | + p.url.relative( |
| 330 | + p.url.join(Uri.file(p.absolute(p.current)).toString(), |
| 331 | + parsed.toString()), |
| 332 | + from: p.toUri(p.absolute(entrypoint.root.dir)).toString()); |
316 | 333 |
|
317 | 334 | /// Process the git options to return the simplest representation to be
|
318 | 335 | /// added to the pubspec.
|
319 | 336 | if (gitRef == null && gitPath == null) {
|
320 |
| - git = gitUrl; |
| 337 | + git = urlRelativeToEntrypoint; |
321 | 338 | } else {
|
322 |
| - git = {'url': gitUrl, 'ref': gitRef, 'path': gitPath}; |
| 339 | + git = {'url': urlRelativeToEntrypoint, 'ref': gitRef, 'path': gitPath}; |
323 | 340 | git.removeWhere((key, value) => value == null);
|
324 | 341 | }
|
325 | 342 |
|
326 | 343 | packageRange = cache.sources['git']
|
327 |
| - .parseRef(packageName, git) |
| 344 | + .parseRef(packageName, git, containingPath: entrypoint.pubspecPath) |
328 | 345 | .withConstraint(constraint ?? VersionConstraint.any);
|
329 | 346 | pubspecInformation = {'git': git};
|
330 | 347 | } else if (path != null) {
|
| 348 | + final relativeToEntryPoint = p.isRelative(path) |
| 349 | + ? PathSource.relativePathWithPosixSeparators( |
| 350 | + p.relative(path, from: entrypoint.root.dir)) |
| 351 | + : path; |
331 | 352 | packageRange = cache.sources['path']
|
332 |
| - .parseRef(packageName, path, containingPath: entrypoint.pubspecPath) |
| 353 | + .parseRef(packageName, relativeToEntryPoint, |
| 354 | + containingPath: entrypoint.pubspecPath) |
333 | 355 | .withConstraint(constraint ?? VersionConstraint.any);
|
334 |
| - pubspecInformation = {'path': path}; |
| 356 | + pubspecInformation = {'path': relativeToEntryPoint}; |
335 | 357 | } else if (sdk != null) {
|
336 | 358 | packageRange = cache.sources['sdk']
|
337 | 359 | .parseRef(packageName, sdk)
|
|
0 commit comments