Skip to content

Commit

Permalink
fix: Use / in relative path for Windows (#788)
Browse files Browse the repository at this point in the history
## 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
86a87a6.

### Diff on Windows

Before:

```xml
<component name="ProjectRunConfigurationManager">
  <configuration default="false" name="Flutter Run -&gt; 'app'" type="FlutterRunConfigurationType" factoryName="Flutter">
    <option name="filePath" value="$PROJECT_DIR$/app\lib\main.dart" />
    <method v="2" />
  </configuration>
</component>
```

```yaml
dependency_overrides:
  package_a:
    path: ..\\package_a
```

After:

```xml
<component name="ProjectRunConfigurationManager">
  <configuration default="false" name="Flutter Run -&gt; 'app'" type="FlutterRunConfigurationType" factoryName="Flutter">
    <option name="filePath" value="$PROJECT_DIR$/app/lib/main.dart" />
    <method v="2" />
  </configuration>
</component>
```

```yaml
dependency_overrides:
  package_a:
    path: ../package_a
```

## Type of Change

* [x]  🛠️ `fix` -- Bug fix (non-breaking change which fixes an issue)
  • Loading branch information
AlexV525 authored Nov 16, 2024
1 parent e804822 commit 7f96e02
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 16 deletions.
15 changes: 9 additions & 6 deletions packages/melos/lib/src/common/intellij_project.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 -&gt; '${package.name}'",
'flutterRunMainDartPathRelative':
p.join(package.pathRelativeToWorkspace, 'lib', 'main.dart'),
});
final generatedRunConfiguration = injectTemplateVariables(
flutterTestTemplate,
{
'flutterRunName': "Flutter Run -&gt; '${package.name}'",
'flutterRunMainDartPathRelative': p
.join(package.pathRelativeToWorkspace, 'lib', 'main.dart')
.replaceAll(r'\', '/'),
},
);
final outputFile = p.join(
pathDotIdea,
'runConfigurations',
Expand Down
47 changes: 47 additions & 0 deletions packages/melos/test/common/intellij_project_test.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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'),
);
},
);
}
5 changes: 1 addition & 4 deletions packages/melos/test/package_filter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -137,9 +136,7 @@ void main() {
Glob('packages/ab*'),
],
},
path: currentPlatform.isWindows
? p.windows.normalize(path).replaceAll(r'\', r'\\')
: path,
path: path,
);
}

Expand Down
8 changes: 2 additions & 6 deletions packages/melos/test/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Directory> createTemporaryWorkspace({
Expand All @@ -116,9 +114,7 @@ Future<Directory> 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),
Expand Down

0 comments on commit 7f96e02

Please sign in to comment.