From 7f96e02ead4c9f04e477c4c4c6e3c7714a98a5df Mon Sep 17 00:00:00 2001 From: Alex Li Date: Sat, 16 Nov 2024 21:03:21 +0800 Subject: [PATCH] fix: Use `/` in relative path for Windows (#788) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description As we all know, Windows uses `\` as a path separator, so it is reasonable to generate configs that use the same separator for Windows. But for *Run Configurations* and *Pubspec Dependencies*, `/` works on all platforms. Using `/` will significantly reduce the pain when those configurations are checked out to their code version control. This is first implemented by https://github.com/invertase/melos/commit/86a87a66253808c138e32e5df95d4a9a61f58145. ### Diff on Windows Before: ```xml ``` ```yaml dependency_overrides: package_a: path: ..\\package_a ``` After: ```xml ``` ```yaml dependency_overrides: package_a: path: ../package_a ``` ## Type of Change * [x] 🛠️ `fix` -- Bug fix (non-breaking change which fixes an issue) --- .../lib/src/common/intellij_project.dart | 15 +++--- .../test/common/intellij_project_test.dart | 47 +++++++++++++++++++ packages/melos/test/package_filter_test.dart | 5 +- packages/melos/test/utils.dart | 8 +--- 4 files changed, 59 insertions(+), 16 deletions(-) diff --git a/packages/melos/lib/src/common/intellij_project.dart b/packages/melos/lib/src/common/intellij_project.dart index c08eabb6d..a0bc6f199 100644 --- a/packages/melos/lib/src/common/intellij_project.dart +++ b/packages/melos/lib/src/common/intellij_project.dart @@ -284,12 +284,15 @@ class IntellijProject { await Future.forEach(_workspace.filteredPackages.values, (package) async { if (!package.isFlutterApp) return; - final generatedRunConfiguration = - injectTemplateVariables(flutterTestTemplate, { - 'flutterRunName': "Flutter Run -> '${package.name}'", - 'flutterRunMainDartPathRelative': - p.join(package.pathRelativeToWorkspace, 'lib', 'main.dart'), - }); + final generatedRunConfiguration = injectTemplateVariables( + flutterTestTemplate, + { + 'flutterRunName': "Flutter Run -> '${package.name}'", + 'flutterRunMainDartPathRelative': p + .join(package.pathRelativeToWorkspace, 'lib', 'main.dart') + .replaceAll(r'\', '/'), + }, + ); final outputFile = p.join( pathDotIdea, 'runConfigurations', diff --git a/packages/melos/test/common/intellij_project_test.dart b/packages/melos/test/common/intellij_project_test.dart index 1be7dec9e..60207acf4 100644 --- a/packages/melos/test/common/intellij_project_test.dart +++ b/packages/melos/test/common/intellij_project_test.dart @@ -1,5 +1,9 @@ +import 'dart:io'; + import 'package:melos/src/common/intellij_project.dart'; import 'package:melos/src/common/io.dart'; +import 'package:path/path.dart' as p; +import 'package:pubspec/pubspec.dart'; import 'package:test/test.dart'; import '../utils.dart'; @@ -54,4 +58,47 @@ void main() { expect(modulesXml, contains(r'file://$PROJECT_DIR$/test/melos_test.iml')); }, ); + + // https://github.com/invertase/melos/issues/788 + test( + 'Use / in relative path for Windows', + () async { + final tempDir = createTestTempDir(); + await createProject( + tempDir, + const PubSpec( + name: 'test', + dependencies: { + 'flutter': SdkReference('flutter'), + }, + ), + path: 'packages/test', + ); + File( + p.join(tempDir.path, 'packages', 'test', 'lib', 'main.dart'), + ).createSync(recursive: true); + + final workspaceBuilder = VirtualWorkspaceBuilder( + path: tempDir.path, + ''' + packages: + - packages/test + ''', + ); + workspaceBuilder.addPackage( + File(p.join(tempDir.path, 'packages', 'test', 'pubspec.yaml')) + .readAsStringSync(), + ); + final workspace = workspaceBuilder.build(); + final project = IntellijProject.fromWorkspace(workspace); + await project.generate(); + final runXml = readTextFile( + p.join(project.runConfigurationsDir.path, 'melos_flutter_run_test.xml'), + ); + expect( + runXml, + contains(r'$PROJECT_DIR$/packages/test/lib/main.dart'), + ); + }, + ); } diff --git a/packages/melos/test/package_filter_test.dart b/packages/melos/test/package_filter_test.dart index 5f2847623..921a1065a 100644 --- a/packages/melos/test/package_filter_test.dart +++ b/packages/melos/test/package_filter_test.dart @@ -2,7 +2,6 @@ import 'package:glob/glob.dart'; import 'package:melos/melos.dart'; import 'package:melos/src/common/glob.dart'; import 'package:melos/src/common/io.dart'; -import 'package:melos/src/common/platform.dart'; import 'package:path/path.dart' as p; import 'package:pubspec/pubspec.dart'; import 'package:test/test.dart'; @@ -137,9 +136,7 @@ void main() { Glob('packages/ab*'), ], }, - path: currentPlatform.isWindows - ? p.windows.normalize(path).replaceAll(r'\', r'\\') - : path, + path: path, ); } diff --git a/packages/melos/test/utils.dart b/packages/melos/test/utils.dart index b87e2d2a8..4a1594517 100644 --- a/packages/melos/test/utils.dart +++ b/packages/melos/test/utils.dart @@ -103,9 +103,7 @@ MelosWorkspaceConfig _defaultWorkspaceConfigBuilder(String path) => packages: [ createGlob('packages/**', currentDirectoryPath: path), ], - path: currentPlatform.isWindows - ? p.windows.normalize(path).replaceAll(r'\', r'\\') - : path, + path: path, ); Future createTemporaryWorkspace({ @@ -116,9 +114,7 @@ Future createTemporaryWorkspace({ final tempDir = createTempDir(p.join(Directory.current.path, '.dart_tool')); addTearDown(() => deleteEntry(tempDir)); - final workspacePath = currentPlatform.isWindows - ? p.windows.normalize(tempDir).replaceAll(r'\', r'\\') - : tempDir; + final workspacePath = tempDir; await createProject( Directory(workspacePath),