From 1165eef92fba2ac984136ae7e0f371bc908280f9 Mon Sep 17 00:00:00 2001 From: Hedwin Coursimault Date: Fri, 25 Oct 2024 16:02:36 +0200 Subject: [PATCH 1/7] fix: handle custom hosted dependency --- packages/custom_lint/lib/src/workspace.dart | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/custom_lint/lib/src/workspace.dart b/packages/custom_lint/lib/src/workspace.dart index 60de1fb8..c0e5e59c 100644 --- a/packages/custom_lint/lib/src/workspace.dart +++ b/packages/custom_lint/lib/src/workspace.dart @@ -110,7 +110,21 @@ String _buildDependencyConstraint( switch (sharedConstraint) { case HostedDependency(): - return ' ${sharedConstraint.getDisplayString()}'; + final hosted = sharedConstraint.hosted; + if (hosted == null) { + return ' ${sharedConstraint.getDisplayString()}'; + } + final result = StringBuffer('\n hosted:'); + if (hosted.declaredName != null) { + result.write('\n name: ${hosted.declaredName}'); + } + if (hosted.url != null) { + result.write('\n url: ${hosted.url}'); + } + result.write('\n version: "${sharedConstraint.getDisplayString()}"'); + + return result.toString(); + case PathDependency(): // Use appropriate path separators across platforms final path = posix.prettyUri(sharedConstraint.path); From b947921c352daa35f66d38a8be7018b4a5a0b044 Mon Sep 17 00:00:00 2001 From: Hedwin Coursimault Date: Fri, 25 Oct 2024 16:31:13 +0200 Subject: [PATCH 2/7] fix: add writeln instead of "\n" --- packages/custom_lint/lib/src/workspace.dart | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/custom_lint/lib/src/workspace.dart b/packages/custom_lint/lib/src/workspace.dart index c0e5e59c..6d3ac4b2 100644 --- a/packages/custom_lint/lib/src/workspace.dart +++ b/packages/custom_lint/lib/src/workspace.dart @@ -114,14 +114,16 @@ String _buildDependencyConstraint( if (hosted == null) { return ' ${sharedConstraint.getDisplayString()}'; } - final result = StringBuffer('\n hosted:'); + final result = StringBuffer(); + result.writeln(); + result.writeln(' hosted:'); if (hosted.declaredName != null) { - result.write('\n name: ${hosted.declaredName}'); + result.writeln(' name: ${hosted.declaredName}'); } if (hosted.url != null) { - result.write('\n url: ${hosted.url}'); + result.writeln(' url: ${hosted.url}'); } - result.write('\n version: "${sharedConstraint.getDisplayString()}"'); + result.writeln(' version: ${sharedConstraint.getDisplayString()}'); return result.toString(); @@ -562,6 +564,7 @@ publish_to: 'none' _writeEnvironment(buffer); _writePubspecDependencies(buffer); + print('pubpsec: $buffer'); return buffer.toString(); } From dcfab2de1648d979d95957131e4b65840e2859f6 Mon Sep 17 00:00:00 2001 From: Hedwin Coursimault Date: Fri, 25 Oct 2024 17:10:10 +0200 Subject: [PATCH 3/7] feat: add test --- .../custom_lint/test/src/workspace_test.dart | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/packages/custom_lint/test/src/workspace_test.dart b/packages/custom_lint/test/src/workspace_test.dart index bf156053..9d3e0a5b 100644 --- a/packages/custom_lint/test/src/workspace_test.dart +++ b/packages/custom_lint/test/src/workspace_test.dart @@ -2213,6 +2213,46 @@ publish_to: 'none' dependencies: plugin1: ">=1.5.0 <2.0.0" +'''); + }); + test( + 'If a dependency comes from a custom hosted source, the generated pubspec.yaml should contain the hosted source', + () async { + final workingDir = await createSimpleWorkspace([ + Pubspec( + 'a', + dependencies: { + 'custom_lint_builder': HostedDependency( + hosted: HostedDetails( + 'custom_lint_builder', + Uri.parse('https://custom.com'), + ), + version: Version(1, 0, 0), + ), + }, + ), + ]); + + final workspace = await fromContextRootsFromPaths( + ['a'], + workingDirectory: workingDir, + ); + + expect(workspace.computePubspec(), ''' +name: custom_lint_client +description: A client for custom_lint +version: 0.0.1 +publish_to: 'none' + +environment: + sdk: ">=3.0.0 <4.0.0" + +dependencies: + custom_lint_builder: + hosted: + name: custom_lint_builder + url: https://custom.com + version: "1.0.0" '''); }); }); From d0d2700e830fb20ac077372d51dcf93b16b5a5e8 Mon Sep 17 00:00:00 2001 From: Remi Rousselet Date: Sun, 27 Oct 2024 16:21:19 +0100 Subject: [PATCH 4/7] Apply suggestions from code review --- packages/custom_lint/lib/src/workspace.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/custom_lint/lib/src/workspace.dart b/packages/custom_lint/lib/src/workspace.dart index 6d3ac4b2..31476d24 100644 --- a/packages/custom_lint/lib/src/workspace.dart +++ b/packages/custom_lint/lib/src/workspace.dart @@ -564,7 +564,6 @@ publish_to: 'none' _writeEnvironment(buffer); _writePubspecDependencies(buffer); - print('pubpsec: $buffer'); return buffer.toString(); } From 0f08abf5f8fe6118b9dfb0e89ba348ae9746fa4d Mon Sep 17 00:00:00 2001 From: Hedwin Coursimault Date: Tue, 5 Nov 2024 17:07:29 +0100 Subject: [PATCH 5/7] fix: write instead of writln for last line oh custom hosted dependency --- packages/custom_lint/lib/src/workspace.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/custom_lint/lib/src/workspace.dart b/packages/custom_lint/lib/src/workspace.dart index 31476d24..16c84d07 100644 --- a/packages/custom_lint/lib/src/workspace.dart +++ b/packages/custom_lint/lib/src/workspace.dart @@ -123,8 +123,7 @@ String _buildDependencyConstraint( if (hosted.url != null) { result.writeln(' url: ${hosted.url}'); } - result.writeln(' version: ${sharedConstraint.getDisplayString()}'); - + result.write(' version: ${sharedConstraint.getDisplayString()}'); return result.toString(); case PathDependency(): From 56e0d5295cc339403a77bb5ea8204afd2ba6b3b1 Mon Sep 17 00:00:00 2001 From: Hedwin Coursimault Date: Tue, 5 Nov 2024 17:08:33 +0100 Subject: [PATCH 6/7] feat: add tests --- .../custom_lint/test/src/workspace_test.dart | 102 +++++++++++++++++- 1 file changed, 101 insertions(+), 1 deletion(-) diff --git a/packages/custom_lint/test/src/workspace_test.dart b/packages/custom_lint/test/src/workspace_test.dart index 9d3e0a5b..0cab8398 100644 --- a/packages/custom_lint/test/src/workspace_test.dart +++ b/packages/custom_lint/test/src/workspace_test.dart @@ -46,9 +46,18 @@ extension on Dependency { final that = this; if (that is HostedDependency) { if (that.hosted != null) { + String? safeName; + try { + safeName = that.hosted!.name; + + // `that.hosted!.name` could throw an error if `_nameOfPackage` is null in the getter. + // We need to safely handle this scenario because we can't guarantee that the value is not null. + // ignore: avoid_catching_errors + } on Error catch (_) {} + return { 'hosted': { - 'name': that.hosted!.name, + if (safeName != null) 'name': safeName, 'url': that.hosted!.url.toString(), }, 'version': that.version.toString(), @@ -2255,6 +2264,97 @@ dependencies: version: "1.0.0" '''); }); + group( + 'Support hosted project with custom source', + () { + test( + 'If a dependency comes from a custom hosted source, the generated pubspec.yaml should contain the hosted source', + () async { + final workingDir = await createSimpleWorkspace([ + Pubspec( + 'plugin1', + dependencies: { + 'custom_lint_builder': HostedDependency(), + }, + ), + Pubspec( + 'a', + devDependencies: { + 'plugin1': HostedDependency( + hosted: HostedDetails( + 'plugin1', + Uri.parse('https://custom.com'), + ), + version: Version(1, 0, 0), + ), + }, + ), + ]); + + final workspace = await fromContextRootsFromPaths( + ['a'], + workingDirectory: workingDir, + ); + + expect(workspace.computePubspec(), ''' +name: custom_lint_client +description: A client for custom_lint +version: 0.0.1 +publish_to: 'none' + +dependencies: + plugin1: + hosted: + name: plugin1 + url: https://custom.com + version: "1.0.0" +'''); + }); + test( + 'Hosted withouth name should still work', + () async { + final workingDir = await createSimpleWorkspace([ + Pubspec( + 'plugin1', + dependencies: { + 'custom_lint_builder': HostedDependency(), + }, + ), + Pubspec( + 'a', + devDependencies: { + 'plugin1': HostedDependency( + hosted: HostedDetails( + null, + Uri.parse('https://custom.com'), + ), + version: Version(1, 0, 0), + ), + }, + ), + ]); + + final workspace = await fromContextRootsFromPaths( + ['a'], + workingDirectory: workingDir, + ); + + expect(workspace.computePubspec(), ''' +name: custom_lint_client +description: A client for custom_lint +version: 0.0.1 +publish_to: 'none' + +dependencies: + plugin1: + hosted: + url: https://custom.com + version: "1.0.0" +'''); + }, + ); + }, + ); }); group(CustomLintWorkspace.fromPaths, () { From 6d2e1a5960700aaea22509e3c5da921bb2612365 Mon Sep 17 00:00:00 2001 From: Hedwin Coursimault Date: Thu, 21 Nov 2024 10:27:43 +0100 Subject: [PATCH 7/7] fix: doubled test --- .../custom_lint/test/src/workspace_test.dart | 40 ------------------- 1 file changed, 40 deletions(-) diff --git a/packages/custom_lint/test/src/workspace_test.dart b/packages/custom_lint/test/src/workspace_test.dart index 0cab8398..ad01a810 100644 --- a/packages/custom_lint/test/src/workspace_test.dart +++ b/packages/custom_lint/test/src/workspace_test.dart @@ -2222,46 +2222,6 @@ publish_to: 'none' dependencies: plugin1: ">=1.5.0 <2.0.0" -'''); - }); - test( - 'If a dependency comes from a custom hosted source, the generated pubspec.yaml should contain the hosted source', - () async { - final workingDir = await createSimpleWorkspace([ - Pubspec( - 'a', - dependencies: { - 'custom_lint_builder': HostedDependency( - hosted: HostedDetails( - 'custom_lint_builder', - Uri.parse('https://custom.com'), - ), - version: Version(1, 0, 0), - ), - }, - ), - ]); - - final workspace = await fromContextRootsFromPaths( - ['a'], - workingDirectory: workingDir, - ); - - expect(workspace.computePubspec(), ''' -name: custom_lint_client -description: A client for custom_lint -version: 0.0.1 -publish_to: 'none' - -environment: - sdk: ">=3.0.0 <4.0.0" - -dependencies: - custom_lint_builder: - hosted: - name: custom_lint_builder - url: https://custom.com - version: "1.0.0" '''); }); group(