From 2802e4fbdce40c026a0c8a911678db5d66d0a462 Mon Sep 17 00:00:00 2001 From: Brian Wilkerson Date: Sat, 27 Mar 2021 22:42:56 +0000 Subject: [PATCH] Make the map file explicit rather than trying to coordinate between caller and callee Change-Id: I6f7b7807eeca7a023772c0bdcf4992e4762f84e3 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/193260 Reviewed-by: Konstantin Shcheglov Commit-Queue: Brian Wilkerson --- .../code_completion/completion_metrics.dart | 47 +++++++------------ .../relevance_table_generator.dart | 45 ++++++------------ 2 files changed, 31 insertions(+), 61 deletions(-) diff --git a/pkg/analysis_server/tool/code_completion/completion_metrics.dart b/pkg/analysis_server/tool/code_completion/completion_metrics.dart index 850ffd72820..147959510cd 100644 --- a/pkg/analysis_server/tool/code_completion/completion_metrics.dart +++ b/pkg/analysis_server/tool/code_completion/completion_metrics.dart @@ -106,33 +106,11 @@ Future main(List args) async { print(''); print('Metrics computed in $duration'); - File uniqueDataFile() { - var dataDir = result['mapDir']; - var baseFileName = provider.pathContext.basename(rootPath); - var index = 1; - while (index < 10000) { - var suffix = (index++).toString(); - suffix = '0000'.substring(suffix.length) + suffix + '.json'; - var fileName = baseFileName + suffix; - var filePath = provider.pathContext.join(dataDir, fileName); - var file = provider.getFile(filePath); - if (!file.exists) { - return file; - } - } - - /// If there are more than 10000 directories with the same name, just - /// overwrite a previously generated file. - var fileName = baseFileName + '9999'; - var filePath = provider.pathContext.join(dataDir, fileName); - return provider.getFile(filePath); - } - - if (result.wasParsed('mapDir')) { - var dataFile = uniqueDataFile(); + if (result.wasParsed('mapFile')) { + var mapFile = provider.getFile(result['mapFile'] as String); var map = computer.targetMetrics.map((metrics) => metrics.toJson()).toList(); - dataFile.writeAsStringSync(json.encode(map)); + mapFile.writeAsStringSync(json.encode(map)); } else { computer.printResults(); } @@ -210,10 +188,10 @@ ArgParser createArgParser() { 'worst mrr scores.', negatable: false) ..addOption( - 'mapDir', - help: 'The absolute path of the directory to which the completion ' - 'metrics data will be written. Using this option will prevent the ' - 'completion results from being written in a textual form.', + 'mapFile', + help: 'The absolute path of the file to which the completion metrics ' + 'data will be written. Using this option will prevent the completion ' + 'results from being written in a textual form.', ) ..addOption( 'reduceDir', @@ -247,8 +225,15 @@ bool validArguments(ArgParser parser, ArgResults result) { printUsage(parser, error: 'No package path specified.'); return false; } - if (result.wasParsed('mapDir')) { - return validateDir(parser, result['mapDir']); + if (result.wasParsed('mapFile')) { + var mapFilePath = result['mapFile']; + if (mapFilePath is! String || + !PhysicalResourceProvider.INSTANCE.pathContext + .isAbsolute(mapFilePath)) { + printUsage(parser, + error: 'The path "$mapFilePath" must be an absolute path.'); + return false; + } } return validateDir(parser, result.rest[0]); } diff --git a/pkg/analysis_server/tool/code_completion/relevance_table_generator.dart b/pkg/analysis_server/tool/code_completion/relevance_table_generator.dart index 4b2ee62ed13..60a3f5bf607 100644 --- a/pkg/analysis_server/tool/code_completion/relevance_table_generator.dart +++ b/pkg/analysis_server/tool/code_completion/relevance_table_generator.dart @@ -75,34 +75,12 @@ Future main(List args) async { var rootPath = result.rest[0]; print('Analyzing root: "$rootPath"'); - File uniqueDataFile() { - var dataDir = result['mapDir']; - var baseFileName = provider.pathContext.basename(rootPath); - var index = 1; - while (index < 10000) { - var suffix = (index++).toString(); - suffix = '0000'.substring(suffix.length) + suffix + '.json'; - var fileName = baseFileName + suffix; - var filePath = provider.pathContext.join(dataDir, fileName); - var file = provider.getFile(filePath); - if (!file.exists) { - return file; - } - } - - /// If there are more than 10000 directories with the same name, just - /// overwrite a previously generated file. - var fileName = baseFileName + '9999'; - var filePath = provider.pathContext.join(dataDir, fileName); - return provider.getFile(filePath); - } - var computer = RelevanceMetricsComputer(); var stopwatch = Stopwatch()..start(); await computer.compute(rootPath, verbose: result['verbose']); - if (result.wasParsed('mapDir')) { - var dataFile = uniqueDataFile(); - dataFile.writeAsStringSync(computer.data.toJson()); + if (result.wasParsed('mapFile')) { + var mapFile = provider.getFile(result['mapFile'] as String); + mapFile.writeAsStringSync(computer.data.toJson()); } else { writeRelevanceTable(computer.data); } @@ -123,9 +101,9 @@ ArgParser createArgParser() { negatable: false, ); parser.addOption( - 'mapDir', - help: 'The absolute path of the directory to which the relevance data will ' - 'be written. Using this option will prevent the relevance table from ' + 'mapFile', + help: 'The absolute path of the file to which the relevance data will be ' + 'written. Using this option will prevent the relevance table from ' 'being written.', ); parser.addOption( @@ -167,8 +145,15 @@ bool validArguments(ArgParser parser, ArgResults result) { printUsage(parser, error: 'No package path specified.'); return false; } - if (result.wasParsed('mapDir')) { - return validateDir(parser, result['mapDir']); + if (result.wasParsed('mapFile')) { + var mapFilePath = result['mapFile']; + if (mapFilePath is! String || + !PhysicalResourceProvider.INSTANCE.pathContext + .isAbsolute(mapFilePath)) { + printUsage(parser, + error: 'The path "$mapFilePath" must be an absolute path.'); + return false; + } } return validateDir(parser, result.rest[0]); }