Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt paths for different development environments #336

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion web/test/dart_fix_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ void main() {

try {
// set up project
// Convert the current path to a POSIX path to avoid 'path_not_posix' lint.
writeFile(tempDir, 'pubspec.yaml', '''
name: test_project
environment:
sdk: '^$sdkVersion'
dependencies:
web:
path: ${Directory.current.path}
path: ${p.current.replaceAll(r'\', '/')}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Directory.current.uri.toFilePath() makes more sense here than us manually modifying the path.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I missed that. Thank you for the mention.

Since the issue trying to fix here is to force yaml using POSIX-like path (here is the lint), so the upcomming fix will use Directory.current.uri.toFilePath(windows: false).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this fix will encounter another problem.

For example, web path in Windows is C:\a\b, the lint suggests the fix is to simply change \ to /.
The correct path is C:/a/b.
This results C:/a/b is not Windows path nor POSIX path, so it cannot be fixed by using uri.toFilePath(windows: false) since the output will be something like /C:/a/b/

Perhaps there's an approach to avoid manually modifying the path that I'm unaware of ?

Copy link
Contributor

@srujzs srujzs Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, it looks like it's not correctly interpreting the C: as the authority. We've tried different APIs to see what the right way of emitting a POSIX path from a Windows path is, but everything else also seems janky (maybe adding a directory:/// or something similar may help, but that doesn't look like it's sufficient anyways), so I'm okay keeping the p.current.replaceAll(r'\', '/') change you initially had. I'd just add a comment mentioning why we do it like:

// Convert the current path to a POSIX path to avoid 'path_not_posix' lint.

Thanks for investigating!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a minor detail, I think the beat looking comment position is above the writeFile() because we have full control of the yaml file.

      // set up project
      // COMMENT_HERE
      writeFile(tempDir, 'pubspec.yaml', '''
...
 ''');

There are another two options

  1. commenting inside the yaml.
  2. commenting out of multiline string.

Since none of them look concise enough comparing to the above, so I choose to the above over the others.

  1. inside yaml:
      writeFile(tempDir, 'pubspec.yaml', '''
name: test_project
environment:
  sdk: '^$sdkVersion'
dependencies:
  web:
    # COMMENT_HERE
    path: ${p.current.replaceAll(r'\', '/')}
  ''');
  1. out of multiline string:
      writeFile(tempDir, 'pubspec.yaml', '''
name: test_project
environment:
  sdk: '^$sdkVersion'
dependencies:
  web:
  '''
    // COMMENT_HERE
    'path: ${p.current.replaceAll(r'\', '/')}');

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it makes sense to keep it outside of the yaml as an implementation detail and place it above writeFile.

''');
final sourceFile = File(p.join('test_fixes', 'renames.dart'));
writeFile(
Expand Down
1 change: 1 addition & 0 deletions web_generator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
description.
- Added `--generate-all` option to generate all bindings, including experimental
and non-standard APIs.
- Adapt paths for different development environments.
11 changes: 6 additions & 5 deletions web_generator/bin/scrape_mdn.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ Future<void> main(List<String> args) async {
final offline = args.length == 1 && args.first == '--offline';

// clone the repo
final repoDir =
Directory(Platform.script.resolve('../.dart_tool/mdn_content').path);
final repoPath =
p.fromUri(Platform.script.resolve('../.dart_tool/mdn_content'));
final repoDir = Directory(repoPath);
if (!repoDir.existsSync()) {
await _run(
'git',
Expand Down Expand Up @@ -77,9 +78,9 @@ Future<void> main(List<String> args) async {
print('${interfaces.length} items read from $gitUrl.');

const encoder = JsonEncoder.withIndent(' ');

final file =
File(Platform.script.resolve('../../third_party/mdn/mdn.json').path);
final filePath =
p.fromUri(Platform.script.resolve('../../third_party/mdn/mdn.json'));
final file = File(filePath);
final json = {
'__meta__': {
'source': '[MDN Web Docs]($mdnUrl)',
Expand Down
31 changes: 16 additions & 15 deletions web_generator/bin/update_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ $_usage''');
return;
}

assert(p.fromUri(Platform.script).endsWith(_thisScript));
assert(p.fromUri(Platform.script).endsWith(_thisScript.toFilePath()));

// Run `npm install` or `npm update` as needed.
final update = argResult['update'] as bool;
Expand Down Expand Up @@ -68,7 +68,7 @@ $_usage''');
}

// Determine the set of previously generated files.
final domDir = Directory(p.join(_webPackagePath, 'lib/src/dom'));
final domDir = Directory(p.join(_webPackagePath, 'lib', 'src', 'dom'));
final existingFiles =
domDir.listSync(recursive: true).whereType<File>().where((file) {
if (!file.path.endsWith('.dart')) return false;
Expand All @@ -86,7 +86,7 @@ $_usage''');
'node',
[
'main.mjs',
'--output-directory=${p.join(_webPackagePath, 'lib/src')}',
'--output-directory=${p.join(_webPackagePath, 'lib', 'src')}',
if (generateAll) '--generate-all',
],
workingDirectory: _bindingsGeneratorPath,
Expand All @@ -101,9 +101,8 @@ $_usage''');
}

// Update readme.
final readmeFile = File(
p.normalize(Platform.script.resolve('../README.md').path),
);
final readmeFile =
File(p.normalize(p.fromUri(Platform.script.resolve('../README.md'))));

final sourceContent = readmeFile.readAsStringSync();

Expand Down Expand Up @@ -148,7 +147,7 @@ Future<String> _webPackageLanguageVersion(String pkgPath) async {
return '$languageVersion.0';
}

final _webPackagePath = Platform.script.resolve('../../web').path;
final _webPackagePath = p.fromUri(Platform.script.resolve('../../web'));

String _packageLockVersion(String package) {
final packageLockData = jsonDecode(
Expand All @@ -161,18 +160,19 @@ String _packageLockVersion(String package) {
return webRefIdl['version'] as String;
}

final _bindingsGeneratorPath = Platform.script.resolve('../lib/src').path;
final _bindingsGeneratorPath = p.fromUri(Platform.script.resolve('../lib/src'));

const _webRefCss = '@webref/css';
const _webRefElements = '@webref/elements';
const _webRefIdl = '@webref/idl';

const _thisScript = 'bin/update_bindings.dart';
final _thisScript = Uri.parse('bin/update_bindings.dart');
final _scriptPOSIXPath = _thisScript.toFilePath(windows: false);

const _startComment =
'<!-- START updated by $_thisScript. Do not modify by hand -->';
const _endComment =
'<!-- END updated by $_thisScript. Do not modify by hand -->';
final _startComment =
'<!-- START updated by $_scriptPOSIXPath. Do not modify by hand -->';
final _endComment =
'<!-- END updated by $_scriptPOSIXPath. Do not modify by hand -->';

Future<void> _runProc(
String executable,
Expand All @@ -184,6 +184,7 @@ Future<void> _runProc(
executable,
arguments,
mode: ProcessStartMode.inheritStdio,
runInShell: Platform.isWindows,
workingDirectory: workingDirectory,
);
final procExit = await proc.exitCode;
Expand All @@ -197,7 +198,7 @@ Future<void> _runProc(
Future<void> _generateJsTypeSupertypes() async {
// Use a file that uses `dart:js_interop` for analysis.
final contextCollection = AnalysisContextCollection(
includedPaths: [p.join(_webPackagePath, 'lib/src/dom.dart')]);
includedPaths: [p.join(_webPackagePath, 'lib', 'src', 'dom.dart')]);
final dartJsInterop = (await contextCollection.contexts.single.currentSession
.getLibraryByUri('dart:js_interop') as LibraryElementResult)
.element;
Expand Down Expand Up @@ -240,7 +241,7 @@ Future<void> _generateJsTypeSupertypes() async {
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// Updated by $_thisScript. Do not modify by hand.
// Updated by $_scriptPOSIXPath. Do not modify by hand.

const Map<String, String?> jsTypeSupertypes = {
${jsTypeSupertypes.entries.map((e) => " ${e.key}: ${e.value},").join('\n')}
Expand Down
Loading