• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2018 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #endregion
18 
19 using System.Collections.Generic;
20 using Microsoft.Build.Framework;
21 using Microsoft.Build.Utilities;
22 
23 namespace Grpc.Tools
24 {
25     public class ProtoReadDependencies : Task
26     {
27         /// <summary>
28         /// The collection is used to collect possible additional dependencies
29         /// of proto files cached under ProtoDepDir.
30         /// </summary>
31         [Required]
32         public ITaskItem[] Protobuf { get; set; }
33 
34         /// <summary>
35         /// Directory where protoc dependency files are cached.
36         /// </summary>
37         [Required]
38         public string ProtoDepDir { get; set; }
39 
40         /// <summary>
41         /// Additional items that a proto file depends on. This list may include
42         /// extra dependencies; we do our best to include as few extra positives
43         /// as reasonable to avoid missing any. The collection item is the
44         /// dependency, and its Source metadatum is the dependent proto file, like
45         ///     <ItemName Include="/usr/include/proto/wrapper.proto"
46         ///               Source="my_proto.proto" />
47         /// </summary>
48         [Output]
49         public ITaskItem[] Dependencies { get; private set; }
50 
Execute()51         public override bool Execute()
52         {
53             // Read dependency files, where available. There might be none,
54             // just use a best effort.
55             if (ProtoDepDir != null)
56             {
57                 var dependencies = new List<ITaskItem>();
58                 foreach (var proto in Protobuf)
59                 {
60                     string[] deps = DepFileUtil.ReadDependencyInputs(ProtoDepDir, proto.ItemSpec, Log);
61                     foreach (string dep in deps)
62                     {
63                         var ti = new TaskItem(dep);
64                         ti.SetMetadata(Metadata.Source, proto.ItemSpec);
65                         dependencies.Add(ti);
66                     }
67                 }
68                 Dependencies = dependencies.ToArray();
69             }
70             else
71             {
72                 Dependencies = new ITaskItem[0];
73             }
74 
75             return !Log.HasLoggedErrors;
76         }
77     };
78 }
79