Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <sstream> | 5 #include <sstream> |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/strings/string_split.h" | 8 #include "base/strings/string_split.h" |
| 9 #include "tools/gn/commands.h" | 9 #include "tools/gn/commands.h" |
| 10 #include "tools/gn/filesystem_utils.h" | |
| 10 #include "tools/gn/input_file.h" | 11 #include "tools/gn/input_file.h" |
| 11 #include "tools/gn/parser.h" | 12 #include "tools/gn/parser.h" |
| 12 #include "tools/gn/scheduler.h" | 13 #include "tools/gn/scheduler.h" |
| 13 #include "tools/gn/setup.h" | 14 #include "tools/gn/setup.h" |
| 14 #include "tools/gn/source_file.h" | 15 #include "tools/gn/source_file.h" |
| 15 #include "tools/gn/tokenizer.h" | 16 #include "tools/gn/tokenizer.h" |
| 16 | 17 |
| 17 namespace commands { | 18 namespace commands { |
| 18 | 19 |
| 19 const char kSwitchDumpTree[] = "dump-tree"; | 20 const char kSwitchDumpTree[] = "dump-tree"; |
| 21 const char kSwitchInPlace[] = "in-place"; | |
| 20 | 22 |
| 21 const char kFormat[] = "format"; | 23 const char kFormat[] = "format"; |
| 22 const char kFormat_HelpShort[] = | 24 const char kFormat_HelpShort[] = |
| 23 "format: Format .gn file."; | 25 "format: Format .gn file. (ALPHA, WILL DESTROY DATA!)"; |
| 24 const char kFormat_Help[] = | 26 const char kFormat_Help[] = |
| 25 "gn format: Format .gn file. (ALPHA, WILL CURRENTLY DESTROY DATA!)\n" | 27 "gn format [--dump-tree] [--in-place] BUILD.gn\n" |
| 26 "\n" | |
| 27 " gn format //some/BUILD.gn\n" | |
| 28 " gn format some\\BUILD.gn\n" | |
| 29 "\n" | 28 "\n" |
| 30 " Formats .gn file to a standard format. THIS IS NOT FULLY IMPLEMENTED\n" | 29 " Formats .gn file to a standard format. THIS IS NOT FULLY IMPLEMENTED\n" |
| 31 " YET! IT WILL EAT YOUR BEAUTIFUL .GN FILES. AND YOUR LAUNDRY.\n" | 30 " YET! IT WILL EAT YOUR BEAUTIFUL .GN FILES. AND YOUR LAUNDRY.\n" |
| 32 " At a minimum, make sure everything is `git commit`d so you can\n" | 31 " At a minimum, make sure everything is `git commit`d so you can\n" |
| 33 " `git checkout -f` to recover.\n"; | 32 " `git checkout -f` to recover.\n" |
| 33 "\n" | |
| 34 "Arguments\n" | |
| 35 " --dump-tree\n" | |
| 36 " For debugging only, dumps the parse tree.\n" | |
| 37 "\n" | |
| 38 " --in-place\n" | |
| 39 " Instead writing the formatted file to stdout, replace the input\n" | |
| 40 " with the formatted output.\n" | |
| 41 "\n" | |
| 42 "Examples\n" | |
| 43 " gn format //some/BUILD.gn\n" | |
| 44 " gn format some\\BUILD.gn\n" | |
| 45 " gn format /abspath/some/BUILD.gn\n"; | |
| 34 | 46 |
| 35 namespace { | 47 namespace { |
| 36 | 48 |
| 37 const int kIndentSize = 2; | 49 const int kIndentSize = 2; |
| 38 const int kMaximumWidth = 80; | 50 const int kMaximumWidth = 80; |
| 39 | 51 |
| 40 class Printer { | 52 class Printer { |
| 41 public: | 53 public: |
| 42 Printer(); | 54 Printer(); |
| 43 ~Printer(); | 55 ~Printer(); |
| (...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 483 else if (style == kSequenceStyleList) | 495 else if (style == kSequenceStyleList) |
| 484 Print("]"); | 496 Print("]"); |
| 485 else if (style == kSequenceStyleBracedBlock) | 497 else if (style == kSequenceStyleBracedBlock) |
| 486 Print("}"); | 498 Print("}"); |
| 487 | 499 |
| 488 margin_ = old_margin; | 500 margin_ = old_margin; |
| 489 } | 501 } |
| 490 | 502 |
| 491 } // namespace | 503 } // namespace |
| 492 | 504 |
| 493 bool FormatFileToString(const std::string& input_filename, | 505 bool FormatFileToString(Setup* setup, |
| 506 const SourceFile& file, | |
| 494 bool dump_tree, | 507 bool dump_tree, |
| 495 std::string* output) { | 508 std::string* output) { |
| 496 Setup setup; | |
| 497 Err err; | 509 Err err; |
| 498 SourceFile input_file(input_filename); | |
| 499 const ParseNode* parse_node = | 510 const ParseNode* parse_node = |
| 500 setup.scheduler().input_file_manager()->SyncLoadFile( | 511 setup->scheduler().input_file_manager()->SyncLoadFile( |
| 501 LocationRange(), &setup.build_settings(), input_file, &err); | 512 LocationRange(), &setup->build_settings(), file, &err); |
| 502 if (err.has_error()) { | 513 if (err.has_error()) { |
| 503 err.PrintToStdout(); | 514 err.PrintToStdout(); |
| 504 return false; | 515 return false; |
| 505 } | 516 } |
| 506 if (dump_tree) { | 517 if (dump_tree) { |
| 507 std::ostringstream os; | 518 std::ostringstream os; |
| 508 parse_node->Print(os, 0); | 519 parse_node->Print(os, 0); |
| 509 printf("----------------------\n"); | 520 printf("----------------------\n"); |
| 510 printf("-- PARSE TREE --------\n"); | 521 printf("-- PARSE TREE --------\n"); |
| 511 printf("----------------------\n"); | 522 printf("----------------------\n"); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 525 // print the formatted output to stdout. | 536 // print the formatted output to stdout. |
| 526 if (args.size() != 1) { | 537 if (args.size() != 1) { |
| 527 Err(Location(), "Expecting exactly one argument, see `gn help format`.\n") | 538 Err(Location(), "Expecting exactly one argument, see `gn help format`.\n") |
| 528 .PrintToStdout(); | 539 .PrintToStdout(); |
| 529 return 1; | 540 return 1; |
| 530 } | 541 } |
| 531 | 542 |
| 532 bool dump_tree = | 543 bool dump_tree = |
| 533 base::CommandLine::ForCurrentProcess()->HasSwitch(kSwitchDumpTree); | 544 base::CommandLine::ForCurrentProcess()->HasSwitch(kSwitchDumpTree); |
| 534 | 545 |
| 535 std::string input_name = args[0]; | 546 Setup setup; |
| 536 if (input_name[0] != '/') { | 547 SourceDir source_dir = |
| 537 std::replace(input_name.begin(), input_name.end(), '\\', '/'); | 548 SourceDirForCurrentDirectory(setup.build_settings().root_path()); |
| 538 input_name = "//" + input_name; | 549 SourceFile file = source_dir.ResolveRelativeFile(args[0]); |
| 539 } | 550 |
| 540 std::string output_string; | 551 std::string output_string; |
| 541 if (FormatFileToString(input_name, dump_tree, &output_string)) { | 552 if (FormatFileToString(&setup, file, dump_tree, &output_string)) { |
| 553 bool in_place = | |
| 554 base::CommandLine::ForCurrentProcess()->HasSwitch(kSwitchInPlace); | |
| 555 CHECK(!in_place) << "TODO(scottmg)"; | |
|
scottmg
2014/10/02 21:54:29
(is there an easy way to map a SourceFile back to
brettw
2014/10/02 22:03:27
setup.build_settings()->GetFullPath(source_file)
| |
| 542 printf("%s", output_string.c_str()); | 556 printf("%s", output_string.c_str()); |
| 543 } | 557 } |
| 544 | 558 |
| 545 return 0; | 559 return 0; |
| 546 } | 560 } |
| 547 | 561 |
| 548 } // namespace commands | 562 } // namespace commands |
| OLD | NEW |