Refactor protos to add package and license lines (#696)

Follow official protobuf style guide:

1. License header (if applicable)
2. File overview
3. Syntax
4. Package
5. Imports (sorted)
6. File options
7. Everything else

Internally we want a `package` line in every proto, and it's easier to
do add `package` lines to protos than to add them as exceptions.

License headers added the old files based on creation date.

protos in google/protobuf are not updated as they are copied from
protoc.

The proto file `unittest_nested_any.proto` was added #568. Since it's
not copied from protoc I moved it to test/protos.

Fixes #692
diff --git a/protoc_plugin/Makefile b/protoc_plugin/Makefile
index 939c4c0..83ca893 100644
--- a/protoc_plugin/Makefile
+++ b/protoc_plugin/Makefile
@@ -21,7 +21,6 @@
 	google/protobuf/timestamp \
 	google/protobuf/type \
 	google/protobuf/unittest_import \
-	google/protobuf/unittest_nested_any \
 	google/protobuf/unittest_optimize_for \
 	google/protobuf/unittest_well_known_types \
 	google/protobuf/unittest \
@@ -46,6 +45,7 @@
 	map_field \
 	mixins \
 	multiple_files_test \
+	nested_any \
 	nested_extension \
 	nested_message \
 	non_nested_extension \
diff --git a/protoc_plugin/test/any_test.dart b/protoc_plugin/test/any_test.dart
index df1cb12..19c6703 100644
--- a/protoc_plugin/test/any_test.dart
+++ b/protoc_plugin/test/any_test.dart
@@ -13,10 +13,10 @@
 void main() {
   test('pack -> unpack', () {
     var any = Any.pack(SearchRequest()..query = 'hest');
-    expect(any.typeUrl, 'type.googleapis.com/SearchRequest');
+    expect(any.typeUrl, 'type.googleapis.com/service.SearchRequest');
     var any1 = Any.pack(SearchRequest()..query = 'hest1',
         typeUrlPrefix: 'example.com');
-    expect(any1.typeUrl, 'example.com/SearchRequest');
+    expect(any1.typeUrl, 'example.com/service.SearchRequest');
     expect(any1.canUnpackInto(SearchRequest.getDefault()), true);
     expect(any1.canUnpackInto(SearchResponse.getDefault()), false);
     var searchRequest = any.unpackInto(SearchRequest());
@@ -42,7 +42,7 @@
     var any = Any.pack(toplevel.T()
       ..a = 127
       ..b = 'hest');
-    expect(any.typeUrl, 'type.googleapis.com/T');
+    expect(any.typeUrl, 'type.googleapis.com/toplevel.T');
     var t2 = any.unpackInto(toplevel.T());
     expect(t2.a, 127);
     expect(t2.b, 'hest');
diff --git a/protoc_plugin/test/proto3_json_test.dart b/protoc_plugin/test/proto3_json_test.dart
index 1fb4fea..606f338 100644
--- a/protoc_plugin/test/proto3_json_test.dart
+++ b/protoc_plugin/test/proto3_json_test.dart
@@ -16,10 +16,10 @@
 import '../out/protos/google/protobuf/struct.pb.dart';
 import '../out/protos/google/protobuf/timestamp.pb.dart';
 import '../out/protos/google/protobuf/unittest.pb.dart';
-import '../out/protos/google/protobuf/unittest_nested_any.pb.dart';
 import '../out/protos/google/protobuf/unittest_well_known_types.pb.dart';
 import '../out/protos/google/protobuf/wrappers.pb.dart';
 import '../out/protos/map_field.pb.dart';
+import '../out/protos/nested_any.pb.dart';
 import '../out/protos/oneof.pb.dart';
 import 'oneof_test.dart';
 import 'test_util.dart';
@@ -281,12 +281,10 @@
           {
             'anyField2': {
               'value': '1',
-              '@type':
-                  'type.googleapis.com/protobuf_unittest_nested_any.AnyMessage1'
+              '@type': 'type.googleapis.com/nested_any.AnyMessage1'
             },
             'value': '2',
-            '@type':
-                'type.googleapis.com/protobuf_unittest_nested_any.AnyMessage2'
+            '@type': 'type.googleapis.com/nested_any.AnyMessage2'
           });
     });
 
@@ -941,12 +939,10 @@
         ..mergeFromProto3Json({
           'anyField2': {
             'value': '1',
-            '@type':
-                'type.googleapis.com/protobuf_unittest_nested_any.AnyMessage1'
+            '@type': 'type.googleapis.com/nested_any.AnyMessage1'
           },
           'value': '2',
-          '@type':
-              'type.googleapis.com/protobuf_unittest_nested_any.AnyMessage2'
+          '@type': 'type.googleapis.com/nested_any.AnyMessage2'
         }, typeRegistry: TypeRegistry([AnyMessage1(), AnyMessage2()]));
 
       expect(
diff --git a/protoc_plugin/test/protos/ExtensionEnumNameConflict.proto b/protoc_plugin/test/protos/ExtensionEnumNameConflict.proto
index e26f8d0..7fc55b1 100644
--- a/protoc_plugin/test/protos/ExtensionEnumNameConflict.proto
+++ b/protoc_plugin/test/protos/ExtensionEnumNameConflict.proto
@@ -1,3 +1,7 @@
+// Copyright (c) 2018, the Dart project authors.  Please see the AUTHORS file
+// 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.
+
 syntax = "proto2";
 
 package protobuf_unittest;
diff --git a/protoc_plugin/test/protos/ExtensionNameConflict.proto b/protoc_plugin/test/protos/ExtensionNameConflict.proto
index 21217eb..5e6ff92 100644
--- a/protoc_plugin/test/protos/ExtensionNameConflict.proto
+++ b/protoc_plugin/test/protos/ExtensionNameConflict.proto
@@ -1,3 +1,7 @@
+// Copyright (c) 2018, the Dart project authors.  Please see the AUTHORS file
+// 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.
+
 syntax = "proto2";
 
 package protobuf_unittest;
diff --git a/protoc_plugin/test/protos/bytes.proto b/protoc_plugin/test/protos/bytes.proto
index 4ba43f3..1fdb9b8 100644
--- a/protoc_plugin/test/protos/bytes.proto
+++ b/protoc_plugin/test/protos/bytes.proto
@@ -1,5 +1,11 @@
+// Copyright (c) 2022, the Dart project authors.  Please see the AUTHORS file
+// 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.
+
 syntax = "proto2";
 
+package bytes;
+
 message BytesEntity {
   optional bytes value = 1;
   repeated bytes values = 2;
diff --git a/protoc_plugin/test/protos/custom_option.proto b/protoc_plugin/test/protos/custom_option.proto
index 05d39bd..cb4fc2e 100644
--- a/protoc_plugin/test/protos/custom_option.proto
+++ b/protoc_plugin/test/protos/custom_option.proto
@@ -4,6 +4,8 @@
 
 syntax = "proto2";
 
+package custom_option;
+
 import "descriptor.proto";
 
 extend google.protobuf.MessageOptions {
diff --git a/protoc_plugin/test/protos/dart_name.proto b/protoc_plugin/test/protos/dart_name.proto
index a09c742..c7b9c11 100644
--- a/protoc_plugin/test/protos/dart_name.proto
+++ b/protoc_plugin/test/protos/dart_name.proto
@@ -4,10 +4,10 @@
 
 syntax = "proto2";
 
-import "dart_options.proto";
-
 package dart_name;
 
+import "dart_options.proto";
+
 message DartName {
   optional string field = 1 [
     (dart_options.dart_name) = "renamedField"
diff --git a/protoc_plugin/test/protos/default_value_escape.proto b/protoc_plugin/test/protos/default_value_escape.proto
index 4063cc5..256e2a0 100644
--- a/protoc_plugin/test/protos/default_value_escape.proto
+++ b/protoc_plugin/test/protos/default_value_escape.proto
@@ -4,6 +4,8 @@
 
 syntax = "proto2";
 
+package default_value_escape;
+
 message F {
   optional string a = 1 [default = "a\nb"];
   optional string b = 2 [default = "a'b"];
diff --git a/protoc_plugin/test/protos/enum_name.proto b/protoc_plugin/test/protos/enum_name.proto
index 9adce88..1be9224 100644
--- a/protoc_plugin/test/protos/enum_name.proto
+++ b/protoc_plugin/test/protos/enum_name.proto
@@ -1,3 +1,7 @@
+// Copyright (c) 2019, the Dart project authors.  Please see the AUTHORS file
+// 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.
+
 syntax = "proto2";
 
 package enum.name;
diff --git a/protoc_plugin/test/protos/extend_unittest.proto b/protoc_plugin/test/protos/extend_unittest.proto
index 1b5a1cd..2532ba2 100644
--- a/protoc_plugin/test/protos/extend_unittest.proto
+++ b/protoc_plugin/test/protos/extend_unittest.proto
@@ -4,6 +4,8 @@
 
 syntax = "proto2";
 
+package extend_unittest;
+
 import "google/protobuf/unittest.proto";
 
 message Outer {
diff --git a/protoc_plugin/test/protos/google/protobuf/unittest_nested_any.proto b/protoc_plugin/test/protos/google/protobuf/unittest_nested_any.proto
deleted file mode 100644
index d929b54..0000000
--- a/protoc_plugin/test/protos/google/protobuf/unittest_nested_any.proto
+++ /dev/null
@@ -1,15 +0,0 @@
-syntax = "proto3";
-
-package protobuf_unittest_nested_any;
-
-import "google/protobuf/any.proto";
-
-message AnyMessage1 {
-  google.protobuf.Any any_field1 = 1;
-  string value = 2;
-}
-
-message AnyMessage2 {
-  google.protobuf.Any any_field2 = 1;
-  string value = 2;
-}
diff --git a/protoc_plugin/test/protos/high_tagnumber.proto b/protoc_plugin/test/protos/high_tagnumber.proto
index 8c473eb..404f2b4 100644
--- a/protoc_plugin/test/protos/high_tagnumber.proto
+++ b/protoc_plugin/test/protos/high_tagnumber.proto
@@ -4,6 +4,8 @@
 
 syntax = "proto2";
 
+package high_tagnumber;
+
 message M {
   // Make sure that we handle 29 bits of tagnumber.
   // (1 << 28) + 1
diff --git a/protoc_plugin/test/protos/import_clash.proto b/protoc_plugin/test/protos/import_clash.proto
index ae9ecd9..a3bc0cc 100644
--- a/protoc_plugin/test/protos/import_clash.proto
+++ b/protoc_plugin/test/protos/import_clash.proto
@@ -4,10 +4,10 @@
 
 syntax = "proto2";
 
-import "foo.proto";
-
 package import_clash;
 
+import "foo.proto";
+
 message Clasher {
   required foo.Foo foo = 1;
 }
diff --git a/protoc_plugin/test/protos/import_public.proto b/protoc_plugin/test/protos/import_public.proto
index effed9f..9ec85d3 100644
--- a/protoc_plugin/test/protos/import_public.proto
+++ b/protoc_plugin/test/protos/import_public.proto
@@ -1,5 +1,11 @@
+// Copyright (c) 2019, the Dart project authors.  Please see the AUTHORS file
+// 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.
+
 syntax = "proto2";
 
+package import_public;
+
 // A public import can be used in the file importing this file. Similar to a
 // Dart export.
 //
diff --git a/protoc_plugin/test/protos/json_name.proto b/protoc_plugin/test/protos/json_name.proto
index 6bd0c14..388ef71 100644
--- a/protoc_plugin/test/protos/json_name.proto
+++ b/protoc_plugin/test/protos/json_name.proto
@@ -4,6 +4,8 @@
 
 syntax = "proto3";
 
+package json_name;
+
 message JsonNamedMessage {
   int32 foo_name = 1 [json_name = "barName"];
 
diff --git a/protoc_plugin/test/protos/map_api.proto b/protoc_plugin/test/protos/map_api.proto
index 5a3ddb4..85ccc3f 100644
--- a/protoc_plugin/test/protos/map_api.proto
+++ b/protoc_plugin/test/protos/map_api.proto
@@ -1,3 +1,7 @@
+// Copyright (c) 2018, the Dart project authors.  Please see the AUTHORS file
+// 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.
+
 syntax = "proto2";
 
 package protobuf_unittest;
diff --git a/protoc_plugin/test/protos/map_api2.proto b/protoc_plugin/test/protos/map_api2.proto
index 509904d..78e9bb0 100644
--- a/protoc_plugin/test/protos/map_api2.proto
+++ b/protoc_plugin/test/protos/map_api2.proto
@@ -1,3 +1,7 @@
+// Copyright (c) 2018, the Dart project authors.  Please see the AUTHORS file
+// 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.
+
 syntax = "proto2";
 
 package protobuf_unittest;
diff --git a/protoc_plugin/test/protos/mixins.proto b/protoc_plugin/test/protos/mixins.proto
index 9395e52..5c47d84 100644
--- a/protoc_plugin/test/protos/mixins.proto
+++ b/protoc_plugin/test/protos/mixins.proto
@@ -1,5 +1,11 @@
+// Copyright (c) 2018, the Dart project authors.  Please see the AUTHORS file
+// 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.
+
 syntax = "proto2";
 
+package mixins;
+
 import "dart_options.proto";
 
 option (dart_options.imports) = {
diff --git a/protoc_plugin/test/protos/multiple_files_test.proto b/protoc_plugin/test/protos/multiple_files_test.proto
index 19414e5..81010ca 100644
--- a/protoc_plugin/test/protos/multiple_files_test.proto
+++ b/protoc_plugin/test/protos/multiple_files_test.proto
@@ -34,14 +34,14 @@
 
 syntax = "proto2";
 
+package protobuf_unittest;
+
 // Some generic_services option(s) added automatically.
 // See:  http://go/proto2-generic-services-default
 option java_generic_services = true;   // auto-added
 
 import "google/protobuf/unittest.proto";
 
-package protobuf_unittest;
-
 option java_multiple_files = true;
 option java_outer_classname = "MultipleFilesTestProto";
 
diff --git a/protoc_plugin/test/protos/nested_any.proto b/protoc_plugin/test/protos/nested_any.proto
new file mode 100644
index 0000000..4c187b1
--- /dev/null
+++ b/protoc_plugin/test/protos/nested_any.proto
@@ -0,0 +1,19 @@
+// Copyright (c) 2022, the Dart project authors.  Please see the AUTHORS file
+// 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.
+
+syntax = "proto3";
+
+package nested_any;
+
+import "google/protobuf/any.proto";
+
+message AnyMessage1 {
+  google.protobuf.Any any_field1 = 1;
+  string value = 2;
+}
+
+message AnyMessage2 {
+  google.protobuf.Any any_field2 = 1;
+  string value = 2;
+}
diff --git a/protoc_plugin/test/protos/nested_extension.proto b/protoc_plugin/test/protos/nested_extension.proto
index 4478006..6c9d927 100644
--- a/protoc_plugin/test/protos/nested_extension.proto
+++ b/protoc_plugin/test/protos/nested_extension.proto
@@ -35,10 +35,10 @@
 
 syntax = "proto2";
 
-import "non_nested_extension.proto";
-
 package protobuf_unittest;
 
+import "non_nested_extension.proto";
+
 message MyNestedExtension {
   extend MessageToBeExtended {
     optional MessageToBeExtended recursiveExtension = 2;
diff --git a/protoc_plugin/test/protos/nested_message.proto b/protoc_plugin/test/protos/nested_message.proto
index fd35154..9f6ffcd 100644
--- a/protoc_plugin/test/protos/nested_message.proto
+++ b/protoc_plugin/test/protos/nested_message.proto
@@ -4,6 +4,8 @@
 
 syntax = "proto3";
 
+package nested_message;
+
 message Top {
   repeated Nested nested_message_list = 1;
   map<int32, Nested> nested_message_map = 2;
diff --git a/protoc_plugin/test/protos/proto2_repeated.proto b/protoc_plugin/test/protos/proto2_repeated.proto
index ee3b156..a737eb8 100644
--- a/protoc_plugin/test/protos/proto2_repeated.proto
+++ b/protoc_plugin/test/protos/proto2_repeated.proto
@@ -4,6 +4,8 @@
 
 syntax = "proto2";
 
+package proto2_repeated;
+
 message Proto2Repeated {
   repeated int32 ints_default = 1;
   repeated int32 ints_packed = 2 [packed=true];
diff --git a/protoc_plugin/test/protos/proto3_repeated.proto b/protoc_plugin/test/protos/proto3_repeated.proto
index 723d154..e354c63 100644
--- a/protoc_plugin/test/protos/proto3_repeated.proto
+++ b/protoc_plugin/test/protos/proto3_repeated.proto
@@ -4,6 +4,8 @@
 
 syntax = "proto3";
 
+package proto3_repeated;
+
 message Proto3Repeated {
   repeated int32 ints_default = 1;
   repeated int32 ints_packed = 2 [packed=true];
diff --git a/protoc_plugin/test/protos/reserved_names_extension.proto b/protoc_plugin/test/protos/reserved_names_extension.proto
index bf43916..5ceb4cc 100644
--- a/protoc_plugin/test/protos/reserved_names_extension.proto
+++ b/protoc_plugin/test/protos/reserved_names_extension.proto
@@ -44,4 +44,4 @@
   optional int32 void = 1031;
   optional int32 while = 1032;
   optional int32 with = 1033;
-}
\ No newline at end of file
+}
diff --git a/protoc_plugin/test/protos/service.proto b/protoc_plugin/test/protos/service.proto
index 6cbb64a..51a9ea9 100644
--- a/protoc_plugin/test/protos/service.proto
+++ b/protoc_plugin/test/protos/service.proto
@@ -1,5 +1,11 @@
+// Copyright (c) 2018, the Dart project authors.  Please see the AUTHORS file
+// 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.
+
 syntax = "proto2";
 
+package service;
+
 import "service2.proto";
 
 message SearchRequest {
@@ -11,6 +17,6 @@
 }
 
 service SearchService {
-	rpc Search (SearchRequest) returns (SearchResponse);
-	rpc Search2 (service2.SearchRequest) returns (service2.SearchResponse);
+  rpc Search (SearchRequest) returns (SearchResponse);
+  rpc Search2 (service2.SearchRequest) returns (service2.SearchResponse);
 }
diff --git a/protoc_plugin/test/protos/service2.proto b/protoc_plugin/test/protos/service2.proto
index 18a2bbb..4f4e1f5 100644
--- a/protoc_plugin/test/protos/service2.proto
+++ b/protoc_plugin/test/protos/service2.proto
@@ -1,3 +1,7 @@
+// Copyright (c) 2018, the Dart project authors.  Please see the AUTHORS file
+// 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.
+
 syntax = "proto2";
 
 package service2;
diff --git a/protoc_plugin/test/protos/service3.proto b/protoc_plugin/test/protos/service3.proto
index d5a5ce9..32887da 100644
--- a/protoc_plugin/test/protos/service3.proto
+++ b/protoc_plugin/test/protos/service3.proto
@@ -1,3 +1,7 @@
+// Copyright (c) 2018, the Dart project authors.  Please see the AUTHORS file
+// 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.
+
 syntax = "proto2";
 
 package service3;
diff --git a/protoc_plugin/test/protos/toplevel.proto b/protoc_plugin/test/protos/toplevel.proto
index b15f091..956895a 100644
--- a/protoc_plugin/test/protos/toplevel.proto
+++ b/protoc_plugin/test/protos/toplevel.proto
@@ -4,6 +4,8 @@
 
 syntax = "proto2";
 
+package toplevel;
+
 message T {
   optional int32 a = 1;
   optional string b = 2;
diff --git a/protoc_plugin/test/service_test.dart b/protoc_plugin/test/service_test.dart
index ca06f81..dcb9475 100644
--- a/protoc_plugin/test/service_test.dart
+++ b/protoc_plugin/test/service_test.dart
@@ -99,8 +99,8 @@
   test('can read message descriptors from JSON', () {
     var map = service.$messageJson;
     expect(map.keys, [
-      '.SearchRequest',
-      '.SearchResponse',
+      '.service.SearchRequest',
+      '.service.SearchResponse',
       '.service2.SearchRequest',
       '.service2.SearchResponse',
       '.service3.SearchResult',
@@ -112,7 +112,7 @@
       return descriptor.name;
     }
 
-    expect(readMessageName('.SearchRequest'), 'SearchRequest');
+    expect(readMessageName('.service.SearchRequest'), 'SearchRequest');
     expect(readMessageName('.service2.SearchRequest'), 'SearchRequest');
     expect(readMessageName('.service3.SearchResult'), 'SearchResult');
   });